Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Suggestion: Add WindowStateChanged event #112

Closed
Donsroom opened this issue Mar 10, 2023 · 9 comments
Closed

Feature Suggestion: Add WindowStateChanged event #112

Donsroom opened this issue Mar 10, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@Donsroom
Copy link

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

@dotMorten
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

@Donsroom
Copy link
Author

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
Copy link
Owner

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>

@Donsroom
Copy link
Author

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 dotMorten added the enhancement New feature or request label Apr 13, 2023
@dotMorten dotMorten changed the title Feature Suggestion: Additional properties for window state. Feature Suggestion: Add WindowStateChanged event May 11, 2023
@dotMorten
Copy link
Owner

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
Copy link
Owner

Now available in v2.3 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants