From 96235fba17b7cc0112ee167eca9980c4f17bfcee Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 3 Feb 2016 11:28:43 +0100 Subject: [PATCH] Listen for raw key events in DevTools. That way we can detect the Ctrl+Shift key combination anywhere. --- src/Perspex.Diagnostics/DevTools.paml.cs | 36 ++++++++++++++++++------ 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Perspex.Diagnostics/DevTools.paml.cs b/src/Perspex.Diagnostics/DevTools.paml.cs index 3b96ebcf5ad..3b5b6ada444 100644 --- a/src/Perspex.Diagnostics/DevTools.paml.cs +++ b/src/Perspex.Diagnostics/DevTools.paml.cs @@ -1,11 +1,16 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; using Perspex.Controls; +using Perspex.Controls.Primitives; using Perspex.Controls.Templates; using Perspex.Diagnostics.ViewModels; using Perspex.Input; +using Perspex.Input.Raw; using Perspex.Interactivity; using Perspex.Markup.Xaml; +using Perspex.VisualTree; using ReactiveUI; namespace Perspex.Diagnostics @@ -13,13 +18,17 @@ namespace Perspex.Diagnostics public class DevTools : UserControl { private static Dictionary s_open = new Dictionary(); + private IDisposable _keySubscription; public DevTools(IControl root) { InitializeComponent(); Root = root; DataContext = new DevToolsViewModel(root); - Root.PointerMoved += RootPointerMoved; + + _keySubscription = InputManager.Instance.RawEventReceived + .OfType() + .Subscribe(RawKeyDown); } public IControl Root { get; } @@ -45,31 +54,34 @@ private static void WindowPreviewKeyDown(object sender, KeyEventArgs e) } else { + var devTools = new DevTools(window); + devToolsWindow = new Window { Width = 1024, Height = 512, - Content = new DevTools(window), + Content = devTools, DataTemplates = new DataTemplates { new ViewLocator(), } }; - devToolsWindow.Closed += DevToolsClosed; + devToolsWindow.Closed += devTools.DevToolsClosed; s_open.Add((Window)sender, devToolsWindow); devToolsWindow.Show(); } } } - private static void DevToolsClosed(object sender, EventArgs e) + private void DevToolsClosed(object sender, EventArgs e) { var devToolsWindow = (Window)sender; var devTools = (DevTools)devToolsWindow.Content; var window = (Window)devTools.Root; s_open.Remove(window); + _keySubscription.Dispose(); devToolsWindow.Closed -= DevToolsClosed; } @@ -78,14 +90,20 @@ private void InitializeComponent() PerspexXamlLoader.Load(this); } - private void RootPointerMoved(object sender, PointerEventArgs e) + private void RawKeyDown(RawKeyEventArgs e) { - var modifiers = InputModifiers.Control | InputModifiers.Shift; + const InputModifiers modifiers = InputModifiers.Control | InputModifiers.Shift; - if ((e.InputModifiers & modifiers) == modifiers) + if ((e.Modifiers) == modifiers) { - var vm = (DevToolsViewModel)DataContext; - vm.SelectControl((IControl)e.Source); + var point = MouseDevice.Instance.GetPosition(Root); + var control = Root.GetVisualsAt(point).FirstOrDefault(x => !(x is AdornerLayer)); + + if (control != null) + { + var vm = (DevToolsViewModel)DataContext; + vm.SelectControl((IControl)control); + } } } }