checkpoint 3
This commit is contained in:
1 parent
5e8a69eea6
commit
089133cede
6 files changed
+133
-87
No files matched your search
@@ -10,7 +10,9 @@ using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
|
||||
@@ -40,7 +42,7 @@ namespace mbts.ui.spark {
|
||||
|
||||
public void show(FrameworkElement view, int delay_measure_ms = 100) { }
|
||||
|
||||
public void show(Func<FrameworkElement> view_factory, int delay_measure_ms = 100) {
|
||||
public async void show(Func<FrameworkElement> view_factory, int delay_measure_ms = 100) {
|
||||
var layout = _grid.GetColumnGeometry().ToList();
|
||||
if (layout.Count == 0)
|
||||
return;
|
||||
@@ -56,50 +58,49 @@ namespace mbts.ui.spark {
|
||||
col.unclipped_left - x_origin,
|
||||
Math.Ceiling(mouse_pos.Y / _grid.RowHeight) * _grid.RowHeight);
|
||||
// popup
|
||||
ZPopup popup = null;
|
||||
var popup = await _mk_popup(_owner, view_factory, delay_measure_ms, top_left);
|
||||
// attach to [parent] win
|
||||
IDisposable win_hook = null, win_pos_hook = null;
|
||||
Point last_pt = default;
|
||||
win_hook = WindowStyle.ObserveUnsafeWindow(_owner).OnDispatcher().Subscribe(async win => {
|
||||
bool initialized = false;
|
||||
WindowStyle.AddUnsafeWindowChangedHandler(popup, on_unsafe_win_changed);
|
||||
popup.SetBinding(WindowStyle.UnsafeWindowProperty, new Binding {
|
||||
Path = new PropertyPath(WindowStyle.UnsafeWindowProperty),
|
||||
Source = _owner,
|
||||
Mode = BindingMode.OneWay
|
||||
});
|
||||
popup.Closed += on_closed;
|
||||
void on_unsafe_win_changed(object sender, RoutedPropertyChangedEventArgs<Window> e) {
|
||||
var win = e.NewValue;
|
||||
Disposable.dispose(ref win_pos_hook);
|
||||
if (win != null) {
|
||||
bool initialized = popup != null;
|
||||
var window_pos_src = Observable.Create<Point>(o => {
|
||||
o.OnNext(new Point(win.Left, win.Top));
|
||||
win.LocationChanged += on_location_changed;
|
||||
return Disposable.Create(() => win.LocationChanged -= on_location_changed);
|
||||
void on_location_changed(object sender, EventArgs e) =>
|
||||
o.OnNext(new Point(win.Left, win.Top));
|
||||
})
|
||||
.SubscribeOn(win.Dispatcher)
|
||||
.DistinctUntilChanged()
|
||||
.ObserveOnDispatcher(DispatcherPriority.Normal);
|
||||
|
||||
if (popup == null) {
|
||||
popup = await _mk_popup(_owner, view_factory, delay_measure_ms, initialized ? new Point(popup.HorizontalOffset, popup.VerticalOffset) : top_left, false);
|
||||
popup.Closed += on_closed;
|
||||
if (!initialized) {
|
||||
// The popup's offset is initialized to the screen coordinates (further adjustments are made according to the delta),
|
||||
// so last_pt must be initialized to the window's top left.
|
||||
last_pt = new Point(win.Left, win.Top);
|
||||
initialized = true;
|
||||
}
|
||||
popup.SetCurrentValue(WindowStyle.UnsafeWindowProperty, win);
|
||||
|
||||
win_pos_hook = window_pos_src.Subscribe(pt => {
|
||||
if (!initialized) {
|
||||
initialized = true;
|
||||
} else {
|
||||
var (xlast, ylast) = last_pt.deconstruct();
|
||||
popup.HorizontalOffset += pt.X - xlast;
|
||||
popup.VerticalOffset += pt.Y - ylast;
|
||||
}
|
||||
last_pt = pt;
|
||||
});
|
||||
win_pos_hook = win.ObserveTopLeft()
|
||||
.SubscribeOn(win.Dispatcher)
|
||||
.ObserveOnDispatcher(DispatcherPriority.Normal)
|
||||
.Subscribe(pt => {
|
||||
popup.HorizontalOffset += pt.X - last_pt.X;
|
||||
popup.VerticalOffset += pt.Y - last_pt.Y;
|
||||
last_pt = pt;
|
||||
});
|
||||
}
|
||||
void on_closed(object sender, EventArgs e) {
|
||||
popup.Closed -= on_closed;
|
||||
Disposable.dispose(ref win_pos_hook);
|
||||
Disposable.dispose(ref win_hook);
|
||||
}
|
||||
});
|
||||
}
|
||||
void on_closed(object sender, EventArgs e) {
|
||||
popup.Closed -= on_closed;
|
||||
WindowStyle.RemoveUnsafeWindowChangedHandler(_owner, on_unsafe_win_changed);
|
||||
Disposable.dispose(ref win_pos_hook);
|
||||
Disposable.dispose(ref win_hook);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<ZPopup> _mk_popup(FrameworkElement owner, Func<FrameworkElement> view_factory, int delay_measure_ms, Point? offset = null, bool stays_open = false) {
|
||||
async Task<ZPopup> _mk_popup(FrameworkElement owner, Func<FrameworkElement> view_factory, int delay_measure_ms, Point offset) {
|
||||
var view = view_factory();
|
||||
if (delay_measure_ms > 0)
|
||||
await Task.Delay(delay_measure_ms);
|
||||
@@ -110,22 +111,21 @@ namespace mbts.ui.spark {
|
||||
Placement = PlacementMode.Absolute,
|
||||
Child = view,
|
||||
IsOpen = true,
|
||||
StaysOpen = stays_open
|
||||
StaysOpen = false
|
||||
};
|
||||
if (offset.HasValue)
|
||||
(popup.HorizontalOffset, popup.VerticalOffset) = offset.Value.deconstruct();
|
||||
(popup.HorizontalOffset, popup.VerticalOffset) = offset.deconstruct();
|
||||
view.InputBindings.Add(new KeyBinding(ApplicationCommands.Close, Key.Escape, ModifierKeys.None));
|
||||
view.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => popup.IsOpen = false));
|
||||
|
||||
view.PreviewMouseDown += on_view_preview_mouse_down;
|
||||
view.ConnectDataContext<IRequestViewClose>(rvc => {
|
||||
rvc.RequestClose += on_request_close;
|
||||
return Disposable.Create(() => rvc.RequestClose -= on_request_close);
|
||||
void on_request_close(object sender, EventArgs e) {
|
||||
popup.IsOpen = false;
|
||||
}
|
||||
});
|
||||
var drag_hooks = PopupViewController.enable_drag_move(_owner, popup);
|
||||
var hooks = Disposable.Create(
|
||||
view.SubscribePreviewMouseDown(on_view_preview_mouse_down),
|
||||
view.ConnectDataContext<IRequestViewClose>(rvc => {
|
||||
rvc.RequestClose += on_request_close;
|
||||
return Disposable.Create(() => rvc.RequestClose -= on_request_close);
|
||||
void on_request_close(object sender, EventArgs e) =>
|
||||
popup.IsOpen = false;
|
||||
}),
|
||||
PopupViewController.enable_drag_move(_owner, popup));
|
||||
_children.Add(popup);
|
||||
_owner.ReleaseMouseCapture();
|
||||
popup.Closed += on_closed;
|
||||
@@ -135,8 +135,7 @@ namespace mbts.ui.spark {
|
||||
return popup;
|
||||
//
|
||||
void on_closed(object sender, EventArgs e) {
|
||||
view.PreviewMouseDown -= on_view_preview_mouse_down;
|
||||
Disposable.dispose(ref drag_hooks);
|
||||
Disposable.dispose(ref hooks);
|
||||
popup.ReleaseMouseCapture();
|
||||
if (Mouse.Captured == _owner)
|
||||
_owner.ReleaseMouseCapture();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using mbts.ui.dpi;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Input;
|
||||
@@ -55,23 +56,33 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable enable_drag_move(FrameworkElement surface, Popup popup) {
|
||||
/// <summary>
|
||||
/// Enables drag move behavior by incrementing the popoup's <see cref="Popup.HorizontalOffset"/> and <see cref="Popup.VerticalOffset"/> properties
|
||||
/// according to the mouse drag delta.
|
||||
/// </summary>
|
||||
/// <param name="surface">The element to initiate drag ops.</param>
|
||||
/// <param name="popup">The popup to exhibit drag move behavior.</param>
|
||||
/// <param name="adjust_to_actual_offset">
|
||||
/// Whether or not the drag delta should be calculated by the popup's position actual position on the screen, or the popup's offset values
|
||||
/// (the latter are not neccesarily within the bounds of the screen).
|
||||
/// </param>
|
||||
/// <returns>An <see cref="IDisposable"/> that will disable the drag behavior when disposed.</returns>
|
||||
public static IDisposable enable_drag_move(FrameworkElement surface, Popup popup, bool adjust_to_actual_offset = true) {
|
||||
var child = popup.Child;
|
||||
Point offset = get_offset();
|
||||
Point? last_drag_pt = null;
|
||||
return DragDropHelper.subscribe_drag_coordinates(surface, popup.Child as FrameworkElement).Subscribe(drag_pt => {
|
||||
child = popup.Child;
|
||||
offset = get_offset();
|
||||
if (drag_pt.HasValue) {
|
||||
var d = drag_pt.Value;
|
||||
var rs = surface.RenderSize;
|
||||
if (last_drag_pt.HasValue) {
|
||||
popup.HorizontalOffset += d.X - last_drag_pt.Value.X;
|
||||
popup.VerticalOffset += d.Y - last_drag_pt.Value.Y;
|
||||
last_drag_pt = d;
|
||||
(popup.HorizontalOffset, popup.VerticalOffset) =
|
||||
offset.plus(drag_pt.Value.minus(last_drag_pt.Value)).deconstruct();
|
||||
} else {
|
||||
popup.SetCurrentValue(Popup.StaysOpenProperty, true);
|
||||
last_drag_pt = d;
|
||||
Mouse.Capture(surface);
|
||||
}
|
||||
} else {
|
||||
last_drag_pt = null;
|
||||
if (popup.StaysOpen) {
|
||||
// popup.Child must be focused BEFORE the StaysOpen binding target (i.e., the popup itself) is updated;
|
||||
// Otherwise, if StaysOpen becomes false as a result of the update,
|
||||
@@ -85,7 +96,13 @@ namespace mbts.ui.controls {
|
||||
surface.ReleaseMouseCapture();
|
||||
}
|
||||
}
|
||||
last_drag_pt = drag_pt;
|
||||
});
|
||||
|
||||
Point get_offset() =>
|
||||
adjust_to_actual_offset ?
|
||||
(child == null ? default : child.PointToScreenPixels(default)) :
|
||||
new Point(popup.HorizontalOffset, popup.VerticalOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,18 +78,33 @@ namespace mbts.ui.controls {
|
||||
base.OnClosed(e);
|
||||
}
|
||||
|
||||
static string _win_handle_name(IntPtr? handle) {
|
||||
if (handle.HasValue) {
|
||||
if (handle.Value == NativeMethods.HWND_BOTTOM)
|
||||
return "Bottom";
|
||||
if (handle.Value == NativeMethods.HWND_TOP)
|
||||
return "Top";
|
||||
if (handle.Value == NativeMethods.HWND_TOPMOST)
|
||||
return "TopMost";
|
||||
if (handle.Value == NativeMethods.HWND_NOTOPMOST)
|
||||
return "NoTopMost";
|
||||
return "Custom";
|
||||
}
|
||||
return "Null";
|
||||
}
|
||||
|
||||
async void _on_window(Window win) {
|
||||
Disposable.dispose(ref _window_sub);
|
||||
var old_win = _window;
|
||||
_window = win;
|
||||
if (win == null)
|
||||
return;
|
||||
// state
|
||||
object locker = new object();
|
||||
bool cancel = false;
|
||||
Action unhook = null;
|
||||
if (IsOpen && old_win != null) {
|
||||
// sorcery to recreate the popup child window :)
|
||||
// close then reopen to force the base class to detach from the old presentation source
|
||||
// then reattach to the new one
|
||||
_log.info("Parent window changed - closing then reopneing popup to detach from previous parent's presentation source.");
|
||||
_is_parent_changing = true;
|
||||
SetCurrentValue(IsOpenProperty, false);
|
||||
@@ -162,12 +177,12 @@ namespace mbts.ui.controls {
|
||||
flags |= SWP.SWP_ASYNCWINDOWPOS;
|
||||
}
|
||||
if (!_try_get_insert_after(insert_after, out var ia_handle)) {
|
||||
_log.info($"IGNORED setting top most because an explicit insert after handle was not specified, and the parent window's handle was not found - reason (for attempt): {reason}.");
|
||||
_log.info($"Ignored setting top most because an explicit 'insert after' handle was not specified, and the parent window's handle was not found - reason (for attempt): {reason}.");
|
||||
} else if (_try_get_handle(out var handle) && NativeMethods.GetWindowRect(handle, out var rect)) {
|
||||
_log.info($"setting top most - reason: {reason}; insert after handle: {_win_handle_name(insert_after)}.");
|
||||
NativeMethods.SetWindowPos(handle, ia_handle, rect.Left, rect.Top, (int)Width, (int)Height, flags);
|
||||
} else {
|
||||
_log.info($"IGNORED setting top most because the child window's handle was not found - reason (for attempt): {reason}; insert after handle: {_win_handle_name(insert_after)}.");
|
||||
_log.info($"Ignored setting top most because the child window's handle was not found - reason (for attempt): {reason}; insert after handle: {_win_handle_name(insert_after)}.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,15 +195,6 @@ namespace mbts.ui.controls {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _try_get_parent_handle(out IntPtr handle) {
|
||||
if (_window != null && PresentationSource.FromVisual(_window) is HwndSource hwnd) {
|
||||
handle = hwnd.Handle;
|
||||
return true;
|
||||
}
|
||||
handle = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _try_get_insert_after(IntPtr? curr, out IntPtr result) {
|
||||
if (curr.HasValue) {
|
||||
result = curr.Value;
|
||||
@@ -200,20 +206,5 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static string _win_handle_name(IntPtr? handle) {
|
||||
if (handle.HasValue) {
|
||||
if (handle.Value == NativeMethods.HWND_BOTTOM)
|
||||
return "Bottom";
|
||||
if (handle.Value == NativeMethods.HWND_TOP)
|
||||
return "Top";
|
||||
if (handle.Value == NativeMethods.HWND_TOPMOST)
|
||||
return "TopMost";
|
||||
if (handle.Value == NativeMethods.HWND_NOTOPMOST)
|
||||
return "NoTopMost";
|
||||
return "Custom";
|
||||
}
|
||||
return "Null";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using mbts.ui.utility.text;
|
||||
using bts.utils;
|
||||
using mbts.ui.utility.text;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -56,6 +57,22 @@ namespace mbts.ui.extensions {
|
||||
public static IDisposable DeferInvalidateVisual(this UIElement target) =>
|
||||
Disposable.Create(target.InvalidateVisual);
|
||||
|
||||
public static IObservable<Point> ObserveTopLeft(this Window win) =>
|
||||
Observable.Create<Point>(o => {
|
||||
var last_pt = new Point(win.Left, win.Top);
|
||||
o.OnNext(last_pt);
|
||||
win.LocationChanged += on_location_changed;
|
||||
return Disposable.Create(() => win.LocationChanged -= on_location_changed);
|
||||
|
||||
void on_location_changed(object sender, EventArgs e) =>
|
||||
notify();
|
||||
|
||||
void notify() {
|
||||
if (Swap.compare_exchange(ref last_pt, new Point(win.Left, win.Top)))
|
||||
o.OnNext(last_pt);
|
||||
}
|
||||
});
|
||||
|
||||
public static IDisposable SubscribeGotFocus(this UIElement target, RoutedEventHandler callback) {
|
||||
target.GotFocus += callback;
|
||||
return Disposable.Create(() => target.GotFocus -= callback);
|
||||
|
||||
@@ -35,6 +35,12 @@ namespace mbts.ui {
|
||||
public static (double x, double y) deconstruct(this Point point) =>
|
||||
(point.X, point.Y);
|
||||
|
||||
public static Point plus(this Point point, Point other) =>
|
||||
new Point(point.X + other.X, point.Y + other.Y);
|
||||
|
||||
public static Point minus(this Point point, Point other) =>
|
||||
new Point(point.X - other.X, point.Y - other.Y);
|
||||
|
||||
public static object get_template_child(this Control control, string name) {
|
||||
if (control == null) throw new ArgumentNullException(nameof(control));
|
||||
return control?.Template?.FindName(name, control);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace bts.utils {
|
||||
public static class Swap {
|
||||
public static bool compare_exchange<T>(ref T store, T new_value) =>
|
||||
compare_exchange(ref store, new_value, EqualityComparer<T>.Default);
|
||||
|
||||
public static bool compare_exchange<T>(ref T store, T new_value, IEqualityComparer<T> comparer) {
|
||||
if (!comparer.Equals(store, new_value)) {
|
||||
store = new_value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user