Skip to content

Commit 83919eb

Browse files
authored
Merge pull request #1061 from Microsoft/dev
1.4.0
2 parents 47c395b + fcd3474 commit 83919eb

File tree

368 files changed

+8498
-1129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

368 files changed

+8498
-1129
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// ******************************************************************
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
5+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
7+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
8+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
9+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
10+
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
11+
// ******************************************************************
12+
13+
using Windows.UI.Xaml;
14+
using Windows.UI.Xaml.Controls;
15+
using Windows.UI.Xaml.Media;
16+
using Windows.UI.Xaml.Shapes;
17+
18+
namespace Microsoft.Toolkit.Uwp.DeveloperTools
19+
{
20+
/// <summary>
21+
/// AlignmentGrid is used to display a grid to help aligning controls
22+
/// </summary>
23+
public class AlignmentGrid: ContentControl
24+
{
25+
/// <summary>
26+
/// Identifies the <see cref="LineBrush"/> dependency property.
27+
/// </summary>
28+
public static readonly DependencyProperty LineBrushProperty = DependencyProperty.Register(nameof(LineBrush), typeof(Brush), typeof(AlignmentGrid), new PropertyMetadata(null, OnPropertyChanged));
29+
30+
/// <summary>
31+
/// Identifies the <see cref="HorizontalStep"/> dependency property.
32+
/// </summary>
33+
public static readonly DependencyProperty HorizontalStepProperty = DependencyProperty.Register(nameof(HorizontalStep), typeof(double), typeof(AlignmentGrid), new PropertyMetadata(20.0, OnPropertyChanged));
34+
35+
/// <summary>
36+
/// Identifies the <see cref="VerticalStep"/> dependency property.
37+
/// </summary>
38+
public static readonly DependencyProperty VerticalStepProperty = DependencyProperty.Register(nameof(VerticalStep), typeof(double), typeof(AlignmentGrid), new PropertyMetadata(20.0, OnPropertyChanged));
39+
40+
private static void OnPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
41+
{
42+
var alignmentGrid = dependencyObject as AlignmentGrid;
43+
44+
alignmentGrid?.Rebuild();
45+
}
46+
47+
private readonly Canvas containerCanvas = new Canvas();
48+
49+
/// <summary>
50+
/// Gets or sets the step to use horizontally.
51+
/// </summary>
52+
public Brush LineBrush
53+
{
54+
get { return (Brush)GetValue(LineBrushProperty); }
55+
set { SetValue(LineBrushProperty, value); }
56+
}
57+
58+
/// <summary>
59+
/// Gets or sets the step to use horizontally.
60+
/// </summary>
61+
public double HorizontalStep
62+
{
63+
get { return (double)GetValue(HorizontalStepProperty); }
64+
set { SetValue(HorizontalStepProperty, value); }
65+
}
66+
67+
/// <summary>
68+
/// Gets or sets the step to use horizontally.
69+
/// </summary>
70+
public double VerticalStep
71+
{
72+
get { return (double)GetValue(VerticalStepProperty); }
73+
set { SetValue(VerticalStepProperty, value); }
74+
}
75+
76+
77+
/// <summary>
78+
/// Initializes a new instance of the <see cref="AlignmentGrid"/> class.
79+
/// </summary>
80+
public AlignmentGrid()
81+
{
82+
SizeChanged += AlignmentGrid_SizeChanged;
83+
84+
IsHitTestVisible = false;
85+
86+
Opacity = 0.5;
87+
88+
HorizontalContentAlignment = HorizontalAlignment.Stretch;
89+
VerticalContentAlignment = VerticalAlignment.Stretch;
90+
Content = containerCanvas;
91+
}
92+
93+
private void Rebuild()
94+
{
95+
containerCanvas.Children.Clear();
96+
var horizontalStep = HorizontalStep;
97+
var verticalStep = VerticalStep;
98+
var brush = LineBrush ?? (Brush)Application.Current.Resources["ApplicationForegroundThemeBrush"];
99+
100+
for (double x = 0; x < ActualWidth; x += horizontalStep)
101+
{
102+
var line = new Rectangle
103+
{
104+
Width = 1,
105+
Height = ActualHeight,
106+
Fill = brush
107+
};
108+
Canvas.SetLeft(line, x);
109+
110+
containerCanvas.Children.Add(line);
111+
}
112+
113+
for (double y = 0; y < ActualHeight; y += verticalStep)
114+
{
115+
var line = new Rectangle
116+
{
117+
Width = ActualWidth,
118+
Height = 1,
119+
Fill = brush
120+
};
121+
Canvas.SetTop(line, y);
122+
123+
containerCanvas.Children.Add(line);
124+
}
125+
}
126+
127+
private void AlignmentGrid_SizeChanged(object sender, SizeChangedEventArgs e)
128+
{
129+
Rebuild();
130+
}
131+
}
132+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// ******************************************************************
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
5+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
7+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
8+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
9+
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
10+
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
11+
// ******************************************************************
12+
13+
using System;
14+
using System.Collections.Generic;
15+
using System.Linq;
16+
using System.Text;
17+
using System.Threading.Tasks;
18+
using Windows.UI.Xaml;
19+
using Windows.UI.Xaml.Automation;
20+
using Windows.UI.Xaml.Controls;
21+
using Windows.UI.Xaml.Controls.Primitives;
22+
using Windows.UI.Xaml.Input;
23+
using Windows.UI.Xaml.Media;
24+
using Windows.UI.Xaml.Shapes;
25+
26+
namespace Microsoft.Toolkit.Uwp.DeveloperTools
27+
{
28+
/// <summary>
29+
/// FocusTracker can be used to display information about the current focused XAML element.
30+
/// </summary>
31+
[TemplatePart(Name = "ControlName", Type = typeof(TextBlock))]
32+
[TemplatePart(Name = "ControlType", Type = typeof(TextBlock))]
33+
[TemplatePart(Name = "ControlAutomationName", Type = typeof(TextBlock))]
34+
[TemplatePart(Name = "ControlFirstParentWithName", Type = typeof(TextBlock))]
35+
public class FocusTracker: Control
36+
{
37+
/// <summary>
38+
/// Defines the <see cref="IsActive"/> dependency property.
39+
/// </summary>
40+
public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(nameof(IsActive), typeof(bool), typeof(FocusTracker), new PropertyMetadata(false, OnIsActiveChanged));
41+
42+
private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
43+
{
44+
var focusTracker = d as FocusTracker;
45+
46+
if (e.NewValue != null && (bool) e.NewValue)
47+
{
48+
focusTracker?.Start();
49+
}
50+
else
51+
{
52+
focusTracker?.Stop();
53+
}
54+
}
55+
56+
private DispatcherTimer updateTimer;
57+
private TextBlock controlName;
58+
private TextBlock controlType;
59+
private TextBlock controlAutomationName;
60+
private TextBlock controlFirstParentWithName;
61+
62+
/// <summary>
63+
/// Gets or sets a boolean indicating whether the tracker is running or not.
64+
/// </summary>
65+
public bool IsActive
66+
{
67+
get { return (bool) GetValue(IsActiveProperty); }
68+
set
69+
{
70+
SetValue(IsActiveProperty, value);
71+
}
72+
}
73+
74+
/// <summary>
75+
/// Initializes a new instance of the <see cref="FocusTracker"/> class.
76+
/// </summary>
77+
public FocusTracker()
78+
{
79+
DefaultStyleKey = typeof(FocusTracker);
80+
}
81+
82+
private void Start()
83+
{
84+
if (updateTimer == null)
85+
{
86+
updateTimer = new DispatcherTimer();
87+
updateTimer.Tick += UpdateTimer_Tick;
88+
}
89+
updateTimer.Start();
90+
}
91+
92+
private void Stop()
93+
{
94+
updateTimer?.Stop();
95+
ClearContent();
96+
}
97+
98+
/// <summary>
99+
/// Update the visual state of the control when its template is changed.
100+
/// </summary>
101+
protected override void OnApplyTemplate()
102+
{
103+
controlName = GetTemplateChild("ControlName") as TextBlock;
104+
controlType = GetTemplateChild("ControlType") as TextBlock;
105+
controlAutomationName = GetTemplateChild("ControlAutomationName") as TextBlock;
106+
controlFirstParentWithName = GetTemplateChild("ControlFirstParentWithName") as TextBlock;
107+
}
108+
109+
private void ClearContent()
110+
{
111+
controlName.Text = string.Empty;
112+
controlType.Text = string.Empty;
113+
controlAutomationName.Text = string.Empty;
114+
controlFirstParentWithName.Text = string.Empty;
115+
}
116+
117+
private void UpdateTimer_Tick(object sender, object e)
118+
{
119+
var focusedControl = FocusManager.GetFocusedElement() as FrameworkElement;
120+
121+
if (focusedControl == null)
122+
{
123+
ClearContent();
124+
return;
125+
}
126+
127+
controlName.Text = focusedControl.Name;
128+
controlType.Text = focusedControl.GetType().Name;
129+
controlAutomationName.Text = AutomationProperties.GetName(focusedControl);
130+
131+
var parentWithName = FindVisualAscendantWithName(focusedControl);
132+
133+
controlFirstParentWithName.Text = parentWithName?.Name ?? string.Empty;
134+
}
135+
136+
private FrameworkElement FindVisualAscendantWithName(FrameworkElement element)
137+
{
138+
var parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
139+
140+
if (parent == null)
141+
{
142+
return null;
143+
}
144+
145+
if (!string.IsNullOrEmpty(parent.Name))
146+
{
147+
return parent;
148+
}
149+
150+
return FindVisualAscendantWithName(parent);
151+
}
152+
}
153+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:developerTools="using:Microsoft.Toolkit.Uwp.DeveloperTools">
4+
5+
<Style TargetType="developerTools:FocusTracker">
6+
<Setter Property="IsTabStop" Value="False"></Setter>
7+
<Setter Property="MinWidth" Value="300"></Setter>
8+
<Setter Property="Foreground" Value="{ThemeResource ApplicationForegroundThemeBrush}"></Setter>
9+
<Setter Property="Background" Value="{ThemeResource ApplicationPageBackgroundThemeBrush}"></Setter>
10+
<Setter Property="Template">
11+
<Setter.Value>
12+
<ControlTemplate>
13+
<Grid>
14+
<Border Background="{TemplateBinding Background}"
15+
BorderBrush="{TemplateBinding Foreground}"
16+
BorderThickness="1"
17+
/>
18+
<Grid Padding="10,10,10,5">
19+
<Grid.RowDefinitions>
20+
<RowDefinition Height="30"/>
21+
<RowDefinition Height="30"/>
22+
<RowDefinition Height="30"/>
23+
<RowDefinition Height="30"/>
24+
<RowDefinition Height="20"/>
25+
</Grid.RowDefinitions>
26+
<Grid.ColumnDefinitions>
27+
<ColumnDefinition Width="180"/>
28+
<ColumnDefinition/>
29+
</Grid.ColumnDefinitions>
30+
<TextBlock Text="Name:"
31+
FontSize="14"
32+
Grid.Row="0"
33+
Grid.Column="0"/>
34+
<TextBlock x:Name="ControlName"
35+
Grid.Row="0"
36+
Grid.Column="1"/>
37+
38+
<TextBlock Text="Type:"
39+
FontSize="14"
40+
Grid.Row="1"
41+
Grid.Column="0"/>
42+
<TextBlock x:Name="ControlType"
43+
Grid.Row="1"
44+
Grid.Column="1"/>
45+
46+
<TextBlock Text="Automation.Name:"
47+
FontSize="14"
48+
Grid.Row="2"
49+
Grid.Column="0"/>
50+
<TextBlock x:Name="ControlAutomationName"
51+
Grid.Row="2"
52+
Grid.Column="1"/>
53+
54+
<TextBlock Text="Parent with Name:"
55+
FontSize="14"
56+
Grid.Row="3"
57+
Grid.Column="0"/>
58+
<TextBlock x:Name="ControlFirstParentWithName"
59+
Grid.Row="3"
60+
Grid.Column="1"/>
61+
62+
<TextBlock Text="FocusTracker"
63+
Opacity="0.5"
64+
FontSize="14"
65+
HorizontalAlignment="Center"
66+
Grid.Row="4"
67+
Grid.ColumnSpan="2"/>
68+
</Grid>
69+
</Grid>
70+
</ControlTemplate>
71+
</Setter.Value>
72+
</Setter>
73+
</Style>
74+
</ResourceDictionary>

0 commit comments

Comments
 (0)