dotMorten/WinUIEx

Feature Suggestion: Add `WindowStateChanged` event

Closed this issue · 9 comments

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:

  • A "WindowState" property
  • A "WindowStateChanged" event
  • A "WindowState" enum

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

  • An "IsMinimized" property
  • An "IsNormal" property
  • An "IsMaximized" property

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

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

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.

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

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.

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:

private class FilePersistence : IDictionary<string, object>

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.

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;
            }
        }
    }

Now available in v2.3 release