dotMorten / WinUIEx

WinUI Extensions
https://dotmorten.github.io/WinUIEx
MIT License
572 stars 36 forks source link

Feature Suggestion: Add `WindowStateChanged` event #112

Closed Donsroom closed 1 year ago

Donsroom commented 1 year ago

WinUIEx already includes methods to change the window state (Maximize, Minimize, Restore). To compliment these it would be nice to have properties to easily check the current state of the window. As a workaround I created a class that inherits from WindowEx and adds the following:

In order to make some xaml bindings easier I also added the following properties:

While my workaround works fine for me. It would be nice to have this functionality available in WinUIEx itself.

I have attached a small project that illustrates my implimentation. WindowStateReportingDemoForWinUiEx.zip

dotMorten commented 1 year ago

Nice idea. Could you share what the use case is for that? As in what is a good example of why you’d need to know that? That’ll help me understand the best way to expose it

Donsroom commented 1 year ago

One use case is that some of my application have custom code to handle minimizing to the system tray. This is configurable so the user can choose whether to minimize to the tray or minimize normally. To facilitate this my application needs to know when the user minimizes the application so it can act according to the users preference. This is why I added the WindowStateChanged event.

Another use case is that I have applications that use custom code for persisting the window size, position, and state. That code needs to be able to check the current state of the window.

The IsMinimized, IsNormal, and IsMaximized properties are mainly for cases where I want to use the window state in xaml bindings. I found these properties to be easier than trying to bind to the WindowState property.

dotMorten commented 1 year ago

The second use case is already covered by WinUIEx with the Persistence api https://dotmorten.github.io/WinUIEx/api/WinUIEx.WindowManager.html#WinUIEx_WindowManager_PersistenceId

dotMorten commented 1 year ago

Current state is available here: https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.overlappedpresenter.state?view=windows-app-sdk-1.2#microsoft-ui-windowing-overlappedpresenter-state

so only the event seems to be missing

Donsroom commented 1 year ago

Thank you so much for the quick replies.

I will check out the PersistenceStorage. I didn't realize that it supports unpackaged applications. It also looks like the OverlappedPresenter.State property will meet my needs for checking the current state. I definitely like not reinventing the wheel.

dotMorten commented 1 year ago

didn't realize that it supports unpackaged applications

Well sort of. You still need to provide the saving/loading from disk when unpackaged. Here’s one simple example of it: https://github.com/dotMorten/WinUIEx/blob/81bbfbc53a97009d3702f41665b8a3fea3620bee/src/WinUIExSample/App.xaml.cs#L56

Donsroom commented 1 year ago

I tested the PersistanceStorage and it works great! Saving/loading from disk was no problem since I already had that infrastructure in place.

Now I am going to dive into window presenters to see what I have been missing out on there.

dotMorten commented 1 year ago

For now you can subscribe to the min/max/restore like this:

    var manager = WindowManager.Get(this);
    manager.WindowMessageReceived += WindowMessageReceived;

   // ...
    private void WindowMessageReceived(object sender, WindowMessageEventArgs e)
    {
        if(e.Message.MessageId == 0x0005) //WM_SIZE
        {
            // https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-size
            switch (e.Message.WParam)
            {
                case 0: Debug.WriteLine("Restored"); break;
                case 1: Debug.WriteLine("Minimized"); break;
                case 2: Debug.WriteLine("Maximized"); break;
            }
        }
    }
dotMorten commented 9 months ago

Now available in v2.3 release