Compare commits
45
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
089133cede | ||
|
|
5e8a69eea6 | ||
|
|
8bc628e481 | ||
|
|
3013b9a572 | ||
|
|
5d5a51e4fb | ||
|
|
fd4a299c32 | ||
|
|
ca39a34959 | ||
|
|
62c9a06de0 | ||
|
|
37cbf45acd | ||
|
|
da5dae1fc9 | ||
|
|
015f7108da | ||
|
|
eb44285916 | ||
|
|
3bed04f559 | ||
|
|
cde18089f6 | ||
|
|
c026bdfffb | ||
|
|
8e91829f04 | ||
|
|
ec1728e3c3 | ||
|
|
fd2323b453 | ||
|
|
a9d3326c81 | ||
|
|
a2fa75e52b | ||
|
|
db87241d03 | ||
|
|
34bd734d7f | ||
|
|
c1447346f2 | ||
|
|
09caebf7af | ||
|
|
f76a74c063 | ||
|
|
872ab595e8 | ||
|
|
b46d3637cb | ||
|
|
8508e275b8 | ||
|
|
cbf872a615 | ||
|
|
62b7fa603c | ||
|
|
67317d7222 | ||
|
|
e9bf2d96fe | ||
|
|
13772d1941 | ||
|
|
dc93150ba5 | ||
|
|
15d7b95cbb | ||
|
|
657d922a39 | ||
|
|
09631447c7 | ||
|
|
6692b52718 | ||
|
|
dd518ec2c5 | ||
|
|
3eb6fb3dac | ||
|
|
ef0905d756 | ||
|
|
94859dab2e | ||
|
|
d257c94071 | ||
|
|
8864a65f94 | ||
|
|
9ceac92637 |
No files matched your search
@@ -83,7 +83,7 @@ namespace mbts.spark {
|
||||
};
|
||||
_rows_view.LiveFilteringProperties.Add(nameof(Row.is_visible));
|
||||
// Important: these selected_security/md wrappers should be instantiated in the constructor (rather than lazily),
|
||||
// to prevent situations where callers subscribe to the selected_rows.CollectionChanged event before the wrappers do.
|
||||
// to prevent situations where callers subscribe up to the selected_rows.CollectionChanged event before the wrappers do.
|
||||
// This way, the wrappers are protected from unintended side effects of the foreign callbacks, as they will always receive CollectionChange notifications first.
|
||||
selected_securities = selected_rows.SelectObservable(r => r.security);
|
||||
selected_security_mds = selected_rows.SelectObservable(r => r.security_md);
|
||||
|
||||
@@ -3,6 +3,7 @@ using mbts.spark;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.converters;
|
||||
using mbts.ui.extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
@@ -118,11 +119,19 @@ namespace mbts.ui.spark {
|
||||
|
||||
void _show_mini_orderbook(IReadOnlyList<SecurityImmutable> securities, Columns col) {
|
||||
var (vm, context, title, fg) = _vm.orderbook_params(securities, col);
|
||||
_show_popup(vm, new AuxiliaryDataHeader {
|
||||
var header = new AuxiliaryDataHeader {
|
||||
context = context,
|
||||
title = title,
|
||||
title_foreground = fg
|
||||
});
|
||||
};
|
||||
_show_popup(vm, header,
|
||||
curr => {
|
||||
var old_vm = curr;
|
||||
var old_state = old_vm.create();
|
||||
(vm, context, title, fg) = _vm.orderbook_params(securities, col);
|
||||
vm.load_from(old_state);
|
||||
return vm;
|
||||
});
|
||||
}
|
||||
|
||||
void _show_mini_fills(SecurityImmutable security, Columns col) =>
|
||||
@@ -145,23 +154,35 @@ namespace mbts.ui.spark {
|
||||
_show_popup(ticket_vm, new OrderTicketHeader { bbo = ticket_vm.bbo }, resources);
|
||||
}
|
||||
|
||||
void _show_popup(object content, object header) {
|
||||
void _show_popup<T>(T content, object header, Func<T, T> clone_content = null) {
|
||||
var resources = new AuxiliaryDataResources();
|
||||
resources.MergedDictionaries.Add(new AutoHideScrollbarResources());
|
||||
_show_popup(content, header, resources);
|
||||
_show_popup(content, header, resources, clone_content);
|
||||
}
|
||||
|
||||
void _show_popup(object content, object header, ResourceDictionary resources) {
|
||||
var view = new HeaderedContentControl {
|
||||
DataContext = content,
|
||||
Content = content,
|
||||
Header = header,
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Resources = resources ?? new AuxiliaryDataResources()
|
||||
};
|
||||
view.Style = view.Resources[AuxiliaryDataResources.PopupChildStyleKey] as Style;
|
||||
_popup_man.show_async(view).ignore();
|
||||
/// Impl Note:
|
||||
/// When the main window changes (e.g., when it is tabbed, or moved via a tab-header drag) the view (i.e., the popup's child) must also be recreated.
|
||||
/// If the view consists of a <see cref="DIYGrid"/>, and the <see cref="DIYGrid.ColumnsProperty"/> is bound to a property in the content (viewmodel),
|
||||
/// those columns must also be recreated because <see cref="GridViewColumn"/>s cannot be reassigned to a new view.
|
||||
/// <param name="clone_content"/> provides a way to handle such nuances.
|
||||
void _show_popup<T>(T content, object header, ResourceDictionary resources, Func<T, T> clone_content = null) {
|
||||
bool initialized = false;
|
||||
_popup_man.show(() => {
|
||||
if (initialized && clone_content != null) {
|
||||
content = clone_content(content);
|
||||
}
|
||||
initialized = true;
|
||||
var view = new HeaderedContentControl {
|
||||
DataContext = content,
|
||||
Content = content,
|
||||
Header = header,
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Resources = resources ?? new AuxiliaryDataResources()
|
||||
};
|
||||
view.Style = view.Resources[AuxiliaryDataResources.PopupChildStyleKey] as Style;
|
||||
return view;
|
||||
});
|
||||
}
|
||||
|
||||
static class CommandImpl {
|
||||
|
||||
@@ -94,11 +94,14 @@ namespace bts.shell {
|
||||
|
||||
public static fills.FillsSettings DefaultAuxiliaryFills {
|
||||
get {
|
||||
var col_keys = new HashSet<mbts.ui.spark.FillsView.Columns>(
|
||||
fills.FillsSettings.mini_column_keys);
|
||||
col_keys.Add(mbts.ui.spark.FillsView.Columns.Account);
|
||||
var grid = fills.FillsSettings.MiniGridSettings;
|
||||
//if (grid.columns.TryGetFirst(c => c.id == nameof(mbts.ui.spark.FillsView.Columns.Side), out var side_col, out int side_col_pos)) {
|
||||
// var cols = grid.columns.ToList();
|
||||
// cols[side_col_pos] = side_col.with(is_visible: false);
|
||||
// grid = grid.with(cols);
|
||||
//}
|
||||
var rv = fills.FillsSettings.Default();
|
||||
rv.grid = fills.FillsSettings.create_grid(col_keys);
|
||||
rv.grid = grid;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using bts.collections;
|
||||
using mbts.ui;
|
||||
using mbts.ui;
|
||||
using mbts.ui.settings;
|
||||
using ProtoBuf;
|
||||
using System;
|
||||
@@ -35,27 +34,11 @@ namespace bts.shell.fills {
|
||||
|
||||
public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criterion);
|
||||
public static GridSettings MiniGridSettings => GridSettings.Default.with(DefaultColumns.Mini.all, DefaultColumns.Mini.sorting_criterion);
|
||||
public static IEnumerable<Columns> mini_column_keys => DefaultColumns.Mini.mini_cols_data.Keys;
|
||||
|
||||
public IReadOnlyList<SortingCriteria> sorting_criterion => _sorting_criteria ?? (_sorting_criteria = DefaultColumns.default_sorting_criterion.ToList());
|
||||
public uint[] root_securities { get; set; }
|
||||
|
||||
public static FillsSettings Default() =>
|
||||
new FillsSettings(null, null, new FillsFilterStates(), null);
|
||||
|
||||
public static GridSettings create_grid(IEnumerable<Columns> keys) {
|
||||
var key_set = keys as ISet<Columns> ?? new HashSet<Columns>(keys.empty_if_null());
|
||||
var grid = DefaultGridSettings;
|
||||
var cols = new LazyMutatorList<GridColumnSettings>(grid.columns);
|
||||
for (int i = 0; i < cols.Count; i++) {
|
||||
var c = cols[i];
|
||||
if (c.is_visible != key_set.Contains(c.strong_id<Columns>()))
|
||||
cols[i] = c.with(is_visible: !c.is_visible);
|
||||
}
|
||||
if (cols.IsMutated)
|
||||
grid = grid.with(cols);
|
||||
return grid;
|
||||
}
|
||||
public uint[] root_securities { get; set; }
|
||||
|
||||
static class DefaultColumns {
|
||||
public static GridColumnSettings security => new GridColumnSettings(col_id(Columns.Security), DefaultColumnHeaders.Security, width: DefaultColumnWidths.Security);
|
||||
@@ -102,21 +85,21 @@ namespace bts.shell.fills {
|
||||
}
|
||||
|
||||
public static class Mini {
|
||||
public static readonly IReadOnlyDictionary<Columns, (bool is_visible, double? width_override)> mini_cols_data =
|
||||
static readonly IReadOnlyDictionary<Columns, (bool is_visible, double? width_override)> _mini_cols_data =
|
||||
new Dictionary<Columns, (bool is_visible, double? width_override)> {
|
||||
{ Columns.Security, (true, null) },
|
||||
{ Columns.FillTime, (true, ShortColumnWidths.Time) },
|
||||
{ Columns.OrderType, (true, ShortColumnWidths.OrderType) },
|
||||
{ Columns.Side, (true, ShortColumnWidths.Side) },
|
||||
{ Columns.Size, (true, ShortColumnWidths.Size) },
|
||||
{ Columns.Price, (true, ShortColumnWidths.Price) }
|
||||
{ Columns.Price, (true, ShortColumnWidths.Price) },
|
||||
};
|
||||
|
||||
public static IEnumerable<GridColumnSettings> all => DefaultColumns.all.Select(_convert);
|
||||
public static IEnumerable<SortingCriteria> sorting_criterion => default_sorting_criterion.Where(c => _try_get_data(c.col_name, out (bool is_visible, double? _) d) && d.is_visible);
|
||||
|
||||
static bool _try_get_data(string id_str, out (bool is_visible, double? width_override) data) {
|
||||
if (Enum.TryParse(id_str, out Columns k) && mini_cols_data.TryGetValue(k, out data))
|
||||
if (Enum.TryParse(id_str, out Columns k) && _mini_cols_data.TryGetValue(k, out data))
|
||||
return true;
|
||||
data = default;
|
||||
return false;
|
||||
@@ -129,4 +112,4 @@ namespace bts.shell.fills {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:pnl="clr-namespace:bts.shell.pnl"
|
||||
xmlns:bv="clr-namespace:bts.view"
|
||||
xmlns:ft="clr-namespace:mbts.spark.filters"
|
||||
xmlns:u="clr-namespace:mbts.ui"
|
||||
xmlns:c="clr-namespace:mbts.ui.controls"
|
||||
@@ -12,7 +11,6 @@
|
||||
xmlns:usp="clr-namespace:mbts.ui.spark"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
FocusManager.IsFocusScope="True"
|
||||
u:WindowStyle.UnsafeWindow="{Binding Path='(u:WindowStyle.UnsafeWindow)', RelativeSource={RelativeSource AncestorType=bv:SparkWindowView}}"
|
||||
Background="{StaticResource CommonBackground}"
|
||||
Tag="{Binding ElementName=_grid}"
|
||||
>
|
||||
|
||||
@@ -388,7 +388,7 @@ namespace bts.shell.pnl {
|
||||
Resources = new AuxiliaryDataResources()
|
||||
};
|
||||
view.Style = view.Resources[AuxiliaryDataResources.PopupChildStyleKey] as Style;
|
||||
_popup_man.show_async(view).ignore();
|
||||
_popup_man.show(view);
|
||||
}
|
||||
|
||||
class PNLGroupDescription : PropertyGroupDescription, IEquatable<PNLGroupDescription> {
|
||||
|
||||
@@ -12,6 +12,7 @@ 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;
|
||||
|
||||
@@ -39,7 +40,9 @@ namespace mbts.ui.spark {
|
||||
_children = children;
|
||||
}
|
||||
|
||||
public void show(FrameworkElement view) {
|
||||
public void show(FrameworkElement view, 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;
|
||||
@@ -54,7 +57,8 @@ namespace mbts.ui.spark {
|
||||
var top_left = _scroll_content.PointToScreenPixels(
|
||||
col.unclipped_left - x_origin,
|
||||
Math.Ceiling(mouse_pos.Y / _grid.RowHeight) * _grid.RowHeight);
|
||||
var popup = _mk_popup(_owner, view, top_left);
|
||||
// popup
|
||||
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;
|
||||
@@ -96,24 +100,20 @@ namespace mbts.ui.spark {
|
||||
}
|
||||
}
|
||||
|
||||
public async Task show_async(FrameworkElement view, int delay_measure_ms = 100) {
|
||||
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);
|
||||
view.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
show(view);
|
||||
}
|
||||
|
||||
ZPopup _mk_popup(FrameworkElement owner, FrameworkElement view, Point offset) {
|
||||
// popup
|
||||
var popup = new ZPopup {
|
||||
PlacementTarget = _grid,
|
||||
Placement = PlacementMode.Absolute,
|
||||
Child = view,
|
||||
HorizontalOffset = offset.X,
|
||||
VerticalOffset = offset.Y,
|
||||
IsOpen = true,
|
||||
StaysOpen = false
|
||||
};
|
||||
(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));
|
||||
|
||||
@@ -143,17 +143,12 @@ namespace mbts.ui.spark {
|
||||
_children.Remove(popup);
|
||||
}
|
||||
|
||||
async void on_view_preview_mouse_down(object sender, MouseButtonEventArgs e) {
|
||||
void on_view_preview_mouse_down(object sender, MouseButtonEventArgs e) {
|
||||
if (WindowStyle.GetUnsafeWindow(popup) is Window win) {
|
||||
//bool activated = false;
|
||||
await win.Dispatcher.InvokeAsync(() => {
|
||||
if (!win.IsActive) {
|
||||
win.Dispatcher.InvokeAsync(() => {
|
||||
if (!win.IsActive)
|
||||
win.Activate();
|
||||
//activated = true;
|
||||
}
|
||||
});
|
||||
//if (activated)
|
||||
popup.BringToFront();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:u="clr-namespace:mbts.ui"
|
||||
xmlns:c="clr-namespace:mbts.ui.controls"
|
||||
xmlns:cv="clr-namespace:mbts.ui.converters"
|
||||
xmlns:usp="clr-namespace:mbts.ui.spark"
|
||||
xmlns:sp="clr-namespace:mbts.spark"
|
||||
@@ -74,12 +75,17 @@
|
||||
VerticalAlignment="Center"
|
||||
Margin="4,0,0,0"
|
||||
FontWeight="Bold"
|
||||
Text="{Binding title}"
|
||||
ToolTip="{Binding title}"
|
||||
>
|
||||
<TextBlock.Foreground>
|
||||
<SolidColorBrush Color="{Binding title_foreground}"/>
|
||||
</TextBlock.Foreground>
|
||||
<TextBlock.Text>
|
||||
<MultiBinding StringFormat="{}{0} ({1})">
|
||||
<Binding Path="title"/>
|
||||
<Binding Path="ID" RelativeSource="{RelativeSource AncestorType=c:ZPopup}"/>
|
||||
</MultiBinding>
|
||||
</TextBlock.Text>
|
||||
</TextBlock>
|
||||
<CheckBox x:Name="PART_KeepOpen" Grid.Column="2" VerticalAlignment="Center" Margin="4,0,0,0" Focusable="False" IsChecked="{Binding StaysOpen, RelativeSource={RelativeSource AncestorType=Popup}}">Keep Open</CheckBox>
|
||||
<Button x:Name="PART_Close" Grid.Column="3" HorizontalAlignment="Right" Margin="4" Command="{x:Static ApplicationCommands.Close}" CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=HeaderedContentControl}}" Style="{StaticResource RoundCloseButton}"/>
|
||||
@@ -130,4 +136,4 @@
|
||||
<Setter Property="HorizontalContentAlignment" Value="Left"/>
|
||||
<Setter Property="Template" Value="{StaticResource PopupChildTemplate}"/>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</ResourceDictionary>
|
||||
@@ -10,22 +10,20 @@ using System.Windows.Threading;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
/// <summary>
|
||||
/// A <see cref="Popup"/> that can change parent windows (according to the <see cref="WindowStyle.UnsafeWindowProperty"/>).
|
||||
/// Also unlike normal popups, it vanishes when its parent window is minimized, and reappears when it is restored/maximized.
|
||||
/// Popup with an adjustable 'Z' order (ordinary Popup control is always top-most).
|
||||
/// </summary>
|
||||
class ZPopup : Popup {
|
||||
static int _id_counter;
|
||||
static ZPopup _topmost_zpopup;
|
||||
|
||||
readonly object _parent_window_lock = new object();
|
||||
readonly LoggerContext _log;
|
||||
IDisposable _window_sub;
|
||||
Window _window;
|
||||
bool _is_parent_changing;
|
||||
|
||||
public ZPopup() {
|
||||
_log = new LoggerContext(Log.Ui, $"ZPop {ID} - ", null);
|
||||
SetCurrentValue(IDProperty, Interlocked.Increment(ref _id_counter));
|
||||
WindowStyle.AddUnsafeWindowChangedHandler(this, _on_unsafe_window_changed);
|
||||
WindowStyle.AddUnsafeWindowChangedHandler(this, (s, e) => _on_window(e.NewValue));
|
||||
_log = new LoggerContext(Log.Ui, $"ZPop {ID} - ", null);
|
||||
}
|
||||
|
||||
// The ID property helped diagnose issues during developement, by making it easy to keep track of popup instances
|
||||
@@ -43,21 +41,33 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void BringToFront() {
|
||||
/// The logic below assumes that there will only be a single generation of <see cref="ZPopup"/>s
|
||||
/// (i.e., that there won't be any <see cref="ZPopup"/>s nested inside of another <see cref="ZPopup"/>).
|
||||
/// Don't reset the popup's position if it is already topmost wrt the other <see cref="ZPopup"/>s;
|
||||
/// this prevents this popup from covering an open child <see cref="Popup"/>, as it would become the topmost of all windows.
|
||||
if (Interlocked.Exchange(ref _topmost_zpopup, this) != this) {
|
||||
// Reset the unsafe window hooks, so the callbacks are moved to the back of the invocation list.
|
||||
// As each popup window is assigned the 'TopMost' z position in the parent window 'Activated' callback, and 'NoTopMost' in the 'Deactivated' callback,
|
||||
// the relative z positions of sibling popups (i.e., w/ a common parent window) are a reverse linear translation of the invocation list (a LIFO/Stack arrangment).
|
||||
WindowStyle.RemoveUnsafeWindowChangedHandler(this, _on_unsafe_window_changed);
|
||||
WindowStyle.AddUnsafeWindowChangedHandler(this, _on_unsafe_window_changed);
|
||||
_on_window(_window, false);
|
||||
}
|
||||
protected override void OnOpened(EventArgs e) {
|
||||
// Links that explain the nuances of SetWindowPos:
|
||||
// https://www.pinvoke.net/default.aspx/user32.setwindowpos
|
||||
// https://devblogs.microsoft.com/oldnewthing/20051121-10/?p=33263
|
||||
// Links to the Popup base class code that calls SetWindowPos:
|
||||
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/Popup.cs,4fabcf08378ac9be,references
|
||||
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/Popup.cs,fef52e73f7d51076
|
||||
// https://referencesource.microsoft.com/#WindowsBase/Shared/MS/Win32/UnsafeNativeMethodsCLR.cs,d9c926e7b8eb6e2c
|
||||
/// The main purpose of this class, as a Popup implementation, is to maintain the popup window's Z index so that it is always just above the main/parent window;
|
||||
/// in other words, this should always be the case: popup_win.ZIndex == parent_win.ZIndex + 1.
|
||||
|
||||
/// The base class <see cref="Popup"/> was not designed to behave this way.
|
||||
/// In the base class, when the popup is opened, a native call to set the window's [z]position (<see cref="NativeMethods.SetWindowPos(IntPtr, IntPtr, int, int, int, int, SWP)"/>)
|
||||
/// is made with <see cref="NativeMethods.HWND_TOPMOST"/> as the 'insertAfter' param (just before this method is called actually).
|
||||
|
||||
/// There are 2 'z order' groups of windows: 1) 'Normal' windows, and 2) 'TopMost' windows.
|
||||
/// 'TopMost' windows are always on top of 'Normal' windows. The former group typically includes modal dialogs and popups,
|
||||
/// while the latter includes normal (for lack of a better word) windows.
|
||||
|
||||
/// Positioning the popup window just above the main window (wrt z order) requires 2 calls:
|
||||
/// 1) a call to remove it from the 'TopMost' group (this call is idempotent and safe to make every time the popup is displayed),
|
||||
/// 2) a call to set it on top of the 'Normal' windows.
|
||||
_set_top_most(NativeMethods.HWND_NOTOPMOST, "intializing z position on opened (1/2)");
|
||||
}
|
||||
|
||||
bool _is_parent_changing;
|
||||
|
||||
protected override void OnClosed(EventArgs e) {
|
||||
_log.info($"disposing parent window sub - reason: {(_is_parent_changing ? "parent change" : "closed")}.");
|
||||
Disposable.dispose(ref _window_sub);
|
||||
@@ -68,33 +78,6 @@ namespace mbts.ui.controls {
|
||||
base.OnClosed(e);
|
||||
}
|
||||
|
||||
protected override void OnOpened(EventArgs e) {
|
||||
// Links that explain the nuances of SetWindowPos:
|
||||
// https://www.pinvoke.net/default.aspx/user32.setwindowpos
|
||||
// https://devblogs.microsoft.com/oldnewthing/20051121-10/?p=33263
|
||||
// Links to the Popup base class code that calls SetWindowPos:
|
||||
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/Popup.cs,4fabcf08378ac9be,references
|
||||
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/Popup.cs,fef52e73f7d51076
|
||||
// https://referencesource.microsoft.com/#WindowsBase/Shared/MS/Win32/UnsafeNativeMethodsCLR.cs,d9c926e7b8eb6e2c
|
||||
|
||||
/// The main purpose of this class is to maintain the popup window's Z index so that it is always just above the main/parent window (and beneath every other window on top of it);
|
||||
/// in other words, this should always be the case: popup_win.ZIndex == parent_win.ZIndex + 1.
|
||||
|
||||
/// Brief notes on window z order, which can be manipulated via <see cref="native.NativeMethods.SetWindowPos(IntPtr, IntPtr, int, int, int, int, SWP)"/>:
|
||||
/// - There are 2 'z order' groups of windows
|
||||
/// 1) 'Normal' windows: windows displayed via <see cref="Window.Show"/> (including the shell windows in BTS), and non-modal dialogs
|
||||
/// 2) 'TopMost' windows: modal dialogs, and popups; these windows are always on top of 'Normal' windows.
|
||||
|
||||
/// The base class <see cref="Popup"/> was not designed to behave this way.
|
||||
/// In the base class, when the popup is opened, a call to set the window's [z]position (<see cref="NativeMethods.SetWindowPos(IntPtr, IntPtr, int, int, int, int, SWP)"/>)
|
||||
/// is made with <see cref="NativeMethods.HWND_TOPMOST"/> as the 'insertAfter' param (just before this method is called).
|
||||
|
||||
/// Positioning the popup window just above the main window (wrt z order) requires 2 calls:
|
||||
/// 1) a call to remove it from the 'TopMost' group (this call is idempotent and safe to make every time the popup is displayed),
|
||||
/// 2) a call to set it on top of the 'Normal' windows.
|
||||
_set_top_most(NativeMethods.HWND_NOTOPMOST, "intializing z position on opened (1/2)");
|
||||
}
|
||||
|
||||
static string _win_handle_name(IntPtr? handle) {
|
||||
if (handle.HasValue) {
|
||||
if (handle.Value == NativeMethods.HWND_BOTTOM)
|
||||
@@ -110,21 +93,16 @@ namespace mbts.ui.controls {
|
||||
return "Null";
|
||||
}
|
||||
|
||||
void _on_unsafe_window_changed(object sender, RoutedPropertyChangedEventArgs<Window> e) =>
|
||||
_on_window(e.NewValue);
|
||||
|
||||
async void _on_window(Window win, bool reopen = true) {
|
||||
async void _on_window(Window win) {
|
||||
Disposable.dispose(ref _window_sub);
|
||||
var old_win = _window;
|
||||
_window = win;
|
||||
if (win == null)
|
||||
return;
|
||||
/// the lock object can be local to this method,
|
||||
/// bc <see cref="_window_sub"/> will [always]be disposed on the same thread (this instance's dispatcher).
|
||||
object locker = new object();
|
||||
bool cancel = false;
|
||||
Action unhook = null;
|
||||
if (reopen && IsOpen && old_win != null) {
|
||||
if (IsOpen && old_win != null) {
|
||||
// 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.");
|
||||
|
||||
@@ -9,6 +9,9 @@ namespace mbts.ui
|
||||
public static IProvideValueTarget GetValueTargetProvider(this IServiceProvider serviceProvider) {
|
||||
return serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
|
||||
}
|
||||
public static bool GetTargetObj<T>(this IServiceProvider serviceProvider, out T result) where T : class =>
|
||||
(result = serviceProvider.GetValueTargetProvider().TargetObject as T) != null;
|
||||
|
||||
public static DependencyObject GetTargetObj(this IServiceProvider serviceProvider) {
|
||||
return serviceProvider.GetTargetObj<DependencyObject>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace mbts.ui {
|
||||
class ThreadedBindingExtension : MarkupExtension {
|
||||
public ThreadedBindingExtension() { }
|
||||
|
||||
public ThreadedBindingExtension(PropertyPath Path) {
|
||||
this.Path = Path;
|
||||
}
|
||||
|
||||
~ThreadedBindingExtension() =>
|
||||
Disposable.dispose(ref _sub);
|
||||
|
||||
[ConstructorArgument(nameof(Path))]
|
||||
public PropertyPath Path { get; set; }
|
||||
|
||||
public IValueConverter Converter { get; set; }
|
||||
|
||||
IDisposable _sub;
|
||||
public override object ProvideValue(IServiceProvider serviceProvider) {
|
||||
if (serviceProvider.GetTargetObj(out UIElement el) && serviceProvider.GetTargetProperty() is DependencyProperty dp) {
|
||||
WindowStyle.AddUnsafeWindowChangedHandler(el, on_unsafe_window_changed);
|
||||
void on_unsafe_window_changed(object sender, RoutedPropertyChangedEventArgs<Window> e) {
|
||||
Disposable.dispose(ref _sub);
|
||||
if (e.NewValue != null) {
|
||||
BindingProxy proxy = null;
|
||||
var value_src = Observable.Create<object>(o => {
|
||||
if (proxy == null) {
|
||||
proxy = new BindingProxy();
|
||||
proxy.ValueChanged += on_value_changed;
|
||||
proxy.SetBinding(BindingProxy.ValueProperty, new Binding {
|
||||
Path = Path,
|
||||
Source = e.NewValue
|
||||
});
|
||||
}
|
||||
return Disposable.Create(() => proxy.ValueChanged -= on_value_changed);
|
||||
void on_value_changed(object s, DependencyPropertyChangedEventArgs dpe) =>
|
||||
o.OnNext(dpe.NewValue);
|
||||
})
|
||||
.SubscribeOn(e.NewValue.Dispatcher)
|
||||
.OnDispatcher();
|
||||
_sub = value_src.Subscribe(v => {
|
||||
el.SetCurrentValue(dp, v);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,12 +35,12 @@ namespace mbts.ui {
|
||||
public static (double x, double y) deconstruct(this Point point) =>
|
||||
(point.X, point.Y);
|
||||
|
||||
public static Point minus(this Point point, Point other) =>
|
||||
new Point(point.X - other.X, point.Y - other.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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts;
|
||||
using mbts.ui.native;
|
||||
using System;
|
||||
using System.Reactive.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
@@ -41,6 +42,17 @@ namespace mbts.ui {
|
||||
el.RemoveHandler(UnsafeWindowChangedEvent, handler);
|
||||
#endregion
|
||||
|
||||
public static IObservable<Window> ObserveUnsafeWindow(UIElement el) =>
|
||||
Observable.Create<Window>(o => {
|
||||
var win = GetUnsafeWindow(el);
|
||||
if (win != null)
|
||||
o.OnNext(win);
|
||||
AddUnsafeWindowChangedHandler(el, on_unsafe_window_changed);
|
||||
return Disposable.Create(() => RemoveUnsafeWindowChangedHandler(el, on_unsafe_window_changed));
|
||||
void on_unsafe_window_changed(object sender, RoutedPropertyChangedEventArgs<Window> e) =>
|
||||
o.OnNext(e.NewValue);
|
||||
});
|
||||
|
||||
static class WindowCallbackHelper {
|
||||
public static ExecutedRoutedEventHandler safe_command_executed_handler(Action<Window> action, string action_name) =>
|
||||
(s, e) => _safe_execute_on_window(s, action, action_name);
|
||||
@@ -102,6 +114,8 @@ namespace mbts.ui {
|
||||
}
|
||||
public static readonly DependencyProperty ShowMaximizeButtonProperty =
|
||||
DependencyProperty.RegisterAttached("ShowMaximizeButton", typeof(bool), typeof(WindowStyle), new PropertyMetadata(false));
|
||||
|
||||
internal static void AddUnsafeWindowChangedHandler(object on_unsafe_win_changed) => throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region AllowResize
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace mbts.ui {
|
||||
class BindingProxy : FrameworkElement {
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register(nameof(Value), typeof(object), typeof(BindingProxy));
|
||||
|
||||
public object Value {
|
||||
get => GetValue(ValueProperty);
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
}
|
||||
using System.Windows;
|
||||
|
||||
namespace mbts.ui {
|
||||
class BindingProxy : FrameworkElement {
|
||||
public event DependencyPropertyChangedEventHandler ValueChanged;
|
||||
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register(nameof(Value), typeof(object), typeof(BindingProxy),
|
||||
new PropertyMetadata(OnValueChanged));
|
||||
|
||||
static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
if (d is BindingProxy bp)
|
||||
bp.ValueChanged?.Invoke(bp, e);
|
||||
}
|
||||
|
||||
public object Value {
|
||||
get => GetValue(ValueProperty);
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,25 @@
|
||||
using mbts.utils;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace mbts.ui.utilities {
|
||||
static class MouseHelper {
|
||||
public static MouseButtonState button_state(MouseButton button) {
|
||||
switch (button) {
|
||||
case MouseButton.Left: return Mouse.LeftButton;
|
||||
case MouseButton.Middle: return Mouse.MiddleButton;
|
||||
case MouseButton.Right: return Mouse.RightButton;
|
||||
case MouseButton.XButton1: return Mouse.XButton1;
|
||||
case MouseButton.XButton2: return Mouse.XButton2;
|
||||
default: throw ExceptionBuilder.Argument.unexpected_case(button, nameof(button));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using mbts.ui.dpi;
|
||||
using mbts.utils;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace mbts.ui.utilities {
|
||||
static class MouseHelper {
|
||||
public static Point get_absolute_position() {
|
||||
var wf_pt = System.Windows.Forms.Control.MousePosition;
|
||||
return new Point(wf_pt.X * HighDpiExtensions.pixel_unit, wf_pt.Y * HighDpiExtensions.pixel_unit);
|
||||
}
|
||||
|
||||
public static MouseButtonState button_state(MouseButton button) {
|
||||
switch (button) {
|
||||
case MouseButton.Left: return Mouse.LeftButton;
|
||||
case MouseButton.Middle: return Mouse.MiddleButton;
|
||||
case MouseButton.Right: return Mouse.RightButton;
|
||||
case MouseButton.XButton1: return Mouse.XButton1;
|
||||
case MouseButton.XButton2: return Mouse.XButton2;
|
||||
default: throw ExceptionBuilder.Argument.unexpected_case(button, nameof(button));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace bts.collections {
|
||||
/// <summary>
|
||||
/// Implements both <see cref="IReadOnlyList{T}"/> and <see cref="IList{T}"/>, as a wrapper around a source list.
|
||||
/// When (not before) a mutation method of the latter interface is called (for the first time), the source list will be copied to a new instance of <see cref="List{T}"/>,
|
||||
/// which will become the target of all subsequent calls to methods of either interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the list.</typeparam>
|
||||
class LazyMutatorList<T> : IList<T>, IReadOnlyList<T> {
|
||||
IReadOnlyList<T> _result;
|
||||
List<T> _mutable_backing;
|
||||
|
||||
public LazyMutatorList(IReadOnlyList<T> source) =>
|
||||
_result = source ?? throw new ArgumentNullException(nameof(source));
|
||||
|
||||
public T this[int index] {
|
||||
get => _result[index];
|
||||
set => _mutable[index] = value;
|
||||
}
|
||||
|
||||
public int Count => _result.Count;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public bool IsMutated => _mutable_backing != null;
|
||||
|
||||
public IReadOnlyList<T> Result => _result;
|
||||
|
||||
public List<T> MutatedResult => _mutable_backing ?? throw new InvalidOperationException("List has not been mutated.");
|
||||
|
||||
IList<T> _mutable {
|
||||
get {
|
||||
if (_mutable_backing == null) {
|
||||
_mutable_backing = new List<T>(_result.Count);
|
||||
for (int i = 0; i < _result.Count; i++)
|
||||
_mutable.Add(_result[i]);
|
||||
_result = _mutable_backing;
|
||||
}
|
||||
return _mutable_backing;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(T item) =>
|
||||
_mutable.Add(item);
|
||||
|
||||
public void Clear() =>
|
||||
_mutable.Clear();
|
||||
|
||||
public bool Contains(T item) =>
|
||||
IndexOf(item) >= 0;
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) {
|
||||
var r = _result;
|
||||
for (int i = 0; i < r.Count; i++)
|
||||
array[i + arrayIndex] = r[i];
|
||||
}
|
||||
|
||||
public int IndexOf(T item) {
|
||||
var r = _result;
|
||||
for (int i = 0; i < r.Count; i++) {
|
||||
if (EqualityComparer<T>.Default.Equals(r[i], item))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void Insert(int index, T item) =>
|
||||
_mutable.Insert(index, item);
|
||||
|
||||
public bool Remove(T item) =>
|
||||
_mutable.Remove(item);
|
||||
|
||||
public void RemoveAt(int index) =>
|
||||
_mutable.RemoveAt(index);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() =>
|
||||
(_mutable_backing ?? _result).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user