google / flutter-desktop-embedding

Experimental plugins for Flutter for Desktop
Apache License 2.0
7.1k stars 607 forks source link

window_size does not save the latest dimensions and position of the window on macOS Big Sur #830

Closed moazelshebly closed 3 years ago

moazelshebly commented 3 years ago

Describe the bug It seems that since macOS Big Sur came out, the window dimensions and position of the last app session are not saved anymore. This leads to the app being launched every time in the same position and same dimension that are set in the app and not the ones that the user set during their last use of the app.

It used to work on macOS Catalina but now it is not anymore.

Doctor Output

[✓] Flutter (Channel dev, 1.26.0-12.0.pre, on macOS 11.2 20D64 darwin-x64, locale en-GB)
    • Flutter version 1.26.0-12.0.pre at /Users/moazelshebly/flutter
    • Framework revision a706cd2112 (3 weeks ago), 2021-01-14 18:20:26 -0500
    • Engine revision effb529ece
    • Dart version 2.12.0 (build 2.12.0-224.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/moazelshebly/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.52.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.17.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 11.2 20D64 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 88.0.4324.96

• No issues found!
stuartmorgan commented 3 years ago

The window_size plugin has never had that functionality; any restoration behavior you saw previously was entirely controlled by the OS.

Given that it's relatively straightforward to implement that logic in Dart using existing window_size functionality and something like shared_preferences, I don't plan to implement auto-save in this temporary plugin. (It's certainly something that would be reasonable to build into the eventual Flutter framework support for window management.)

jagmit commented 3 years ago

@stuartmorgan Sorry for bothering you again, but this feature that an application window restores it's dimensions and position from a previous session is common on Windows and macOS and the absence of it makes Flutter application feel less native on desktop. I think it is worth considering whether this should be implemented as a default behaviour.

As you have stated it is trivial to implement such behaviour thanks to the great window_size and shared_preferences plugins, but I am having difficulties figuring out when and where to hook into a "application window closed" event to read and persist the dimensions and position somewhere.

Can you give a hint where this could be done? Maybe somewhere in Dart, in the runner application or as part of the engine?

stuartmorgan commented 3 years ago

I think it is worth considering whether this should be implemented as a default behaviour.

I specifically said I thought it likely should be. Just not in a temporary plugin; the goal of window_size is not for it to do everything someone might want to do, it's to address some very high-priority gaps in the short term.

If someone really wants autosave behavior prior to Flutter itself having window management APIs, they could certainly implement their own plugin to do so; there's no reason it needs to be part of this plugin. The overlap between the existing window_size code and such a plugin would be nil, since it would all happen on the native side, using standard platform APIs.

I am having difficulties figuring out when and where to hook into a "application window closed" event to read and persist the dimensions and position somewhere.

Can you give a hint where this could be done? Maybe somewhere in Dart, in the runner application or as part of the engine?

There's currently no hook for that in Dart; there's an open feature request for it in flutter/flutter. But you can check the window size on every build, and persist it if it's changed, rather than using a window close hook.

(If you really want to add a window close hook locally, you could do so in your runner, using platform channels; you'd need to look into window close delegation for each of the native platforms, and then add a method channel to coordinate with your Dart code. And if you want to add that functionality to Flutter, it would be implemented in each embedding, with communication over a system channel. Both are doable, but non-trivial.)