Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix odd sizes of toolitems when there is no label #2483

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
}
44 changes: 3 additions & 41 deletions src/Eto.Wpf/Forms/ToolBar/DropDownToolItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,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 +19,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 +40,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