Files
spark/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceCellView.xaml.cs
T
John Colvin b499a5c38b refactor: Move IDisposable utils to Spark.Core.Disposables ns
* Move Anonymous/FinalizableDisposable to own file
* Rename DisposableCombo -> ImmutableDisposable and move it to its own file
* Clean up IDisposable utils impl, but keep changes to calling code minimial
2022-08-22 00:05:04 -04:00

250 lines
9.9 KiB
C#

namespace mbts.ui.controls {
using bts;
using mbts.ui.converters;
using mbts.ui.extensions;
using Spark.Core.Disposables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
/// <summary>
/// Interaction logic for VolSurfaceCellView.xaml
/// </summary>
class VolSurfaceCellView : Control {
public static readonly RoutedCommand BeginEditCommand = new();
public static readonly RoutedCommand EndEditCommand = new();
public static readonly RoutedCommand CancelEditCommand = new();
public static readonly ExecutedRoutedEventHandler BeginEditExecuted = (s, e) => (s as VolSurfaceCellView)?.SetCurrentValue(IsEditingProperty, true);
public static readonly ExecutedRoutedEventHandler EndEditExecuted = (s, e) => (s as VolSurfaceCellView)?.end_edit();
public static readonly ExecutedRoutedEventHandler CancelEditExecuted = (s, e) => (s as VolSurfaceCellView)?.cancel_edit();
static int _id_counter = 0;
/// <summary>
/// A unique id per VolSurfaceCellView instance, useful for diagnosing ui
/// behavior, such as focus traversal.
/// </summary>
readonly int _id = System.Threading.Interlocked.Increment(ref _id_counter);
TextBlock _committed_view;
FrameworkElement _candidate_view;
TextBox _candidate_text_box_backing;
TextBox _candidate_text_box =>
_candidate_text_box_backing ??= _candidate_view?.descendants_of_type<TextBox>()?.FirstOrDefault();
IDisposable _candidate_view_hooks;
static VolSurfaceCellView() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(VolSurfaceCellView), new FrameworkPropertyMetadata(typeof(VolSurfaceCellView)));
public static IMultiValueConverter CommittedValueConverter =>
ValueConverter.create<object, object, bool, object>(
(com_val, can_val, is_dirty) => is_dirty ?
$"{can_val} ({com_val})" : (can_val ?? com_val)?.ToString() ?? string.Empty);
#region IsEditing
public static readonly DependencyProperty IsEditingProperty =
WPFHelper.create_dp<bool, VolSurfaceCellView>(
nameof(IsEditing),
(o, v) => {
// A hack to force the grid to select this cell's row without
// forfeiting mouse capture from the candidate textbox
if (v && o.try_find_ancestor_of_type(out ListBoxItem row_view) &&
row_view.DataContext is object row_item &&
row_view.try_find_ancestor_of_type(out DIYGridRowsPresenter row_pres)) {
row_pres.SetSelectedItems(new[] { row_item });
}
if (v) {
o.GetBindingExpression(CommittedValueProperty)?.UpdateTarget();
o.GetBindingExpression(CandidateValueProperty)?.UpdateTarget();
o._candidate_text_box?.GetBindingExpression(TextBox.TextProperty)?.UpdateTarget();
o._focus_candidate_text_box();
} else if (o._candidate_text_box != null && Keyboard.FocusedElement == o._candidate_text_box) {
o._log($"is_editing -> false. clearing focus from candidate text box");
Keyboard.ClearFocus();
}
o.set_current_if_changed(FocusableProperty, !v);
});
public bool IsEditing {
get => (bool)GetValue(IsEditingProperty);
set => SetValue(IsEditingProperty, value);
}
#endregion
#region CommittedValue
public static readonly DependencyProperty CommittedValueProperty =
WPFHelper.create_dp<object, VolSurfaceCellView>(
nameof(CommittedValue), new PropertyMetadata((d, _) => d.CoerceValue(IsDirtyProperty)));
public object CommittedValue {
get => GetValue(CommittedValueProperty);
set => SetValue(CommittedValueProperty, value);
}
#endregion
#region CandidateValue
public static readonly DependencyProperty CandidateValueProperty =
WPFHelper.create_dp<object, VolSurfaceCellView>(
nameof(CandidateValue),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(d, _) => d.CoerceValue(IsDirtyProperty)));
public object CandidateValue {
get => GetValue(CandidateValueProperty);
set => SetValue(CandidateValueProperty, value);
}
#endregion
#region TickDownCommand
public static readonly DependencyProperty TickDownCommandProperty =
WPFHelper.create_dp<ICommand, VolSurfaceCellView>(nameof(TickDownCommand));
public ICommand TickDownCommand {
get => GetValue(TickDownCommandProperty) as ICommand;
set => SetValue(TickDownCommandProperty, value);
}
#endregion
#region TickUpCommand
public static readonly DependencyProperty TickUpCommandProperty =
WPFHelper.create_dp<ICommand, VolSurfaceCellView>(nameof(TickUpCommand));
public ICommand TickUpCommand {
get => GetValue(TickUpCommandProperty) as ICommand;
set => SetValue(TickUpCommandProperty, value);
}
#endregion
#region IsDirty
public static readonly DependencyPropertyKey IsDirtyPropertyKey =
DependencyProperty.RegisterReadOnly(nameof(IsDirty), typeof(bool), typeof(VolSurfaceCellView),
new PropertyMetadata(false, null, (d, base_value) =>
d is VolSurfaceCellView view && !Equals(view.CommittedValue, view.CandidateValue)));
public static DependencyProperty IsDirtyProperty => IsDirtyPropertyKey.DependencyProperty;
public bool IsDirty {
get => (bool)GetValue(IsDirtyProperty);
private set => SetValue(IsDirtyPropertyKey, value);
}
#endregion
#region ElementStyle
public static readonly DependencyProperty ElementStyleProperty =
WPFHelper.create_dp<Style, VolSurfaceCellView>(
nameof(ElementStyle),
(o, v) => WPFHelper.apply_style(ref o._committed_view, v));
public Style ElementStyle {
get => GetValue(ElementStyleProperty) as Style;
set => SetValue(ElementStyleProperty, value);
}
#endregion
#region EditingElementStyle
public static readonly DependencyProperty EditingElementStyleProperty =
WPFHelper.create_dp<Style, VolSurfaceCellView>(
nameof(EditingElementStyle),
(o, v) => WPFHelper.apply_style(ref o._candidate_view, v));
public Style EditingElementStyle {
get => GetValue(EditingElementStyleProperty) as Style;
set => SetValue(EditingElementStyleProperty, value);
}
#endregion
#region EndEditOnLostKeyboardFocus
public static readonly DependencyProperty EndEditOnLostKeyboardFocusProperty =
WPFHelper.create_dp<bool, VolSurfaceCellView>(nameof(EndEditOnLostKeyboardFocus), true);
public bool EndEditOnLostKeyboardFocus {
get => (bool)GetValue(EndEditOnLostKeyboardFocusProperty);
set => SetValue(EndEditOnLostKeyboardFocusProperty, value);
}
#endregion
public void begin_edit() =>
this.set_current_if_changed(IsEditingProperty, true);
public void end_edit() {
if (!IsEditing) {
return;
}
_log("end edit");
// Flush the edited value from the binding target (view) to the
// binding source (viewmodel) to commit the edit
_candidate_text_box?.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
SetCurrentValue(IsEditingProperty, false);
}
public void cancel_edit() {
if (!IsEditing) {
return;
}
_log("cancel edit");
// Flush the unedited value from the binding source (viewmodel) back
// to the binding target (view) to cancel the edit
GetBindingExpression(CandidateValueProperty)?.UpdateTarget();
SetCurrentValue(IsEditingProperty, false);
}
public override void OnApplyTemplate() {
_candidate_text_box_backing = null;
Disposable.dispose(ref _candidate_view_hooks);
_committed_view = this.assert_template_child<TextBlock>("committed_view");
_candidate_view = this.assert_template_child<FrameworkElement>("candidate_view");
WPFHelper.apply_style(ref _committed_view, ElementStyle);
WPFHelper.apply_style(ref _candidate_view, EditingElementStyle);
// _candidate_text_box?.SetCurrentValue(TextBox.TextProperty, CandidateValue);
_candidate_view_hooks = _candidate_view.SubscribeLostFocus(_on_candidate_view_lost_focus);
}
protected override void OnGotFocus(RoutedEventArgs e) {
_log("got focus");
begin_edit();
}
void _focus_candidate_text_box() {
_log("invoke focus candidate_text_box");
Dispatcher.InvokeAsync(() => {
_log("focus candidate_text_box");
_candidate_view?.Focus();
if (_candidate_text_box != null) {
Keyboard.Focus(_candidate_text_box);
}
}, DispatcherPriority.ApplicationIdle);
}
/// <summary>
/// Finishes (commits or cancels) an edit if focus is gone from thisd
/// control and all of its children
/// </summary>
void _on_candidate_view_lost_focus(object sender, RoutedEventArgs e) {
if ((_candidate_view?.IsKeyboardFocusWithin ?? false) || (_candidate_text_box?.IsFocused ?? false)) {
return;
}
_log("candidate_view lost focus");
if (EndEditOnLostKeyboardFocus) {
end_edit();
} else {
cancel_edit();
}
this.set_current_if_changed(IsEditingProperty, false);
}
void _log(string msg) =>
Log.Ui.info($"VolSurfaceCellView {_id} {msg}");
}
}