Skip to content

Commit de40558

Browse files
committed
Use DefaultTextForegroundThemeBrush as default foreground;
Add default control style properties; Update sample app; Add icon for nuget;
1 parent f229dff commit de40558

File tree

9 files changed

+383
-204
lines changed

9 files changed

+383
-204
lines changed

Sample.Win2D.UWP/App.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<Application.Resources>
88
<controls:XamlControlsResources>
99
<controls:XamlControlsResources.MergedDictionaries>
10-
<!-- Other app resources here -->
10+
<ResourceDictionary>
11+
<Style TargetType="controls:Expander">
12+
<Setter Property="Padding" Value="5" />
13+
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
14+
</Style>
15+
</ResourceDictionary>
1116
</controls:XamlControlsResources.MergedDictionaries>
1217
</controls:XamlControlsResources>
1318
</Application.Resources>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices.WindowsRuntime;
5+
using Windows.Globalization;
6+
using Windows.UI.Xaml;
7+
using Windows.UI.Xaml.Controls;
8+
using Windows.UI.Xaml.Data;
9+
using Windows.UI.Xaml.Documents;
10+
using Windows.UI.Xaml.Input;
11+
using Windows.UI.Xaml.Media;
12+
using Microsoft.Graphics.Canvas.Text;
13+
14+
// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235
15+
16+
namespace Sample.Win2D.UWP.Controls
17+
{
18+
public sealed class FontPickerBox : Control
19+
{
20+
private ComboBox _comboBox;
21+
22+
public FontPickerBox()
23+
{
24+
this.DefaultStyleKey = typeof(FontPickerBox);
25+
}
26+
27+
protected override void OnApplyTemplate()
28+
{
29+
base.OnApplyTemplate();
30+
31+
_comboBox = GetTemplateChild("FontComboBox") as ComboBox;
32+
33+
var fontFamilyNames = CanvasTextFormat.GetSystemFontFamilies(ApplicationLanguages.Languages).OrderBy(k => k);
34+
35+
foreach (string fontFamilyName in fontFamilyNames)
36+
{
37+
ComboBoxItem item = new ComboBoxItem();
38+
item.Content = fontFamilyName;
39+
item.FontFamily = new FontFamily(fontFamilyName);
40+
_comboBox.Items.Add(item);
41+
}
42+
43+
_comboBox.SelectionChanged += ComboBox_SelectionChanged;
44+
45+
SelectDefaultFont();
46+
}
47+
48+
public event SelectionChangedEventHandler SelectionChanged
49+
{
50+
add { _comboBox.SelectionChanged += value; }
51+
remove { _comboBox.SelectionChanged -= value; }
52+
}
53+
54+
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
55+
{
56+
FontFamily = (_comboBox.SelectedItem as ComboBoxItem).FontFamily;
57+
}
58+
59+
private void SelectDefaultFont()
60+
{
61+
SelectFont("Segoe UI");
62+
}
63+
64+
public void SelectFont(string name)
65+
{
66+
for (int i = 0; i < _comboBox.Items.Count; ++i)
67+
{
68+
ComboBoxItem item = _comboBox.Items[i] as ComboBoxItem;
69+
if ((item.Content as string) == name)
70+
{
71+
_comboBox.SelectedIndex = i;
72+
return;
73+
}
74+
}
75+
}
76+
}
77+
}

Sample.Win2D.UWP/MainPage.xaml

Lines changed: 122 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,133 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:TBFX="using:TextBlockFX.Win2D.UWP"
99
xmlns:effects="using:TextBlockFX.Win2D.UWP.Effects"
10+
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
11+
xmlns:controls="using:Sample.Win2D.UWP.Controls"
1012
mc:Ignorable="d"
1113
Background="{ThemeResource AcrylicBackgroundFillColorDefaultBrush}">
1214

1315
<Grid>
14-
<Grid.RowDefinitions>
15-
<RowDefinition Height="*"/>
16-
<RowDefinition Height="Auto"/>
17-
<RowDefinition Height="*"/>
18-
</Grid.RowDefinitions>
16+
<Grid.ColumnDefinitions>
17+
<ColumnDefinition Width="400"/>
18+
<ColumnDefinition Width="*"/>
19+
</Grid.ColumnDefinitions>
20+
21+
<Border
22+
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
23+
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
24+
BorderThickness="1"
25+
CornerRadius="4"
26+
Padding="10"
27+
Margin="10">
28+
<ScrollViewer>
29+
<StackPanel Orientation="Vertical" Spacing="10">
30+
<muxc:Expander
31+
IsExpanded="True"
32+
HorizontalAlignment="Stretch"
33+
Header="Effects">
34+
<StackPanel Orientation="Vertical"
35+
Spacing="10"
36+
HorizontalAlignment="Stretch">
37+
<ComboBox x:Name="EffectComboBox"
38+
HorizontalAlignment="Stretch"
39+
ItemsSource="{x:Bind BuiltInEffects}"
40+
DisplayMemberPath="Name"
41+
SelectedValuePath="Effect"
42+
SelectedValue="{x:Bind SelectedEffect, Mode = TwoWay}"
43+
Loaded="EffectComboBox_OnLoaded"/>
44+
</StackPanel>
45+
</muxc:Expander>
46+
<muxc:Expander
47+
IsExpanded="True"
48+
HorizontalAlignment="Stretch"
49+
Header="Text Format">
50+
<StackPanel Orientation="Vertical"
51+
Spacing="10"
52+
HorizontalAlignment="Stretch">
53+
<controls:FontPickerBox x:Name="FontPicker" HorizontalAlignment="Stretch"/>
54+
<muxc:NumberBox x:Name="FontSizeNumBox"
55+
Header="Font size"
56+
Value="36"
57+
SpinButtonPlacementMode="Inline"
58+
Maximum="72"
59+
Minimum="9"
60+
SmallChange="1"
61+
LargeChange="10"/>
62+
<ComboBox x:Name="FontStretchComboBox"
63+
HorizontalAlignment="Stretch"
64+
Header="Font Stretch"
65+
ItemsSource="{x:Bind FontStretches}"
66+
DisplayMemberPath="Name"
67+
SelectedValuePath="Value"/>
68+
<ComboBox x:Name="FontStyleComboBox"
69+
HorizontalAlignment="Stretch"
70+
Header="Font Style"
71+
ItemsSource="{x:Bind FontStyles}"
72+
DisplayMemberPath="Name"
73+
SelectedValuePath="Value"/>
74+
<ComboBox x:Name="FontWeightComboBox"
75+
HorizontalAlignment="Stretch"
76+
Header="Font Weight"
77+
ItemsSource="{x:Bind FontWeightsList}"
78+
DisplayMemberPath="Name"
79+
SelectedValuePath="Value"/>
80+
</StackPanel>
81+
</muxc:Expander>
82+
<muxc:Expander
83+
IsExpanded="True"
84+
HorizontalAlignment="Stretch"
85+
Header="Text">
86+
<StackPanel Orientation="Vertical" Spacing="10" HorizontalAlignment="Stretch">
87+
<ComboBox x:Name="TextComboBox"
88+
Margin="5,0"
89+
MinWidth="200"
90+
HorizontalAlignment="Stretch"
91+
SelectedIndex="{x:Bind SelectedSampleTextIndex, Mode = TwoWay}"
92+
Loaded="TextComboBox_OnLoaded">
93+
<x:String>In Other Words</x:String>
94+
<x:String>Mencius (in Chinese)</x:String>
95+
</ComboBox>
96+
<ToggleButton x:Name="AutoPlayButton"
97+
Margin="5,0"
98+
Click="AutoPlayButton_OnClick"
99+
HorizontalAlignment="Center">
100+
<StackPanel Orientation="Horizontal" Spacing="5">
101+
<SymbolIcon Symbol="Play"/>
102+
<TextBlock Text="Play sample texts"/>
103+
</StackPanel>
104+
</ToggleButton>
105+
<TextBox x:Name="InputBox"
106+
Grid.Row="2"
107+
Grid.ColumnSpan="2"
108+
Margin="10"
109+
AcceptsReturn="True"
110+
TextWrapping="Wrap"
111+
HorizontalAlignment="Stretch"
112+
ScrollViewer.HorizontalScrollMode="Disabled"
113+
ScrollViewer.VerticalScrollMode="Auto"
114+
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
115+
ScrollViewer.VerticalScrollBarVisibility="Auto"
116+
TextChanged="InputBox_OnTextChanged"/>
117+
</StackPanel>
118+
</muxc:Expander>
119+
</StackPanel>
120+
</ScrollViewer>
121+
</Border>
122+
19123
<TBFX:TextBlockFX x:Name="TBFX"
20-
Margin="10"
21-
TextWrapping="Wrap"
22-
FontSize="36"
23-
TextAlignment="Center"
24-
TextTrimming="CharacterEllipsis"
25-
RedrawStateChanged="TBFX_OnRedrawStateChanged">
124+
Grid.Column="1"
125+
Margin="10"
126+
MinHeight="300"
127+
MaxHeight="600"
128+
FontFamily="{Binding ElementName=FontPicker, Path=FontFamily,Mode=OneWay}"
129+
FontSize="{Binding ElementName=FontSizeNumBox, Path=Value, Mode=OneWay}"
130+
FontStyle="{Binding ElementName=FontStyleComboBox, Path=SelectedValue, Mode= OneWay}"
131+
FontStretch="{Binding ElementName=FontStretchComboBox, Path=SelectedValue, Mode=OneWay}"
132+
FontWeight="{Binding ElementName=FontWeightComboBox, Path=SelectedValue, Mode=OneWay}"
133+
TextAlignment="Center"
134+
TextTrimming="CharacterEllipsis"
135+
TextWrapping="Wrap"
136+
RedrawStateChanged="TBFX_OnRedrawStateChanged">
26137
</TBFX:TextBlockFX>
27-
28-
<CommandBar Grid.Row="1"
29-
IsOpen="False"
30-
DefaultLabelPosition="Right"
31-
HorizontalAlignment="Center">
32-
<AppBarElementContainer VerticalContentAlignment="Center">
33-
<ComboBox x:Name="TextComboBox"
34-
Margin="5,0"
35-
MinWidth="200"
36-
SelectedIndex="{x:Bind SelectedSampleTextIndex, Mode = TwoWay}"
37-
Loaded="TextComboBox_OnLoaded">
38-
<x:String>In Other Words</x:String>
39-
<x:String>Mencius (in Chinese)</x:String>
40-
</ComboBox>
41-
</AppBarElementContainer>
42-
<AppBarSeparator/>
43-
<AppBarElementContainer VerticalContentAlignment="Center">
44-
<TextBlock Text="Effect:" Margin="5,0"/>
45-
</AppBarElementContainer>
46-
<AppBarElementContainer VerticalContentAlignment="Center">
47-
<ComboBox x:Name="EffectComboBox"
48-
Margin="5,0"
49-
MinWidth="200"
50-
ItemsSource="{x:Bind BuiltInEffects}"
51-
DisplayMemberPath="Name"
52-
SelectedValuePath="Effect"
53-
SelectedValue="{x:Bind SelectedEffect, Mode = TwoWay}"
54-
Loaded="EffectComboBox_OnLoaded"/>
55-
</AppBarElementContainer>
56-
<AppBarSeparator/>
57-
<AppBarToggleButton x:Name="AutoPlayButton"
58-
Margin="5,0"
59-
Icon="Play"
60-
Label="Play sample texts"
61-
Click="AutoPlayButton_OnClick"/>
62-
</CommandBar>
63-
<TextBox x:Name="InputBox"
64-
Grid.Row="2"
65-
Grid.ColumnSpan="2"
66-
Margin="10"
67-
AcceptsReturn="True"
68-
TextWrapping="Wrap"
69-
ScrollViewer.HorizontalScrollMode="Disabled"
70-
ScrollViewer.VerticalScrollMode="Auto"
71-
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
72-
ScrollViewer.VerticalScrollBarVisibility="Auto"
73-
TextChanged="InputBox_OnTextChanged"/>
74138
</Grid>
75139
</Page>

Sample.Win2D.UWP/MainPage.xaml.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using TextBlockFX;
45
using TextBlockFX.Win2D.UWP;
56
using TextBlockFX.Win2D.UWP.Effects;
7+
using Windows.UI.Text;
68
using Windows.UI.Xaml;
79
using Windows.UI.Xaml.Controls;
810

@@ -89,14 +91,61 @@ public int SelectedSampleTextIndex
8991
}
9092
}
9193

94+
public List<ComboWrapper<FontStretch>> FontStretches => GetEnumAsList<FontStretch>();
95+
96+
public List<ComboWrapper<FontStyle>> FontStyles => GetEnumAsList<FontStyle>();
97+
98+
public List<ComboWrapper<FontWeight>> FontWeightsList => new List<ComboWrapper<FontWeight>>()
99+
{
100+
new ComboWrapper<FontWeight>("ExtraBlack", FontWeights.ExtraBlack),
101+
new ComboWrapper<FontWeight>("Black", FontWeights.Black),
102+
new ComboWrapper<FontWeight>("ExtraBold", FontWeights.ExtraBold),
103+
new ComboWrapper<FontWeight>("Bold", FontWeights.Bold),
104+
new ComboWrapper<FontWeight>("SemiBold", FontWeights.SemiBold),
105+
new ComboWrapper<FontWeight>("Medium", FontWeights.Medium),
106+
new ComboWrapper<FontWeight>("Normal", FontWeights.Normal),
107+
new ComboWrapper<FontWeight>("SemiLight", FontWeights.SemiLight),
108+
new ComboWrapper<FontWeight>("Light", FontWeights.Light),
109+
new ComboWrapper<FontWeight>("ExtraLight", FontWeights.ExtraLight),
110+
new ComboWrapper<FontWeight>("Thin", FontWeights.Thin),
111+
};
112+
92113
public MainPage()
93114
{
94115
this.InitializeComponent();
116+
this.Loaded += MainPage_Loaded;
95117
_timer.Interval = TimeSpan.FromMilliseconds(1000);
96118
_timer.Tick += _timer_Tick;
97119
_sampleTexts = _inOtherWords;
98120
}
99121

122+
private void MainPage_Loaded(object sender, RoutedEventArgs e)
123+
{
124+
for (int i = 0; i < FontStretches.Count; i++)
125+
{
126+
if (FontStretches[i].Value == FontStretch.Normal)
127+
{
128+
FontStretchComboBox.SelectedIndex = i;
129+
}
130+
}
131+
132+
for (int i = 0; i < FontStyles.Count; i++)
133+
{
134+
if (FontStyles[i].Value == FontStyle.Normal)
135+
{
136+
FontStyleComboBox.SelectedIndex = i;
137+
}
138+
}
139+
140+
for (int i = 0; i < FontWeightsList.Count; i++)
141+
{
142+
if (FontWeightsList[i].Value.Weight == FontWeights.Normal.Weight)
143+
{
144+
FontWeightComboBox.SelectedIndex = i;
145+
}
146+
}
147+
}
148+
100149
private void _timer_Tick(object sender, object e)
101150
{
102151
SetSampleText();
@@ -147,6 +196,26 @@ private void TextComboBox_OnLoaded(object sender, RoutedEventArgs e)
147196
{
148197
TextComboBox.SelectedIndex = 0;
149198
}
199+
200+
private static List<ComboWrapper<T>> GetEnumAsList<T>()
201+
{
202+
var names = Enum.GetNames(typeof(T)).ToList();
203+
var values = Enum.GetValues(typeof(T)).Cast<T>().ToList();
204+
return names.Zip(values, (k, v) => new ComboWrapper<T>(k, v)).ToList();
205+
}
206+
}
207+
208+
public class ComboWrapper<T>
209+
{
210+
public string Name { get; }
211+
212+
public T Value { get; }
213+
214+
public ComboWrapper(string name, T value)
215+
{
216+
Name = name;
217+
Value = value;
218+
}
150219
}
151220

152221
public class BuiltInEffect

0 commit comments

Comments
 (0)