From 9b09942bda598ca212256d5632f59923feffa018 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Thu, 25 Jun 2020 23:24:20 +0200 Subject: [PATCH] UI - Store window size and position --- src/Artemis.Core/Constants.cs | 3 ++ src/Artemis.UI/Screens/RootViewModel.cs | 51 +++++++++++++++++++------ src/Artemis.UI/Utilities/WindowState.cs | 32 ++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 src/Artemis.UI/Utilities/WindowState.cs diff --git a/src/Artemis.Core/Constants.cs b/src/Artemis.Core/Constants.cs index 96f6f0653..b572a03ac 100644 --- a/src/Artemis.Core/Constants.cs +++ b/src/Artemis.Core/Constants.cs @@ -1,10 +1,13 @@ using System; +using System.IO; +using System.Reflection; using Artemis.Core.Plugins.Models; namespace Artemis.Core { public static class Constants { + public static readonly string ApplicationFolder = Path.GetDirectoryName(typeof(Constants).Assembly.Location); public static readonly string DataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Artemis\\"; public static readonly string ConnectionString = $"FileName={DataFolder}\\database.db"; public static readonly PluginInfo CorePluginInfo = new PluginInfo {Guid = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff"), Name = "Artemis Core"}; diff --git a/src/Artemis.UI/Screens/RootViewModel.cs b/src/Artemis.UI/Screens/RootViewModel.cs index e64bfc986..b47f68c9f 100644 --- a/src/Artemis.UI/Screens/RootViewModel.cs +++ b/src/Artemis.UI/Screens/RootViewModel.cs @@ -12,6 +12,7 @@ using Artemis.UI.Screens.Settings; using Artemis.UI.Screens.Sidebar; using Artemis.UI.Services.Interfaces; using Artemis.UI.Utilities; +using MaterialDesignExtensions.Controls; using MaterialDesignThemes.Wpf; using Stylet; @@ -23,8 +24,10 @@ namespace Artemis.UI.Screens private readonly ICoreService _coreService; private readonly IDebugService _debugService; private readonly IEventAggregator _eventAggregator; + private readonly ISettingsService _settingsService; private readonly ThemeWatcher _themeWatcher; private readonly Timer _titleUpdateTimer; + private readonly PluginSetting _windowSize; private bool _lostFocus; public RootViewModel(IEventAggregator eventAggregator, SidebarViewModel sidebarViewModel, ISettingsService settingsService, ICoreService coreService, @@ -33,12 +36,14 @@ namespace Artemis.UI.Screens SidebarViewModel = sidebarViewModel; MainMessageQueue = snackbarMessageQueue; _eventAggregator = eventAggregator; + _settingsService = settingsService; _coreService = coreService; _debugService = debugService; _titleUpdateTimer = new Timer(500); _titleUpdateTimer.Elapsed += (sender, args) => UpdateWindowTitle(); _colorScheme = settingsService.GetSetting("UI.ColorScheme", ApplicationColorScheme.Automatic); + _windowSize = settingsService.GetSetting("UI.RootWindowSize"); _colorScheme.SettingChanged += (sender, args) => ApplyColorSchemeSetting(); _themeWatcher = new ThemeWatcher(); _themeWatcher.ThemeChanged += (sender, args) => ApplyWindowsTheme(args.Theme); @@ -54,7 +59,7 @@ namespace Artemis.UI.Screens public bool IsSidebarVisible { get; set; } public bool ActiveItemReady { get; set; } public string WindowTitle { get; set; } - + public void WindowDeactivated() { var windowState = ((Window) View).WindowState; @@ -89,16 +94,6 @@ namespace Artemis.UI.Screens _eventAggregator.Publish(new MainWindowKeyEvent(false, e)); } - protected override void OnActivate() - { - UpdateWindowTitle(); - _titleUpdateTimer.Start(); - } - - protected override void OnDeactivate() - { - _titleUpdateTimer.Stop(); - } private void UpdateWindowTitle() { @@ -152,5 +147,39 @@ namespace Artemis.UI.Screens var extensionsPaletteHelper = new MaterialDesignExtensions.Themes.PaletteHelper(); extensionsPaletteHelper.SetLightDark(colorScheme == ApplicationColorScheme.Dark); } + + #region Overrides of Screen + + protected override void OnViewLoaded() + { + var window = (MaterialWindow) View; + if (_windowSize.Value != null) + _windowSize.Value.ApplyToWindow(window); + else + { + _windowSize.Value = new WindowSize(); + _windowSize.Value.ApplyFromWindow(window); + } + + base.OnViewLoaded(); + } + + protected override void OnActivate() + { + UpdateWindowTitle(); + _titleUpdateTimer.Start(); + } + + protected override void OnDeactivate() + { + _titleUpdateTimer.Stop(); + + var window = (MaterialWindow) View; + _windowSize.Value ??= new WindowSize(); + _windowSize.Value.ApplyFromWindow(window); + _windowSize.Save(); + } + + #endregion } } \ No newline at end of file diff --git a/src/Artemis.UI/Utilities/WindowState.cs b/src/Artemis.UI/Utilities/WindowState.cs new file mode 100644 index 000000000..cde656696 --- /dev/null +++ b/src/Artemis.UI/Utilities/WindowState.cs @@ -0,0 +1,32 @@ +using System.Windows; +using MaterialDesignExtensions.Controls; + +namespace Artemis.UI.Utilities +{ + public class WindowSize + { + public double Top { get; set; } + public double Left { get; set; } + public double Width { get; set; } + public double Height { get; set; } + public bool IsMaximized { get; set; } + + public void ApplyFromWindow(MaterialWindow window) + { + Top = window.Top; + Left = window.Left; + Height = window.Height; + Width = window.Width; + IsMaximized = window.WindowState == WindowState.Maximized; + } + + public void ApplyToWindow(MaterialWindow window) + { + window.Top = Top; + window.Left = Left; + window.Height = Height; + window.Width = Width; + window.WindowState = IsMaximized ? WindowState.Maximized : WindowState.Normal; + } + } +} \ No newline at end of file