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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+199
No files matched your search
@@ -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;
|
||||
|
||||
/// <summary>Where a segment sits in its group; decides which corners are rounded.</summary>
|
||||
public enum SegmentPosition
|
||||
{
|
||||
Only,
|
||||
First,
|
||||
Middle,
|
||||
Last,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="Selector.SelectedIndex"/> or
|
||||
/// <see cref="Selector.SelectedValue"/> (with <see cref="Selector.SelectedValuePath"/> = Tag). Left/Right move the
|
||||
/// selection; the group is one tab stop.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>One segment: an icon (<see cref="Glyph"/>) and/or content; give it a ToolTip and an automation name.</summary>
|
||||
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); }
|
||||
|
||||
/// <summary>Place in the group, maintained by <see cref="SegmentedControl"/>.</summary>
|
||||
public SegmentPosition Position { get => (SegmentPosition)GetValue(PositionProperty); internal set => SetValue(PositionKey, value); }
|
||||
}
|
||||
Reference in new issue
Block a user