Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThemeListener Refactor & Disposal #4361

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Controls/CodeRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void Current_ThemeChanged(object sender, Models.ThemeChangedArgs e)
/// <returns>Element Theme</returns>
private ElementTheme SystemTheme()
{
return SampleController.Current.SystemTheme() == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;
return SampleController.Current.GetSystemTheme() == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public sealed partial class SampleController : Page, INotifyPropertyChanged
{
public static SampleController Current { get; private set; }

public ThemeListener ThemeListener => _themeListener;

public SampleController()
{
_themeListener = new ThemeListener();
Expand Down Expand Up @@ -71,7 +73,7 @@ public ElementTheme GetCurrentTheme()
/// Gets the current System Theme.
/// </summary>
/// <returns>System Theme</returns>
public ApplicationTheme SystemTheme()
public ApplicationTheme GetSystemTheme()
{
return _themeListener.CurrentTheme;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
Expand All @@ -16,9 +17,7 @@ public sealed partial class ThemeListenerPage : Page
public ThemeListenerPage()
{
this.InitializeComponent();
Listener = new ThemeListener();
this.Loaded += ThemeListenerPage_Loaded;
Listener.ThemeChanged += Listener_ThemeChanged;
SampleController.Current.ThemeChanged += Current_ThemeChanged;
}

Expand All @@ -32,17 +31,16 @@ private void ThemeListenerPage_Loaded(object sender, RoutedEventArgs e)
UpdateThemeState();
}

private void Listener_ThemeChanged(ThemeListener sender)
{
UpdateThemeState();
}

private void UpdateThemeState()
{
SystemTheme.Text = Listener.CurrentThemeName;
SystemTheme.Text = SampleController.Current.ThemeListener.CurrentThemeName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a SystemTheme method as well:

(Not sure why this isn't prefixed with 'Get' like CurrentTheme is, probably something else we should clean-up.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

CurrentTheme.Text = SampleController.Current.GetCurrentTheme().ToString();
}

public ThemeListener Listener { get; }
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
Loaded -= ThemeListenerPage_Loaded;
SampleController.Current.ThemeChanged -= Current_ThemeChanged;
}
}
}