Wrap save and restore in tyy/catch blocks
This commit is contained in:
parent
ab38eb5571
commit
6dd885f0b2
@ -26,65 +26,79 @@ namespace LibationAvalonia
|
||||
public static void RestoreSizeAndLocation(this Window form, Configuration config)
|
||||
{
|
||||
if (Design.IsDesignMode) return;
|
||||
|
||||
FormSizeAndPosition savedState = config.GetNonString<FormSizeAndPosition>(form.GetType().Name);
|
||||
|
||||
if (savedState is null)
|
||||
return;
|
||||
|
||||
// too small -- something must have gone wrong. use defaults
|
||||
if (savedState.Width < form.MinWidth || savedState.Height < form.MinHeight)
|
||||
try
|
||||
{
|
||||
savedState.Width = (int)form.Width;
|
||||
savedState.Height = (int)form.Height;
|
||||
|
||||
FormSizeAndPosition savedState = config.GetNonString<FormSizeAndPosition>(form.GetType().Name);
|
||||
|
||||
if (savedState is null)
|
||||
return;
|
||||
|
||||
// too small -- something must have gone wrong. use defaults
|
||||
if (savedState.Width < form.MinWidth || savedState.Height < form.MinHeight)
|
||||
{
|
||||
savedState.Width = (int)form.Width;
|
||||
savedState.Height = (int)form.Height;
|
||||
}
|
||||
|
||||
// Fit to the current screen size in case the screen resolution changed since the size was last persisted
|
||||
if (savedState.Width > form.Screens.Primary.WorkingArea.Width)
|
||||
savedState.Width = form.Screens.Primary.WorkingArea.Width;
|
||||
if (savedState.Height > form.Screens.Primary.WorkingArea.Height)
|
||||
savedState.Height = form.Screens.Primary.WorkingArea.Height;
|
||||
|
||||
var rect = new PixelRect(savedState.X, savedState.Y, savedState.Width, savedState.Height);
|
||||
|
||||
form.Width = savedState.Width;
|
||||
form.Height = savedState.Height;
|
||||
|
||||
// is proposed rect on a screen?
|
||||
if (form.Screens.All.Any(screen => screen.WorkingArea.Contains(rect)))
|
||||
{
|
||||
form.WindowStartupLocation = WindowStartupLocation.Manual;
|
||||
form.Position = new PixelPoint(savedState.X, savedState.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||
}
|
||||
|
||||
// FINAL: for Maximized: start normal state, set size and location, THEN set max state
|
||||
form.WindowState = savedState.IsMaximized ? WindowState.Maximized : WindowState.Normal;
|
||||
}
|
||||
|
||||
// Fit to the current screen size in case the screen resolution changed since the size was last persisted
|
||||
if (savedState.Width > form.Screens.Primary.WorkingArea.Width)
|
||||
savedState.Width = form.Screens.Primary.WorkingArea.Width;
|
||||
if (savedState.Height > form.Screens.Primary.WorkingArea.Height)
|
||||
savedState.Height = form.Screens.Primary.WorkingArea.Height;
|
||||
|
||||
var rect = new PixelRect(savedState.X, savedState.Y, savedState.Width, savedState.Height);
|
||||
|
||||
form.Width = savedState.Width;
|
||||
form.Height = savedState.Height;
|
||||
|
||||
// is proposed rect on a screen?
|
||||
if (form.Screens.All.Any(screen => screen.WorkingArea.Contains(rect)))
|
||||
catch (Exception ex)
|
||||
{
|
||||
form.WindowStartupLocation = WindowStartupLocation.Manual;
|
||||
form.Position = new PixelPoint(savedState.X, savedState.Y);
|
||||
Serilog.Log.Logger.Error(ex, "Failed to save {form} size and location", form.GetType().Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||
}
|
||||
|
||||
// FINAL: for Maximized: start normal state, set size and location, THEN set max state
|
||||
form.WindowState = savedState.IsMaximized ? WindowState.Maximized : WindowState.Normal;
|
||||
}
|
||||
public static void SaveSizeAndLocation(this Window form, Configuration config)
|
||||
{
|
||||
if (Design.IsDesignMode) return;
|
||||
|
||||
var saveState = new FormSizeAndPosition();
|
||||
|
||||
saveState.IsMaximized = form.WindowState == WindowState.Maximized;
|
||||
|
||||
// restore normal state to get real window size.
|
||||
if (form.WindowState != WindowState.Normal)
|
||||
try
|
||||
{
|
||||
form.WindowState = WindowState.Normal;
|
||||
var saveState = new FormSizeAndPosition();
|
||||
|
||||
saveState.IsMaximized = form.WindowState == WindowState.Maximized;
|
||||
|
||||
// restore normal state to get real window size.
|
||||
if (form.WindowState != WindowState.Normal)
|
||||
{
|
||||
form.WindowState = WindowState.Normal;
|
||||
}
|
||||
|
||||
saveState.X = form.Position.X;
|
||||
saveState.Y = form.Position.Y;
|
||||
|
||||
saveState.Width = (int)form.Bounds.Size.Width;
|
||||
saveState.Height = (int)form.Bounds.Size.Height;
|
||||
|
||||
config.SetObject(form.GetType().Name, saveState);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Serilog.Log.Logger.Error(ex, "Failed to save {form} size and location", form.GetType().Name);
|
||||
}
|
||||
|
||||
saveState.X = form.Position.X;
|
||||
saveState.Y = form.Position.Y;
|
||||
|
||||
saveState.Width = (int)form.Bounds.Size.Width;
|
||||
saveState.Height = (int)form.Bounds.Size.Height;
|
||||
|
||||
config.SetObject(form.GetType().Name, saveState);
|
||||
}
|
||||
|
||||
class FormSizeAndPosition
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user