Skip to content

Commit

Permalink
Auto focus on page load (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelBawa-msft committed Aug 7, 2024
1 parent d044e8b commit c126ed2
Show file tree
Hide file tree
Showing 31 changed files with 82 additions and 91 deletions.
21 changes: 21 additions & 0 deletions common/Behaviors/AutoFocusBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using CommunityToolkit.WinUI.Behaviors;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace DevHome.Common.Behaviors;

/// <summary>
/// This behavior automatically sets the focus on the associated <see cref="Control"/> when it is loaded.
/// </summary>
/// <remarks>
/// This implementation is based on the code from the Windows Community Toolkit.
/// Reference: https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/winui/CommunityToolkit.WinUI.UI.Behaviors/Focus/AutoFocusBehavior.cs
/// Issue: https://github.com/CommunityToolkit/Windows/issues/443
/// </remarks>
public sealed class AutoFocusBehavior : BehaviorBase<Control>
{
protected override void OnAssociatedObjectLoaded() => AssociatedObject.Focus(FocusState.Programmatic);
}
25 changes: 0 additions & 25 deletions common/Views/DevHomePage.cs

This file was deleted.

25 changes: 0 additions & 25 deletions common/Views/DevHomeUserControl.cs

This file was deleted.

14 changes: 13 additions & 1 deletion common/Views/ToolPage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace DevHome.Common.Views;

public abstract class ToolPage : DevHomePage
public abstract class ToolPage : Page
{
public ToolPage()
{
Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
Focus(FocusState.Programmatic);
}
}
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/AboutPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.AboutPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -44,4 +44,4 @@
</StackPanel>
</Grid>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
2 changes: 1 addition & 1 deletion settings/DevHome.Settings/Views/AboutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace DevHome.Settings.Views;

public sealed partial class AboutPage : DevHomePage
public sealed partial class AboutPage : ToolPage
{
public AboutViewModel ViewModel { get; }

Expand Down
5 changes: 2 additions & 3 deletions settings/DevHome.Settings/Views/AccountsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.AccountsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -10,7 +10,6 @@
xmlns:views="using:DevHome.Common.Views"
behaviors:NavigationViewHeaderBehavior.HeaderTemplate="{StaticResource BreadcrumbBarDataTemplate}"
behaviors:NavigationViewHeaderBehavior.HeaderContext="{x:Bind ViewModel}">

<Page.Resources>
<converters:DoubleToVisibilityConverter x:Key="CountToVisibilityConverter" GreaterThan="0" FalseValue="Collapsed" TrueValue="Visible"/>

Expand Down Expand Up @@ -73,4 +72,4 @@
</StackPanel>
</Grid>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
2 changes: 1 addition & 1 deletion settings/DevHome.Settings/Views/AccountsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace DevHome.Settings.Views;

public sealed partial class AccountsPage : DevHomePage
public sealed partial class AccountsPage : ToolPage
{
private readonly ILogger _log = Log.ForContext("SourceContext", nameof(AccountsPage));

Expand Down
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/ExperimentalFeaturesPage.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.ExperimentalFeaturesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -45,4 +45,4 @@
</StackPanel>
</Grid>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DevHome.Settings.Views;

public sealed partial class ExperimentalFeaturesPage : DevHomePage
public sealed partial class ExperimentalFeaturesPage : ToolPage
{
public ExperimentalFeaturesViewModel ViewModel { get; }

Expand Down
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/FeedbackPage.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Copyright (c) Microsoft Corporation.. -->
<!-- Licensed under the MIT License. -->

<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.FeedbackPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -150,4 +150,4 @@
</StackPanel>
</Grid>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
2 changes: 1 addition & 1 deletion settings/DevHome.Settings/Views/FeedbackPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace DevHome.Settings.Views;
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class FeedbackPage : DevHomePage
public sealed partial class FeedbackPage : ToolPage
{
private static readonly double ByteSizeGB = 1024 * 1024 * 1024;
private static string wmiCPUInfo = string.Empty;
Expand Down
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/PreferencesPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.PreferencesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -33,4 +33,4 @@
</ctControls:SettingsCard>
</StackPanel>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
2 changes: 1 addition & 1 deletion settings/DevHome.Settings/Views/PreferencesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace DevHome.Settings.Views;

public sealed partial class PreferencesPage : DevHomePage
public sealed partial class PreferencesPage : ToolPage
{
public PreferencesViewModel ViewModel { get; }

Expand Down
4 changes: 2 additions & 2 deletions settings/DevHome.Settings/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Copyright (c) Microsoft Corporation.. -->
<!-- Licensed under the MIT License. -->

<views:DevHomePage
<views:ToolPage
x:Class="DevHome.Settings.Views.SettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -28,4 +28,4 @@
</ItemsRepeater>
</Grid>
</ScrollViewer>
</views:DevHomePage>
</views:ToolPage>
2 changes: 1 addition & 1 deletion settings/DevHome.Settings/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DevHome.Settings.Views;

public sealed partial class SettingsPage : DevHomePage
public sealed partial class SettingsPage : ToolPage
{
public SettingsViewModel ViewModel { get; }

Expand Down
6 changes: 3 additions & 3 deletions src/Views/WhatsNewPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<commonviews:DevHomePage
<commonviews:ToolPage
x:Class="DevHome.Views.WhatsNewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -7,7 +7,7 @@
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
xmlns:models="using:DevHome.Models"
xmlns:commonviews="using:DevHome.Common.Views"
xmlns:behaviors="using:DevHome.Common.Behaviors"
xmlns:behaviors="using:DevHome.Common.Behaviors"
behaviors:NavigationViewHeaderBehavior.HeaderMode="Never"
Loaded="OnLoaded"
SizeChanged="OnSizeChanged"
Expand Down Expand Up @@ -318,4 +318,4 @@
</Grid>
</RelativePanel>
</ScrollViewer>
</commonviews:DevHomePage>
</commonviews:ToolPage>
2 changes: 1 addition & 1 deletion src/Views/WhatsNewPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace DevHome.Views;

public sealed partial class WhatsNewPage : DevHomePage
public sealed partial class WhatsNewPage : ToolPage
{
private readonly Uri _devDrivePageKeyUri = new("ms-settings:disksandvolumes");
private readonly Uri _devDriveLearnMoreLinkUri = new("https://go.microsoft.com/fwlink/?linkid=2236041");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<commonViews:DevHomePage
<commonViews:ToolPage
x:Class="DevHome.Customization.Views.DevDriveInsightsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -13,4 +13,4 @@
<views:DevDriveInsightsView />
</Grid>
</ScrollView>
</commonViews:DevHomePage>
</commonViews:ToolPage>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace DevHome.Customization.Views;

public sealed partial class DevDriveInsightsPage : DevHomePage
public sealed partial class DevDriveInsightsPage : ToolPage
{
public DevDriveInsightsViewModel ViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<views:DevHomeUserControl
<UserControl
x:Class="DevHome.Customization.Views.DevDriveInsightsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -181,4 +181,4 @@
</ListView>
</StackPanel>
</ScrollViewer>
</views:DevHomeUserControl>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the MIT License.

using DevHome.Common.Extensions;
using DevHome.Common.Views;
using DevHome.Customization.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace DevHome.Customization.Views;

public sealed partial class DevDriveInsightsView : DevHomeUserControl
public sealed partial class DevDriveInsightsView : UserControl
{
public DevDriveInsightsViewModel ViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<commonViews:DevHomePage
<commonViews:ToolPage
x:Class="DevHome.Customization.Views.FileExplorerPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -16,4 +16,4 @@
</StackPanel>
</Grid>
</ScrollViewer>
</commonViews:DevHomePage>
</commonViews:ToolPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DevHome.Customization.Views;

public sealed partial class FileExplorerPage : DevHomePage
public sealed partial class FileExplorerPage : ToolPage
{
public FileExplorerViewModel ViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<commonViews:DevHomePage
<commonViews:ToolPage
x:Class="DevHome.Customization.Views.GeneralSystemPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -13,4 +13,4 @@
<views:GeneralSystemView />
</ScrollView>
</Grid>
</commonViews:DevHomePage>
</commonViews:ToolPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DevHome.Customization.Views;

public sealed partial class GeneralSystemPage : DevHomePage
public sealed partial class GeneralSystemPage : ToolPage
{
public GeneralSystemViewModel ViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Copyright (c) Microsoft Corporation.. -->
<!-- Licensed under the MIT License. -->

<commonviews:DevHomePage
<commonviews:ToolPage
x:Class="DevHome.ExtensionLibrary.Views.ExtensionSettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand All @@ -25,4 +25,4 @@
</StackPanel>
</ScrollViewer>
</Grid>
</commonviews:DevHomePage>
</commonviews:ToolPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DevHome.ExtensionLibrary.Views;

public sealed partial class ExtensionSettingsPage : DevHomePage
public sealed partial class ExtensionSettingsPage : ToolPage
{
public ExtensionSettingsViewModel ViewModel { get; }

Expand Down
3 changes: 3 additions & 0 deletions tools/PI/DevHome.PI/Pages/PreferencesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
xmlns:xaml="using:Microsoft.UI.Xaml"
xmlns:behaviors="using:DevHome.Common.Behaviors"
Loaded="Page_Loaded">
<i:Interaction.Behaviors >
<behaviors:AutoFocusBehavior />
</i:Interaction.Behaviors>

<ScrollViewer VerticalAlignment="Top" VerticalScrollBarVisibility="Auto">
<StackPanel>
Expand Down
Loading

0 comments on commit c126ed2

Please sign in to comment.