diff --git a/Perspex.Diagnostics/DevTools.cs b/Perspex.Diagnostics/DevTools.cs index 5cbe24dc6f..a6c347c2d3 100644 --- a/Perspex.Diagnostics/DevTools.cs +++ b/Perspex.Diagnostics/DevTools.cs @@ -64,48 +64,50 @@ namespace Perspex.Diagnostics { this.DataTemplates.Add(new ViewLocator()); - this.Content = new TabControl + this.Content = new Grid { - Items = new[] + RowDefinitions = new RowDefinitions { - new TabItem + new RowDefinition(new GridLength(1, GridUnitType.Star)), + new RowDefinition(GridLength.Auto), + }, + Children = new Controls + { + new TabControl { - Header = "Logical Tree", - [!TabItem.ContentProperty] = this.viewModel.WhenAnyValue(x => x.LogicalTree), + Items = new[] + { + new TabItem + { + Header = "Logical Tree", + [!TabItem.ContentProperty] = this.viewModel.WhenAnyValue(x => x.LogicalTree), + }, + new TabItem + { + Header = "Visual Tree", + [!TabItem.ContentProperty] = this.viewModel.WhenAnyValue(x => x.VisualTree), + } + }, }, - new TabItem + new StackPanel { - Header = "Visual Tree", - [!TabItem.ContentProperty] = this.viewModel.WhenAnyValue(x => x.VisualTree), + Orientation = Orientation.Horizontal, + Gap = 4, + [Grid.RowProperty] = 1, + Children = new Controls + { + new TextBlock + { + Text = "Focused: " + }, + new TextBlock + { + [!TextBlock.TextProperty] = this.viewModel.WhenAnyValue(x => x.FocusedControl).Select(x => x?.GetType().Name) + } + } } - }, + } }; } - - //private Control GetHeader(VisualTreeNode node) - //{ - // var result = new StackPanel - // { - // Orientation = Orientation.Horizontal, - // Gap = 8, - // Children = new Controls - // { - // new TextBlock - // { - // Text = node.Type, - // FontStyle = node.IsInTemplate ? Media.FontStyle.Italic : Media.FontStyle.Normal, - // }, - // new TextBlock - // { - // [!TextBlock.TextProperty] = node.WhenAnyValue(x => x.Classes), - // } - // } - // }; - - // result.PointerEnter += this.AddAdorner; - // result.PointerLeave += this.RemoveAdorner; - - // return result; - //} } } diff --git a/Perspex.Diagnostics/ViewModels/DevToolsViewModel.cs b/Perspex.Diagnostics/ViewModels/DevToolsViewModel.cs index e1342551fd..91ae0dc25e 100644 --- a/Perspex.Diagnostics/ViewModels/DevToolsViewModel.cs +++ b/Perspex.Diagnostics/ViewModels/DevToolsViewModel.cs @@ -9,6 +9,7 @@ namespace Perspex.Diagnostics.ViewModels using System; using Perspex.Controls; using ReactiveUI; + using Perspex.Input; internal class DevToolsViewModel : ReactiveObject { @@ -18,6 +19,8 @@ namespace Perspex.Diagnostics.ViewModels private VisualTreeViewModel visualTree; + private ObservableAsPropertyHelper focusedControl; + public DevToolsViewModel() { this.WhenAnyValue(x => x.Root).Subscribe(x => @@ -25,6 +28,9 @@ namespace Perspex.Diagnostics.ViewModels this.LogicalTree = new LogicalTreeViewModel(root); this.VisualTree = new VisualTreeViewModel(root); }); + + this.focusedControl = KeyboardDevice.Instance.WhenAnyValue(x => x.FocusedElement) + .ToProperty(this, x => x.FocusedControl); } public Control Root @@ -44,5 +50,10 @@ namespace Perspex.Diagnostics.ViewModels get { return this.visualTree; } private set { this.RaiseAndSetIfChanged(ref this.visualTree, value); } } + + public IInputElement FocusedControl + { + get { return this.focusedControl.Value; } + } } } diff --git a/Perspex.Input/KeyboardDevice.cs b/Perspex.Input/KeyboardDevice.cs index 743d26ea34..6758cbbd0b 100644 --- a/Perspex.Input/KeyboardDevice.cs +++ b/Perspex.Input/KeyboardDevice.cs @@ -7,14 +7,18 @@ namespace Perspex.Input { using System; + using System.ComponentModel; using System.Linq; using System.Reactive.Linq; + using System.Runtime.CompilerServices; using Perspex.Input.Raw; using Perspex.Interactivity; using Splat; - public abstract class KeyboardDevice : IKeyboardDevice + public abstract class KeyboardDevice : IKeyboardDevice, INotifyPropertyChanged { + private IInputElement focusedElement; + public KeyboardDevice() { this.InputManager.RawEventReceived @@ -23,6 +27,8 @@ namespace Perspex.Input .Subscribe(this.ProcessRawEvent); } + public event PropertyChangedEventHandler PropertyChanged; + public static IKeyboardDevice Instance { get { return Locator.Current.GetService(); } @@ -40,8 +46,16 @@ namespace Perspex.Input public IInputElement FocusedElement { - get; - private set; + get + { + return this.focusedElement; + } + + private set + { + this.focusedElement = value; + this.RaisePropertyChanged(); + } } public abstract ModifierKeys Modifiers { get; } @@ -74,6 +88,11 @@ namespace Perspex.Input } } + protected void RaisePropertyChanged([CallerMemberName] string propertyName = "") + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + private void ProcessRawEvent(RawKeyEventArgs e) { IInputElement element = this.FocusedElement;