Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions WinUIGallery/Helpers/SettingsHelper/SettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,46 @@ public void UpdateRecentlyVisited(Action<List<string>> updater)
updater(list);
RecentlyVisited = list;
}

public int MainWindowPositionX
{
get => GetOrCreateDefault<int>(0);
set => Set(value);
}

public int MainWindowPositionY
{
get => GetOrCreateDefault<int>(0);
set => Set(value);
}

public int MainWindowWidth
{
get => GetOrCreateDefault<int>(0);
set => Set(value);
}

public int MainWindowHeight
{
get => GetOrCreateDefault<int>(0);
set => Set(value);
}

public bool IsMainWindowMaximized
{
get => GetOrCreateDefault<bool>(false);
set => Set(value);
}

public double MainWindowScale
{
get => GetOrCreateDefault<double>(1.0);
set => Set(value);
}

public bool SaveWindowState
{
get => GetOrCreateDefault<bool>(false);
set => Set(value);
}
}
109 changes: 109 additions & 0 deletions WinUIGallery/Helpers/WindowStateHelper.cs
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();
}
}
}
18 changes: 18 additions & 0 deletions WinUIGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public MainWindow()
AdjustNavigationViewMargin(force: true);
AppWindow.Changed += (_, _) => AdjustNavigationViewMargin();
}

// Restore window state on load
if (SettingsHelper.Current.SaveWindowState)
{
if (Content is FrameworkElement content)
{
content.Loaded += (_, _) => WindowStateHelper.ApplySavedState(this);
}
}

// Save window state on close
Closed += (_, _) =>
{
if (SettingsHelper.Current.SaveWindowState)
{
WindowStateHelper.Save(this);
}
};
}

// Adjusts the NavigationView margin based on the window state
Expand Down
14 changes: 14 additions & 0 deletions WinUIGallery/Models/WindowState.cs
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; }
}
9 changes: 9 additions & 0 deletions WinUIGallery/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
</StackPanel>
</toolkit:SettingsCard>

<toolkit:SettingsCard Header="Save window state"
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Description="Automatically save and restore window size and position between sessions">
<toolkit:SettingsCard.HeaderIcon>
<FontIcon Glyph="&#xE7C4;" />
</toolkit:SettingsCard.HeaderIcon>
<ToggleSwitch IsOn="{x:Bind Settings.SaveWindowState, Mode=TwoWay}"
OffContent="No"
OnContent="Yes" />
</toolkit:SettingsCard>

<!-- About -->
<TextBlock Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" Text="About" />
Expand Down
4 changes: 3 additions & 1 deletion WinUIGallery/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public string Version
}

public string WinAppSdkRuntimeDetails => VersionHelper.WinAppSdkRuntimeDetails;
private int lastNavigationSelectionMode = 0;
private int lastNavigationSelectionMode = 0;

private SettingsHelper Settings => SettingsHelper.Current;

public SettingsPage()
{
Expand Down