Skip to content

Commit 29bcb91

Browse files
Use System.CommandLine to configure initial app values (#3312)
* Use System.CommandLine to configure initial app values * Clean up command line Summary of changes: 1. Drop the config command. You can attach options directly to any command. RootCommand is just a regular command with a default name set for you. 2. Update options to use a different constructor so we can setup everything in the ctor. 3. Create new method that returns a ValueTuple after parsing. 4. Convert App.xaml.cs properties to instance members (not a huge deal), and set them all leveraging ValueTuple syntax. 5. To share code between the two projects, file line the CommandLineOptions class in both. Adjusted namespace to a "shared" namespace so it looks less weird. * Update MainDemo.Wpf/CommandLineOptions.cs Co-authored-by: Kevin B <[email protected]> --------- Co-authored-by: Kevin Bost <[email protected]> Co-authored-by: Kevin B <[email protected]>
1 parent a4dfbc5 commit 29bcb91

13 files changed

+129
-23
lines changed

Directory.packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageVersion Include="ShowMeTheXAML" Version="2.0.0" />
2222
<PackageVersion Include="ShowMeTheXAML.AvalonEdit" Version="2.0.0" />
2323
<PackageVersion Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
24+
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
2425
<PackageVersion Include="VirtualizingWrapPanel" Version="1.5.7" />
2526
<PackageVersion Include="XAMLTest" Version="1.1.0" />
2627
<PackageVersion Include="xunit" Version="2.5.1" />

MainDemo.Wpf/App.xaml.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using ShowMeTheXAML;
1+
using MaterialDesign.Shared;
2+
using MaterialDesignThemes.Wpf;
3+
using ShowMeTheXAML;
24

35
namespace MaterialDesignDemo;
46

@@ -7,14 +9,13 @@ namespace MaterialDesignDemo;
79
/// </summary>
810
public partial class App : Application
911
{
10-
internal static string? StartupItem { get; private set; }
12+
internal string? StartupPage { get; set; }
13+
internal FlowDirection InitialFlowDirection { get; set; }
14+
internal BaseTheme InitialTheme { get; set; }
1115

1216
protected override void OnStartup(StartupEventArgs e)
1317
{
14-
if (e.Args.Length > 0)
15-
{
16-
StartupItem = e.Args[0];
17-
}
18+
(StartupPage, InitialFlowDirection, InitialTheme) = CommandLineOptions.ParseCommandLine(e.Args);
1819

1920
//This is an alternate way to initialize MaterialDesignInXAML if you don't use the MaterialDesignResourceDictionary in App.xaml
2021
//Color primaryColor = SwatchHelper.Lookup[MaterialDesignColor.DeepPurple];

MainDemo.Wpf/CommandLineOptions.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.CommandLine;
2+
using System.CommandLine.Parsing;
3+
using MaterialDesignThemes.Wpf;
4+
5+
namespace MaterialDesign.Shared;
6+
7+
internal static class CommandLineOptions
8+
{
9+
private static readonly Option<string> PageOption =
10+
new(aliases: new[] {"--page", "-p"},
11+
getDefaultValue: () => "Home",
12+
description: "Sets the startup page of the Demo app.");
13+
14+
private static readonly Option<FlowDirection> FlowDirectionOption =
15+
new(aliases: new[] { "--flowDirection", "-f" },
16+
getDefaultValue: () => FlowDirection.LeftToRight,
17+
description: "Sets the startup flow direction of the Demo app.");
18+
19+
private static readonly Option<BaseTheme> ThemeOption =
20+
new(aliases: new[] { "--theme", "-t" },
21+
getDefaultValue: () => BaseTheme.Inherit,
22+
description: "Sets the startup theme of the Demo app.");
23+
24+
private static readonly RootCommand RootCommand =
25+
new(description: "MaterialDesignInXamlToolkit Demo app command line options.")
26+
{
27+
PageOption,
28+
FlowDirectionOption,
29+
ThemeOption
30+
};
31+
32+
public static (string? StartPage, FlowDirection FlowDirection, BaseTheme BaseTheme) ParseCommandLine(string[] args)
33+
{
34+
ParseResult parseResult = RootCommand.Parse(args);
35+
36+
return new(
37+
parseResult.GetValueForOption(PageOption),
38+
parseResult.GetValueForOption(FlowDirectionOption),
39+
parseResult.GetValueForOption(ThemeOption)
40+
);
41+
}
42+
}

MainDemo.Wpf/Domain/MainWindowViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MaterialDesignDemo.Domain;
99

1010
public class MainWindowViewModel : ViewModelBase
1111
{
12-
public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
12+
public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, string? startupPage)
1313
{
1414
DemoItems = new ObservableCollection<DemoItem>(new[]
1515
{
@@ -31,7 +31,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
3131
{
3232
DemoItems.Add(item);
3333
}
34-
SelectedItem = DemoItems.FirstOrDefault(di => string.Equals(di.Name, App.StartupItem, StringComparison.CurrentCultureIgnoreCase)) ?? DemoItems.First();
34+
SelectedItem = DemoItems.FirstOrDefault(di => string.Equals(di.Name, startupPage, StringComparison.CurrentCultureIgnoreCase)) ?? DemoItems.First();
3535
_demoItemsView = CollectionViewSource.GetDefaultView(DemoItems);
3636
_demoItemsView.Filter = DemoItemsFilter;
3737

MainDemo.Wpf/MainWindow.xaml.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,29 @@ public MainWindow()
1919
//need to get the message queue from the snackbar, so need to be on the dispatcher
2020
MainSnackbar.MessageQueue?.Enqueue("Welcome to Material Design In XAML Toolkit");
2121
}, TaskScheduler.FromCurrentSynchronizationContext());
22+
App app = (App)Application.Current;
2223

23-
DataContext = new MainWindowViewModel(MainSnackbar.MessageQueue!);
24+
DataContext = new MainWindowViewModel(MainSnackbar.MessageQueue!, app.StartupPage);
2425

2526
var paletteHelper = new PaletteHelper();
2627
var theme = paletteHelper.GetTheme();
2728

29+
switch (app.InitialTheme)
30+
{
31+
case BaseTheme.Dark:
32+
ModifyTheme(true);
33+
break;
34+
case BaseTheme.Light:
35+
ModifyTheme(false);
36+
break;
37+
}
38+
39+
if (app.InitialFlowDirection == FlowDirection.RightToLeft)
40+
{
41+
FlowDirectionToggleButton.IsChecked = true;
42+
FlowDirection = FlowDirection.RightToLeft;
43+
}
44+
2845
DarkModeToggleButton.IsChecked = theme.GetBaseTheme() == BaseTheme.Dark;
2946

3047
if (paletteHelper.GetThemeManager() is { } themeManager)

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<NoWarn>NU1701</NoWarn>
4343
</PackageReference>
4444
<PackageReference Include="CommunityToolkit.Mvvm" />
45+
<PackageReference Include="System.CommandLine" />
4546
<PackageReference Include="VirtualizingWrapPanel" />
4647
<PackageReference Include="ShowMeTheXAML" />
4748
<PackageReference Include="ShowMeTheXAML.AvalonEdit" />

MainDemo.Wpf/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Demo App": {
44
"commandName": "Project",
5-
"commandLineArgs": "Home"
5+
"commandLineArgs": "-p Home -t Inherit -f LeftToRight"
66
}
77
}
88
}

MaterialDesign3.Demo.Wpf/App.xaml.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using ShowMeTheXAML;
1+
using MaterialDesign.Shared;
2+
using MaterialDesignThemes.Wpf;
3+
using ShowMeTheXAML;
24

35
namespace MaterialDesign3Demo;
46

@@ -7,14 +9,13 @@ namespace MaterialDesign3Demo;
79
/// </summary>
810
public partial class App : Application
911
{
10-
internal static string? StartupItem { get; private set; }
12+
internal string? StartupPage { get; set; }
13+
internal FlowDirection InitialFlowDirection { get; set; }
14+
internal BaseTheme InitialTheme { get; set; }
1115

1216
protected override void OnStartup(StartupEventArgs e)
1317
{
14-
if (e.Args.Length > 0)
15-
{
16-
StartupItem = e.Args[0];
17-
}
18+
(StartupPage, InitialFlowDirection, InitialTheme) = CommandLineOptions.ParseCommandLine(e.Args);
1819

1920
//This is an alternate way to initialize MaterialDesignInXAML if you don't use the MaterialDesignResourceDictionary in App.xaml
2021
//Color primaryColor = SwatchHelper.Lookup[MaterialDesignColor.DeepPurple];

MaterialDesign3.Demo.Wpf/Domain/MainWindowViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MaterialDesign3Demo.Domain;
99

1010
public class MainWindowViewModel : ViewModelBase
1111
{
12-
public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
12+
public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, string? startupPage)
1313
{
1414
DemoItems = new ObservableCollection<DemoItem>
1515
{
@@ -41,7 +41,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
4141
DemoItems.First(x => x.Name == "Fields"),
4242
DemoItems.First(x => x.Name == "Pickers")
4343
};
44-
SelectedItem = DemoItems.FirstOrDefault(di => string.Equals(di.Name, App.StartupItem, StringComparison.CurrentCultureIgnoreCase)) ?? DemoItems.First();
44+
SelectedItem = DemoItems.FirstOrDefault(di => string.Equals(di.Name, startupPage, StringComparison.CurrentCultureIgnoreCase)) ?? DemoItems.First();
4545
_demoItemsView = CollectionViewSource.GetDefaultView(DemoItems);
4646
_demoItemsView.Filter = DemoItemsFilter;
4747

MaterialDesign3.Demo.Wpf/MainWindow.xaml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,41 @@
205205
<Grid.RowDefinitions>
206206
<RowDefinition />
207207
<RowDefinition />
208+
<RowDefinition />
208209
</Grid.RowDefinitions>
209-
<TextBlock Margin="0,0,10,0" Text="Light" />
210+
<TextBlock Margin="0,0,10,0"
211+
Text="Light"
212+
VerticalAlignment="Center" />
210213
<ToggleButton x:Name="DarkModeToggleButton"
211214
Grid.Column="1"
212215
Click="MenuDarkModeButton_Click" />
213216
<TextBlock Grid.Column="2"
214217
Margin="10,0,0,0"
215-
Text="Dark" />
218+
Text="Dark"
219+
VerticalAlignment="Center"/>
216220
<TextBlock Grid.Row="1"
217221
Margin="0,10,10,0"
218-
Text="Enabled" />
222+
Text="Enabled"
223+
VerticalAlignment="Center"/>
219224
<ToggleButton x:Name="ControlsEnabledToggleButton"
220225
Grid.Row="1"
221226
Grid.Column="1"
222227
Margin="0,10,0,0"
223228
IsChecked="{Binding ControlsEnabled}" />
229+
<TextBlock Grid.Row="2"
230+
Margin="0,10,10,0"
231+
Text="LTR"
232+
VerticalAlignment="Center"/>
233+
<ToggleButton x:Name="FlowDirectionToggleButton"
234+
Grid.Row="2"
235+
Grid.Column="1"
236+
Margin="0,10,0,0"
237+
Click="FlowDirectionButton_Click" />
238+
<TextBlock Grid.Row="2"
239+
Grid.Column="2"
240+
Margin="10,10,0,0"
241+
Text="RTL"
242+
VerticalAlignment="Center"/>
224243
</Grid>
225244

226245
<Separator />

0 commit comments

Comments
 (0)