diff --git a/src/client/spark/Notifications/INotifier.cs b/src/client/spark/Notifications/INotifier.cs index 9442ee679..2561cbea2 100644 --- a/src/client/spark/Notifications/INotifier.cs +++ b/src/client/spark/Notifications/INotifier.cs @@ -10,6 +10,8 @@ using mbts.spark; using mbts.ui.extensions; using mbts.ui.utility; +using Spark.ViewModel; + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -36,12 +38,12 @@ namespace mbts.ui { readonly IToastNotification _data; public ToastNotificationVM(IBus bus, IToastNotification data) { _data = data; - spark_commands = _data.menu_items.Select(cmd => new SparkCommandVM(cmd, bus)).ToArray(); + spark_commands = _data.menu_items.Select(cmd => new SparkMenuItemVM(cmd, bus)).ToArray(); } public ImageSource icon => _data.content_icon; public string text => _data.text; - public IReadOnlyCollection spark_commands { get; } + public IReadOnlyCollection spark_commands { get; } } [Export(typeof(INotifier))] diff --git a/src/client/spark/ViewModel/SparkMenuItemVM.cs b/src/client/spark/ViewModel/SparkMenuItemVM.cs new file mode 100644 index 000000000..ca76172bd --- /dev/null +++ b/src/client/spark/ViewModel/SparkMenuItemVM.cs @@ -0,0 +1,86 @@ +namespace Spark.ViewModel { + using bts.api; + using bts.spark; + + using mbts; + using mbts.ui; + using mbts.ui.utility; + + using System; + using System.Collections.ObjectModel; + using System.Collections.Specialized; + using System.Linq; + using System.Threading.Tasks; + using System.Windows.Data; + using System.Windows.Input; + using System.Windows.Media; + using System.Windows.Threading; + + public class SparkMenuItemVM : mbts.ui.shell.ViewModelBase, ICommand { + readonly SparkCommand _cmd; + readonly IBus _bus; + readonly ISelectable _selectable_cmd; + readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher; + object _first_child_group; + + public SparkMenuItemVM(SparkCommand cmd, IBus bus) { + _cmd = cmd; + _bus = bus; + cmd.title_changed += () => RaisePropertyChanged(nameof(title)); + 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)); + cmd.icon_changed += c => _dispatcher.check_invoke_async(() => RaisePropertyChanged(nameof(icon))).ignore(); + + if (cmd.child_items is not null) { + ObservableAutoWrapper.connect( + cmd.child_items, child_items, item => new SparkMenuItemVM(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 = cmd as ISelectable; + if (_selectable_cmd is not null) { + _selectable_cmd.is_selected_changed += _ => RaisePropertyChanged(nameof(is_selected)); + } + } + + public string title => _cmd.title; + public string group => _cmd.group; + public KeyGesture key_gesture => _cmd.key_gesture; + 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 ?? false; + set { + if (_selectable_cmd != null) + _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); + } + + event EventHandler ICommand.CanExecuteChanged { add { _cmd.CanExecuteChanged += value; } remove { _cmd.CanExecuteChanged -= value; } } + + bool ICommand.CanExecute(object parameter) => _cmd.canExecute(); + async void ICommand.Execute(object parameter) { + await Task.Yield(); + _cmd.execute(_bus); + } + } +} diff --git a/src/client/spark/core/Bus/SparkCommand.cs b/src/client/spark/core/Bus/SparkCommand.cs index bed5e2786..05f65f65e 100644 --- a/src/client/spark/core/Bus/SparkCommand.cs +++ b/src/client/spark/core/Bus/SparkCommand.cs @@ -4,22 +4,17 @@ namespace bts.spark { using mbts; using mbts.ui; - using mbts.ui.utility; using Spark.Core.Reactive; using System; - using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reactive.Linq; - using System.Threading.Tasks; - using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; - using System.Windows.Threading; using static mbts.spark.SparkTags; @@ -383,66 +378,5 @@ namespace bts.spark { public override bool canExecute() => _can_execute; public void Dispose() => Disposable.dispose(ref _subs); } - public class SparkCommandVM : mbts.ui.shell.ViewModelBase, ICommand { - readonly SparkCommand _cmd; - readonly IBus _bus; - readonly ISelectable _selectable_cmd; - readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher; - object _first_child_group; - - public SparkCommandVM(SparkCommand cmd, IBus bus) { - _cmd = cmd; - _bus = bus; - cmd.title_changed += () => RaisePropertyChanged(nameof(title)); - 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)); - cmd.icon_changed += c => _dispatcher.check_invoke_async(() => RaisePropertyChanged(nameof(icon))).ignore(); - 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 = cmd as ISelectable; - if (_selectable_cmd != null) - _selectable_cmd.is_selected_changed += _ => RaisePropertyChanged(nameof(is_selected)); - } - - public string title => _cmd.title; - public string group => _cmd.group; - public KeyGesture key_gesture => _cmd.key_gesture; - 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 ?? false; - set { - if (_selectable_cmd != null) - _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); - } - - event EventHandler ICommand.CanExecuteChanged { add { _cmd.CanExecuteChanged += value; } remove { _cmd.CanExecuteChanged -= value; } } - - bool ICommand.CanExecute(object parameter) => _cmd.canExecute(); - async void ICommand.Execute(object parameter) { - await Task.Yield(); - _cmd.execute(_bus); - } - } } diff --git a/src/client/spark/view/Controls/SparkMenu/SparkMenu.xaml.cs b/src/client/spark/view/Controls/SparkMenu/SparkMenu.xaml.cs index 743bbbff2..a34838a2e 100644 --- a/src/client/spark/view/Controls/SparkMenu/SparkMenu.xaml.cs +++ b/src/client/spark/view/Controls/SparkMenu/SparkMenu.xaml.cs @@ -5,6 +5,8 @@ namespace mbts.ui.controls { using mbts.ui.converters; + using Spark.ViewModel; + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -22,13 +24,13 @@ namespace mbts.ui.controls { SetCurrentValue(FocusableProperty, false); public SparkMenu(IEnumerable commands, IBus bus) : this() { - var cmd_vms = new ObservableCollection( - commands.Select(c => new SparkCommandVM(c, bus))); + var cmd_vms = new ObservableCollection( + commands.Select(c => new SparkMenuItemVM(c, bus))); // ObservableCollection to avoid WPF Binding Leak var items_view = new ListCollectionView(cmd_vms) { - Filter = item => (item as SparkCommandVM)?.show_in_toolbar ?? false + Filter = item => (item as SparkMenuItemVM)?.show_in_toolbar ?? false }; - items_view.LiveFilteringProperties.Add(nameof(SparkCommandVM.show_in_toolbar)); + items_view.LiveFilteringProperties.Add(nameof(SparkMenuItemVM.show_in_toolbar)); ItemsSource = items_view; } diff --git a/src/client/spark/view/Spark/SparkWindowView.xaml.cs b/src/client/spark/view/Spark/SparkWindowView.xaml.cs index 526e26748..87abb2cb7 100644 --- a/src/client/spark/view/Spark/SparkWindowView.xaml.cs +++ b/src/client/spark/view/Spark/SparkWindowView.xaml.cs @@ -5,6 +5,8 @@ using mbts.ui; using mbts.ui.controls; using mbts.ui.converters; +using Spark.ViewModel; + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -90,12 +92,12 @@ namespace bts.view { ContextMenu.GroupStyle.Add(new GroupStyle { ContainerStyle = Resources["ContextMenuGroupItemContainerStyle"] as Style }); - ContextMenuItems = new ObservableCollection(commands.Select(c => new SparkCommandVM(c, bus))); + ContextMenuItems = new ObservableCollection(commands.Select(c => new SparkMenuItemVM(c, bus))); var context_menu_items_view = new ListCollectionView(ContextMenuItems) { - Filter = item => (item as SparkCommandVM)?.show_in_context_menu ?? false + Filter = item => (item as SparkMenuItemVM)?.show_in_context_menu ?? false }; - context_menu_items_view.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SparkCommandVM.group))); - context_menu_items_view.LiveFilteringProperties.Add(nameof(SparkCommandVM.show_in_context_menu)); + context_menu_items_view.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SparkMenuItemVM.group))); + context_menu_items_view.LiveFilteringProperties.Add(nameof(SparkMenuItemVM.show_in_context_menu)); ContextMenu.ItemsSource = context_menu_items_view; } VisualStateManager.GetVisualStateGroups(this).Add(DockStates.get(this, _grid, content, _menu, expand_button)); @@ -106,7 +108,7 @@ namespace bts.view { } } - public ObservableCollection ContextMenuItems { get; } + public ObservableCollection ContextMenuItems { get; } #region Dock public static readonly DependencyProperty MenuDockProperty =