add 'HyperlinkClickMode' to HyperlinkCell to support cmd on click anywhere
This commit is contained in:
1 parent
2473e27b83
commit
79bf995e7b
2 files changed
+89
-37
No files matched your search
@@ -12,6 +12,11 @@ using System.Windows.Media;
|
||||
using FontFamily = System.Windows.Media.FontFamily;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
enum HyperlinkCellClickMode {
|
||||
Text,
|
||||
Anywhere
|
||||
}
|
||||
|
||||
class HyperlinkCell : FrameworkElement, ICommandSource {
|
||||
// these fields each represent properties that are accessed frequently,
|
||||
// they are stored locally to reduce repetative DependencyProperty lookups
|
||||
@@ -19,7 +24,11 @@ namespace mbts.ui.controls {
|
||||
Brush _background, _foreground_backing, _border_brush_backing;
|
||||
double _glyph_width => _gcr.glyph_run.AdvanceWidths.Sum();
|
||||
GlyphRun _glyph_run => _gcr.glyph_run;
|
||||
bool _is_mouse_over_text, _can_execute, _snaps_to_device_pixels;
|
||||
bool
|
||||
_is_mouse_over_text,
|
||||
_is_mouse_over_click_area,
|
||||
_can_execute,
|
||||
_snaps_to_device_pixels;
|
||||
Pen _underline_pen_backing, _border_stroke_pen_backing;
|
||||
GlyphContentRect _gcr = new GlyphContentRect {
|
||||
// add 2 pixels to account for underline (1 for space, 1 for the line itself)
|
||||
@@ -65,6 +74,7 @@ namespace mbts.ui.controls {
|
||||
_border_stroke_pen_backing;
|
||||
|
||||
static HyperlinkCell() {
|
||||
IsMouseOverClickAreaProperty = IsMouseOverClickAreaPropertyKey.DependencyProperty;
|
||||
WPFHelper.override_metadata<HorizontalAlignment, HyperlinkCell>(
|
||||
HorizontalAlignmentProperty,
|
||||
FrameworkPropertyMetadataOptions.AffectsArrange |
|
||||
@@ -108,6 +118,36 @@ namespace mbts.ui.controls {
|
||||
_snaps_to_device_pixels = SnapsToDevicePixels;
|
||||
}
|
||||
|
||||
#region IsMouseOverClickArea
|
||||
static readonly DependencyPropertyKey IsMouseOverClickAreaPropertyKey =
|
||||
DependencyProperty.RegisterReadOnly(
|
||||
"IsMouseOverClickArea",
|
||||
typeof(bool),
|
||||
typeof(HyperlinkCell),
|
||||
new FrameworkPropertyMetadata(
|
||||
false,
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
OnIsMouseOverClickAreaChanged,
|
||||
CoerceIsMouseOverClickArea));
|
||||
|
||||
public static readonly DependencyProperty IsMouseOverClickAreaProperty;
|
||||
|
||||
static void OnIsMouseOverClickAreaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
if (d is HyperlinkCell hlc)
|
||||
hlc._is_mouse_over_click_area = e.NewValue is bool b && b;
|
||||
}
|
||||
|
||||
static object CoerceIsMouseOverClickArea(DependencyObject d, object baseValue) =>
|
||||
d is HyperlinkCell hlc ?
|
||||
hlc._is_mouse_over_text || (hlc.IsMouseOver && hlc.HyperlinkClickMode == HyperlinkCellClickMode.Anywhere) :
|
||||
baseValue;
|
||||
|
||||
public bool IsMouseOverClickArea {
|
||||
get => (bool)GetValue(IsMouseOverClickAreaProperty);
|
||||
private set => SetValue(IsMouseOverClickAreaPropertyKey, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region BorderThickness
|
||||
public static readonly DependencyProperty BorderThicknessProperty =
|
||||
Control.BorderThicknessProperty.AddOwner(typeof(HyperlinkCell),
|
||||
@@ -252,6 +292,18 @@ namespace mbts.ui.controls {
|
||||
public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); }
|
||||
#endregion
|
||||
|
||||
#region HyperlinkClickMode
|
||||
public static readonly DependencyProperty HyperlinkClickModeProperty =
|
||||
WPFHelper.create_dp<HyperlinkCellClickMode, HyperlinkCell>(
|
||||
nameof(HyperlinkClickMode),
|
||||
(o, v) => o.CoerceValue(IsMouseOverClickAreaProperty));
|
||||
|
||||
public HyperlinkCellClickMode HyperlinkClickMode {
|
||||
get => (HyperlinkCellClickMode)GetValue(HyperlinkClickModeProperty);
|
||||
set => SetValue(HyperlinkClickModeProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ICommandSource
|
||||
|
||||
#region Command
|
||||
@@ -318,8 +370,8 @@ namespace mbts.ui.controls {
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
_check_mouse_over_text(e);
|
||||
if (_is_mouse_over_text) {
|
||||
_on_mouse_changed(e);
|
||||
if (_is_mouse_over_click_area) {
|
||||
// Hyperlink should take focus when left mouse button is clicked on it
|
||||
// This is consistent with all ButtonBase controls and current Win32 behavior
|
||||
Focus();
|
||||
@@ -336,23 +388,21 @@ namespace mbts.ui.controls {
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) {
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
_check_mouse_over_text(e);
|
||||
_on_mouse_changed(e);
|
||||
if (IsMouseCaptured)
|
||||
ReleaseMouseCapture();
|
||||
if (_can_execute && _is_mouse_over_text)
|
||||
if (_can_execute && _is_mouse_over_click_area)
|
||||
CommandHelper.ExecuteCommandSource(this);
|
||||
_on_mouse_changed(e);
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e) {
|
||||
if (_is_mouse_over_text != IsMouseDirectlyOver)
|
||||
_toggle_mouse_over_text();
|
||||
}
|
||||
protected override void OnPreviewMouseMove(MouseEventArgs e) => _check_mouse_over_text(e);
|
||||
protected override void OnMouseEnter(MouseEventArgs e) => _check_mouse_over_text(e);
|
||||
protected override void OnMouseLeave(MouseEventArgs e) => _check_mouse_over_text(e);
|
||||
protected override void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e) => _on_mouse_changed();
|
||||
protected override void OnPreviewMouseMove(MouseEventArgs e) => _on_mouse_changed(e);
|
||||
protected override void OnMouseEnter(MouseEventArgs e) => _on_mouse_changed(e);
|
||||
protected override void OnMouseLeave(MouseEventArgs e) => _on_mouse_changed(e);
|
||||
|
||||
protected override Size ArrangeOverride(Size finalSize) => finalSize;
|
||||
|
||||
@@ -373,7 +423,7 @@ namespace mbts.ui.controls {
|
||||
if (gr != null) {
|
||||
drawingContext.PushClip(new RectangleGeometry(rect));
|
||||
drawingContext.DrawGlyphRun(_foreground, gr);
|
||||
if (_is_mouse_over_text) {
|
||||
if (IsMouseOverClickArea) {
|
||||
// draw underline
|
||||
var ul_pen = _underline_pen;
|
||||
if (ul_pen != null) {
|
||||
@@ -385,26 +435,15 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
}
|
||||
|
||||
void _check_mouse_over_text(MouseEventArgs e) {
|
||||
var pt = e.GetPosition(this);
|
||||
var orig = _gcr.content_area;
|
||||
var exp = orig.Expanded(1);
|
||||
if (_is_mouse_over_text != exp.Contains(pt)) {
|
||||
bts.Log.assert(_is_mouse_over_text != _gcr.content_area.Expanded(1).Contains(e.GetPosition(this)));
|
||||
_toggle_mouse_over_text();
|
||||
return;
|
||||
}
|
||||
if (_is_mouse_over_text != _gcr.content_area.Expanded(1).Contains(e.GetPosition(this)))
|
||||
_toggle_mouse_over_text();
|
||||
}
|
||||
void _on_mouse_changed() =>
|
||||
_on_mouse_changed(Mouse.GetPosition(this));
|
||||
|
||||
void _toggle_mouse_over_text() {
|
||||
_is_mouse_over_text = !_is_mouse_over_text;
|
||||
if (_is_mouse_over_text)
|
||||
SetCurrentValue(CursorProperty, Cursors.Hand);
|
||||
else
|
||||
ClearValue(CursorProperty);
|
||||
InvalidateVisual();
|
||||
void _on_mouse_changed(MouseEventArgs e) =>
|
||||
_on_mouse_changed(e.GetPosition(this));
|
||||
|
||||
void _on_mouse_changed(Point pt) {
|
||||
_is_mouse_over_text = _gcr.content_area.Expanded(1).Contains(pt);
|
||||
CoerceValue(IsMouseOverClickAreaProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-5
@@ -1,9 +1,8 @@
|
||||
using mbts.spark.filters;
|
||||
using mbts.ui.converters;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
partial class DIYGrid {
|
||||
@@ -22,9 +21,13 @@ namespace mbts.ui.controls {
|
||||
public IDataGridColumnFilter filter { get; }
|
||||
public string filter_template { get; }
|
||||
public Thickness padding { get; set; } = _default_padding;
|
||||
|
||||
public HyperlinkCellClickMode HyperlinkClickMode { get; set; }
|
||||
|
||||
public override FrameworkElement view_factory() {
|
||||
var hl = new HyperlinkCell { Padding = padding };
|
||||
var hl = new HyperlinkCell {
|
||||
HyperlinkClickMode = HyperlinkClickMode,
|
||||
Padding = padding
|
||||
};
|
||||
hl.SetBinding(HyperlinkCell.ForegroundProperty, new Binding(nameof(font_brush)) { Source = this });
|
||||
hl.SetBinding(HyperlinkCell.TextProperty, _binding);
|
||||
hl.SetBinding(HyperlinkCell.FontFamilyProperty, new Binding(nameof(font_family)) { Source = this });
|
||||
@@ -33,8 +36,18 @@ namespace mbts.ui.controls {
|
||||
hl.SetBinding(HyperlinkCell.CommandProperty, _command_binding);
|
||||
hl.SetBinding(HyperlinkCell.WidthProperty, new Binding(nameof(ActualWidth)) { Source = this });
|
||||
hl.SetBinding(HyperlinkCell.BackgroundProperty, new Binding(nameof(background_brush)) { Source = this });
|
||||
hl.SetBinding(
|
||||
CursorProperty,
|
||||
new Binding {
|
||||
Source = hl,
|
||||
Path = new PropertyPath(HyperlinkCell.IsMouseOverClickAreaProperty),
|
||||
Converter = new BooleanToObjectConverter {
|
||||
ValueForFalse = Cursors.Arrow,
|
||||
ValueForTrue = Cursors.Hand
|
||||
}
|
||||
});
|
||||
return hl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user