-
Notifications
You must be signed in to change notification settings - Fork 718
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
973149a
efbacfd
f13df60
d430b1e
c2996aa
5e12152
10483e4
2534002
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.GetFromWindowId(window.AppWindow.Id, 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(); | ||
| } | ||
| } | ||
| } | ||
| 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; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,15 @@ | |
| </StackPanel> | ||
| </toolkit:SettingsCard> | ||
|
|
||
| <toolkit:SettingsCard Header="Save window state" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| Description="Automatically save and restore window size and position between sessions"> | ||
| <toolkit:SettingsCard.HeaderIcon> | ||
| <FontIcon Glyph="" /> | ||
| </toolkit:SettingsCard.HeaderIcon> | ||
| <ToggleSwitch IsOn="{x:Bind Settings.SaveWindowState, Mode=TwoWay}" | ||
| OffContent="No" | ||
| OnContent="Yes" /> | ||
| </toolkit:SettingsCard> | ||
|
|
||
| <!-- About --> | ||
| <TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" Text="About" /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.