Skip to content

Commit

Permalink
Listen for raw key events in DevTools.
Browse files Browse the repository at this point in the history
That way we can detect the Ctrl+Shift key combination anywhere.
  • Loading branch information
grokys committed Feb 3, 2016
1 parent ae747c6 commit 96235fb
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/Perspex.Diagnostics/DevTools.paml.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
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
{
public class DevTools : UserControl
{
private static Dictionary<Window, Window> s_open = new Dictionary<Window, Window>();
private IDisposable _keySubscription;

public DevTools(IControl root)
{
InitializeComponent();
Root = root;
DataContext = new DevToolsViewModel(root);
Root.PointerMoved += RootPointerMoved;

_keySubscription = InputManager.Instance.RawEventReceived
.OfType<RawKeyEventArgs>()
.Subscribe(RawKeyDown);
}

public IControl Root { get; }
Expand All @@ -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<ReactiveObject>(),
}
};

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;
}

Expand All @@ -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);
}
}
}
}
Expand Down

0 comments on commit 96235fb

Please sign in to comment.