Skip to content

Commit

Permalink
Persist sidebar state
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Sep 16, 2024
1 parent 874e98f commit ef1b69e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions NAPS2.Lib/Config/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
1 change: 1 addition & 0 deletions NAPS2.Lib/Config/InternalDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static CommonConfig GetCommonConfig() =>
DesktopToolStripDock = DockStyle.Top,
EventLogging = EventType.None,
ShowPageNumbers = false,
SidebarVisible = true,
PdfSettings = new PdfSettings
{
Metadata = new PdfMetadata
Expand Down
2 changes: 2 additions & 0 deletions NAPS2.Lib/EtoForms/Desktop/Sidebar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void NewProfile()

public LayoutElement CreateView(IFormBase parentWindow)
{
_sidebarVis.IsVisible = _config.Get(c => c.SidebarVisible);
_profile.SelectedItem = _profileManager.DefaultProfile;

_deviceSelectorWidget = new DeviceSelectorWidget(_scanPerformer, _deviceCapsCache, _iconProvider, parentWindow)
Expand Down Expand Up @@ -217,5 +218,6 @@ private void UpdateUiForProfile()
public void ToggleVisibility()
{
_sidebarVis.IsVisible = !_sidebarVis.IsVisible;
_config.User.Set(c => c.SidebarVisible, _sidebarVis.IsVisible);
}
}
2 changes: 1 addition & 1 deletion NAPS2.Lib/EtoForms/Layout/L.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static LayoutElement Buffer(LayoutElement element, int left, int top, int
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);
}
Expand Down
12 changes: 11 additions & 1 deletion NAPS2.Lib/EtoForms/Layout/LayoutLeftPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class LayoutLeftPanel : LayoutElement
private readonly LayoutOverlay _overlay;
private readonly Splitter _splitter;

private Func<int> _widthGetter = () => 0;
private Action<int> _widthSetter = _ => { };
private bool _isInitialized;

public LayoutLeftPanel(LayoutElement left, LayoutElement right)
Expand Down Expand Up @@ -37,12 +39,13 @@ public override void DoLayout(LayoutContext context, RectangleF bounds)

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();
}
};
Expand Down Expand Up @@ -74,4 +77,11 @@ protected override SizeF GetPreferredSizeCore(LayoutContext context, RectangleF
{
return _overlay.GetPreferredSize(context, parentBounds);
}

public LayoutLeftPanel SizeConfig(Func<int> getter, Action<int> setter)
{
_widthGetter = getter;
_widthSetter = setter;
return this;
}
}
4 changes: 3 additions & 1 deletion NAPS2.Lib/EtoForms/Ui/DesktopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ protected override void BuildLayout()
_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)
Expand Down

0 comments on commit ef1b69e

Please sign in to comment.