Skip to content

Commit

Permalink
Merge pull request #2481 from cwensley/curtis/wpf-toolbar-and-menu-ic…
Browse files Browse the repository at this point in the history
…on-sizes

Wpf: Allow ToolItem and MenuItem to have different icon sizes
  • Loading branch information
cwensley committed May 19, 2023
2 parents 8fe54c2 + a20524c commit 82b5ea5
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"request": "launch",
"preLaunchTask": "build-wpf",
"program": "${workspaceFolder}/artifacts/test/Eto.Test.Wpf/${config:var.configuration}/net6.0-windows/Eto.Test.Wpf.exe",
"targetArchitecture": "x86_64",
// "targetArchitecture": "x86_64",
"args": [],
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
Expand Down
28 changes: 26 additions & 2 deletions src/Eto.Wpf/Forms/Menu/MenuItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ interface IMenuItemHandler
{
void Validate();
}

public static class MenuItemHandler
{
// note this does not affect the main menu by default due to WPF hard coding a size of 16,16 in its styles.
public static Size? DefaultImageSize = new Size(16, 16);
internal static readonly object ImageSize_Key = new object();
}

public class MenuItemHandler<TControl, TWidget, TCallback> : MenuHandler<TControl, TWidget, TCallback>, MenuItem.IHandler, swi.ICommand, IWpfValidateBinding, IMenuItemHandler
where TControl : swc.MenuItem
Expand All @@ -32,14 +39,31 @@ protected virtual void OnClick()
{
Callback.OnClick(Widget, EventArgs.Empty);
}

public Size? ImageSize
{
get => Widget.Properties.Get<Size?>(MenuItemHandler.ImageSize_Key, MenuItemHandler.DefaultImageSize);
set
{
if (Widget.Properties.TrySet(MenuItemHandler.ImageSize_Key, value, MenuItemHandler.DefaultImageSize))
{
OnImageSizeChanged();
}
}
}

protected virtual void OnImageSizeChanged()
{
Control.Icon = image.ToWpfImage(Screen.PrimaryScreen.LogicalPixelSize, ImageSize);
}

public Image Image
{
get { return image; }
get => image;
set
{
image = value;
Control.Icon = image.ToWpfImage(Screen.PrimaryScreen.LogicalPixelSize, new Size(16, 16));
OnImageSizeChanged();
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/Eto.Wpf/Forms/ToolBar/ButtonToolItemHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public class ButtonToolItemHandler : ToolItemHandler<swc.Button, ButtonToolItem>
public ButtonToolItemHandler ()
{
Control = new swc.Button();
swcImage = new swc.Image { MaxHeight = 16, MaxWidth = 16 };
swcImage = new swc.Image();
label = new swc.TextBlock();
label.VerticalAlignment = sw.VerticalAlignment.Center;
label.Margin = new Thickness(2, 0, 2, 0);
var panel = new swc.StackPanel { Orientation = swc.Orientation.Horizontal };
panel.Children.Add(swcImage);
panel.Children.Add(label);
Expand All @@ -27,6 +29,14 @@ public ButtonToolItemHandler ()
sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

protected override void OnImageSizeChanged()
{
base.OnImageSizeChanged();
var size = ImageSize;
swcImage.MaxHeight = size?.Height ?? double.PositiveInfinity;
swcImage.MaxWidth = size?.Width ?? double.PositiveInfinity;
}

private void Control_Click(object sender, RoutedEventArgs e)
{
Widget.OnClick(EventArgs.Empty);
Expand Down
12 changes: 11 additions & 1 deletion src/Eto.Wpf/Forms/ToolBar/CheckToolItemHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public CheckToolItemHandler ()
Control = new swc.Primitives.ToggleButton {
IsThreeState = false
};
swcImage = new swc.Image { MaxHeight = 16, MaxWidth = 16 };
swcImage = new swc.Image();
label = new swc.TextBlock ();
label.Margin = new Thickness(2, 0, 2, 0);
label.VerticalAlignment = sw.VerticalAlignment.Center;
var panel = new swc.StackPanel { Orientation = swc.Orientation.Horizontal };
panel.Children.Add (swcImage);
panel.Children.Add (label);
Expand All @@ -32,6 +34,14 @@ public CheckToolItemHandler ()
sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

protected override void OnImageSizeChanged()
{
base.OnImageSizeChanged();
var size = ImageSize;
swcImage.MaxHeight = size?.Height ?? double.PositiveInfinity;
swcImage.MaxWidth = size?.Width ?? double.PositiveInfinity;
}

private void Control_Click(object sender, RoutedEventArgs e)
{
Widget.OnClick(EventArgs.Empty);
Expand Down
17 changes: 14 additions & 3 deletions src/Eto.Wpf/Forms/ToolBar/DropDownToolItemHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,29 @@ public DropDownToolItemHandler ()
{
root = new swc.MenuItem();
Control = new swc.Menu();
Control.Background = swm.Brushes.Transparent;
Control.Items.Add(root);
swcImage = new swc.Image { MaxHeight = 16, MaxWidth = 16 };
swcImage = new swc.Image();
label = new swc.TextBlock();
arrow = new sw.Shapes.Path { Data = swm.Geometry.Parse("M 0 0 L 3 3 L 6 0 Z"), VerticalAlignment = sw.VerticalAlignment.Center, Margin = new Thickness(8, 2, 0, 0), Fill = swm.Brushes.Black };
label.Margin = new Thickness(2, 0, 2, 0);
label.VerticalAlignment = sw.VerticalAlignment.Center;
arrow = new sw.Shapes.Path { Data = swm.Geometry.Parse("M 0 0 L 3 3 L 6 0 Z"), VerticalAlignment = sw.VerticalAlignment.Center, Margin = new Thickness(2, 2, 0, 0), Fill = swm.Brushes.Black };
var panel = new swc.StackPanel { Orientation = swc.Orientation.Horizontal, Children = { swcImage, label, arrow } };

root.Header = panel;
root.Click += Control_Click;
root.SubmenuOpened += Control_Click;
sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}


protected override void OnImageSizeChanged()
{
base.OnImageSizeChanged();
var size = ImageSize;
swcImage.MaxHeight = size?.Height ?? double.PositiveInfinity;
swcImage.MaxWidth = size?.Width ?? double.PositiveInfinity;
}

private void Control_Click(object sender, RoutedEventArgs e)
{
// WPF raises this event for all child items as well as the root menu, so check sender
Expand Down
13 changes: 12 additions & 1 deletion src/Eto.Wpf/Forms/ToolBar/RadioToolItemHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public RadioToolItemHandler()
{
IsThreeState = false
};
swcImage = new swc.Image { MaxHeight = 16, MaxWidth = 16 };
swcImage = new swc.Image();
label = new swc.TextBlock();
label.Margin = new Thickness(2, 0, 2, 0);
label.VerticalAlignment = sw.VerticalAlignment.Center;
var panel = new swc.StackPanel { Orientation = swc.Orientation.Horizontal };
panel.Children.Add(swcImage);
panel.Children.Add(label);
Expand All @@ -37,6 +39,15 @@ public RadioToolItemHandler()
sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

protected override void OnImageSizeChanged()
{
base.OnImageSizeChanged();
var size = ImageSize;
swcImage.MaxHeight = size?.Height ?? double.PositiveInfinity;
swcImage.MaxWidth = size?.Width ?? double.PositiveInfinity;
}


private void Control_Click(object sender, RoutedEventArgs e)
{
Widget.OnClick(EventArgs.Empty);
Expand Down
3 changes: 2 additions & 1 deletion src/Eto.Wpf/Forms/ToolBar/SeparatorToolItemHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Eto.Forms;
using sw = System.Windows;
using swc = System.Windows.Controls;
using swm = System.Windows.Media;
using Eto.Drawing;
Expand Down Expand Up @@ -40,7 +41,7 @@ public SeparatorToolItemType Type
switch (value)
{
case SeparatorToolItemType.Divider:
control = new swc.Separator { LayoutTransform = new swm.RotateTransform(90) };
control = new swc.Separator { LayoutTransform = new swm.RotateTransform(90), Margin = new sw.Thickness(2, 0, 2, 0) };
break;
case SeparatorToolItemType.FlexibleSpace:
case SeparatorToolItemType.Space:
Expand Down
1 change: 1 addition & 0 deletions src/Eto.Wpf/Forms/ToolBar/ToolBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ToolBarHandler : WidgetHandler<swc.ToolBar, Eto.Forms.ToolBar>, Eto
public ToolBarHandler()
{
Control = new swc.ToolBar { IsTabStop = false, Tag = this };
swc.ToolBarTray.SetIsLocked(Control, true);
swi.KeyboardNavigation.SetTabNavigation(Control, swi.KeyboardNavigationMode.Continue);
}

Expand Down
28 changes: 28 additions & 0 deletions src/Eto.Wpf/Forms/ToolBar/ToolItemHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

namespace Eto.Wpf.Forms.ToolBar
{
public static class ToolItemHandler
{
public static Size? DefaultImageSize = new Size(16, 16);
internal static readonly object ImageSize_Key = new object();
}

public abstract class ToolItemHandler<TControl, TWidget> : WidgetHandler<TControl, TWidget>, ToolItem.IHandler
where TControl : System.Windows.UIElement
where TWidget : ToolItem
Expand All @@ -15,6 +21,28 @@ public abstract class ToolItemHandler<TControl, TWidget> : WidgetHandler<TContro

public abstract Image Image { get; set; }

public Size? ImageSize
{
get => Widget.Properties.Get<Size?>(ToolItemHandler.ImageSize_Key, ToolItemHandler.DefaultImageSize);
set
{
if (Widget.Properties.TrySet(ToolItemHandler.ImageSize_Key, value, ToolItemHandler.DefaultImageSize))
{
OnImageSizeChanged();
}
}
}

protected virtual void OnImageSizeChanged()
{
}

protected override void Initialize()
{
base.Initialize();
OnImageSizeChanged();
}

public abstract bool Enabled { get; set; }
public bool Visible
{
Expand Down

0 comments on commit 82b5ea5

Please sign in to comment.