macosui / macos_window_utils.dart

macos_window_utils is a Flutter package that provides a set of methods for modifying the NSWindow of a Flutter application on macOS.
https://pub.dev/packages/macos_window_utils
MIT License
49 stars 9 forks source link

Because both the window_manager plug-in and this plug-in use NSWindowDelegate, there is an incompatibility problem between the two, which will cause all monitoring in window_manager to fail. This problem has been encountered in the macos_ui library. #45

Closed yuanhoujun closed 6 months ago

yuanhoujun commented 7 months ago
  // === Closing Windows ===

  /// Tells the delegate that the user has attempted to close a window or the
  /// window has received a `performClose(_:)` message.
  void windowShouldClose() {}

Can this interface add a boolean return value? Used to control whether the window needs to be closed

Adrian-Samoticha commented 7 months ago

The WindowManipulator’s initialize method has a named parameter called enableWindowDelegate. If it is set to false, macos_window_utils will not use the NSWindowDelegate.

Adding a return value to the Dart code is impossible due to the asynchronous nature of the platform channels, however, it’d be possible to override the return value of the windowShouldClose method of the FlutterWindowDelegate.

I’ll assign myself.

yuanhoujun commented 7 months ago

I know this method, but in macos_ui, When initializing the window using the following code:

 const config = MacosWindowUtilsConfig();
 await config.apply();
  Future<void> apply() async {
    WidgetsFlutterBinding.ensureInitialized();
    // Here is true
    await WindowManipulator.initialize(enableWindowDelegate: true);
    await WindowManipulator.setMaterial(
      NSVisualEffectViewMaterial.windowBackground,
    );
    if (enableFullSizeContentView) {
      await WindowManipulator.enableFullSizeContentView();
    }
    if (makeTitlebarTransparent) {
      await WindowManipulator.makeTitlebarTransparent();
    }
    if (hideTitle) {
      await WindowManipulator.hideTitle();
    }
    await WindowManipulator.addToolbar();

    await WindowManipulator.setToolbarStyle(
      toolbarStyle: toolbarStyle,
    );

    if (removeMenubarInFullScreenMode) {
      final delegate = _FlutterWindowDelegate();
      WindowManipulator.addNSWindowDelegate(delegate);
    }

    if (autoHideToolbarAndMenuBarInFullScreenMode) {
      final options = NSAppPresentationOptions.from({
        NSAppPresentationOption.fullScreen,
        NSAppPresentationOption.autoHideToolbar,
        NSAppPresentationOption.autoHideMenuBar,
        NSAppPresentationOption.autoHideDock,
      });
      options.applyAsFullScreenPresentationOptions();
    }
  }

It has actually set the parameter to true and relies on this parameter. I cannot modify it and may produce unpredictable behavior, such as the toolbar not being removed correctly, etc.

Do you have any idea to handle this ?

Adrian-Samoticha commented 7 months ago

If what you are trying to achieve is preventing the window from being closed by the user, then there is currently no way to do that, however, I could implement such a feature in a future version of macos_window_utils, which is what I have assigned myself to this issue for.

Adrian-Samoticha commented 6 months ago

macos_window_utils 1.5.0 now provides the following methods:

These will allow you to prevent the window from being closable by the user as well as close the window programmatically.