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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proof of concept] List lightweight styling resources #1426

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 42 additions & 2 deletions WinUIGallery/ControlPages/ButtonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<Page x:Class="AppUIBasics.ControlPages.ButtonPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:AppUIBasics"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pages="using:AppUIBasics.ControlPages"
xmlns:contract7Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,7)"
mc:Ignorable="d">
<StackPanel>
Expand Down Expand Up @@ -119,6 +120,45 @@
</x:String>
</local:ControlExample.Xaml>
</local:ControlExample>


<TextBlock Text="Lightweight styling" Style="{ThemeResource BodyStrongTextBlockStyle}" AutomationProperties.HeadingLevel="Level2" Margin="0,16,0,4" />
<ListView x:Name="LightweightStylesBox" HorizontalAlignment="Left">
<ListView.Header>
<Grid ColumnSpacing="8" Padding="0,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" MinWidth="100" />
</Grid.ColumnDefinitions>

<TextBlock Text="Light" Style="{ThemeResource BodyTextBlockStyle}" Grid.Column="1" VerticalAlignment="Center" TextAlignment="Center" />
<TextBlock Text="Dark" Style="{ThemeResource BodyTextBlockStyle}" Grid.Column="2" VerticalAlignment="Center" TextAlignment="Center" />
<TextBlock Text="High Contrast" Style="{ThemeResource BodyTextBlockStyle}" Grid.Column="3" VerticalAlignment="Center" TextAlignment="Center" />
</Grid>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate x:DataType="pages:ButtonResource">
<Grid ColumnSpacing="8" Padding="0,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" MinWidth="100" />
<ColumnDefinition Width="*" MinWidth="100" />
</Grid.ColumnDefinitions>

<TextBlock Text="{x:Bind Name}" Style="{ThemeResource BodyTextBlockStyle}" Grid.Column="0" VerticalAlignment="Center" TextAlignment="Right" />

<!-- TODO: include hex in view -->
<!-- TODO: allow live restyling? -->
<Border Height="40" Grid.Column="1" Background="{x:Bind LightBrush}" CornerRadius="4" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" BorderThickness="1" />
<Border Height="50" Margin="-5" Grid.Column="2" Background="Black" CornerRadius="4" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" BorderThickness="1" Padding="4">
<Border Height="40" Background="{x:Bind DarkBrush}" CornerRadius="4" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" BorderThickness="1" />
</Border>
<Border Height="40" Grid.Column="3" Background="{x:Bind HighContrastBrush}" CornerRadius="4" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" BorderThickness="1" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Page>
125 changes: 84 additions & 41 deletions WinUIGallery/ControlPages/ButtonPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,84 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace AppUIBasics.ControlPages
{
public sealed partial class ButtonPage : Page
{
public ButtonPage()
{
this.InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button b)
{
string name = b.Name;

switch (name)
{
case "Button1":
Control1Output.Text = "You clicked: " + name;
break;
case "Button2":
Control2Output.Text = "You clicked: " + name;
break;

}
}
}
}
}
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Linq;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;

namespace AppUIBasics.ControlPages
{
public sealed class ButtonResource
{
public string Name;
public SolidColorBrush LightBrush;
public SolidColorBrush DarkBrush;
public SolidColorBrush HighContrastBrush;
}

public sealed partial class ButtonPage : Page
{
public ButtonPage()
{
this.InitializeComponent();

var rawButtonResources = Application.Current.Resources.MergedDictionaries
.SelectMany((d) =>
{
return d.ThemeDictionaries.SelectMany((t) =>
{
// TODO: this isn't the best approach. It works, since lightweight styles always have a key that starts with "Button",
// but it would be nicer to inspect the default template somehow & get a complete list of styles.
//
// Similarly, it'd be nice to automatically know to fetch things like "AccentButton".
return (t.Value as ResourceDictionary)
.Where((r) => r.Key.ToString().StartsWith("Button") && r.Value is SolidColorBrush)
.Select((r) =>
{
return new Tuple<string, string, SolidColorBrush>(t.Key.ToString(), r.Key.ToString(), r.Value as SolidColorBrush);
});
});
});

var buttonResources = rawButtonResources
.GroupBy((t) => t.Item2)
.Select((grouped) => {
return new ButtonResource
{
Name = grouped.Key,
LightBrush = grouped.Where((t) => t.Item1 == "Light").Select((t) => t.Item3).FirstOrDefault(),
DarkBrush = grouped.Where((t) => t.Item1 == "Default").Select((t) => t.Item3).FirstOrDefault(),
HighContrastBrush = grouped.Where((t) => t.Item1 == "HighContrast").Select((t) => t.Item3).FirstOrDefault()
};
});

LightweightStylesBox.ItemsSource = buttonResources;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button b)
{
string name = b.Name;

switch (name)
{
case "Button1":
Control1Output.Text = "You clicked: " + name;
break;
case "Button2":
Control2Output.Text = "You clicked: " + name;
break;

}
}
}
}
}