Skip to content

Commit

Permalink
Added option to reposition window if off-screen. #137
Browse files Browse the repository at this point in the history
  • Loading branch information
Timthreetwelve committed Feb 9, 2025
1 parent 1eeae96 commit 1e0fcf6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
8 changes: 7 additions & 1 deletion GetMyIP/Configuration/UserSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Tim Kennedy. All Rights Reserved. Licensed under the MIT License.
// Copyright (c) Tim Kennedy. All Rights Reserved. Licensed under the MIT License.

namespace GetMyIP.Configuration;

Expand Down Expand Up @@ -51,6 +51,12 @@ public partial class UserSettings : ConfigManager<UserSettings>
[ObservableProperty]
private bool _keepOnTop;

/// <summary>
/// Keep window on screen.
/// </summary>
[ObservableProperty]
private bool _keepWindowOnScreen = true;

/// <summary>
/// Enable language testing.
/// </summary>
Expand Down
44 changes: 43 additions & 1 deletion GetMyIP/Helpers/MainWindowHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Tim Kennedy. All Rights Reserved. Licensed under the MIT License.
// Copyright (c) Tim Kennedy. All Rights Reserved. Licensed under the MIT License.

namespace GetMyIP.Helpers;

Expand Down Expand Up @@ -86,6 +86,8 @@ private static void SetWindowPosition()
{
mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}

KeepWindowOnScreen();
}

/// <summary>
Expand All @@ -101,6 +103,41 @@ private static void SaveWindowPosition()
}
#endregion Set and Save MainWindow position and size

#region Reposition off-screen window back to the desktop
/// <summary>
/// Keep the window on the screen.
/// </summary>
private static void KeepWindowOnScreen()
{
if (_mainWindow is null || !UserSettings.Setting!.KeepWindowOnScreen)
{
return;
}

if (_mainWindow.Top < SystemParameters.VirtualScreenTop)
{
_mainWindow.Top = SystemParameters.VirtualScreenTop;
}

if (_mainWindow.Left < SystemParameters.VirtualScreenLeft)
{
_mainWindow.Left = SystemParameters.VirtualScreenLeft;
}

if (_mainWindow.Left + _mainWindow.Width > SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth)
{
_mainWindow.Left = SystemParameters.VirtualScreenWidth + SystemParameters.VirtualScreenLeft - _mainWindow.Width;
}

if (_mainWindow.Top + _mainWindow.Height > SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight)
{
_mainWindow.Top = SystemParameters.WorkArea.Size.Height + SystemParameters.VirtualScreenTop - _mainWindow.Height;
}
}
#endregion Reposition off-screen window back to the desktop

#region Center the window on the screen

#region Window Title
/// <summary>
/// Puts the version number in the title bar as well as Administrator if running elevated
Expand Down Expand Up @@ -142,6 +179,11 @@ private static async void MainWindow_StateChanged(object sender, EventArgs e)
{
try
{
if (_mainWindow!.WindowState == WindowState.Normal)
{
KeepWindowOnScreen();
}

// if window state is minimized and minimize to tray setting is true then hide the window
if (_mainWindow!.WindowState == WindowState.Minimized && UserSettings.Setting!.MinimizeToTray)
{
Expand Down
1 change: 1 addition & 0 deletions GetMyIP/Languages/Strings.en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<sys:String x:Key="SettingsItem_IncludeIPV6">Include internal IPV6 addresses</sys:String>
<sys:String x:Key="SettingsItem_InitialPage">Initial page displayed</sys:String>
<sys:String x:Key="SettingsItem_KeepOnTop">Keep Get My IP on top of other windows</sys:String>
<sys:String x:Key="SettingsItem_KeepWindowOnScreen">Reposition off-screen window</sys:String>
<sys:String x:Key="SettingsItem_LastRefresh">Last refresh: </sys:String>
<sys:String x:Key="SettingsItem_Language">Language</sys:String>
<sys:String x:Key="SettingsItem_MapProvider">Map provider</sys:String>
Expand Down
6 changes: 6 additions & 0 deletions GetMyIP/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@
IsChecked="{Binding StartCentered,
Source={x:Static config:UserSettings.Setting}}" />
<CheckBox Grid.Row="2"
<CheckBox Grid.Row="3"
Padding="10,0"
Content="{DynamicResource SettingsItem_KeepWindowOnScreen}"
IsChecked="{Binding KeepWindowOnScreen,
Source={x:Static config:UserSettings.Setting}}" />
<CheckBox Grid.Row="4"
Padding="10,0"
Content="{DynamicResource SettingsItem_KeepOnTop}"
IsChecked="{Binding KeepOnTop,
Expand Down

0 comments on commit 1e0fcf6

Please # to comment.