diff --git a/src/client/spark/Images/Icons.cs b/src/client/spark/Images/Icons.cs index 4223bde21..f342bf9b7 100644 --- a/src/client/spark/Images/Icons.cs +++ b/src/client/spark/Images/Icons.cs @@ -45,6 +45,7 @@ namespace mbts.ui { KeyGeometryKey = nameof(KeyGeometryKey), LetterAKey = nameof(LetterAKey), LetterDKey = nameof(LetterDKey), + MenuOrientationGeometryKey = nameof(MenuOrientationGeometryKey), OrdersGeometryKey = nameof(OrdersGeometryKey), PauseGeometryKey = nameof(PauseGeometryKey), PaymentGeometryKey = nameof(PaymentGeometryKey), @@ -179,6 +180,7 @@ namespace mbts.ui { public static ImageSource Import => load_resource_and_freeze("Import"); public static ImageSource Key => create_icon(KeyGeometryKey); public static ImageSource Knight { get { return load_resource_and_freeze("Knight"); } } + public static ImageSource MenuOrientation => create_icon(MenuOrientationGeometryKey); public static ImageSource Pause => create_icon(PauseGeometryKey); public static ImageSource Payment => create_icon(PaymentGeometryKey); public static ImageSource Pencil => create_icon(PencilGeometryKey); diff --git a/src/client/spark/Images/Vector/Icons.xaml b/src/client/spark/Images/Vector/Icons.xaml index 3b4f882f6..f87f1de82 100644 --- a/src/client/spark/Images/Vector/Icons.xaml +++ b/src/client/spark/Images/Vector/Icons.xaml @@ -280,6 +280,10 @@ M22.399981,2.4000006C22.099982,2.4000006 21.899982,2.5 21.599982,2.6999998 21.199983,3.1000004 21.199983,3.8000002 21.599982,4.2000003L26.199979,8.9000001C26.599978,9.3000002 27.299977,9.3000002 27.699977,8.9000001 28.099977,8.5000002 28.099977,7.8000002 27.699977,7.4000006L23.099981,2.6999998C22.899981,2.5,22.699981,2.4000006,22.399981,2.4000006z M21.499982,0C22.299982,0,23.199981,0.30000019,23.79998,0.90000057L29.299976,6.4000001C30.599975,7.7000003,30.599975,9.7000003,29.299976,11L22.599981,17.7C21.299983,19,19.299984,19,17.999985,17.7L17.299985,17 12.699989,21.6 10.399991,20.900001 10.899991,23.400001 8.4999928,22.8 8.9999924,25.5 6.2999945,25.1 6.7999945,27.6 4.2999964,27 4.7999954,29.6 2.4999971,32 0,26.6 13.499989,13.1 16.999986,16.6 12.699989,12.3C11.399991,11,11.399991,9.0000002,12.699989,7.7000003L19.399984,1C19.899983,0.30000019,20.699983,0,21.499982,0z + + M14.997001,19.999038L15.000001,23.499012C15.000001,24.327005,14.327008,25,13.500016,25L9.9970522,25 9.9970522,23.000015 13.000022,23.000015 12.997022,20.001038z M9.000001,19.000006L9.000001,30.000006 30.000001,30.000006 30.000001,19.000006z M2,2L2,23 7.000001,23 7.000001,18.500006C7.000001,17.673006,7.6730013,17.000006,8.500001,17.000006L13.000001,17.000006 13.000001,2z M1.5,0L13.500001,0C14.327001,0,15.000001,0.67300034,15.000001,1.5L15.000001,17.000006 26.956518,17.000006 23.162404,13.207182C22.772376,12.816143 22.772376,12.184205 23.162404,11.793225 23.357907,11.597736 23.613928,11.499992 23.869827,11.499992 24.125725,11.499992 24.381502,11.597736 24.576517,11.793225L25.889371,13.105454 25.866285,12.914201C25.554945,10.547287 24.699391,8.7684691 23.308905,7.6089654 21.974919,6.4954615 20.356436,6.1229606 19.066825,6.0297103L19.059648,6.0292783 20.545799,7.5160289C20.936786,7.9070184 20.936786,8.5390015 20.545799,8.929991 20.350807,9.1249857 20.094815,9.2229831 19.838825,9.2229831 19.582834,9.2229831 19.326843,9.1249857 19.131849,8.929991L15.185988,4.9830971 18.981854,1.1871986C19.371841,0.79620934 20.004819,0.79620934 20.395805,1.1871986 20.785791,1.5771885 20.785791,2.2101717 20.395805,2.601161L18.976535,4.020443 18.999199,4.0214248C20.641604,4.1119318 22.785724,4.567862 24.590892,6.0739603 26.493747,7.660778 27.606404,10.054969 27.91277,13.209869L27.913735,13.221022 29.341699,11.793225C29.536714,11.597736 29.792459,11.499992 30.048328,11.499992 30.304196,11.499992 30.560187,11.597736 30.75569,11.793225 31.146694,12.184205 31.146694,12.816143 30.75569,13.207182L26.962552,17.000006 30.500001,17.000006C31.327001,17.000006,32.000001,17.673006,32.000001,18.500006L32.000001,30.500006C32.000001,31.327005,31.327001,32.000006,30.500001,32.000006L8.500001,32.000006C7.6730013,32.000006,7.000001,31.327005,7.000001,30.500006L7.000001,25 1.5,25C0.67300034,25,0,24.327001,0,23.5L0,1.5C0,0.67300034,0.67300034,0,1.5,0z + + M50.14375,33.389126C50.416863,33.394688,50.653496,33.587773,50.705998,33.864617L50.015995,45.114388C49.810295,46.291462,49.130394,45.097489,49.130394,45.097489L47.65659,42.640538C42.747776,49.635197 34.633152,54.221001 25.433924,54.221001 14.884193,54.221001 5.7437552,48.204125 1.2449617,39.419205 0.80087067,38.552021 0.21358696,37.705738 0.052086769,36.736958 -0.49735315,33.490925 3.4507786,32.901036 4.8049124,34.432404 5.3674143,35.067692 6.034036,36.16407 6.5079177,37.026051 10.106778,43.799416 17.2321,48.413819 25.433924,48.413819 32.656544,48.413819 39.026364,44.826695 42.903977,39.351506L40.229367,38.854115C40.229367,38.854115,38.677464,38.647118,39.742466,37.996033L50.024895,33.398526C50.064972,33.391365,50.104733,33.388333,50.14375,33.389126z M23.030291,9.0310004L27.889918,9.0310004 27.889918,13.028299C30.539786,13.205498,32.324932,13.958096,33.464161,14.528294L33.964176,14.775793 32.417435,20.038878 31.706417,19.68068C30.579987,19.11958 28.839241,18.244583 26.149073,18.244583 24.006916,18.244583 22.723183,19.122181 22.723183,20.590876 22.723183,21.894272 23.943314,22.804369 27.551308,24.187165 32.706444,26.124659 34.911,28.702853 34.911,32.817341 34.911,36.892929 32.090527,40.05172 27.673611,40.989117L27.673611,45.540001 22.767485,45.540001 22.767485,41.330414C20.252919,41.155815,17.695551,40.424118,16.101811,39.404721L15.683999,39.137619 17.221739,33.731438 17.969059,34.180634C19.088988,34.851235 21.402748,35.97873 24.340325,35.97873 26.734886,35.97873 28.276728,34.921635 28.276728,33.291336 28.276728,31.986641 27.547409,30.853846 23.986016,29.57255 20.083815,28.144253 16.095511,26.04406 16.095511,21.239174 16.095511,17.280985 18.737379,14.291395 23.030291,13.329098z M25.268765,0C35.819447,2.7347414E-07 44.961449,6.0155495 49.458798,14.804573 49.902695,15.669173 50.490094,16.516972 50.651287,17.48427 51.198281,20.734265 47.255726,21.318962 45.901341,19.789065 45.336346,19.153567 44.669552,18.051968 44.19566,17.19517 40.5957,10.421881 33.473275,5.8072398 25.268765,5.8072393 18.047545,5.8072398 11.678013,9.3918336 7.8003562,14.867073L10.474627,15.367072C10.474627,15.367072,12.026909,15.574172,10.962921,16.223872L0.67933281,20.826762C0.36037083,20.880062,0.057029126,20.669165,0.00099972012,20.354063L0.69093342,9.1118043C0.89552094,7.9296158,1.5738537,9.124924,1.5738539,9.124924L3.0452772,11.583379C7.9566038,4.584462,16.073666,2.7347414E-07,25.268765,0z diff --git a/src/client/spark/Themes/generic.xaml b/src/client/spark/Themes/generic.xaml index c9f6ac225..43e8de107 100644 --- a/src/client/spark/Themes/generic.xaml +++ b/src/client/spark/Themes/generic.xaml @@ -525,8 +525,11 @@ @@ -546,7 +549,11 @@ BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" > - + @@ -559,4 +566,4 @@ - \ No newline at end of file + diff --git a/src/client/spark/core/Bus/SparkCommand.cs b/src/client/spark/core/Bus/SparkCommand.cs index a52eb45fc..c329240e3 100644 --- a/src/client/spark/core/Bus/SparkCommand.cs +++ b/src/client/spark/core/Bus/SparkCommand.cs @@ -1,10 +1,15 @@ using bts.api; using mbts; using mbts.ui; +using mbts.ui.utility; using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using static mbts.spark.SparkTags; @@ -60,6 +65,7 @@ namespace bts.spark { } } } + public ObservableCollection child_items { get; } = new ObservableCollection(); public abstract void execute(IBus bus); public virtual bool canExecute() => true; @@ -328,6 +334,8 @@ namespace bts.spark { public class SparkCommandVM : mbts.ui.shell.ViewModelBase, ICommand { readonly SparkCommand _cmd; readonly IBus _bus; + readonly ISelectable _selectable_cmd_backing; + object _first_child_group; public SparkCommandVM(SparkCommand cmd, IBus bus) { _cmd = cmd; @@ -336,6 +344,20 @@ namespace bts.spark { cmd.group_changed += () => RaisePropertyChanged(nameof(group)); cmd.show_in_toolbar_changed += () => RaisePropertyChanged(nameof(show_in_toolbar)); cmd.show_in_context_menu_changed += () => RaisePropertyChanged(nameof(show_in_context_menu)); + if (cmd.child_items != null) { + ObservableAutoWrapper.connect(cmd.child_items, child_items, item => new SparkCommandVM(item, bus)); + child_items_view = new ListCollectionView(child_items); + child_items_view.GroupDescriptions.Add(new PropertyGroupDescription(nameof(group))); + if (child_items_view.Groups is INotifyCollectionChanged child_groups_ncc) { + child_groups_ncc.Subscribe((s, e) => refresh_first_child_group()); + refresh_first_child_group(); + void refresh_first_child_group() => + first_child_group = (child_items_view.Groups.FirstOrDefault() as CollectionViewGroup)?.Name; + } + } + _selectable_cmd_backing = cmd as ISelectable; + if (_selectable_cmd_backing != null) + _selectable_cmd_backing.is_selected_changed += _ => RaisePropertyChanged(nameof(is_selected)); } public string title => _cmd.title; @@ -344,7 +366,22 @@ namespace bts.spark { public ImageSource icon => _cmd.icon; public bool show_in_toolbar => _cmd.show_in_toolbar; public bool show_in_context_menu => _cmd.show_in_context_menu; + public bool is_selectable => _selectable_cmd != null; + public bool is_selected { + get => _selectable_cmd.is_selected; + set => _selectable_cmd.is_selected = value; + } public ICommand command => this; + public ObservableCollection child_items { get; } = new ObservableCollection(); + public ListCollectionView child_items_view { get; } + public object first_child_group { + get => _first_child_group; + set => Set(ref _first_child_group, value); + } + + ISelectable _selectable_cmd => + _selectable_cmd_backing ?? throw new InvalidOperationException($"Underlying {typeof(SparkCommand)} does not implement {typeof(ISelectable)}."); + event EventHandler ICommand.CanExecuteChanged { add { _cmd.CanExecuteChanged += value; } remove { _cmd.CanExecuteChanged -= value; } } bool ICommand.CanExecute(object parameter) => _cmd.canExecute(); diff --git a/src/client/spark/core/Commands/DelegateCommand.cs b/src/client/spark/core/Commands/DelegateCommand.cs index 6e4ae58b2..935afad47 100644 --- a/src/client/spark/core/Commands/DelegateCommand.cs +++ b/src/client/spark/core/Commands/DelegateCommand.cs @@ -37,6 +37,8 @@ namespace mbts.ui } public class DelegateCommand : DelegateCommand { + public static DelegateCommand empty => new DelegateCommand(() => { }); + public DelegateCommand(Action execute) : this(_ => execute()) { } public DelegateCommand(Action execute, Func can_execute) : this(_ => execute(), _ => can_execute()) { } public DelegateCommand(Action execute) : base(execute) { } diff --git a/src/client/spark/view/Controls/SparkMenu/SparkMenu.cs b/src/client/spark/view/Controls/SparkMenu/SparkMenu.cs index 5847111cd..187cab752 100644 --- a/src/client/spark/view/Controls/SparkMenu/SparkMenu.cs +++ b/src/client/spark/view/Controls/SparkMenu/SparkMenu.cs @@ -1,5 +1,6 @@ using bts.api; using bts.spark; +using mbts.ui.converters; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -25,11 +26,33 @@ namespace mbts.ui.controls { ItemsSource = items_view; } + public static IValueConverter ItemContentFlowDirectionConverter = + ValueConverter.create(o => { + switch (o) { + case Orientation.Horizontal: return ContentFlowDirection.TopToBottom; + case Orientation.Vertical: return ContentFlowDirection.LeftToRight; + default: return ContentFlowDirection.LeftToRight; + } + }); + + #region ShowText + public static readonly DependencyProperty ShowTextProperty = + DependencyProperty.Register("ShowText", typeof(bool), typeof(SparkMenu), new PropertyMetadata(true)); + public bool ShowText { get { return (bool)GetValue(ShowTextProperty); } set { SetValue(ShowTextProperty, value); } } - public static readonly DependencyProperty ShowTextProperty = - DependencyProperty.Register("ShowText", typeof(bool), typeof(SparkMenu), new PropertyMetadata(true)); + #endregion + + #region Orientation + public static readonly DependencyProperty OrientationProperty = + StackPanel.OrientationProperty.AddOwner(typeof(SparkMenu), new PropertyMetadata(Orientation.Vertical)); + + public Orientation Orientation { + get => (Orientation)GetValue(OrientationProperty); + set => SetValue(OrientationProperty, value); + } + #endregion } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/SparkMenu/SparkMenuItem.cs b/src/client/spark/view/Controls/SparkMenu/SparkMenuItem.cs index 1d1ee666c..a42fab2d8 100644 --- a/src/client/spark/view/Controls/SparkMenu/SparkMenuItem.cs +++ b/src/client/spark/view/Controls/SparkMenu/SparkMenuItem.cs @@ -3,7 +3,14 @@ using System.Windows.Controls; using System.Windows.Media; namespace mbts.ui.controls { - public class SparkMenuItem : Button { + enum ContentFlowDirection { + LeftToRight, + RightToLeft, + TopToBottom, + BottomToTop + } + + class SparkMenuItem : Button { static SparkMenuItem() => DefaultStyleKeyProperty.OverrideMetadata(typeof(SparkMenuItem), new FrameworkPropertyMetadata(typeof(SparkMenuItem))); @@ -22,5 +29,17 @@ namespace mbts.ui.controls { get { return (bool)GetValue(CollapsedProperty); } set { SetValue(CollapsedProperty, value); } } + + #region ContentFlowDirection + public static readonly DependencyProperty ContentFlowDirectionProperty = + WPFHelper.create_dp( + nameof(ContentFlowDirection), + ContentFlowDirection.LeftToRight); + + public ContentFlowDirection ContentFlowDirection { + get => (ContentFlowDirection)GetValue(ContentFlowDirectionProperty); + set => SetValue(ContentFlowDirectionProperty, value); + } + #endregion } } diff --git a/src/client/spark/view/Converters/ImageSourceIconConverter.cs b/src/client/spark/view/Converters/ImageSourceIconConverter.cs index fde913bf2..ce91e925f 100644 --- a/src/client/spark/view/Converters/ImageSourceIconConverter.cs +++ b/src/client/spark/view/Converters/ImageSourceIconConverter.cs @@ -5,11 +5,11 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; -namespace mbts.ui.converters -{ +namespace mbts.ui.converters { [ValueConversion(typeof(ImageSource), typeof(Image))] - internal class ImageSourceIconConverter : IValueConverter - { + class ImageSourceIconConverter : IValueConverter { + public static ImageSourceIconConverter Instance => new ImageSourceIconConverter(); + public Thickness Margin { get; set; } = new Thickness(0); public double Width { get; set; } = double.NaN; public double Height { get; set; } = double.NaN; diff --git a/src/client/spark/view/Resources/Buttons.xaml b/src/client/spark/view/Resources/Buttons.xaml index c7e524120..4910bcc05 100644 --- a/src/client/spark/view/Resources/Buttons.xaml +++ b/src/client/spark/view/Resources/Buttons.xaml @@ -1,9 +1,12 @@  + xmlns:controls="clr-namespace:mbts.ui.controls" + xmlns:cv="clr-namespace:mbts.ui.converters" +> @@ -89,14 +92,35 @@ - - + > + + + + + + + + + + + + + + + + + + + + + @@ -124,17 +148,15 @@ Fill="{mbts:BTSColor UI.transparent}" Margin="-10 0 0 0" /> - - - + + + - - - + + + - @@ -239,6 +261,38 @@ + + - + - + ToolTip="{Binding MenuDisplayState, RelativeSource={RelativeSource AncestorType=bv:SparkWindowView}, Converter={x:Static bv:SparkWindowView.ExpanderToolTipConverter}}" + > + + + + + + + - - - - - - - - - - - - - - - + - \ No newline at end of file + diff --git a/src/client/spark/view/Spark/SparkWindowView.xaml.cs b/src/client/spark/view/Spark/SparkWindowView.xaml.cs index f4c9f2ad7..685a0316f 100644 --- a/src/client/spark/view/Spark/SparkWindowView.xaml.cs +++ b/src/client/spark/view/Spark/SparkWindowView.xaml.cs @@ -1,32 +1,80 @@ using bts.api; using bts.spark; +using mbts.ui; using mbts.ui.controls; +using mbts.ui.converters; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Reactive.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Animation; namespace bts.view { - public partial class SparkWindowView : UserControl { + enum SparkMenuDisplayState { + // Don't change the default value of this enum + // (it may be stored in protobufs). + Collapsed = 0, + Expanded, + Hidden + } + + partial class SparkWindowView : UserControl { + public static readonly GridLength ExpanderControlLength = new GridLength(5, GridUnitType.Pixel); + + public static IMultiValueConverter ExpanderCursorConverter => + ValueConverter.create((display_state, dock) => { + bool is_expanded = display_state == SparkMenuDisplayState.Expanded; + switch (dock) { + case Dock.Left: return is_expanded ? Cursors.ScrollW : Cursors.ScrollE; + case Dock.Top: return is_expanded ? Cursors.ScrollN : Cursors.ScrollS; + case Dock.Right: return is_expanded ? Cursors.ScrollE : Cursors.ScrollW; + case Dock.Bottom: return is_expanded ? Cursors.ScrollS : Cursors.ScrollN; + default: return Cursors.Arrow; + } + }); + + public static IValueConverter ExpanderToolTipConverter => + ValueConverter.create(display_state => { + switch (display_state) { + case SparkMenuDisplayState.Hidden: return "Show Menu"; + case SparkMenuDisplayState.Collapsed: return "Expand"; + case SparkMenuDisplayState.Expanded: return "Collapse"; + default: return "(N/A)"; + } + }); + readonly SparkMenu _menu; public SparkWindowView(IBus bus, IEnumerable commands, UIElement content) { InitializeComponent(); Grid.SetColumn(content, 2); - Grid.SetRowSpan(content, 2); + Grid.SetRowSpan(content, 3); _grid.Children.Add(content); if (commands != null && commands.Any()) { - expand_button.Click += collapse_click; - _menu = new SparkMenu(commands, bus); + _menu = new SparkMenu(commands, bus) { RenderTransform = new RotateTransform() }; + _menu.SetBinding(VisibilityProperty, new Binding { + Path = new PropertyPath(MenuDisplayStateProperty), + Source = this, + Converter = ValueConverter.create(mds => mds == SparkMenuDisplayState.Hidden ? Visibility.Collapsed : Visibility.Visible) + }); + _menu.SetBinding(SparkMenu.ShowTextProperty, new Binding { + Path = new PropertyPath(MenuDisplayStateProperty), + Mode = BindingMode.OneWay, + Source = this, + Converter = ValueConverter.create(mds => mds == SparkMenuDisplayState.Expanded) + }); Grid.SetColumn(_menu, 0); Grid.SetRow(_menu, 0); + Grid.SetRowSpan(_menu, 3); _menu.BorderThickness = new Thickness(0, 0, 0, 0); - _menu.ShowText = false; _grid.Children.Add(_menu); foreach (var cmd in commands) { if (cmd.key_gesture != null) @@ -34,6 +82,12 @@ namespace bts.view { } if (ContextMenu != null) { // ObservableCollection to avoid WPF Binding Leak + ContextMenu.GroupStyle.Add(new GroupStyle { + ContainerStyle = Resources["ContextMenuRootGroupItemContainerStyle"] as Style + }); + ContextMenu.GroupStyle.Add(new GroupStyle { + ContainerStyle = Resources["ContextMenuGroupItemContainerStyle"] as Style + }); ContextMenuItems = new ObservableCollection(commands.Select(c => new SparkCommandVM(c, bus))); var context_menu_items_view = new ListCollectionView(ContextMenuItems) { Filter = item => (item as SparkCommandVM)?.show_in_context_menu ?? false @@ -42,6 +96,8 @@ namespace bts.view { context_menu_items_view.LiveFilteringProperties.Add(nameof(SparkCommandVM.show_in_context_menu)); ContextMenu.ItemsSource = context_menu_items_view; } + VisualStateManager.GetVisualStateGroups(this).Add(DockStates.get(this, _grid, content, _menu, expand_button)); + expand_button.Command = new DelegateCommand(() => SetCurrentValue(MenuDisplayStateProperty, MenuDisplayState == SparkMenuDisplayState.Collapsed ? SparkMenuDisplayState.Expanded : SparkMenuDisplayState.Collapsed)); } else { expand_button.Visibility = Visibility.Collapsed; ContextMenu = null; @@ -50,8 +106,112 @@ namespace bts.view { public ObservableCollection ContextMenuItems { get; } - public void collapse_click(object sender, RoutedEventArgs e) { - _menu.ShowText = !_menu.ShowText; + #region Dock + public static readonly DependencyProperty MenuDockProperty = + WPFHelper.create_dp( + nameof(MenuDock), + (o, v) => StateManager.GoToState(o, v.ToString(), _ => true), + Dock.Left); + + public Dock MenuDock { + get => (Dock)GetValue(MenuDockProperty); + set => SetValue(MenuDockProperty, value); + } + #endregion + + #region MenuDislpayState + public static readonly DependencyProperty MenuDisplayStateProperty = + WPFHelper.create_dp(nameof(MenuDisplayState), SparkMenuDisplayState.Collapsed); + + public SparkMenuDisplayState MenuDisplayState { + get => (SparkMenuDisplayState)GetValue(MenuDisplayStateProperty); + set => SetValue(MenuDisplayStateProperty, value); + } + #endregion + + static class DockStates { + static readonly PropertyPath + _column_prop = new PropertyPath(Grid.ColumnProperty), + _column_span_prop = new PropertyPath(Grid.ColumnSpanProperty), + _column_width_prop = new PropertyPath(ColumnDefinition.WidthProperty), + _row_prop = new PropertyPath(Grid.RowProperty), + _row_span_prop = new PropertyPath(Grid.RowSpanProperty), + _row_height_prop = new PropertyPath(RowDefinition.HeightProperty), + _menu_orientation_prop = new PropertyPath(SparkMenu.OrientationProperty); + + public static VisualStateGroup get(SparkWindowView spark_window_view, Grid grid, UIElement content, SparkMenu menu, Button expander) { + var vsg = new VisualStateGroup(); + var control_lengths = new[] { GridLength.Auto, GridLength.Auto, new GridLength(1, GridUnitType.Star) }; + // left + vsg.States.Add(_dock(Names.Left, spark_window_view, grid, content, menu, expander, _row_prop, _row_span_prop, _column_prop, _column_span_prop, 0, 1, 2, + new[] { GridLength.Auto, ExpanderControlLength, new GridLength(1, GridUnitType.Star) }, control_lengths)); + // top + vsg.States.Add(_dock(Names.Top, spark_window_view, grid, content, menu, expander, _column_prop, _column_span_prop, _row_prop, _row_span_prop, 0, 1, 2, + control_lengths, new[] { GridLength.Auto, ExpanderControlLength, new GridLength(1, GridUnitType.Star) })); + // right + vsg.States.Add(_dock(Names.Right, spark_window_view, grid, content, menu, expander, _row_prop, _row_span_prop, _column_prop, _column_span_prop, 2, 1, 0, + new[] { new GridLength(1, GridUnitType.Star), ExpanderControlLength, GridLength.Auto }, control_lengths)); + // bottom + vsg.States.Add(_dock(Names.Bottom, spark_window_view, grid, content, menu, expander, _column_prop, _column_span_prop, _row_prop, _row_span_prop, 2, 1, 0, + control_lengths, new[] { new GridLength(1, GridUnitType.Star), ExpanderControlLength, GridLength.Auto })); + return vsg; + } + + static VisualState _dock( + string name, + SparkWindowView spark_window_view, + Grid grid, + UIElement content, + SparkMenu menu, + Button expander, + PropertyPath control, + PropertyPath control_span, + PropertyPath variable, + PropertyPath variable_span, + int menu_variable, + int expander_variable, + int content_variable, + GridLength[] columns, + GridLength[] rows) { + // + var sb = new Storyboard(); + place(menu, menu_variable); + place(expander, expander_variable); + place(content, content_variable); + set_dimensions(grid.ColumnDefinitions, columns, _column_width_prop); + set_dimensions(grid.RowDefinitions, rows, _row_height_prop); + _animate(menu, sb, _menu_orientation_prop).KeyFrames.Add( + new DiscreteObjectKeyFrame(variable == _column_prop ? Orientation.Vertical : Orientation.Horizontal)); + return new VisualState { Name = name, Storyboard = sb }; + // local methods + void set_dimensions(IList targets, GridLength[] lengths, PropertyPath path) where T : DependencyObject { + for (int i = 0; i < lengths.Length; i++) + _animate(targets[i], sb, path).KeyFrames.Add(new DiscreteObjectKeyFrame(lengths[i], TimeSpan.Zero)); + } + + void place(UIElement target, int variable_value) { + _animate(target, sb, control).To = 0; + _animate(target, sb, control_span).To = 3; + _animate(target, sb, variable).To = variable_value; + _animate(target, sb, variable_span).To = 1; + } + } + + static TAnimation _animate(DependencyObject target, Storyboard storyboard, PropertyPath path) where TAnimation : AnimationTimeline, new() { + var animation = new TAnimation { Duration = TimeSpan.Zero }; + Storyboard.SetTarget(animation, target); + Storyboard.SetTargetProperty(animation, path); + storyboard.Children.Add(animation); + return animation; + } + + public static class Names { + public const string + Left = nameof(Left), + Top = nameof(Top), + Right = nameof(Right), + Bottom = nameof(Bottom); + } } } -} \ No newline at end of file +}