refactor: Rename SparkCommandVM -> SparkMenuItemVM

Also move to 'ViewModels' dir
This commit is contained in:
John Colvin committed 2022-05-02 12:29:02 -04:00
1 parent 5900ce7d12
commit 14af8823ed
5 files changed
+103 -77

No files matched your search

+4 -2
View File
@@ -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<SparkCommandVM> spark_commands { get; }
public IReadOnlyCollection<SparkMenuItemVM> spark_commands { get; }
}
[Export(typeof(INotifier))]
@@ -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<SparkMenuItemVM> child_items { get; } = new ObservableCollection<SparkMenuItemVM>();
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);
}
}
}
-66
View File
@@ -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<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);
}
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);
}
}
}
@@ -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<SparkCommand> commands, IBus bus) : this() {
var cmd_vms = new ObservableCollection<SparkCommandVM>(
commands.Select(c => new SparkCommandVM(c, bus)));
var cmd_vms = new ObservableCollection<SparkMenuItemVM>(
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;
}
@@ -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<SparkCommandVM>(commands.Select(c => new SparkCommandVM(c, bus)));
ContextMenuItems = new ObservableCollection<SparkMenuItemVM>(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<SparkCommandVM> ContextMenuItems { get; }
public ObservableCollection<SparkMenuItemVM> ContextMenuItems { get; }
#region Dock
public static readonly DependencyProperty MenuDockProperty =