Skip to content

Commit

Permalink
Wpf: Fix odd sizes of toolitems when there is no label
Browse files Browse the repository at this point in the history
Also refactor to share more code.
  • Loading branch information
cwensley committed May 19, 2023
1 parent 82b5ea5 commit 23fb7ad
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 166 deletions.
49 changes: 4 additions & 45 deletions src/Eto.Wpf/Forms/ToolBar/ButtonToolItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,33 @@
using swc = System.Windows.Controls;
using swm = System.Windows.Media;
using sw = System.Windows;
using System.Windows;

namespace Eto.Wpf.Forms.ToolBar
{
public class ButtonToolItemHandler : ToolItemHandler<swc.Button, ButtonToolItem>, ButtonToolItem.IHandler
{
Image image;
readonly swc.Image swcImage;
readonly swc.TextBlock label;

public ButtonToolItemHandler ()
{
Control = new swc.Button();
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);
Control.Content = panel;
Control.Click += Control_Click;
sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

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

private void Control_Click(object sender, RoutedEventArgs e)
private void Control_Click(object sender, sw.RoutedEventArgs e)
{
Widget.OnClick(EventArgs.Empty);
}

public override string Text
{
get { return label.Text.ToEtoMnemonic(); }
set { label.Text = value.ToPlatformMnemonic(); }
}

public override string ToolTip
{
get { return Control.ToolTip as string; }
set { Control.ToolTip = value; }
}

public override Image Image
{
get { return image; }
set
{
image = value;
swcImage.Source = image.ToWpf(Screen.PrimaryScreen.LogicalPixelSize, swcImage.GetMaxSize().ToEtoSize());
}
}

public override bool Enabled
{
get { return Control.IsEnabled; }
set
{
Control.IsEnabled = value;
swcImage.IsEnabled = value;
}
}
}
}
44 changes: 3 additions & 41 deletions src/Eto.Wpf/Forms/ToolBar/CheckToolItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,21 @@ namespace Eto.Wpf.Forms.ToolBar
{
public class CheckToolItemHandler : ToolItemHandler<swc.Primitives.ToggleButton, CheckToolItem>, CheckToolItem.IHandler
{
Image image;
readonly swc.Image swcImage;
readonly swc.TextBlock label;
public CheckToolItemHandler ()
{
Control = new swc.Primitives.ToggleButton {
IsThreeState = false
};
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);
Control.Content = panel;

Control.Checked += Control_CheckedChanged;
Control.Unchecked += Control_CheckedChanged;
Control.Click += Control_Click;

sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

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

private void Control_Click(object sender, RoutedEventArgs e)
Expand All @@ -57,33 +42,10 @@ public bool Checked
get { return Control.IsChecked ?? false; }
set { Control.IsChecked = value; }
}

public override string Text
{
get { return label.Text.ToEtoMnemonic(); }
set { label.Text = value.ToPlatformMnemonic(); }
}

public override string ToolTip
{
get { return Control.ToolTip as string; }
set { Control.ToolTip = value; }
}

public override Image Image
{
get { return image; }
set
{
image = value;
swcImage.Source = image.ToWpf(Screen.PrimaryScreen.LogicalPixelSize, swcImage.GetMaxSize().ToEtoSize());
}
}

public override bool Enabled
{
get { return Control.IsEnabled; }
set { Control.IsEnabled = value; }
}
}
}
43 changes: 3 additions & 40 deletions src/Eto.Wpf/Forms/ToolBar/DropDownToolItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Eto.Wpf.Forms.ToolBar
public class DropDownToolItemHandler : ToolItemHandler<swc.Menu, DropDownToolItem>, DropDownToolItem.IHandler
{
Image image;
readonly swc.Image swcImage;
readonly swc.TextBlock label;
readonly swc.MenuItem root;
readonly sw.Shapes.Path arrow;

Expand All @@ -22,25 +20,16 @@ public DropDownToolItemHandler ()
Control = new swc.Menu();
Control.Background = swm.Brushes.Transparent;
Control.Items.Add(root);
swcImage = new swc.Image();
label = new swc.TextBlock();
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()
protected override void Initialize()
{
base.OnImageSizeChanged();
var size = ImageSize;
swcImage.MaxHeight = size?.Height ?? double.PositiveInfinity;
swcImage.MaxWidth = size?.Width ?? double.PositiveInfinity;
base.Initialize();
root.Header = CreateContent(arrow);
}

private void Control_Click(object sender, RoutedEventArgs e)
Expand All @@ -52,38 +41,12 @@ private void Control_Click(object sender, RoutedEventArgs e)
Widget.OnClick(EventArgs.Empty);
}

public override string Text
{
get { return label.Text.ToEtoMnemonic(); }
set { label.Text = value.ToPlatformMnemonic(); }
}

public override string ToolTip
{
get { return Control.ToolTip as string; }
set { Control.ToolTip = value; }
}

public override Image Image
{
get { return image; }
set
{
image = value;
swcImage.Source = image.ToWpf(Screen.PrimaryScreen.LogicalPixelSize, swcImage.GetMaxSize().ToEtoSize());
}
}

public override bool Enabled
{
get { return Control.IsEnabled; }
set
{
Control.IsEnabled = value;
swcImage.IsEnabled = value;
}
}

/// <summary>
/// Gets or sets whether the drop arrow is shown on the button.
/// </summary>
Expand Down
40 changes: 3 additions & 37 deletions src/Eto.Wpf/Forms/ToolBar/RadioToolItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,24 @@ namespace Eto.Wpf.Forms.ToolBar
{
public class RadioToolItemHandler : ToolItemHandler<swc.Primitives.ToggleButton, RadioToolItem>, RadioToolItem.IHandler
{
Image image;
readonly swc.Image swcImage;
readonly swc.TextBlock label;

public RadioToolItemHandler()
{
Control = new swc.Primitives.ToggleButton
{
IsThreeState = false
};
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);
Control.Content = panel;

Control.Checked += Control_Checked;
Control.Unchecked += Control_Unchecked;
Control.PreviewMouseDown += Control_PreviewMouseDown;
Control.Click += Control_Click;

sw.Automation.AutomationProperties.SetLabeledBy(Control, label);
}

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


private void Control_Click(object sender, RoutedEventArgs e)
{
Widget.OnClick(EventArgs.Empty);
Expand Down Expand Up @@ -90,28 +72,12 @@ public bool Checked
set { Control.IsChecked = value; }
}

public override string Text
{
get { return label.Text.ToEtoMnemonic(); }
set { label.Text = value.ToPlatformMnemonic(); }
}

public override string ToolTip
{
get { return Control.ToolTip as string; }
set { Control.ToolTip = value; }
}

public override Image Image
{
get { return image; }
set
{
image = value;
swcImage.Source = image.ToWpf(Screen.PrimaryScreen.LogicalPixelSize, swcImage.GetMaxSize().ToEtoSize());
}
}

public override bool Enabled
{
get { return Control.IsEnabled; }
Expand Down
Loading

0 comments on commit 23fb7ad

Please sign in to comment.