Skip to content

Commit

Permalink
fix(macOS): Traffic lights sometimes become horizontal
Browse files Browse the repository at this point in the history
Use NotificationCenter as a workaround of rewrite BitsdojoWindow
  • Loading branch information
XMLHexagram committed Dec 5, 2024
1 parent d30da3b commit d97b51d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
9 changes: 0 additions & 9 deletions lib/utils/get_dir_path.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import 'dart:io';

import 'package:file_selector/file_selector.dart';

import 'macos_window_control_button_manager.dart';

// TODO(hexagram): This is a temporary solution, not the best way to handle this callback.
// Once BitsdojoWindow is forked and rewritten, using NSWindowDelagate to listen for windowDidEndSheet and windowWillBeginSheet will be a more general and better solution to handle this.
Future<String?> getDirPath() async {
final path = await getDirectoryPath();
if (Platform.isMacOS) {
MacOSWindowControlButtonManager.setVertical();
}
return path;
}
30 changes: 17 additions & 13 deletions lib/widgets/title_bar/window_frame_for_macos.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_fullscreen/flutter_fullscreen.dart';

import '../../utils/macos_window_control_button_manager.dart';
import '../../providers/full_screen.dart';
import '../../providers/router_path.dart';
import '../../providers/responsive_providers.dart';

Expand All @@ -22,25 +21,30 @@ class WindowFrameForMacOS extends StatefulWidget {
State<WindowFrameForMacOS> createState() => _WindowFrameForMacOSState();
}

class _WindowFrameForMacOSState extends State<WindowFrameForMacOS>
with FullScreenListener {
class _WindowFrameForMacOSState extends State<WindowFrameForMacOS> {
late FullScreenProvider _fullscreen;
late ResponsiveProvider _responsiveProvider;

@override
void initState() {
super.initState();
FullScreen.addListener(this);
void didChangeDependencies() {
super.didChangeDependencies();
_fullscreen = Provider.of<FullScreenProvider>(context, listen: false);
_responsiveProvider = Provider.of<ResponsiveProvider>(context, listen: false);

_fullscreen.addListener(updateWindowControlButtons);
_responsiveProvider.addListener(updateWindowControlButtons);
$router.addListener(updateWindowControlButtons);
}

@override
dispose() {
super.dispose();
FullScreen.removeListener(this);
_fullscreen.removeListener(updateWindowControlButtons);
_responsiveProvider.removeListener(updateWindowControlButtons);
$router.removeListener(updateWindowControlButtons);
}

@override
void onFullScreenChanged(bool enabled, SystemUiMode? systemUiMode) {
if (!enabled) {
MacOSWindowControlButtonManager.setVertical();
}
void updateWindowControlButtons() {
setState(() => {});
}

Expand Down
42 changes: 42 additions & 0 deletions macos/Runner/MainFlutterWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,50 @@ class MainFlutterWindow: BitsdojoWindow {

WindowButtonPositioner.shared.mainFlutterWindow = self

NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidEnterFullScreen),
name: NSWindow.didEnterFullScreenNotification,
object: self)

NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidExitFullScreen),
name: NSWindow.didExitFullScreenNotification,
object: self)

NotificationCenter.default.addObserver(
self,
selector: #selector(windowWillBeginSheet),
name: NSWindow.willBeginSheetNotification,
object: self)

NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidEndSheet),
name: NSWindow.didEndSheetNotification,
object: self)

super.awakeFromNib()
}

@objc private func windowDidEnterFullScreen(_ notification: Notification) {}

@objc private func windowDidExitFullScreen(_ notification: Notification) {
WindowButtonPositioner.shared.setVertical()
}

@objc private func windowWillBeginSheet(_ notification: Notification) {
WindowButtonPositioner.shared.setVertical()
}

@objc private func windowDidEndSheet(_ notification: Notification) {
WindowButtonPositioner.shared.setVertical()
}

deinit {
NotificationCenter.default.removeObserver(self)
}
}

class WindowButtonPositioner {
Expand Down

0 comments on commit d97b51d

Please sign in to comment.