AlgoTemplateFavoriteButtonsView - cancel automatic selection of the previous item when SelectedItem is removed from ItemsSource
This commit is contained in:
1 parent
da19945e84
commit
3aedebd0ab
3 files changed
+62
-20
No files matched your search
@@ -5,6 +5,7 @@ using bts.collections;
|
||||
using bts.spark;
|
||||
using mbts.collections;
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
@@ -92,9 +93,11 @@ namespace mbts.spark {
|
||||
_selected_favorite_algo_template = backing<AlgoDefinitionTemplateVM>(nameof(selected_favorite_algo_template));
|
||||
// items source views
|
||||
algo_templates_view = CollectionViewSource.GetDefaultView(algo_templates);
|
||||
var favorite_algo_templates_view = new ListCollectionView(algo_templates);
|
||||
var favorite_algo_templates_view = new ListCollectionView(ObservableAggregateCollection.create(_algo_definitions.AsCollection(), d => d.templates));
|
||||
favorite_algo_templates_view.LiveGroupingProperties.Add(nameof(AlgoDefinitionTemplateVM.owner_vm_key));
|
||||
favorite_algo_templates_view.LiveFilteringProperties.Add(nameof(AlgoDefinitionTemplateVM.is_favorite));
|
||||
favorite_algo_templates_view.Filter = item => (item as AlgoDefinitionTemplateVM)?.is_favorite ?? false;
|
||||
favorite_algo_templates_view.IsLiveFiltering = true;
|
||||
this.favorite_algo_templates_view = favorite_algo_templates_view;
|
||||
// commands
|
||||
save_template_cmd = new AsyncCommand<AlgoDefinitionTemplateVM>(save_template_async);
|
||||
@@ -121,6 +124,7 @@ namespace mbts.spark {
|
||||
_algo_definitions.connect_definitions(_definitions_buffer.OnDispatcher()),
|
||||
model.algo_model.algo_metadata.OnDispatcher().Subscribe(_on_template_metadata),
|
||||
CollectionHelper.dispose_on_removed(_algo_definitions)) {
|
||||
() => Disposable.dispose(ref _template_collection_sync),
|
||||
_algo_definitions.Clear
|
||||
};
|
||||
async Task save_template_async(AlgoDefinitionTemplateVM template) {
|
||||
@@ -133,13 +137,7 @@ namespace mbts.spark {
|
||||
void _selected_algo_definition_Changed(AlgoDefinitionVM old_value, AlgoDefinitionVM new_value, SelectionReason reason) {
|
||||
Disposable.dispose(ref _template_collection_sync);
|
||||
if (new_value != null) {
|
||||
/// <see cref="AlgoDefinitionVM"/> and <see cref="AlgoDefinitionTemplateVM"/> are essentially TreeViewItem viewmodels.
|
||||
/// The definitions are the root/parent nodes, and each one has a collection of templates (child-nodes).
|
||||
/// The ladder uses a single popup window to serve as the view for each definition's child-nodes.
|
||||
/// 1 popup window = 1 ItemsControl, and 1 ItemsControl = 1 ItemsSource.
|
||||
/// In other words, the templates view must be bound to 1 ObservableCollection (selected_definition.algo_templates won't work, as each definition has a different collection of templates).
|
||||
/// <see cref="algo_templates"/> is that 1 collection. This call synchronizes it with selected_definition.algo_templates.
|
||||
_template_collection_sync = ObservableAutoWrapper.connect_two_way(new_value.templates_with_custom, algo_templates);
|
||||
_template_collection_sync = ObservableAutoWrapper.connect_two_way(new_value.templates, algo_templates);
|
||||
} else {
|
||||
algo_templates.Clear();
|
||||
}
|
||||
@@ -165,11 +163,7 @@ namespace mbts.spark {
|
||||
public CustomOrderDefinitionImmutable? definition => selected_algo_template?.algo_root is IOrderTypeVM vm ? vm.createi() : (CustomOrderDefinitionImmutable?)null;
|
||||
// items sources
|
||||
public ObservableCollection<AlgoDefinitionVM> algo_definitions => _algo_definitions;
|
||||
/// The binding source of ItemsControls.ItemsSource should never change (i.e. the actual collection reference should be readonly).
|
||||
/// Although the template-items in the dropdown should always contain the exact same elements as selected_algo_definition.templates, the view must be bound to a single collection instance.
|
||||
/// <see cref="algo_templates"/> is such an instance. It should always be in sync with selected_algo_definition.templates.
|
||||
public ObservableCollection<AlgoDefinitionTemplateVM> algo_templates { get; } = new ObservableCollection<AlgoDefinitionTemplateVM>();
|
||||
public ObservableCollection<AlgoDefinitionTemplateVM> favorite_algo_templates => _favorite_templates;
|
||||
public ICollectionView<AlgoDefinitionVM> algo_definitions_view { get; }
|
||||
public ICollectionView algo_templates_view { get; }
|
||||
public ICollectionView favorite_algo_templates_view { get; }
|
||||
@@ -253,13 +247,7 @@ namespace mbts.spark {
|
||||
var curr_selected_template = selected_algo_template;
|
||||
if (curr_selected_template?.is_favorite ?? false)
|
||||
selected_favorite_algo_template = curr_selected_template;
|
||||
favorite_algo_templates_view.Refresh();
|
||||
/// If the <see cref="selected_favorite_algo_template"/> was just removed from favorites, the favorites listbox will immediately select the previous item in its list.
|
||||
/// This behavior is by microsoft design, and changing it would not be trivial.
|
||||
/// It's a weird situation because there are 2 selector views (templates, and favorite templates), which use the same set of items but with different filters.
|
||||
/// Selecting an item in 1 view causes the same item (if present) to be selected in the other view.
|
||||
/// Therefore, curr_selected_template is reselected to negate any such incidental selection change.
|
||||
selected_algo_template = curr_selected_template;
|
||||
algo_definitions_view.Refresh();
|
||||
}
|
||||
|
||||
// This preserves the selected items through a save op.
|
||||
|
||||
@@ -374,5 +374,26 @@ namespace System.Reactive.Linq
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A less verbose way to call <see cref="Observable.FromEvent{Action{TEventArgs}, TEventArgs}(Action{Action{TEventArgs}}, Action{Action{TEventArgs}})"/>.
|
||||
/// </summary>
|
||||
public static IObservable<T> FromAction<T>(Action<T> event_source) =>
|
||||
Observable.FromEvent<Action<T>, T>(cb => event_source += cb, cb => event_source -= cb);
|
||||
|
||||
/// <summary>
|
||||
/// An inner subscription is made for each value in the source sequence using the specified delegate (<paramref name="subscribe_value"/>).
|
||||
/// When a new value arrives, the subscription for the previous value is disposed before <paramref name="subscribe_value"/> is invoked with the new value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="subscribe_value"></param>
|
||||
/// <returns></returns>
|
||||
public static IDisposable SubscribeEach<T>(this IObservable<T> source, Func<T, IDisposable> subscribe_value) {
|
||||
IDisposable value_sub = null;
|
||||
return new Disposables.CompositeDisposable(source.Subscribe(val => Disposable.replace(ref value_sub, subscribe_value(val)))) {
|
||||
() => Disposable.dispose(ref value_sub)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
using System.Windows;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace mbts.ui.spark.algo {
|
||||
@@ -6,7 +10,36 @@ namespace mbts.ui.spark.algo {
|
||||
/// Interaction logic for AlgoTemplateFavoriteButtonsView.xaml
|
||||
/// </summary>
|
||||
class AlgoTemplateFavoriteButtonsView : ListBox {
|
||||
/// When the SelectedItem is removed from the ItemsSource, ListBox automatically reverts the selection to the previous item.
|
||||
/// This boolean flag (<see cref="_cancel_revert_selection"/>) is used in a workaround to avoid this behavior, and instead set SelectedItem to null.
|
||||
/// When the SelectedItem is removed, the flag is set to true, and in the imminent subsequent call to <see cref="OnSelectionChanged(SelectionChangedEventArgs)"/>, the event is canceled, and the flag reset to false.
|
||||
/// For implementation details: <see cref="OnSelectionChanged(SelectionChangedEventArgs)"/>, and <see cref="_on_items_source_CollectionChanged(object, NotifyCollectionChangedEventArgs)"/>
|
||||
bool _cancel_revert_selection;
|
||||
|
||||
static AlgoTemplateFavoriteButtonsView() =>
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(AlgoTemplateFavoriteButtonsView), new FrameworkPropertyMetadata(typeof(AlgoTemplateFavoriteButtonsView)));
|
||||
|
||||
public AlgoTemplateFavoriteButtonsView() =>
|
||||
System.Reactive.Linq.ObservableExtensions.FromAction(_items_source_changed)
|
||||
.SubscribeEach(items_source => items_source == null ? Disposable.empty : items_source.Subscribe(_on_items_source_CollectionChanged));
|
||||
|
||||
event Action<INotifyCollectionChanged> _items_source_changed;
|
||||
|
||||
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) =>
|
||||
_items_source_changed?.Invoke(newValue as INotifyCollectionChanged);
|
||||
|
||||
protected override void OnSelectionChanged(SelectionChangedEventArgs e) {
|
||||
e.Handled = _cancel_revert_selection;
|
||||
_cancel_revert_selection = false;
|
||||
}
|
||||
|
||||
void _on_items_source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
switch (e.Action) {
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
var si = SelectedItem;
|
||||
_cancel_revert_selection = si != null && e.OldItems.Contains(si);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user