From f18c26ca090f0bd565e339bdf7fa2d4d2c0c76a5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 10:27:10 +0000 Subject: [PATCH] feat(ui-visuals): add SegmentedControl and SplitButton Segmented single-selection list with a shared border for the view switcher, and a split button whose drop-down menu chooses the item its command uses. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Ui/Visuals/SegmentedControl.cs | 73 +++++++++++++ src/Titano.Ui/Visuals/SplitButton.cs | 126 ++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/Titano.Ui/Visuals/SegmentedControl.cs create mode 100644 src/Titano.Ui/Visuals/SplitButton.cs diff --git a/src/Titano.Ui/Visuals/SegmentedControl.cs b/src/Titano.Ui/Visuals/SegmentedControl.cs new file mode 100644 index 00000000..663ebe9e --- /dev/null +++ b/src/Titano.Ui/Visuals/SegmentedControl.cs @@ -0,0 +1,73 @@ +using System.Collections.Specialized; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using Titano.Ui.Controls; + +namespace Titano.Ui.Visuals; + +/// Where a segment sits in its group; decides which corners are rounded. +public enum SegmentPosition +{ + Only, + First, + Middle, + Last, +} + +/// +/// Grouped buttons with one shared border and a single selected segment in Accent (Library toolbar view +/// switcher: grid / list / timeline). A single-selection list: bind or +/// (with = Tag). Left/Right move the +/// selection; the group is one tab stop. +/// +public class SegmentedControl : ListBox +{ + static SegmentedControl() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(SegmentedControl), new FrameworkPropertyMetadata(typeof(SegmentedControl))); + SelectionModeProperty.OverrideMetadata(typeof(SegmentedControl), new FrameworkPropertyMetadata(SelectionMode.Single)); + KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(SegmentedControl), new FrameworkPropertyMetadata(KeyboardNavigationMode.Once)); + } + + protected override DependencyObject GetContainerForItemOverride() => new SegmentedItem(); + + protected override bool IsItemItsOwnContainerOverride(object item) => item is SegmentedItem; + + protected override void PrepareContainerForItemOverride(DependencyObject element, object item) + { + base.PrepareContainerForItemOverride(element, item); + if (element is SegmentedItem segment) segment.Position = PositionOf(ItemContainerGenerator.IndexFromContainer(element), Items.Count); + } + + protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) + { + base.OnItemsChanged(e); + for (var i = 0; i < Items.Count; i++) + if (ItemContainerGenerator.ContainerFromIndex(i) is SegmentedItem segment) segment.Position = PositionOf(i, Items.Count); + } + + private static SegmentPosition PositionOf(int index, int count) => + count <= 1 ? SegmentPosition.Only : index <= 0 ? SegmentPosition.First : index >= count - 1 ? SegmentPosition.Last : SegmentPosition.Middle; +} + +/// One segment: an icon () and/or content; give it a ToolTip and an automation name. +public class SegmentedItem : ListBoxItem +{ + public static readonly DependencyProperty GlyphProperty = DependencyProperty.Register(nameof(Glyph), typeof(IconData), typeof(SegmentedItem)); + + private static readonly DependencyPropertyKey PositionKey = DependencyProperty.RegisterReadOnly( + nameof(Position), typeof(SegmentPosition), typeof(SegmentedItem), new PropertyMetadata(SegmentPosition.Only)); + + public static readonly DependencyProperty PositionProperty = PositionKey.DependencyProperty; + + static SegmentedItem() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(SegmentedItem), new FrameworkPropertyMetadata(typeof(SegmentedItem))); + } + + public IconData? Glyph { get => (IconData?)GetValue(GlyphProperty); set => SetValue(GlyphProperty, value); } + + /// Place in the group, maintained by . + public SegmentPosition Position { get => (SegmentPosition)GetValue(PositionProperty); internal set => SetValue(PositionKey, value); } +} diff --git a/src/Titano.Ui/Visuals/SplitButton.cs b/src/Titano.Ui/Visuals/SplitButton.cs new file mode 100644 index 00000000..cf86a03b --- /dev/null +++ b/src/Titano.Ui/Visuals/SplitButton.cs @@ -0,0 +1,126 @@ +using System.Collections; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Data; +using System.Windows.Input; + +namespace Titano.Ui.Visuals; + +/// +/// Main action on the left, a ▾ part on the right that drops down a menu of choices (Editor: "Render · preset ▾"). +/// The main part executes ; choosing a menu entry sets (checked in +/// the menu) — bind to it to act on the chosen preset. The control's ToolTip +/// describes the main action, the ▾ part. F4 or Alt+Down opens the menu. +/// gives the accent look of the screen's primary button. +/// +public class SplitButton : Control +{ + public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(SplitButton)); + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(SplitButton)); + public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(nameof(CommandParameter), typeof(object), typeof(SplitButton)); + public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register(nameof(CommandTarget), typeof(IInputElement), typeof(SplitButton)); + public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(IEnumerable), typeof(SplitButton)); + public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(SplitButton)); + public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register(nameof(ItemContainerStyle), typeof(Style), typeof(SplitButton)); + public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register(nameof(DisplayMemberPath), typeof(string), typeof(SplitButton)); + public static readonly DependencyProperty DropDownToolTipProperty = DependencyProperty.Register(nameof(DropDownToolTip), typeof(object), typeof(SplitButton)); + public static readonly DependencyProperty IsPrimaryProperty = DependencyProperty.Register(nameof(IsPrimary), typeof(bool), typeof(SplitButton)); + + public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register( + nameof(SelectedItem), typeof(object), typeof(SplitButton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); + + public static readonly DependencyProperty IsDropDownOpenProperty = DependencyProperty.Register( + nameof(IsDropDownOpen), typeof(bool), typeof(SplitButton), + new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged)); + + private ContextMenu? _menu; + + static SplitButton() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(SplitButton), new FrameworkPropertyMetadata(typeof(SplitButton))); + FocusableProperty.OverrideMetadata(typeof(SplitButton), new FrameworkPropertyMetadata(false)); + } + + /// Content of the main part (e.g. "Render · 4K 10-bit HEVC"). + public object? Content { get => GetValue(ContentProperty); set => SetValue(ContentProperty, value); } + + public ICommand? Command { get => (ICommand?)GetValue(CommandProperty); set => SetValue(CommandProperty, value); } + + public object? CommandParameter { get => GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); } + + public IInputElement? CommandTarget { get => (IInputElement?)GetValue(CommandTargetProperty); set => SetValue(CommandTargetProperty, value); } + + /// Choices listed in the drop-down menu. + public IEnumerable? ItemsSource { get => (IEnumerable?)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } + + public DataTemplate? ItemTemplate { get => (DataTemplate?)GetValue(ItemTemplateProperty); set => SetValue(ItemTemplateProperty, value); } + + /// Style of the menu items (e.g. to bind a ToolTip per choice). + public Style? ItemContainerStyle { get => (Style?)GetValue(ItemContainerStyleProperty); set => SetValue(ItemContainerStyleProperty, value); } + + public string? DisplayMemberPath { get => (string?)GetValue(DisplayMemberPathProperty); set => SetValue(DisplayMemberPathProperty, value); } + + /// The chosen entry (two-way). + public object? SelectedItem { get => GetValue(SelectedItemProperty); set => SetValue(SelectedItemProperty, value); } + + public bool IsDropDownOpen { get => (bool)GetValue(IsDropDownOpenProperty); set => SetValue(IsDropDownOpenProperty, value); } + + /// Tooltip of the ▾ part. + public object? DropDownToolTip { get => GetValue(DropDownToolTipProperty); set => SetValue(DropDownToolTipProperty, value); } + + /// Accent fill (the one primary action of a screen). + public bool IsPrimary { get => (bool)GetValue(IsPrimaryProperty); set => SetValue(IsPrimaryProperty, value); } + + protected override void OnKeyDown(KeyEventArgs e) + { + base.OnKeyDown(e); + if (e.Handled) return; + var key = e.Key == Key.System ? e.SystemKey : e.Key; + if (key == Key.F4 || (key == Key.Down && (Keyboard.Modifiers & ModifierKeys.Alt) != 0)) + { + SetCurrentValue(IsDropDownOpenProperty, true); + e.Handled = true; + } + } + + private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var button = (SplitButton)d; + if ((bool)e.NewValue) button.EnsureMenu().IsOpen = true; + else if (button._menu is { IsOpen: true } menu) menu.IsOpen = false; + } + + private ContextMenu EnsureMenu() + { + if (_menu is not null) return _menu; + var menu = new ContextMenu { PlacementTarget = this, Placement = PlacementMode.Bottom, VerticalOffset = 4 }; + menu.SetBinding(ItemsControl.ItemsSourceProperty, new Binding(nameof(ItemsSource)) { Source = this }); + menu.SetBinding(ItemsControl.ItemTemplateProperty, new Binding(nameof(ItemTemplate)) { Source = this }); + menu.SetBinding(ItemsControl.ItemContainerStyleProperty, new Binding(nameof(ItemContainerStyle)) { Source = this }); + menu.SetBinding(ItemsControl.DisplayMemberPathProperty, new Binding(nameof(DisplayMemberPath)) { Source = this }); + menu.SetBinding(DataContextProperty, new Binding(nameof(DataContext)) { Source = this }); + menu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick)); + menu.Opened += (_, _) => MarkSelected(); + menu.Closed += (_, _) => SetCurrentValue(IsDropDownOpenProperty, false); + _menu = menu; + return menu; + } + + private void OnMenuItemClick(object sender, RoutedEventArgs e) + { + if (_menu is null || e.OriginalSource is not MenuItem container) return; + var item = _menu.ItemContainerGenerator.ItemFromContainer(container); + if (item != DependencyProperty.UnsetValue) SetCurrentValue(SelectedItemProperty, item); + } + + /// Checks the menu entry of the current choice. + private void MarkSelected() + { + if (_menu is null) return; + _menu.UpdateLayout(); + for (var i = 0; i < _menu.Items.Count; i++) + if (_menu.ItemContainerGenerator.ContainerFromIndex(i) is MenuItem container) + container.IsChecked = Equals(_menu.Items[i], SelectedItem); + } +}