Skip to content

Commit

Permalink
Merge pull request #16 from SebastienDuruz/ElectronWindow-BugResolve
Browse files Browse the repository at this point in the history
Electron window bug resolve
  • Loading branch information
SebastienDuruz committed Jun 12, 2023
2 parents 24d3a1d + 95f1c37 commit 629cedd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion GestThe/GestThe/electron.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"appId": "com.GestThe.app",
"productName": "GestThé",
"copyright": "Copyright © 2023",
"buildVersion": "1.0.0",
"buildVersion": "1.0.1",
"compression": "maximum",
"directories": {
"output": "../../../bin/Desktop"
Expand Down
40 changes: 35 additions & 5 deletions GestThe/GestTheLib/Data/Electron/ElectronHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static class ElectronHandler
public static async Task BuildElectronWindow()
{
SettingsReader = new SettingsReader();

// Make sure the Window is on a valid display bounds
await ValidateWindowPosition();

// Setup the Electron Window
MainWindow = await ElectronNET.API.Electron.WindowManager.CreateWindowAsync(
Expand All @@ -52,7 +55,7 @@ public static async Task BuildElectronWindow()
MainWindow.OnReadyToShow += () => MainWindow.Show();
MainWindow.OnMove += async () => await SaveMainWindowSizeAndPosition();
MainWindow.OnResize += async () => await SaveMainWindowSizeAndPosition();
MainWindow.OnClose += SaveWindowSettings;
MainWindow.OnClose += SettingsReader.WriteSettings;
}

/// <summary>
Expand Down Expand Up @@ -94,11 +97,38 @@ private static async Task SaveMainWindowSizeAndPosition()
}

/// <summary>
/// Save the window settings to the json file
/// Validate that the application is on the bounds of the displays
/// <returns>True if valid position, false if not</returns>
/// </summary>
private static void SaveWindowSettings()
private static async Task<bool> ValidateWindowPosition()
{
// Write the new Settings
SettingsReader.WriteSettings();
bool windowPositionIsValid = false;

// Get the connected displays
Display[] displays = await ElectronNET.API.Electron.Screen.GetAllDisplaysAsync();

// check Window positions
foreach (Display display in displays)
{
if (display.Bounds.X <= SettingsReader.SettingsValues.WindowLeft
&& display.Bounds.X + display.Bounds.Width >= SettingsReader.SettingsValues.WindowLeft
&& display.Bounds.Y <= SettingsReader.SettingsValues.WindowHeight
&& display.Bounds.Y + display.Bounds.Height >= SettingsReader.SettingsValues.WindowTop)
{
windowPositionIsValid = true;
break;
}
}

// Reset the position values for MainWindow
if (!windowPositionIsValid)
{
SettingsReader.SettingsValues.WindowLeft = 100;
SettingsReader.SettingsValues.WindowTop = 100;
SettingsReader.WriteSettings();
}

// Return the result
return windowPositionIsValid;
}
}

0 comments on commit 629cedd

Please sign in to comment.