pichillilorenzo / window_manager_plus

This plugin allows Flutter desktop apps to create and manage multiple windows, such as resizing and repositioning, and communicate between them.
https://pub.dev/packages/window_manager_plus
MIT License
19 stars 2 forks source link

Sub window is freezed when switch to other app or main window #5

Open CaoGiaHieu-dev opened 1 month ago

CaoGiaHieu-dev commented 1 month ago

same as this issue https://github.com/MixinNetwork/flutter-plugins/issues/319

it also happen when i create new window then quickly switch to other app or main window

yeonsh commented 1 month ago

There is a workaround. Refer to this comment.

https://github.com/MixinNetwork/flutter-plugins/issues/289#issuecomment-1874255766

CaoGiaHieu-dev commented 1 month ago

There is a workaround. Refer to this comment.

MixinNetwork/flutter-plugins#289 (comment)

Thank you . for now this was the only solutions

pichillilorenzo commented 1 month ago

Unfortunately, this was something I didn't expect and I didn't find any solution to fix it. It seems to be something related to Flutter itself on macOS. Instead, on Windows I didn't have any problem.

On macOS, the curious thing is that if I resize the window, it updates. If I lose focus and then regain it by switching to another window of another program, then it works correctly again.

pichillilorenzo commented 1 month ago

for reference: https://github.com/flutter/flutter/issues/133533

pichillilorenzo commented 1 month ago

I was able to create a workaround without changing the Flutter source code, using an AppLifecycleListener to force to trigger _setFramesEnabledState(true); instead of _setFramesEnabledState(false);.

In your app implement this listener this way inside your main widget extending a State:

class _MyAppState extends State<MyApp> {

  // your code ....

  late final AppLifecycleListener? _appLifecycleListener;

  @override
  void initState() {
    // your code ....
    // workaround applies for all sub-windows (all windows with id > 0) on macOS 
    if (WindowManagerPlus.current.id > 0 && Platform.isMacOS) {
      _appLifecycleListener = AppLifecycleListener(
        onStateChange: _handleStateChange,
      );
    }
    super.initState();
  }

  void _handleStateChange(AppLifecycleState state) {
    // workaround applies for all sub-windows (all windows with id > 0) on macOS 
    if (WindowManagerPlus.current.id > 0 && Platform.isMacOS && state == AppLifecycleState.hidden) {
      // trigger _setFramesEnabledState(true); method
      SchedulerBinding.instance.handleAppLifecycleStateChanged(
          AppLifecycleState.inactive);
    }
  }

  @override
  void dispose() {
    // your code ....
    _appLifecycleListener?.dispose();
    super.dispose();
  }

  // your code ....
}

It is using the protected method SchedulerBinding.handleAppLifecycleStateChanged but this seems to be the only way to achieve that without changing the Flutter source code.