made new StateDefinitions: MouseStateDefinition, CheckedStateDefinition
This commit is contained in:
1 parent
c6f98630dc
commit
c124e3dfd2
1 file changed
+73
-1
@@ -1,10 +1,16 @@
|
||||
using mbts.ui.shell;
|
||||
using bts;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace mbts.ui {
|
||||
@@ -28,6 +34,72 @@ namespace mbts.ui {
|
||||
public event PropertyChange<string> StateChanged;
|
||||
}
|
||||
|
||||
class MouseStateDefinition : StateDefinition {
|
||||
IDisposable _mouse_hook;
|
||||
|
||||
#region MouseSource
|
||||
public static readonly DependencyProperty MouseSourceProperty =
|
||||
WPFHelper.create_dp<FrameworkElement, MouseStateDefinition>(
|
||||
nameof(MouseSource),
|
||||
(o, v) => o._on_mouse_source_changed(v));
|
||||
|
||||
public FrameworkElement MouseSource {
|
||||
get => GetValue(MouseSourceProperty) as FrameworkElement;
|
||||
set => SetValue(MouseSourceProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public string HoverState { get; set; } = "MouseOver";
|
||||
public string PressedState { get; set; } = "Pressed";
|
||||
public string NormalState { get; set; } = "Normal";
|
||||
|
||||
void _on_mouse_source_changed(FrameworkElement mouse_source) {
|
||||
Disposable.replace(ref _mouse_hook,
|
||||
mouse_source == null ? null : Disposable.Create(
|
||||
mouse_source.SubscribeMouseEnter(on_mouse_event),
|
||||
mouse_source.SubscribeMouseMove(on_mouse_event),
|
||||
mouse_source.SubscribeMouseLeave(on_mouse_event),
|
||||
mouse_source.SubscribeMouseDown(on_mouse_button_event),
|
||||
mouse_source.SubscribeMouseUp(on_mouse_button_event)));
|
||||
void on_mouse_event(object sender, MouseEventArgs e) => reset();
|
||||
void on_mouse_button_event(object sender, MouseButtonEventArgs e) => reset();
|
||||
void reset() => this.set_current_if_changed(
|
||||
StateProperty, mouse_source.IsMouseOver ? (Mouse.LeftButton == MouseButtonState.Pressed ? PressedState : HoverState) : NormalState);
|
||||
}
|
||||
}
|
||||
|
||||
class CheckedStateDefinition : StateDefinition {
|
||||
public static readonly Func<FrameworkElement, FrameworkElement> FindMenuItemAncestor =
|
||||
target => target?.find_ancestor_of_type<MenuItem>();
|
||||
|
||||
IDisposable _checkable_source_hook;
|
||||
|
||||
#region CheckableSource
|
||||
public static readonly DependencyProperty CheckableSourceProperty =
|
||||
WPFHelper.create_dp<FrameworkElement, CheckedStateDefinition>(
|
||||
nameof(CheckableSource),
|
||||
(o, v) => o._on_checkable_source_changed(v));
|
||||
|
||||
public FrameworkElement CheckableSource {
|
||||
get => GetValue(CheckableSourceProperty) as FrameworkElement;
|
||||
set => SetValue(CheckableSourceProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public string CheckedState { get; set; } = "Checked";
|
||||
public string UncheckedState { get; set; } = "Unchecked";
|
||||
|
||||
void _on_checkable_source_changed(FrameworkElement checkable_source) {
|
||||
Disposable.replace(ref _checkable_source_hook,
|
||||
OneOf.infer<IObservable<bool>, ToggleButton, MenuItem>(
|
||||
checkable_source, FrameworkElementHelper.ObserveIsChecked, FrameworkElementHelper.ObserveIsChecked)
|
||||
?.Subscribe(on_checked_changed));
|
||||
|
||||
void on_checked_changed(bool is_checked) =>
|
||||
this.set_current_if_changed(StateProperty, is_checked ? CheckedState : UncheckedState);
|
||||
}
|
||||
}
|
||||
|
||||
[ContentProperty(nameof(StateDefinitions))]
|
||||
class StateGroupsManager : DependencyObject {
|
||||
IDisposable _state_definitions_hook;
|
||||
|
||||
Reference in new issue
Block a user