From a632b4299aa2c7579fb1f6bc1653ecbcb59e4180 Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Thu, 23 Nov 2023 20:12:14 -0600 Subject: [PATCH] Fix some window placement issues --- DesktopClock/DesktopClock.csproj | 2 +- DesktopClock/MainWindow.xaml | 6 +++--- DesktopClock/MainWindow.xaml.cs | 16 +++++++--------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/DesktopClock/DesktopClock.csproj b/DesktopClock/DesktopClock.csproj index 7b41b29..3d2d700 100644 --- a/DesktopClock/DesktopClock.csproj +++ b/DesktopClock/DesktopClock.csproj @@ -22,7 +22,7 @@ - + diff --git a/DesktopClock/MainWindow.xaml b/DesktopClock/MainWindow.xaml index f265f7e..158a6a6 100644 --- a/DesktopClock/MainWindow.xaml +++ b/DesktopClock/MainWindow.xaml @@ -6,6 +6,7 @@ xmlns:local="clr-namespace:DesktopClock" xmlns:p="clr-namespace:DesktopClock.Properties" xmlns:tb="http://www.hardcodet.net/taskbar" + xmlns:wp="clr-namespace:WpfWindowPlacement;assembly=WpfWindowPlacement" d:DataContext="{d:DesignInstance Type=local:MainWindow}" mc:Ignorable="d" Title="DesktopClock" @@ -21,10 +22,9 @@ MouseDown="Window_MouseDown" MouseDoubleClick="Window_MouseDoubleClick" MouseWheel="Window_MouseWheel" - SourceInitialized="Window_SourceInitialized" - Deactivated="Window_Deactivated" + ContentRendered="Window_ContentRendered" Closed="Window_Closed" - SizeChanged="Window_SizeChanged"> + wp:WindowPlacementProperties.Placement="{Binding Placement, Source={x:Static p:Settings.Default}, Mode=TwoWay}"> diff --git a/DesktopClock/MainWindow.xaml.cs b/DesktopClock/MainWindow.xaml.cs index 6eeb820..144916c 100644 --- a/DesktopClock/MainWindow.xaml.cs +++ b/DesktopClock/MainWindow.xaml.cs @@ -11,7 +11,6 @@ using DesktopClock.Properties; using H.NotifyIcon; using Humanizer; -using WpfWindowPlacement; namespace DesktopClock; @@ -21,6 +20,7 @@ namespace DesktopClock; [ObservableObject] public partial class MainWindow : Window { + private bool _hasInitiallyChangedSize; private readonly SystemClockTimer _systemClockTimer; private TaskbarIcon _trayIcon; private TimeZoneInfo _timeZone; @@ -269,14 +269,9 @@ private void Window_MouseWheel(object sender, MouseWheelEventArgs e) } } - private void Window_SourceInitialized(object sender, EventArgs e) + private void Window_ContentRendered(object sender, EventArgs e) { - WindowPlacementFunctions.SetPlacement(this, Settings.Default.Placement); - } - - private void Window_Deactivated(object sender, EventArgs e) - { - Settings.Default.Placement = WindowPlacementFunctions.GetPlacement(this); + SizeChanged += Window_SizeChanged; } private void Window_Closed(object sender, EventArgs e) @@ -290,10 +285,13 @@ private void Window_Closed(object sender, EventArgs e) private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { - if (e.WidthChanged && Settings.Default.RightAligned) + if (_hasInitiallyChangedSize && e.WidthChanged && Settings.Default.RightAligned) { var previousRight = Left + e.PreviousSize.Width; Left = previousRight - ActualWidth; } + + // Use this to ignore the change when the window is loaded at the beginning. + _hasInitiallyChangedSize = true; } } \ No newline at end of file