diff --git a/src/client/spark/view/Resources/WindowStyle.xaml.cs b/src/client/spark/view/Resources/WindowStyle.xaml.cs index a597aaab3..d2028f715 100644 --- a/src/client/spark/view/Resources/WindowStyle.xaml.cs +++ b/src/client/spark/view/Resources/WindowStyle.xaml.cs @@ -17,6 +17,30 @@ namespace mbts.ui { MinimizeWindowExecutedHandler = WindowCallbackHelper.command_executed_handler(w => w.WindowState = WindowState.Minimized), MaximizeWindowExecutedHandler = WindowCallbackHelper.command_executed_handler(w => w.WindowState = w.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized); + #region UnsafeWindow + // UnsafeWindow is described as 'Unsafe' because the window may be on a different dispatcher than the DependencyObject target. + public static readonly DependencyProperty UnsafeWindowProperty = + DependencyProperty.RegisterAttached("UnsafeWindow", typeof(Window), typeof(WindowStyle), new PropertyMetadata(OnUnsafeWindowChanged)); + + static void OnUnsafeWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is UIElement el) + el.RaiseEvent(new RoutedPropertyChangedEventArgs(e.OldValue as Window, e.NewValue as Window, UnsafeWindowChangedEvent)); + } + + public static Window GetUnsafeWindow(DependencyObject d) => d.GetValue(UnsafeWindowProperty) as Window; + public static void SetUnsafeWindow(DependencyObject d, Window value) => d.SetValue(UnsafeWindowProperty, value); + #endregion + + #region UnsafeWindowChanged (Event) + internal static readonly RoutedEvent UnsafeWindowChangedEvent = EventManager.RegisterRoutedEvent("UnsafeWindowChanged", RoutingStrategy.Direct, typeof(RoutedPropertyChangedEventHandler), typeof(WindowStyle)); + + internal static void AddUnsafeWindowChangedHandler(UIElement el, RoutedPropertyChangedEventHandler handler) => + el.AddHandler(UnsafeWindowChangedEvent, handler); + + internal static void RemoveUnsafeWindowChangedHandler(UIElement el, RoutedPropertyChangedEventHandler handler) => + el.RemoveHandler(UnsafeWindowChangedEvent, handler); + #endregion + static class WindowCallbackHelper { public static ExecutedRoutedEventHandler safe_command_executed_handler(Action action, string action_name) => (s, e) => _safe_execute_on_window(s, action, action_name); diff --git a/src/client/spark/view/Shell/FloatingWindow.xaml.cs b/src/client/spark/view/Shell/FloatingWindow.xaml.cs index 6c695b537..a549c97c9 100644 --- a/src/client/spark/view/Shell/FloatingWindow.xaml.cs +++ b/src/client/spark/view/Shell/FloatingWindow.xaml.cs @@ -109,6 +109,7 @@ namespace mbts.ui.shell this.Items.Remove(document); document.ViewModel.Closing -= TabClosing; document.ViewModel.host_id = string.Empty; + document.ViewModel.owner = null; if (IsEmpty && !IsClosed) { this.Close(); } @@ -117,6 +118,7 @@ namespace mbts.ui.shell // if is_loaded is true, will NOT set width/height public async Task add_document(TabDocument document, int index, bool is_loaded=false) { Log.assert(document.Parent == null); + document.ViewModel.owner = this; // Manually copy over the initial size if this is a new Window if (_tabs.Items.Count == 0 && !is_loaded) await _size_to_content(document); diff --git a/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs b/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs index 094e933ff..08e8f460f 100644 --- a/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs +++ b/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs @@ -11,9 +11,11 @@ namespace mbts.ui.shell { public class ShellWindowVM : ViewModelBase, IDisposable { IShellWindow _model; UIElement _ui; + ShellWindowUI document; IWindowManager _wm; Dispatcher _dispatcher; HwndContentHost _hwnd_host; + FloatingWindow _owner; public ShellWindowVM(IShellWindow model, IWindowManager wm, IShellThreadFactory threads, string host = "") { Log.assert(model != null); @@ -50,6 +52,15 @@ namespace mbts.ui.shell { public string host_id { get; set; } public Dispatcher Dispatcher { get { return _dispatcher; } } + public FloatingWindow owner { + get => _owner; + set { + if (_owner != value) { + _owner = value; + _try_set_wpf_bus(); + } + } + } public string Title { get { return HasCustomTitle? TitleOverride : _model.Title; } } @@ -125,7 +136,6 @@ namespace mbts.ui.shell { } private UIElement create_contents() { - ShellWindowUI document = null; string error_text = null; _dispatcher.Invoke(() => { @@ -166,6 +176,7 @@ namespace mbts.ui.shell { _dispatcher.Invoke(() => { var interop_window = new WPFPanel(document.Content); retval = new System.Runtime.InteropServices.HandleRef(interop_window, interop_window.Handle); + //WindowStyle.SetWPFBus(document.Content); }, DispatcherPriority.Send); _hwnd_host = new HwndContentHost(retval); InitialHeight = _hwnd_host.InitialHeight; @@ -206,8 +217,6 @@ namespace mbts.ui.shell { public bool IsShared { get { return _model is ISharedWindow; } } public bool HasTabAffinity { get { return _model is ITabAffineWindow; } } - - public void Dispose() { _model.TitleChanged -= title_changed; _model.IconChanged -= icon_changed; @@ -228,5 +237,11 @@ namespace mbts.ui.shell { Disposable.dispose(ref _hwnd_host); } + void _try_set_wpf_bus() { + if (_owner != null && document?.Content != null) { + document.Content.Dispatcher.InvokeAsync(() => + WindowStyle.SetUnsafeWindow(document.Content, _owner)); + } + } } } \ No newline at end of file