SP-281 SparkWindow toolbar can be docked to any side of the window, or hidden

This commit is contained in:
John Colvin committed 2020-02-06 12:22:37 -06:00
1 parent 0daa3e0685
commit dcce738d53
14 files changed
+605 -104

No files matched your search

+2
View File
@@ -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);
@@ -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
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.MenuOrientationGeometryKey}">
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
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.OrdersGeometryKey}">
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
</sys:String>
+10 -3
View File
@@ -525,8 +525,11 @@
<Setter.Value>
<DataTemplate>
<control:SparkMenuItem
Command="{Binding command}" Style="{StaticResource SparkMenuButton}" Icon="{Binding icon}"
Command="{Binding command}"
Style="{StaticResource SparkMenuButton}"
Icon="{Binding icon}"
Collapsed="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type control:SparkMenu}}, Path=ShowText, Converter={StaticResource NotConverter}}"
ContentFlowDirection="{Binding Orientation, RelativeSource={RelativeSource AncestorType=control:SparkMenu}, Converter={x:Static control:SparkMenu.ItemContentFlowDirectionConverter}}"
>
<control:SparkMenuItem.Content>
<MultiBinding Converter="{x:Static ut:CommandHelper.DescriptionConverter}">
@@ -546,7 +549,11 @@
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"
>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<StackPanel
IsItemsHost="True"
Orientation="{Binding Orientation, RelativeSource={RelativeSource TemplatedParent}}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
/>
</Border>
</ScrollViewer>
</ControlTemplate>
@@ -559,4 +566,4 @@
<Setter Property="Command" Value="{Binding command}"/>
<Setter Property="Icon" Value="{Binding icon, Converter={StaticResource IconConverter}}"/>
</Style>
</ResourceDictionary>
</ResourceDictionary>
+37
View File
@@ -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<SparkCommand> child_items { get; } = new ObservableCollection<SparkCommand>();
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<SparkCommandVM> child_items { get; } = new ObservableCollection<SparkCommandVM>();
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();
@@ -37,6 +37,8 @@ namespace mbts.ui
}
public class DelegateCommand : DelegateCommand<object> {
public static DelegateCommand empty => new DelegateCommand(() => { });
public DelegateCommand(Action execute) : this(_ => execute()) { }
public DelegateCommand(Action execute, Func<bool> can_execute) : this(_ => execute(), _ => can_execute()) { }
public DelegateCommand(Action<object> execute) : base(execute) { }
@@ -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<Orientation, ContentFlowDirection>(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
}
}
}
@@ -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<ContentFlowDirection, SparkMenuItem>(
nameof(ContentFlowDirection),
ContentFlowDirection.LeftToRight);
public ContentFlowDirection ContentFlowDirection {
get => (ContentFlowDirection)GetValue(ContentFlowDirectionProperty);
set => SetValue(ContentFlowDirectionProperty, value);
}
#endregion
}
}
@@ -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;
+66 -24
View File
@@ -1,9 +1,12 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bv="clr-namespace:bts.view"
xmlns:mbts="clr-namespace:mbts.ui"
xmlns:u="clr-namespace:mbts.ui"
xmlns:controls="clr-namespace:mbts.ui.controls">
xmlns:controls="clr-namespace:mbts.ui.controls"
xmlns:cv="clr-namespace:mbts.ui.converters"
>
<SolidColorBrush x:Key="StartMenuMouseOverBorderBrush" Color="#FFFFB700" />
<SolidColorBrush x:Key="StartMenuMouseOverBackground" Color="#6ddbff" />
@@ -89,14 +92,35 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:SparkMenuItem">
<Border Name="launcher_button_border"
<Border
Name="launcher_button_border"
BorderThickness="0"
MinWidth="150"
BorderBrush="{mbts:BTSColor UI.Button.normal_border}"
Background="{mbts:BTSColor UI.spark_menu_background}"
ToolTipService.InitialShowDelay="30000"
>
>
<u:StateManager.Groups>
<u:StateGroupsManager AttachToMembers="True">
<u:StateDefinitionCollection>
<u:StateDefinition State="{Binding Group.Target.TemplatedParent.ContentFlowDirection, RelativeSource={RelativeSource Self}, Converter={x:Static cv:ValueConverters.SimpleString}}"/>
</u:StateDefinitionCollection>
</u:StateGroupsManager>
</u:StateManager.Groups>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="ContentFlowDirectionStates">
<VisualState Name="LeftToRight"/>
<VisualState Name="TopToBottom">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="content_panel" Storyboard.TargetProperty="(StackPanel.Orientation)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Orientation.Vertical}"/>
</ObjectAnimationUsingKeyFrames>
<ThicknessAnimation Storyboard.TargetName="button_content" Storyboard.TargetProperty="(FrameworkElement.Margin)" Duration="0" To="0,9,0,0"/>
<DoubleAnimation Storyboard.TargetName="launcher_button_border" Storyboard.TargetProperty="(FrameworkElement.MinWidth)" Duration="0" To="52"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border.ToolTip>
<ToolTip>
<ContentPresenter></ContentPresenter>
@@ -124,17 +148,15 @@
Fill="{mbts:BTSColor UI.transparent}"
Margin="-10 0 0 0"
/>
<BulletDecorator Name="bullet_decorator" Grid.Column="1" Margin="5 10 15 10">
<BulletDecorator.Bullet>
<Border MinWidth="29">
<Border Name="bullet_decorator" Grid.Column="1" Margin="5 10 15 10">
<StackPanel Name="content_panel" Orientation="Horizontal">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="29">
<Image Name="launcher_menu_item_icon" Source="{TemplateBinding Icon}" Width="29" Height="29" Opacity="0.8" SnapsToDevicePixels="True" />
</Border>
</BulletDecorator.Bullet>
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"
Name="button_content" Margin="9 0 0 0" TextBlock.FontWeight="SemiBold"/>
</BulletDecorator>
<ContentPresenter Name="button_content" TextBlock.FontWeight="SemiBold" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="9 0 0 0"/>
</StackPanel>
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
@@ -239,6 +261,38 @@
</Style>
<!-- The Expander used in spark menus (Tool Bar) -->
<Style x:Key="MenuExpanderNormalButtonBase" TargetType="Button">
<Setter Property="Background" Value="{mbts:BTSColor UI.spark_menu_background}"/>
<Setter Property="BorderBrush" Value="{mbts:BTSColor UI.form_content_border}"/>
<Setter Property="Width" Value="5"/>
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="ClipToBounds" Value="True" />
<Setter Property="Foreground" Value="{mbts:BTSColor UI.Button.normal_foreground}" />
<Setter Property="Margin" Value="0"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{mbts:BTSColor UI.menu_expander_mouseover}"/>
<Setter Property="BorderBrush" Value="{mbts:BTSColor UI.menu_expander_mouseover}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{mbts:BTSColor UI.Button.pressed_background}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MenuExpanderButtonBase" TargetType="ToggleButton">
<Setter Property="Background" Value="{mbts:BTSColor UI.spark_menu_background}"/>
<Setter Property="BorderBrush" Value="{mbts:BTSColor UI.form_content_border}"/>
@@ -270,18 +324,6 @@
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MenuExpanderButton" TargetType="ToggleButton" BasedOn="{StaticResource MenuExpanderButtonBase}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Cursor" Value="ScrollE"/>
<Setter Property="ToolTip" Value="Expand Menu"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Cursor" Value="ScrollW"/>
<Setter Property="ToolTip" Value="Collapse Menu"/>
</Trigger>
</Style.Triggers>
</Style>
<!--PlayButtonRed/Blue are commented out bc they are not referenced anywhere.
However, RoundPlayButton is a new style, and liable to change; these are good potential alternatives.
@@ -0,0 +1,26 @@
using bts.view;
using ProtoBuf;
using System;
using System.Windows.Controls;
namespace bts.shell {
[ProtoContract]
struct SparkMenuDisplaySettings : IEquatable<SparkMenuDisplaySettings> {
[ProtoMember(1)]
public readonly SparkMenuDisplayState display_state;
[ProtoMember(2)]
public readonly Dock dock;
public SparkMenuDisplaySettings(SparkMenuDisplayState display_state, Dock dock) {
this.display_state = display_state;
this.dock = dock;
}
public static SparkMenuDisplaySettings Default =>
new SparkMenuDisplaySettings(SparkMenuDisplayState.Collapsed, Dock.Left);
public bool Equals(SparkMenuDisplaySettings other) =>
display_state == other.display_state &&
dock == other.dock;
}
}
@@ -0,0 +1,135 @@
using bts.api;
using bts.shell;
using bts.spark;
using bts.view;
using mbts.spark;
using mbts.ui.shell;
using mbts.ui.utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
namespace mbts.ui {
class SparkMenuDisplayVM : DisposableViewModelBase, IValueFactory<SparkMenuDisplaySettings> {
readonly List<IDisposable> _subs = new List<IDisposable>();
readonly Property<Dock> _menu_dock;
readonly Property<SparkMenuDisplayState> _menu_display_state;
public SparkMenuDisplayVM() {
_menu_dock = backing(nameof(menu_dock), Dock.Left);
_menu_display_state = backing(nameof(menu_display_state), SparkMenuDisplayState.Collapsed);
// spark cmd
spark_cmd = new SparkCommandCommand("Menu Display", Icons.MenuOrientation, DelegateCommand.empty) {
group = "Menu Display",
show_in_toolbar = false,
show_in_context_menu = true
};
var toggle_show_menu_spark_cmd = new MenuVisibilityToggleSparkCommand(this);
spark_cmd.child_items.Add(toggle_show_menu_spark_cmd);
spark_cmd.child_items.AddRange(EnumHelper.get_values<Dock>().Select(d => new MenuDockSparkCommand(this, d)));
_dispose = Disposable.Create(() => {
_subs.DisposeAll();
_subs.Clear();
});
}
public Dock menu_dock { get => _menu_dock.Value; set => _menu_dock.Value = value; }
public SparkMenuDisplayState menu_display_state { get => _menu_display_state.Value; set => _menu_display_state.Value = value; }
public SparkCommandCommand spark_cmd { get; }
public SparkMenuDisplaySettings create() =>
new SparkMenuDisplaySettings(menu_display_state, menu_dock);
public void load_from(SparkMenuDisplaySettings settings) {
menu_dock = settings.dock;
menu_display_state = settings.display_state;
}
class MenuDockSparkCommand : SparkCommand, ISelectable {
readonly SparkMenuDisplayVM _owner;
readonly Dock _dock;
public MenuDockSparkCommand(SparkMenuDisplayVM owner, Dock dock)
: base(dock.ToString(), null, false, true) {
_owner = owner;
_dock = dock;
_owner._menu_dock.Changed += on_owner_menu_dock_changed;
group = "Dock States";
void on_owner_menu_dock_changed(Dock oldval, Dock newval) =>
is_selected_changed?.Invoke(is_selected);
}
public event Action<bool> is_selected_changed;
public bool is_selected {
get => _owner.menu_dock == _dock;
set {
if (value != is_selected) {
if (value) {
_owner.menu_dock = _dock;
if (_owner.menu_display_state == SparkMenuDisplayState.Hidden)
_owner.menu_display_state = SparkMenuDisplayState.Collapsed;
}
}
}
}
public override void execute(IBus bus) { }
}
class MenuVisibilityToggleSparkCommand : SparkCommand, ISelectable {
readonly SparkMenuDisplayVM _owner;
public MenuVisibilityToggleSparkCommand(SparkMenuDisplayVM owner)
: base("Show Menu", null, false, true) {
_owner = owner;
_owner._menu_display_state.Changed += (oldval, newval) => is_selected_changed?.Invoke(is_selected);
}
public event Action<bool> is_selected_changed;
public bool is_selected {
get => _owner.menu_display_state != SparkMenuDisplayState.Hidden;
set {
if (is_selected != value) {
_owner.menu_display_state = value ?
(_owner.menu_display_state == SparkMenuDisplayState.Hidden ?
SparkMenuDisplayState.Collapsed :
SparkMenuDisplayState.Expanded) :
SparkMenuDisplayState.Hidden;
}
}
}
public override void execute(IBus bus) { }
}
}
static class SparkMenuDisplayVMExtensions {
public static SparkMenuDisplaySettings safe_create(this SparkMenuDisplayVM spark_menu_display_vm) =>
spark_menu_display_vm == null ?
SparkMenuDisplaySettings.Default :
spark_menu_display_vm.create();
public static void safe_bind_to_view(this SparkMenuDisplayVM spark_menu_display_vm, SparkWindowView parent_view) {
if (spark_menu_display_vm != null)
_bind_to_view(spark_menu_display_vm, parent_view);
}
static void _bind_to_view(SparkMenuDisplayVM spark_menu_display_vm, SparkWindowView parent_view) {
if (spark_menu_display_vm == null)
throw new ArgumentNullException(nameof(spark_menu_display_vm));
parent_view.SetBinding(SparkWindowView.MenuDockProperty, new Binding(nameof(SparkMenuDisplayVM.menu_dock)) {
Source = spark_menu_display_vm
});
parent_view.SetBinding(SparkWindowView.MenuDisplayStateProperty, new Binding(nameof(SparkMenuDisplayVM.menu_display_state)) {
Source = spark_menu_display_vm,
Mode = BindingMode.TwoWay
});
}
}
}
@@ -0,0 +1,35 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace bts.view {
static class SparkWindowContextMenu {
// Note: the number of lines needed to separate 'n' groups is 'n - 1.'
/// <summary>
/// This dp should contain the first element in a relevant instance of <see cref="ICollectionView.Groups"/>.
/// With such information, a <see cref="GroupItem"/> <see cref="Control.Template"/> may hide the separator line in the top (or first) group.
/// </summary>
public static readonly DependencyProperty FirstGroupProperty =
DependencyProperty.RegisterAttached("FirstGroup", typeof(object), typeof(SparkWindowContextMenu));
public static object GetFirstGroup(DependencyObject d) => d.GetValue(FirstGroupProperty);
public static void SetFirstGroup(DependencyObject d, object value) => d.SetValue(FirstGroupProperty, value);
/// <summary>
/// This dp is a bindable wrapper for the (otherwise unbindable) <see cref="ItemsControl.GroupStyle"/> property.
/// </summary>
public static readonly DependencyProperty SingularGroupStyleProperty =
DependencyProperty.RegisterAttached("SingularGroupStyle", typeof(Style), typeof(SparkWindowContextMenu), new PropertyMetadata(OnSingularGroupStyleChanged));
static void OnSingularGroupStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (d is ItemsControl ic) {
ic.GroupStyle.Clear();
if (e.NewValue is Style s)
ic.GroupStyle.Add(new GroupStyle { ContainerStyle = s });
}
}
public static Style GetSingularGroupStyle(DependencyObject d) => d.GetValue(SingularGroupStyleProperty) as Style;
public static void SetSingularGroupStyle(DependencyObject d, object value) => d.SetValue(SingularGroupStyleProperty, value);
}
}
@@ -1,88 +1,97 @@
<UserControl
x:Class="bts.view.SparkWindowView"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bv="clr-namespace:bts.view"
xmlns:mbts="clr-namespace:mbts.ui"
xmlns:cv="clr-namespace:mbts.ui.converters"
xmlns:ut="clr-namespace:mbts.ui.utilities"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
>
<UserControl.Resources>
<Style x:Key="ContextMenuGroupItemContainerStyle" TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Separator x:Name="Separator" Margin="29,0,0,0" Background="LightGray" Visibility="Visible"/>
<ItemsPresenter Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="{mbts:True}">
<DataTrigger.Binding>
<MultiBinding Converter="{x:Static cv:ValueConverters.AreEqualConverter}">
<Binding Path="(bv:SparkWindowContextMenu.FirstGroup)" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
<Binding Path="DataContext.Name" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="Separator" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ContextMenuItem" TargetType="MenuItem">
<Setter Property="Icon" Value="{Binding icon, Converter={x:Static cv:ImageSourceIconConverter.Instance}}"/>
<Setter Property="Header">
<Setter.Value>
<MultiBinding Converter="{x:Static ut:CommandHelper.DescriptionConverter}">
<Binding Path="title"/>
<Binding Path="key_gesture"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding command}"/>
<Setter Property="ItemsSource" Value="{Binding child_items_view}"/>
<Setter Property="IsCheckable" Value="{Binding is_selectable}"/>
<Setter Property="IsChecked" Value="{Binding is_selected}"/>
<Setter Property="ItemContainerStyle" Value="{Binding ItemContainerStyle, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
<Setter Property="bv:SparkWindowContextMenu.SingularGroupStyle" Value="{StaticResource ContextMenuGroupItemContainerStyle}"/>
<Setter Property="bv:SparkWindowContextMenu.FirstGroup" Value="{Binding first_child_group}"/>
</Style>
</UserControl.Resources>
<Border BorderThickness="0 1 0 0" BorderBrush="{mbts:BTSColor UI.form_content_border}">
<Grid x:Name="_grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<!-- expander -->
<ColumnDefinition Width="{x:Static bv:SparkWindowView.ExpanderControlLength}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ToggleButton
Grid.Column="1" BorderThickness="0,0,1,0"
<Button
Name="expand_button"
Grid.Column="1"
Grid.RowSpan="3"
BorderThickness="0,0,1,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Name="expand_button"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Height="auto"
Style="{StaticResource MenuExpanderButton}"
Width="{x:Static sys:Double.NaN}"
Style="{StaticResource MenuExpanderNormalButtonBase}"
Focusable="false"
/>
ToolTip="{Binding MenuDisplayState, RelativeSource={RelativeSource AncestorType=bv:SparkWindowView}, Converter={x:Static bv:SparkWindowView.ExpanderToolTipConverter}}"
>
<Button.Cursor>
<MultiBinding Converter="{x:Static bv:SparkWindowView.ExpanderCursorConverter}">
<Binding Path="MenuDisplayState" RelativeSource="{RelativeSource AncestorType=bv:SparkWindowView}"/>
<Binding Path="MenuDock" RelativeSource="{RelativeSource AncestorType=bv:SparkWindowView}"/>
</MultiBinding>
</Button.Cursor>
</Button>
</Grid>
</Border>
<UserControl.ContextMenu>
<ContextMenu>
<ContextMenu.Resources>
<Image x:Key="icon" x:Shared="False" Source="{Binding icon}"/>
</ContextMenu.Resources>
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Icon" Value="{StaticResource icon}"/>
<Setter Property="Header">
<Setter.Value>
<MultiBinding Converter="{x:Static ut:CommandHelper.DescriptionConverter}">
<Binding Path="title"/>
<Binding Path="key_gesture"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Command" Value="{Binding command}"/>
</Style>
</ContextMenu.ItemContainerStyle>
<ContextMenu.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GroupItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Separator x:Name="Separator" Margin="27,0,0,0" Background="LightGray" Visibility="Visible"/>
<ItemsPresenter Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="{mbts:True}">
<DataTrigger.Binding>
<MultiBinding Converter="{x:Static cv:ValueConverters.AreEqualConverter}">
<Binding Path="ContextMenuItems[0].group" RelativeSource="{RelativeSource AncestorType=bv:SparkWindowView}"/>
<Binding Path="DataContext.Name" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="Separator" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ContextMenu.GroupStyle>
</ContextMenu>
<ContextMenu bv:SparkWindowContextMenu.SingularGroupStyle="{DynamicResource ContextMenuGroupItemContainerStyle}" ItemContainerStyle="{DynamicResource ContextMenuItem}"/>
</UserControl.ContextMenu>
</UserControl>
</UserControl>
@@ -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<SparkMenuDisplayState, Dock, Cursor>((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<SparkMenuDisplayState, string>(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<SparkCommand> 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<SparkMenuDisplayState, Visibility>(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<SparkMenuDisplayState, bool>(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<SparkCommandVM>(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<SparkCommandVM> ContextMenuItems { get; }
public void collapse_click(object sender, RoutedEventArgs e) {
_menu.ShowText = !_menu.ShowText;
#region Dock
public static readonly DependencyProperty MenuDockProperty =
WPFHelper.create_dp<Dock, SparkWindowView>(
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<SparkMenuDisplayState, SparkWindowView>(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<ObjectAnimationUsingKeyFrames>(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<T>(IList<T> targets, GridLength[] lengths, PropertyPath path) where T : DependencyObject {
for (int i = 0; i < lengths.Length; i++)
_animate<ObjectAnimationUsingKeyFrames>(targets[i], sb, path).KeyFrames.Add(new DiscreteObjectKeyFrame(lengths[i], TimeSpan.Zero));
}
void place(UIElement target, int variable_value) {
_animate<Int32Animation>(target, sb, control).To = 0;
_animate<Int32Animation>(target, sb, control_span).To = 3;
_animate<Int32Animation>(target, sb, variable).To = variable_value;
_animate<Int32Animation>(target, sb, variable_span).To = 1;
}
}
static TAnimation _animate<TAnimation>(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);
}
}
}
}
}