-
Notifications
You must be signed in to change notification settings - Fork 719
Add window state save/restore feature #2106
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
Open
Zakariathr22
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
Zakariathr22:WindowStateSaver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
973149a
Add window state save/restore feature
Zakariathr22 efbacfd
Merge branch 'main' into WindowStateSaver
Zakariathr22 f13df60
Merge branch 'main' into WindowStateSaver
Zakariathr22 d430b1e
Merge branch 'main' into WindowStateSaver
niels9001 c2996aa
Update window display area logic to use position, not ID
Zakariathr22 5e12152
Merge branch 'WindowStateSaver' of https://github.com/Zakariathr22/Wi…
Zakariathr22 10483e4
Merge branch 'main' into WindowStateSaver
Zakariathr22 2534002
Merge branch 'main' into WindowStateSaver
Zakariathr22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.UI.Windowing; | ||
| using Microsoft.UI.Xaml; | ||
| using System; | ||
| using Windows.Graphics; | ||
| using WinUIGallery.Models; | ||
|
|
||
| namespace WinUIGallery.Helpers; | ||
|
|
||
| public static class WindowStateHelper | ||
| { | ||
| private static void SaveWindowState(WindowState state) | ||
| { | ||
| var s = SettingsHelper.Current; | ||
|
|
||
| s.MainWindowPositionX = state.PositionX; | ||
| s.MainWindowPositionY = state.PositionY; | ||
| s.MainWindowWidth = state.Width; | ||
| s.MainWindowHeight = state.Height; | ||
| s.MainWindowScale = state.Scale; | ||
| s.IsMainWindowMaximized = state.IsMaximized; | ||
| } | ||
|
|
||
| private static WindowState? LoadWindowState() | ||
| { | ||
| var s = SettingsHelper.Current; | ||
|
|
||
| bool isInvalid = | ||
| s.MainWindowWidth <= 0 || | ||
| s.MainWindowHeight <= 0 || | ||
| ( | ||
| s.MainWindowPositionX == 0 && | ||
| s.MainWindowPositionY == 0 && | ||
| s.MainWindowWidth == 0 && | ||
| s.MainWindowHeight == 0 && | ||
| s.MainWindowScale == 1.0 && | ||
| !s.IsMainWindowMaximized | ||
| ); | ||
|
|
||
| if (isInvalid) | ||
| return null; | ||
|
|
||
| return new WindowState | ||
| { | ||
| PositionX = s.MainWindowPositionX, | ||
| PositionY = s.MainWindowPositionY, | ||
| Width = s.MainWindowWidth, | ||
| Height = s.MainWindowHeight, | ||
| Scale = s.MainWindowScale, | ||
| IsMaximized = s.IsMainWindowMaximized | ||
| }; | ||
| } | ||
|
|
||
| public static void Save(Window window) | ||
| { | ||
| var appWindow = window.AppWindow; | ||
|
|
||
| double scale = window.Content.XamlRoot.RasterizationScale; | ||
| bool isMaximized = appWindow.Presenter is OverlappedPresenter p && | ||
| p.State == OverlappedPresenterState.Maximized; | ||
|
|
||
| // Restore before saving accurate dimensions | ||
| (appWindow.Presenter as OverlappedPresenter)?.Restore(); | ||
|
|
||
| var state = new WindowState | ||
| { | ||
| PositionX = appWindow.Position.X, | ||
| PositionY = appWindow.Position.Y, | ||
| Width = appWindow.Size.Width, | ||
| Height = appWindow.Size.Height, | ||
| Scale = scale, | ||
| IsMaximized = isMaximized | ||
| }; | ||
|
|
||
| SaveWindowState(state); | ||
| } | ||
|
|
||
| public static void ApplySavedState(Window window) | ||
| { | ||
| if (window.Content is FrameworkElement content) | ||
| { | ||
| double currentScale = content.XamlRoot.RasterizationScale; | ||
| var state = LoadWindowState(); | ||
| if (state is null) return; | ||
|
|
||
| // adjust for DPI/scaling change | ||
| double scaleFactor = currentScale / state.Scale; | ||
| int width = (int)(state.Width * scaleFactor); | ||
| int height = (int)(state.Height * scaleFactor); | ||
| int x = state.PositionX; | ||
| int y = state.PositionY; | ||
|
|
||
| // ensure window fits display area | ||
| var displayArea = DisplayArea.GetFromPoint(new PointInt32(x,y), DisplayAreaFallback.Primary).WorkArea; | ||
| width = Math.Min(width, displayArea.Width); | ||
| height = Math.Min(height, displayArea.Height); | ||
| x = Math.Clamp(x, displayArea.X, displayArea.X + displayArea.Width - width); | ||
| y = Math.Clamp(y, displayArea.Y, displayArea.Y + displayArea.Height - height); | ||
|
|
||
| window.AppWindow.Move(new PointInt32(x, y)); | ||
| window.AppWindow.Resize(new SizeInt32(width, height)); | ||
|
|
||
| if (state.IsMaximized) | ||
| (window.AppWindow.Presenter as OverlappedPresenter)?.Maximize(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace WinUIGallery.Models; | ||
|
|
||
| public class WindowState | ||
| { | ||
| public int PositionX { get; set; } | ||
| public int PositionY { get; set; } | ||
| public int Width { get; set; } | ||
| public int Height { get; set; } | ||
| public double Scale { get; set; } | ||
| public bool IsMaximized { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?