From 7cdfcbf6dc5077a20b4ef7c07f250e515b7e8744 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 23 Mar 2022 14:33:50 -0400 Subject: [PATCH] Support changes to icon in SparkMenuItem --- src/client/spark/core/Bus/SparkCommand.cs | 30 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/client/spark/core/Bus/SparkCommand.cs b/src/client/spark/core/Bus/SparkCommand.cs index 1933834e2..bed5e2786 100644 --- a/src/client/spark/core/Bus/SparkCommand.cs +++ b/src/client/spark/core/Bus/SparkCommand.cs @@ -1,10 +1,13 @@ namespace bts.spark { using bts.api; + using bts.api.core; using mbts; using mbts.ui; using mbts.ui.utility; + using Spark.Core.Reactive; + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -16,22 +19,36 @@ namespace bts.spark { using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; + using System.Windows.Threading; using static mbts.spark.SparkTags; public abstract class SparkCommand { public readonly KeyGesture key_gesture; + + readonly IObservableValue _icon = + ObservableValue.create(multi_threaded: true); + string _title, _group; bool _show_in_toolbar, _show_in_context_menu; + public SparkCommand(string title, ImageSource icon, bool show_in_toolbar = true, bool show_in_context_menu = true, KeyGesture key_gesture = null) { _title = title; - this.icon = icon; + _icon.value = icon; _show_in_toolbar = show_in_toolbar; _show_in_context_menu = show_in_context_menu; this.key_gesture = key_gesture; } - public event Action title_changed, show_in_toolbar_changed, show_in_context_menu_changed, group_changed; + public event Action title_changed; + public event Action show_in_toolbar_changed; + public event Action show_in_context_menu_changed; + public event Action group_changed; + public event Action> icon_changed { + add => _icon.value_changed += value; + remove => _icon.value_changed -= value; + } + public string group { get => _group; set { @@ -50,7 +67,12 @@ namespace bts.spark { } } } - public ImageSource icon { get; } + + public ImageSource icon { + get => _icon.value; + set => _icon.value = value; + } + public bool show_in_toolbar { get => _show_in_toolbar; set { @@ -365,6 +387,7 @@ namespace bts.spark { 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) { @@ -374,6 +397,7 @@ 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)); + 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);