Skip to content

Add window state save/restore feature#2106

Open
Zakariathr22 wants to merge 8 commits intomicrosoft:mainfrom
Zakariathr22:WindowStateSaver
Open

Add window state save/restore feature#2106
Zakariathr22 wants to merge 8 commits intomicrosoft:mainfrom
Zakariathr22:WindowStateSaver

Conversation

@Zakariathr22
Copy link
Contributor

@Zakariathr22 Zakariathr22 commented Feb 5, 2026

Description

  • Adds support for saving and restoring main window size, position, scale, and maximized state between sessions.
  • Introduces new settings properties and UI toggle for enabling/disabling this feature.
  • Implements WindowStateHelper and WindowState classes for state management.
  • Window state is restored on load and saved on close (only if enabled in Settings page).

Motivation and Context

How Has This Been Tested?

Manually tested

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Adds support for saving and restoring main window size, position, scale, and maximized state between sessions. Introduces new settings properties and UI toggle for enabling/disabling this feature. Implements `WindowStateHelper` and `WindowState` classes for state management. Window state is restored on load and saved on close if enabled.
@niels9001
Copy link
Collaborator

@Zakariathr22 I haven't tested this yet, will do soon! How will this work when moving between monitors? E.g. opening the app on multi-monitor setup, and then unplugging and only using the laptop's screen?

@Zakariathr22
Copy link
Contributor Author

@Zakariathr22 I haven't tested this yet, will do soon! How will this work when moving between monitors? E.g. opening the app on multi-monitor setup, and then unplugging and only using the laptop's screen?

I tested it, and the window consistently reopens on the main screen after being closed and launched again.

How the code works:

  • Retrieves the AppWindow instance.
  • Uses XamlRoot.RasterizationScale to account for DPI scaling, ensuring the window maintains the same physical size across different monitors.
  • Before saving the window state, it temporarily restores the window if it is maximized. This ensures the actual window size is saved rather than the maximized dimensions.
  • When restoring the window state, it validates the saved position against the display area bounds to keep the window fully visible and prevent it from appearing partially off-screen or on a disconnected monitor.

</StackPanel>
</toolkit:SettingsCard>

<toolkit:SettingsCard Header="Save window state"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldnt be an option, either we support it and it works or we dont support it.

Copy link
Contributor Author

@Zakariathr22 Zakariathr22 Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we negotiate this?

Sometimes, when I use applications that have this functionality, I actually wish it wasn’t enabled. I may resize the window for a specific task, but that doesn’t mean I’ll need it at that size again later.

What if we enable the feature by default (make it true), but also keep providing users with the option to disable it if they prefer not to use it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have use cases why it should (not) be optional? E.g. when I look at most (inbox) Windows apps they have logic to do this, without the option to turn it off?

@ghost1372
Copy link
Contributor

@Zakariathr22 We can use WindowsAppSdk Built-in API for this. Spec is ready and is working fine.
https://github.com/microsoft/WindowsAppSDK/blob/afd4ac42a32a329f4fdfc76d7d443a0200774135/specs/Windowing/AppWindowPlacement.md

@marcelwgn Can you ask your internal friends whether this API will be included in the stable v2 release or not? It is currently available in experimental 5.

@Zakariathr22
Copy link
Contributor Author

@Zakariathr22 We can use WindowsAppSdk Built-in API for this. Spec is ready and is working fine. https://github.com/microsoft/WindowsAppSDK/blob/afd4ac42a32a329f4fdfc76d7d443a0200774135/specs/Windowing/AppWindowPlacement.md

@marcelwgn Can you ask your internal friends whether this API will be included in the stable v2 release or not? It is currently available in experimental 5.

We can proceed with closing this PR once we have confirmation that the API will be part of the upcoming release.

@niels9001
Copy link
Collaborator

@Zakariathr22 @ghost1372 I'm following up internally to understand what to expect from these APIs. Will circle back here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resize the window on startup

4 participants