From 451ee4e84e1bb4783b85fda6cfb226e2d5f4740f Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Sun, 15 Sep 2024 19:48:15 -0700 Subject: [PATCH] Persist sidebar state --- NAPS2.Lib/Config/CommonConfig.cs | 6 ++++++ NAPS2.Lib/Config/InternalDefaults.cs | 1 + NAPS2.Lib/EtoForms/Desktop/Sidebar.cs | 2 ++ NAPS2.Lib/EtoForms/Layout/L.cs | 2 +- NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs | 12 +++++++++++- NAPS2.Lib/EtoForms/Ui/DesktopForm.cs | 4 +++- 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/NAPS2.Lib/Config/CommonConfig.cs b/NAPS2.Lib/Config/CommonConfig.cs index 01ef5cbfb..ee51bbd24 100644 --- a/NAPS2.Lib/Config/CommonConfig.cs +++ b/NAPS2.Lib/Config/CommonConfig.cs @@ -203,4 +203,10 @@ public class CommonConfig [Common] public bool DisableScannerSharing { get; set; } + + [User] + public bool SidebarVisible { get; set; } + + [User] + public int SidebarWidth { get; set; } } \ No newline at end of file diff --git a/NAPS2.Lib/Config/InternalDefaults.cs b/NAPS2.Lib/Config/InternalDefaults.cs index 3859b421b..f68bb544f 100644 --- a/NAPS2.Lib/Config/InternalDefaults.cs +++ b/NAPS2.Lib/Config/InternalDefaults.cs @@ -58,6 +58,7 @@ public static class InternalDefaults DesktopToolStripDock = DockStyle.Top, EventLogging = EventType.None, ShowPageNumbers = false, + SidebarVisible = true, PdfSettings = new PdfSettings { Metadata = new PdfMetadata diff --git a/NAPS2.Lib/EtoForms/Desktop/Sidebar.cs b/NAPS2.Lib/EtoForms/Desktop/Sidebar.cs index 4245d1500..3cb2fa7bf 100644 --- a/NAPS2.Lib/EtoForms/Desktop/Sidebar.cs +++ b/NAPS2.Lib/EtoForms/Desktop/Sidebar.cs @@ -88,6 +88,7 @@ public class Sidebar public LayoutElement CreateView(IFormBase parentWindow) { + _sidebarVis.IsVisible = _config.Get(c => c.SidebarVisible); _profile.SelectedItem = _profileManager.DefaultProfile; _deviceSelectorWidget = new DeviceSelectorWidget(_scanPerformer, _deviceCapsCache, _iconProvider, parentWindow) @@ -217,5 +218,6 @@ public class Sidebar public void ToggleVisibility() { _sidebarVis.IsVisible = !_sidebarVis.IsVisible; + _config.User.Set(c => c.SidebarVisible, _sidebarVis.IsVisible); } } \ No newline at end of file diff --git a/NAPS2.Lib/EtoForms/Layout/L.cs b/NAPS2.Lib/EtoForms/Layout/L.cs index 4dabcc312..c492aa9ef 100644 --- a/NAPS2.Lib/EtoForms/Layout/L.cs +++ b/NAPS2.Lib/EtoForms/Layout/L.cs @@ -56,7 +56,7 @@ public static class L return new BufferLayoutElement(element, left, top, right, bottom); } - public static LayoutElement LeftPanel(LayoutElement left, LayoutElement right) + public static LayoutLeftPanel LeftPanel(LayoutElement left, LayoutElement right) { return new LayoutLeftPanel(left, right); } diff --git a/NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs b/NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs index ef455b947..fa9a7afdc 100644 --- a/NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs +++ b/NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs @@ -10,6 +10,8 @@ public class LayoutLeftPanel : LayoutElement private readonly LayoutOverlay _overlay; private readonly Splitter _splitter; + private Func _widthGetter = () => 0; + private Action _widthSetter = _ => { }; private bool _isInitialized; public LayoutLeftPanel(LayoutElement left, LayoutElement right) @@ -37,12 +39,13 @@ public class LayoutLeftPanel : LayoutElement if (!_isInitialized) { - _left.Width = _splitter.Position = _splitter.Panel1MinimumSize; + _left.Width = _splitter.Position = Math.Max(_widthGetter(), _splitter.Panel1MinimumSize); _splitter.PositionChanged += (_, _) => { if (_left.Width != _splitter.Position) { _left.Width = _splitter.Position; + _widthSetter(_splitter.Position); context.Invalidate(); } }; @@ -74,4 +77,11 @@ public class LayoutLeftPanel : LayoutElement { return _overlay.GetPreferredSize(context, parentBounds); } + + public LayoutLeftPanel SizeConfig(Func getter, Action setter) + { + _widthGetter = getter; + _widthSetter = setter; + return this; + } } \ No newline at end of file diff --git a/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs b/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs index 82b1a99f8..46d1a279a 100644 --- a/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs +++ b/NAPS2.Lib/EtoForms/Ui/DesktopForm.cs @@ -135,7 +135,9 @@ public abstract class DesktopForm : EtoFormBase _notificationArea.Content) ).Padding(8) ).Scale() - ); + ).SizeConfig( + () => Config.Get(c => c.SidebarWidth), + width => Config.User.Set(c => c.SidebarWidth, width)); } private void OpeningContextMenu(object? sender, EventArgs e)