Skip to content

Commit

Permalink
Fix some window placement issues
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Nov 24, 2023
1 parent baa7fc0 commit a632b42
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DesktopClock/DesktopClock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" PrivateAssets="All" />
<PackageReference Include="WpfWindowPlacement" Version="4.0.2" />
<PackageReference Include="WpfWindowPlacement" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions DesktopClock/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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}">
<Window.Resources>
<ContextMenu x:Key="MainContextMenu" x:Shared="False">
<MenuItem Command="{Binding CopyToClipboardCommand}" Header="_Copy" />
Expand Down
16 changes: 7 additions & 9 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using DesktopClock.Properties;
using H.NotifyIcon;
using Humanizer;
using WpfWindowPlacement;

namespace DesktopClock;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}

0 comments on commit a632b42

Please sign in to comment.