window_manager icon indicating copy to clipboard operation
window_manager copied to clipboard

[Windows] SetPreventClose Prevents close too much :)

Open GaelHuyghe opened this issue 1 year ago • 1 comments

Hi,

I have your plugin in my application. I have windowManager.setPreventClose(true); in my main.dart

Then I have

@override
  Future<void> onWindowClose() async {
  
    if(await windowManager.isPreventClose())
    {
      logger.info("MainPage", "OnWindowClose called > Hiding...");
      windowManager.hide();
    }
    else
    {
      logger.info("MainPage", "OnWindowClose called > Destroying...");
      windowManager.destroy();
    }
  }

So when the user click on the close button, the ``windowManager.hide()` method is called

So far, so good.

This application is embedded within a .MSI and when I apply an upgrade, the OS can't kill the application.

I'm guessing that the MSI is issuing a WM_CLOSE command to the application, but is then re-routed as a 'Close' event within the WindowManager and I just "hide" the application.

Is there any way to intercept the User action only and let the System one go through ? (Like having the CloseReason.UserClosing in Winform)

My needs :

  • When the user click the 'x' button > hide
  • When the user alt+f4 the app > hide
  • When the system tries to gracefully close the app > close

I tried fiddling with LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message, WPARAM const wparam, LPARAM const lparam) in win32_window.cpp but to no avail. I don't have any skills in cpp :/

(I think it needs to hide only when message == WM_SYSCOMMAND && wparam == SC_CLOSE instead of WM_CLOSE but again, I'm far from my skills here)

Thanks in advance to whoever can guide me!

GaelHuyghe avatar Jun 20 '23 15:06 GaelHuyghe

Modifying `window_manager_plugin.cpp' and adding the lines

 else if (message == WM_SYSCOMMAND && wParam == SC_CLOSE) {
    _EmitEvent("hide");
    window_manager->Hide();
    return -1;
  }

seems to work for me (hide when the user click the 'x' button and/or close the app via the context menu) and removing setPreventClose(true) will no longer block the graceful from the OS I hope

I will test it further

GaelHuyghe avatar Jun 20 '23 15:06 GaelHuyghe