-Made Theme Events a Shell Event, that way Theming behaviour can be overidden.

-Added a Theme Picker to the SampleController, with choices of System, Light or Dark for Samples.

-Disabled Background Image Switch from Sample Controller, as Background were too distracting and broke many sample Pages. It could possible be brought back in  a future PR.
This commit is contained in:
William Bradley 2018-04-08 20:05:44 +12:00
Родитель fe570aa485
Коммит dd6ac39711
8 изменённых файлов: 109 добавлений и 19 удалений

Просмотреть файл

@ -13,7 +13,6 @@
using System; using System;
using ColorCode; using ColorCode;
using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Popups; using Windows.UI.Popups;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@ -23,7 +22,6 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.Controls
{ {
public class CodeRenderer : Control public class CodeRenderer : Control
{ {
private ThemeListener themeListener;
private RichTextBlockFormatter _formatter; private RichTextBlockFormatter _formatter;
private RichTextBlock _codeView; private RichTextBlock _codeView;
private Button _printButton; private Button _printButton;
@ -34,13 +32,13 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.Controls
private string _displayedText; private string _displayedText;
private ILanguage _language; private ILanguage _language;
private bool _rendered; private bool _rendered;
private ElementTheme _theme;
public CodeRenderer() public CodeRenderer()
{ {
DefaultStyleKey = typeof(CodeRenderer); DefaultStyleKey = typeof(CodeRenderer);
_theme = Shell.Current.GetCurrentTheme();
themeListener = new ThemeListener(); Shell.Current.ThemeChanged += Current_ThemeChanged;
themeListener.ThemeChanged += ThemeListener_ThemeChanged;
} }
/// <summary> /// <summary>
@ -98,13 +96,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.Controls
private void RenderDocument() private void RenderDocument()
{ {
_codeView?.Blocks?.Clear(); _codeView?.Blocks?.Clear();
var theme = themeListener.CurrentTheme == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light; _formatter = new RichTextBlockFormatter(_theme);
if (RequestedTheme != ElementTheme.Default)
{
theme = RequestedTheme;
}
_formatter = new RichTextBlockFormatter(theme);
_formatter.FormatRichTextBlock(_displayedText, _language, _codeView); _formatter.FormatRichTextBlock(_displayedText, _language, _codeView);
_rendered = true; _rendered = true;
} }
@ -170,8 +162,9 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.Controls
ReleasePrintHelper(); ReleasePrintHelper();
} }
private void ThemeListener_ThemeChanged(ThemeListener sender) private void Current_ThemeChanged(object sender, Models.ThemeChangedArgs e)
{ {
_theme = e.Theme;
try try
{ {
_rendered = false; _rendered = false;

Просмотреть файл

@ -16,8 +16,13 @@
<Grid x:Name="Container" <Grid x:Name="Container"
Grid.RowSpan="2" Grid.RowSpan="2"
Opacity="0" /> Opacity="0" />
<ScrollViewer VerticalScrollBarVisibility="Auto"> <ScrollViewer
<RichTextBlock Name="codeView" /> Grid.Row="0"
HorizontalScrollMode="Auto"
HorizontalScrollBarVisibility="Auto">
<RichTextBlock Name="codeView"
FontFamily="Consolas"
Padding="10" />
</ScrollViewer> </ScrollViewer>
<StackPanel Grid.Row="1" <StackPanel Grid.Row="1"
HorizontalAlignment="Center" HorizontalAlignment="Center"

Просмотреть файл

@ -527,6 +527,7 @@
<Compile Include="Models\LandingPageLink.cs" /> <Compile Include="Models\LandingPageLink.cs" />
<Compile Include="Models\PaneState.cs" /> <Compile Include="Models\PaneState.cs" />
<Compile Include="Models\PropertyDescriptor\ThicknessPropertyOptions.cs" /> <Compile Include="Models\PropertyDescriptor\ThicknessPropertyOptions.cs" />
<Compile Include="Models\ThemeChangedArgs.cs" />
<Compile Include="Pages\SampleController.xaml.cs"> <Compile Include="Pages\SampleController.xaml.cs">
<DependentUpon>SampleController.xaml</DependentUpon> <DependentUpon>SampleController.xaml</DependentUpon>
</Compile> </Compile>

Просмотреть файл

@ -0,0 +1,18 @@
using System;
using Windows.UI.Xaml;
namespace Microsoft.Toolkit.Uwp.SampleApp.Models
{
public class ThemeChangedArgs : EventArgs
{
/// <summary>
/// Gets the Current UI Theme
/// </summary>
public ElementTheme Theme { get; internal set; }
/// <summary>
/// Gets a value indicating whether the Theme was set by the User.
/// </summary>
public bool CustomSet { get; internal set; }
}
}

Просмотреть файл

@ -66,11 +66,22 @@
<AppBarSeparator Margin="-10, 3, -10, 0" /> <AppBarSeparator Margin="-10, 3, -10, 0" />
<ToggleSwitch x:Name="BGSwitch" <!-- Sample Display properties -->
Margin="10,0,0,0" <StackPanel Margin="10,0,0,0" Orientation="Horizontal" VerticalAlignment="Center">
<!-- Disabled for Possible Future PR -->
<ToggleSwitch x:Name="BGSwitch"
Visibility="Collapsed"
OffContent="BG Image" OffContent="BG Image"
OnContent="BG Image" OnContent="BG Image"
IsOn="{x:Bind UseBackground, Mode=TwoWay}" /> IsOn="{x:Bind UseBackground, Mode=TwoWay}" />
<TextBlock Text="Theme" VerticalAlignment="Center" Margin="0,0,10,0" />
<ComboBox x:Name="ThemePicker" SelectedIndex="0" SelectionChanged="ThemePicker_SelectionChanged">
<ComboBoxItem Content="System" />
<ComboBoxItem Content="Light" />
<ComboBoxItem Content="Dark" />
</ComboBox>
</StackPanel>
</StackPanel> </StackPanel>
<AppBarButton x:Name="NarrowInfoButton" <AppBarButton x:Name="NarrowInfoButton"

Просмотреть файл

@ -21,7 +21,6 @@ using Microsoft.Toolkit.Uwp.SampleApp.Controls;
using Microsoft.Toolkit.Uwp.SampleApp.Models; using Microsoft.Toolkit.Uwp.SampleApp.Models;
using Microsoft.Toolkit.Uwp.UI.Controls; using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Extensions; using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Windows.System; using Windows.System;
using Windows.System.Profile; using Windows.System.Profile;
using Windows.UI.Core; using Windows.UI.Core;
@ -91,7 +90,9 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
{ {
this.InitializeComponent(); this.InitializeComponent();
Current = this; Current = this;
Shell.Current.ThemeChanged += Current_ThemeChanged;
DocumentationTextblock.RequestedTheme = Shell.Current.GetCurrentTheme();
DocumentationTextblock.SetRenderer<SampleAppMarkdownRenderer>(); DocumentationTextblock.SetRenderer<SampleAppMarkdownRenderer>();
ProcessSampleEditorTime(); ProcessSampleEditorTime();
@ -573,5 +574,18 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
VisualStateManager.GoToState(this, NarrowState.Name, false); VisualStateManager.GoToState(this, NarrowState.Name, false);
} }
} }
private void Current_ThemeChanged(object sender, ThemeChangedArgs e)
{
if (e.CustomSet)
{
DocumentationTextblock.RequestedTheme = e.Theme;
}
}
private void ThemePicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Shell.Current.SetCurrentTheme((ElementTheme)ThemePicker.SelectedIndex);
}
} }
} }

Просмотреть файл

@ -29,20 +29,23 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
public sealed partial class MarkdownTextBlockPage : Page, IXamlRenderListener public sealed partial class MarkdownTextBlockPage : Page, IXamlRenderListener
{ {
private TextBox unformattedText; private TextBox unformattedText;
private MarkdownTextBlock markdownText;
public MarkdownTextBlockPage() public MarkdownTextBlockPage()
{ {
InitializeComponent(); InitializeComponent();
Shell.Current.ThemeChanged += Current_ThemeChanged;
} }
public void OnXamlRendered(FrameworkElement control) public void OnXamlRendered(FrameworkElement control)
{ {
unformattedText = control.FindChildByName("UnformattedText") as TextBox; unformattedText = control.FindChildByName("UnformattedText") as TextBox;
var markdownText = control.FindChildByName("MarkdownText") as MarkdownTextBlock; markdownText = control.FindChildByName("MarkdownText") as MarkdownTextBlock;
if (markdownText != null) if (markdownText != null)
{ {
markdownText.RequestedTheme = Shell.Current.GetCurrentTheme();
markdownText.LinkClicked += MarkdownText_LinkClicked; markdownText.LinkClicked += MarkdownText_LinkClicked;
markdownText.ImageClicked += MarkdownText_ImageClicked; markdownText.ImageClicked += MarkdownText_ImageClicked;
markdownText.CodeBlockResolving += MarkdownText_CodeBlockResolving; markdownText.CodeBlockResolving += MarkdownText_CodeBlockResolving;
@ -52,6 +55,14 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
LoadData(); LoadData();
} }
private void Current_ThemeChanged(object sender, Models.ThemeChangedArgs e)
{
if (e.CustomSet)
{
markdownText.RequestedTheme = e.Theme;
}
}
private async void MarkdownText_ImageClicked(object sender, LinkClickedEventArgs e) private async void MarkdownText_ImageClicked(object sender, LinkClickedEventArgs e)
{ {
if (!Uri.TryCreate(e.Link, UriKind.Absolute, out Uri result)) if (!Uri.TryCreate(e.Link, UriKind.Absolute, out Uri result))

Просмотреть файл

@ -14,9 +14,11 @@ using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers; using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.SampleApp.Models;
using Microsoft.Toolkit.Uwp.SampleApp.Pages; using Microsoft.Toolkit.Uwp.SampleApp.Pages;
using Microsoft.Toolkit.Uwp.UI.Controls; using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Extensions; using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.Toolkit.Uwp.UI.Helpers;
using Windows.Foundation.Metadata; using Windows.Foundation.Metadata;
using Windows.UI.Core; using Windows.UI.Core;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@ -35,6 +37,12 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
InitializeComponent(); InitializeComponent();
Current = this; Current = this;
_themeListener = new ThemeListener();
_themeListener.ThemeChanged += (s) =>
{
ThemeChanged?.Invoke(this, new ThemeChangedArgs { Theme = GetCurrentTheme() });
};
var background = new Image() var background = new Image()
{ {
Source = new BitmapImage(new Uri("ms-appx:///Assets/Photos/Backgrounds/HERO.jpg")), Source = new BitmapImage(new Uri("ms-appx:///Assets/Photos/Backgrounds/HERO.jpg")),
@ -72,6 +80,31 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
} }
} }
/// <summary>
/// Gets the Current UI Theme.
/// </summary>
/// <returns>The Current UI Theme</returns>
public ElementTheme GetCurrentTheme()
{
return RequestedTheme;
}
/// <summary>
/// Sets the Current UI Theme.
/// </summary>
/// <param name="theme">Theme to set</param>
public void SetCurrentTheme(ElementTheme theme)
{
RequestedTheme = theme;
var args = new ThemeChangedArgs
{
CustomSet = true,
Theme = GetCurrentTheme()
};
ThemeChanged?.Invoke(this, args);
}
public void NavigateToSample(Sample sample) public void NavigateToSample(Sample sample)
{ {
NavigationFrame.Navigate(typeof(SampleController), sample); NavigationFrame.Navigate(typeof(SampleController), sample);
@ -198,5 +231,9 @@ namespace Microsoft.Toolkit.Uwp.SampleApp
} }
private UIElement _parallaxView; private UIElement _parallaxView;
private ThemeListener _themeListener;
public event EventHandler<ThemeChangedArgs> ThemeChanged;
} }
} }