Skip to content

Commit

Permalink
WinForms: Implement profiles toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 13, 2024
1 parent e59988b commit f133aa9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 29 deletions.
2 changes: 1 addition & 1 deletion NAPS2.Lib.Gtk/EtoForms/Ui/GtkDesktopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void UpdateToolbarPadding()
");
}

protected override void ConfigureToolbar()
protected override void ConfigureToolbars()
{
_toolbar = ((ToolBarHandler) ToolBar.Handler).Control;
_toolbar.Style = ToolbarStyle.Both;
Expand Down
108 changes: 85 additions & 23 deletions NAPS2.Lib.WinForms/EtoForms/Ui/WinFormsDesktopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NAPS2.EtoForms.Notifications;
using NAPS2.EtoForms.Widgets;
using NAPS2.EtoForms.WinForms;
using NAPS2.Scan;
using NAPS2.WinForms;
using WF = System.Windows.Forms;

Expand All @@ -20,7 +21,8 @@ public class WinFormsDesktopForm : DesktopForm
private readonly Dictionary<DesktopToolbarMenuType, WF.ToolStripSplitButton> _menuButtons = new();
private readonly ToolbarFormatter _toolbarFormatter = new(new StringWrapper());
private readonly WF.Form _form;
private WF.ToolStrip _toolStrip = null!;
private WF.ToolStrip _mainToolStrip = null!;
private WF.ToolStrip _profilesToolStrip = null!;
private WF.ToolStripContainer _container = null!;

public WinFormsDesktopForm(
Expand Down Expand Up @@ -81,7 +83,7 @@ protected override void OnLoad(EventArgs e)

protected override void OnShown(EventArgs e)
{
_toolbarFormatter.RelayoutToolbar(_toolStrip);
_toolbarFormatter.RelayoutToolbar(_mainToolStrip);
base.OnShown(e);
}

Expand Down Expand Up @@ -121,19 +123,72 @@ protected override void SetCulture(string cultureId)
base.SetCulture(cultureId);
}

protected override void ConfigureToolbar()
protected override void ConfigureToolbars()
{
_toolStrip = ((ToolBarHandler) ToolBar.Handler).Control;
_toolStrip.ShowItemToolTips = false;
_toolStrip.TabStop = true;
_toolStrip.ImageScalingSize = new Size(32, 32);
_toolStrip.ParentChanged += (_, _) => _toolbarFormatter.RelayoutToolbar(_toolStrip);
_mainToolStrip = ((ToolBarHandler) ToolBar.Handler).Control;
_mainToolStrip.ShowItemToolTips = false;
_mainToolStrip.TabStop = true;
_mainToolStrip.ImageScalingSize = new Size(32, 32);
_mainToolStrip.ParentChanged += (_, _) => _toolbarFormatter.RelayoutToolbar(_mainToolStrip);

_profilesToolStrip = new WF.ToolStrip();
_profilesToolStrip.ShowItemToolTips = false;
_profilesToolStrip.TabStop = true;
_profilesToolStrip.ImageScalingSize = new Size(16, 16);
_profilesToolStrip.Location = new Point(0, 100);
}

public override void PlaceProfilesToolbar()
{
if (Config.Get(c => c.ShowProfilesToolbar) && _profilesToolStrip.Parent == null)
{
_container.TopToolStripPanel.Controls.Add(_profilesToolStrip);
}
if (!Config.Get(c => c.ShowProfilesToolbar) && _profilesToolStrip.Parent != null)
{
_profilesToolStrip.Parent.Controls.Remove(_profilesToolStrip);
}
}

protected override void UpdateProfilesToolbar()
{
var toolbarItems = _profilesToolStrip.Items;
var profiles = _profileManager.Profiles;
var extra = toolbarItems.Count - profiles.Count;
var missing = profiles.Count - toolbarItems.Count;
for (int i = 0; i < extra; i++)
{
toolbarItems.RemoveAt(toolbarItems.Count - 1);
}
for (int i = 0; i < missing; i++)
{
var item = new WF.ToolStripButton
{
TextImageRelation = WF.TextImageRelation.ImageBeforeText,
ImageAlign = ContentAlignment.MiddleLeft,
TextAlign = ContentAlignment.MiddleLeft,
Image = Image.FromStream(new MemoryStream(Icons.control_play_blue_small))
};
item.Click += (_, _) => _desktopScanController.ScanWithProfile((ScanProfile) item.Tag);
toolbarItems.Add(item);
}
for (int i = 0; i < profiles.Count; i++)
{
var profile = profiles[i];
var item = toolbarItems[i];
item.Tag = profile;
if (item.Text != profile.DisplayName)
{
item.Text = profile.DisplayName;
}
}
}

protected override LayoutElement GetMainContent()
{
_container = new WF.ToolStripContainer();
_container.TopToolStripPanel.Controls.Add(_toolStrip);
_container.TopToolStripPanel.Controls.Add(_mainToolStrip);
PlaceProfilesToolbar();
foreach (var panel in _container.Controls.OfType<WF.ToolStripPanel>())
{
// Allow tabbing through the toolbar for accessibility
Expand Down Expand Up @@ -166,7 +221,7 @@ protected override void CreateToolbarButton(Command command)
TextImageRelation = WF.TextImageRelation.ImageAboveText
};
ApplyCommand(item, command);
_toolStrip.Items.Add(item);
_mainToolStrip.Items.Add(item);
}

protected override void CreateToolbarButtonWithMenu(Command command, DesktopToolbarMenuType menuType,
Expand All @@ -177,7 +232,7 @@ protected override void CreateToolbarButtonWithMenu(Command command, DesktopTool
TextImageRelation = WF.TextImageRelation.ImageAboveText
};
ApplyCommand(item, command);
_toolStrip.Items.Add(item);
_mainToolStrip.Items.Add(item);
menu.Handle(subItems => SetUpMenu(item, subItems));
_menuButtons[menuType] = item;
}
Expand Down Expand Up @@ -217,7 +272,7 @@ protected override void CreateToolbarMenu(Command command, MenuProvider menu)
ShowDropDownArrow = false
};
ApplyCommand(item, command);
_toolStrip.Items.Add(item);
_mainToolStrip.Items.Add(item);
menu.Handle(subItems => SetUpMenu(item, subItems));
}

Expand All @@ -233,7 +288,7 @@ protected override void CreateToolbarStackedButtons(Command command1, Command co
command1.EnabledChanged += (_, _) => item.Enabled = command1.Enabled;
item.FirstClick += (_, _) => command1.Execute();
item.SecondClick += (_, _) => command2.Execute();
_toolStrip.Items.Add(item);
_mainToolStrip.Items.Add(item);
}

private WF.ToolStripItem ApplyCommand(WF.ToolStripItem item, Command command)
Expand Down Expand Up @@ -265,7 +320,7 @@ private WF.ToolStripItem ApplyCommand(WF.ToolStripItem item, Command command)

protected override void CreateToolbarSeparator()
{
_toolStrip.Items.Add(new WF.ToolStripSeparator());
_mainToolStrip.Items.Add(new WF.ToolStripSeparator());
}

public override void ShowToolbarMenu(DesktopToolbarMenuType menuType)
Expand All @@ -275,20 +330,27 @@ public override void ShowToolbarMenu(DesktopToolbarMenuType menuType)

private void SaveToolStripLocation()
{
Config.User.Set(c => c.DesktopToolStripDock, _toolStrip.Parent!.Dock.ToConfig());
Config.User.Set(c => c.DesktopToolStripDock, _mainToolStrip.Parent!.Dock.ToConfig());
Config.User.Set(c => c.ProfilesToolStripDock, _profilesToolStrip.Parent?.Dock.ToConfig() ?? DockStyle.Top);
}

private void LoadToolStripLocation()
{
var dock = Config.Get(c => c.DesktopToolStripDock).ToWinForms();
if (dock != WF.DockStyle.None)
SetDock(_mainToolStrip, Config.Get(c => c.DesktopToolStripDock));
if (_profilesToolStrip.Parent != null)
{
var panel = _container.Controls.OfType<WF.ToolStripPanel>().FirstOrDefault(x => x.Dock == dock);
if (panel != null)
{
_toolStrip.Parent = panel;
}
SetDock(_profilesToolStrip, Config.Get(c => c.ProfilesToolStripDock));
}
}

private void SetDock(WF.ToolStrip toolStrip, DockStyle dock)
{
var wfDock = dock.ToWinForms();
var panel = _container.Controls.OfType<WF.ToolStripPanel>().FirstOrDefault(x => x.Dock == wfDock);
if (panel != null)
{
toolStrip.Parent = panel;
}
_toolStrip.Parent!.TabStop = true;
toolStrip.Parent!.TabStop = true;
}
}
18 changes: 14 additions & 4 deletions NAPS2.Lib/EtoForms/Ui/DesktopForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public abstract class DesktopForm : EtoFormBase
private readonly NotificationManager _notificationManager;
private readonly CultureHelper _cultureHelper;
protected readonly ColorScheme _colorScheme;
private readonly IProfileManager _profileManager;
protected readonly IProfileManager _profileManager;
protected readonly ThumbnailController _thumbnailController;
private readonly UiThumbnailProvider _thumbnailProvider;
protected readonly DesktopController _desktopController;
private readonly IDesktopScanController _desktopScanController;
protected readonly IDesktopScanController _desktopScanController;
private readonly ImageListActions _imageListActions;
private readonly DesktopFormProvider _desktopFormProvider;
private readonly IDesktopSubFormController _desktopSubFormController;
Expand Down Expand Up @@ -73,6 +73,7 @@ public DesktopForm(
//
CreateToolbarsAndMenus();
UpdateScanButton();
UpdateProfilesToolbar();
InitLanguageDropdown();

_listView = EtoPlatform.Current.CreateListView(imageListViewBehavior);
Expand Down Expand Up @@ -189,6 +190,7 @@ private void ImageList_ImagesUpdated(object? sender, ImageListEventArgs e)
private void ProfileManager_ProfilesUpdated(object? sender, EventArgs e)
{
UpdateScanButton();
UpdateProfilesToolbar();
}

private void ThumbnailController_ThumbnailSizeChanged(object? sender, EventArgs e)
Expand Down Expand Up @@ -239,7 +241,7 @@ protected override void OnClosed(EventArgs e)
protected virtual void CreateToolbarsAndMenus()
{
ToolBar = new ToolBar();
ConfigureToolbar();
ConfigureToolbars();

var hiddenButtons = Config.Get(c => c.HiddenButtons);

Expand Down Expand Up @@ -352,7 +354,15 @@ protected MenuProvider GetLanguageMenuProvider()
return new MenuProvider().Dynamic(_languageMenuCommands);
}

protected virtual void ConfigureToolbar()
protected virtual void ConfigureToolbars()
{
}

protected virtual void UpdateProfilesToolbar()
{
}

public virtual void PlaceProfilesToolbar()
{
}

Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Lib/EtoForms/Ui/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void Save()
transact.Set(c => c.SingleInstance, _singleInstance.IsChecked());
transact.Commit();
_desktopFormProvider.DesktopForm.Invalidate();
_desktopFormProvider.DesktopForm.PlaceOptionalToolbars();
_desktopFormProvider.DesktopForm.PlaceProfilesToolbar();
}

private void RestoreDefaults_Click(object? sender, EventArgs e)
Expand Down

0 comments on commit f133aa9

Please sign in to comment.