Skip to content

Commit 60f714f

Browse files
0x5bfayaira2
andauthored
Code Quality: Refactored ToolbarViewModel (#13659)
Co-authored-by: Yair <[email protected]>
1 parent dcff960 commit 60f714f

22 files changed

+725
-714
lines changed

src/Files.App/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ private IHost ConfigureHost()
144144
.AddSingleton<NetworkDrivesViewModel>()
145145
.AddSingleton<StatusCenterViewModel>()
146146
.AddSingleton<AppearanceViewModel>()
147+
.AddSingleton<PathBreadcrumbViewModel>()
148+
.AddSingleton<AddressToolbarViewModel>()
147149
).Build();
148150
}
149151

src/Files.App/Data/Contexts/ContentPage/ContentPageContext.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ private void ToolbarViewModel_PropertyChanged(object? sender, PropertyChangedEve
164164
{
165165
switch (e.PropertyName)
166166
{
167-
case nameof(ToolbarViewModel.CanGoBack):
168-
case nameof(ToolbarViewModel.CanGoForward):
169-
case nameof(ToolbarViewModel.CanNavigateToParent):
170-
case nameof(ToolbarViewModel.HasItem):
171-
case nameof(ToolbarViewModel.CanRefresh):
172-
case nameof(ToolbarViewModel.IsSearchBoxVisible):
167+
case nameof(AddressToolbarViewModel.CanGoBack):
168+
case nameof(AddressToolbarViewModel.CanGoForward):
169+
case nameof(AddressToolbarViewModel.CanNavigateToParent):
170+
case nameof(AddressToolbarViewModel.HasItem):
171+
case nameof(AddressToolbarViewModel.CanRefresh):
172+
case nameof(AddressToolbarViewModel.IsSearchBoxVisible):
173173
OnPropertyChanged(e.PropertyName);
174174
break;
175-
case nameof(ToolbarViewModel.SelectedItems):
175+
case nameof(AddressToolbarViewModel.SelectedItems):
176176
UpdateSelectedItems();
177177
break;
178178
}

src/Files.App/Data/EventArguments/PathBoxItemDroppedEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Files.App.Data.EventArguments
99
{
10-
public class PathBoxItemDroppedEventArgs
10+
public class PathBreadcrumbItemDroppedEventArgs
1111
{
1212
public DataPackageView Package { get; set; }
1313

src/Files.App/Data/EventArguments/ToolbarPathItemLoadedEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public class ToolbarPathItemLoadedEventArgs
1111
{
1212
public MenuFlyout OpenedFlyout { get; set; }
1313

14-
public PathBoxItem Item { get; set; }
14+
public PathBreadcrumbItem Item { get; set; }
1515
}
1616
}

src/Files.App/Data/Items/PathBoxItem.cs renamed to src/Files.App/Data/Items/PathBreadcrumbItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace Files.App.Data.Items
55
{
6-
public class PathBoxItem
6+
public class PathBreadcrumbItem
77
{
8-
public string? Title { get; set; }
8+
public string? Name { get; set; }
99

1010
public string? Path { get; set; }
1111
}

src/Files.App/Data/Models/AppModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public int TabStripSelectedIndex
3737

3838
if (value >= 0 && value < MainPageViewModel.AppInstances.Count)
3939
{
40-
Frame rootFrame = (Frame)MainWindow.Instance.Content;
41-
var mainView = (MainPage)rootFrame.Content;
42-
mainView.ViewModel.SelectedTabItem = MainPageViewModel.AppInstances[value];
40+
var mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
41+
mainPageViewModel.SelectedTabItem = MainPageViewModel.AppInstances[value];
4342
}
4443
}
4544
}

src/Files.App/Data/TemplateSelectors/PathBreadcrumbItemSelector.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@ internal sealed class PathBreadcrumbItemSelector : DataTemplateSelector
1515

1616
public DataTemplate? CurrentItem { get; set; }
1717

18-
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
18+
protected override DataTemplate? SelectTemplateCore(object item, DependencyObject container)
1919
{
2020
var itemsControl = ItemsControl.ItemsControlFromItemContainer(container);
2121

22-
if (itemsControl.ItemsSource is ObservableCollection<PathBoxItem> items)
22+
if (itemsControl.ItemsSource is ObservableCollection<PathBreadcrumbItem> items)
2323
{
2424
return
2525
itemsControl.IndexFromContainer(container) == items.Count - 1
2626
? CurrentItem!
2727
: ParentItems!;
2828
}
29-
else
30-
{
31-
throw new ArgumentException($"Type of {nameof(itemsControl.ItemsSource)} doesn't match ObservableCollection<{nameof(PathBoxItem)}>");
32-
}
29+
30+
return null;
3331
}
3432
}
3533
}

src/Files.App/UserControls/AddressToolbar.xaml

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@
55
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
66
xmlns:animatedvisuals="using:Microsoft.UI.Xaml.Controls.AnimatedVisuals"
77
xmlns:contract8Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,8)"
8-
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
98
xmlns:converters="using:Files.App.Converters"
10-
xmlns:converters1="using:CommunityToolkit.WinUI.UI.Converters"
119
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
12-
xmlns:extensions="using:CommunityToolkit.WinUI.UI"
1310
xmlns:helpers="using:Files.App.Helpers"
1411
xmlns:items="using:Files.App.Data.Items"
1512
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
16-
xmlns:triggers="using:CommunityToolkit.WinUI.UI.Triggers"
1713
xmlns:uc="using:Files.App.UserControls"
18-
x:Name="NavToolbar"
14+
xmlns:wctconverters="using:CommunityToolkit.WinUI.UI.Converters"
15+
xmlns:wctextensions="using:CommunityToolkit.WinUI.UI"
16+
xmlns:wcttriggers="using:CommunityToolkit.WinUI.UI.Triggers"
1917
Height="50"
20-
d:DesignHeight="50"
21-
d:DesignWidth="800"
18+
DataContext="{x:Bind ViewModel, Mode=OneWay}"
2219
Loading="NavToolbar_Loading"
2320
mc:Ignorable="d">
2421

2522
<UserControl.Resources>
2623
<ResourceDictionary>
2724
<converters:NullToTrueConverter x:Key="NullToFalseConverter" Inverse="True" />
28-
<converters1:BoolNegationConverter x:Key="BoolNegationConverter" />
25+
<wctconverters:BoolNegationConverter x:Key="BoolNegationConverter" />
2926

3027
<ResourceDictionary.MergedDictionaries>
3128
<ResourceDictionary>
@@ -226,39 +223,39 @@
226223
x:Name="Back"
227224
x:FieldModifier="public"
228225
AccessKey="B"
229-
AutomationProperties.FullDescription="{x:Bind ViewModel.Commands.NavigateBack.Description, Mode=OneWay}"
230-
AutomationProperties.Name="{x:Bind ViewModel.Commands.NavigateBack.Label, Mode=OneWay}"
231-
Command="{x:Bind ViewModel.Commands.NavigateBack, Mode=OneWay}"
232-
IsEnabled="{x:Bind ViewModel.Commands.NavigateBack.IsExecutable, Mode=OneWay}"
226+
AutomationProperties.FullDescription="{x:Bind Commands.NavigateBack.Description, Mode=OneWay}"
227+
AutomationProperties.Name="{x:Bind Commands.NavigateBack.Label, Mode=OneWay}"
228+
Command="{x:Bind Commands.NavigateBack, Mode=OneWay}"
229+
IsEnabled="{x:Bind Commands.NavigateBack.IsExecutable, Mode=OneWay}"
233230
Style="{StaticResource AddressToolbarButtonStyle}"
234-
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.NavigateBack.LabelWithHotKey, Mode=OneWay}">
235-
<FontIcon FontSize="14" Glyph="{x:Bind ViewModel.Commands.NavigateBack.Glyph.BaseGlyph, Mode=OneWay}" />
231+
ToolTipService.ToolTip="{x:Bind Commands.NavigateBack.LabelWithHotKey, Mode=OneWay}">
232+
<FontIcon FontSize="14" Glyph="{x:Bind Commands.NavigateBack.Glyph.BaseGlyph, Mode=OneWay}" />
236233
</Button>
237234

238235
<Button
239236
x:Name="Forward"
240237
x:FieldModifier="public"
241238
AccessKey="F"
242-
AutomationProperties.FullDescription="{x:Bind ViewModel.Commands.NavigateForward.Description, Mode=OneWay}"
243-
AutomationProperties.Name="{x:Bind ViewModel.Commands.NavigateForward.Label, Mode=OneWay}"
244-
Command="{x:Bind ViewModel.Commands.NavigateForward, Mode=OneWay}"
245-
IsEnabled="{x:Bind ViewModel.Commands.NavigateForward.IsExecutable, Mode=OneWay}"
239+
AutomationProperties.FullDescription="{x:Bind Commands.NavigateForward.Description, Mode=OneWay}"
240+
AutomationProperties.Name="{x:Bind Commands.NavigateForward.Label, Mode=OneWay}"
241+
Command="{x:Bind Commands.NavigateForward, Mode=OneWay}"
242+
IsEnabled="{x:Bind Commands.NavigateForward.IsExecutable, Mode=OneWay}"
246243
Style="{StaticResource AddressToolbarButtonStyle}"
247-
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.NavigateForward.LabelWithHotKey, Mode=OneWay}">
248-
<FontIcon FontSize="14" Glyph="{x:Bind ViewModel.Commands.NavigateForward.Glyph.BaseGlyph, Mode=OneWay}" />
244+
ToolTipService.ToolTip="{x:Bind Commands.NavigateForward.LabelWithHotKey, Mode=OneWay}">
245+
<FontIcon FontSize="14" Glyph="{x:Bind Commands.NavigateForward.Glyph.BaseGlyph, Mode=OneWay}" />
249246
</Button>
250247

251248
<Button
252249
x:Name="Up"
253250
x:FieldModifier="public"
254251
AccessKey="U"
255-
AutomationProperties.FullDescription="{x:Bind ViewModel.Commands.NavigateUp.Description, Mode=OneWay}"
256-
AutomationProperties.Name="{x:Bind ViewModel.Commands.NavigateUp.Label, Mode=OneWay}"
257-
Command="{x:Bind ViewModel.Commands.NavigateUp, Mode=OneWay}"
258-
IsEnabled="{x:Bind ViewModel.Commands.NavigateUp.IsExecutable, Mode=OneWay}"
252+
AutomationProperties.FullDescription="{x:Bind Commands.NavigateUp.Description, Mode=OneWay}"
253+
AutomationProperties.Name="{x:Bind Commands.NavigateUp.Label, Mode=OneWay}"
254+
Command="{x:Bind Commands.NavigateUp, Mode=OneWay}"
255+
IsEnabled="{x:Bind Commands.NavigateUp.IsExecutable, Mode=OneWay}"
259256
Style="{StaticResource AddressToolbarButtonStyle}"
260-
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.NavigateUp.LabelWithHotKey, Mode=OneWay}">
261-
<FontIcon FontSize="14" Glyph="{x:Bind ViewModel.Commands.NavigateUp.Glyph.BaseGlyph, Mode=OneWay}" />
257+
ToolTipService.ToolTip="{x:Bind Commands.NavigateUp.LabelWithHotKey, Mode=OneWay}">
258+
<FontIcon FontSize="14" Glyph="{x:Bind Commands.NavigateUp.Glyph.BaseGlyph, Mode=OneWay}" />
262259
</Button>
263260

264261
<Button
@@ -341,7 +338,7 @@
341338
MinHeight="33"
342339
HorizontalAlignment="Stretch"
343340
VerticalAlignment="Center"
344-
extensions:FrameworkElementExtensions.Cursor="Hand"
341+
wctextensions:FrameworkElementExtensions.Cursor="Hand"
345342
x:Load="{x:Bind ViewModel.ClickablePathLoaded, Mode=OneWay}"
346343
Background="{ThemeResource TextControlBackground}"
347344
BorderBrush="{ThemeResource ControlElevationBorderBrush}"
@@ -353,17 +350,19 @@
353350
<ColumnDefinition Width="*" />
354351
<ColumnDefinition Width="Auto" />
355352
</Grid.ColumnDefinitions>
353+
356354
<uc:PathBreadcrumb
357355
Grid.Column="0"
358356
VerticalAlignment="Stretch"
359357
HorizontalContentAlignment="Stretch"
360-
VerticalContentAlignment="Stretch"
361-
ViewModel="{x:Bind ViewModel, Mode=OneWay}" />
358+
VerticalContentAlignment="Stretch" />
359+
362360
<FontIcon
363361
Grid.Column="1"
364362
Margin="12"
365363
FontSize="16"
366364
Glyph="&#xE70D;" />
365+
367366
</Grid>
368367

369368
<!-- Storage Search Box -->
@@ -390,11 +389,11 @@
390389
<Button
391390
x:Name="ShowSearchButton"
392391
AccessKey="I"
393-
AutomationProperties.Name="{x:Bind ViewModel.Commands.Search.Label}"
394-
Command="{x:Bind ViewModel.Commands.Search, Mode=OneWay}"
395-
IsEnabled="{x:Bind ViewModel.Commands.Search.IsExecutable}"
392+
AutomationProperties.Name="{x:Bind Commands.Search.Label}"
393+
Command="{x:Bind Commands.Search, Mode=OneWay}"
394+
IsEnabled="{x:Bind Commands.Search.IsExecutable}"
396395
Style="{StaticResource AddressToolbarButtonStyle}"
397-
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.Search.LabelWithHotKey, Mode=OneWay}"
396+
ToolTipService.ToolTip="{x:Bind Commands.Search.LabelWithHotKey, Mode=OneWay}"
398397
Visibility="Collapsed">
399398
<FontIcon FontSize="14" Glyph="{x:Bind ViewModel.SearchButtonGlyph, Mode=OneWay}" />
400399
</Button>
@@ -407,15 +406,15 @@
407406
AutomationProperties.Name="{helpers:ResourceString Name=StatusCenter}"
408407
Style="{StaticResource AddressToolbarButtonStyle}"
409408
ToolTipService.ToolTip="{helpers:ResourceString Name=StatusCenter}"
410-
Visibility="{x:Bind OngoingTasksViewModel.HasAnyItem, Mode=OneWay}">
409+
Visibility="{x:Bind StatusCenterViewModel.HasAnyItem, Mode=OneWay}">
411410

412411
<Grid Margin="-16">
413412

414413
<uc:OpacityIcon
415414
x:Name="StatusCenterIcon"
416415
Width="16"
417416
Height="16"
418-
x:Load="{x:Bind OngoingTasksViewModel.HasAnyItemInProgress, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
417+
x:Load="{x:Bind StatusCenterViewModel.HasAnyItemInProgress, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
419418
Style="{StaticResource ColorIconStatusCenter}" />
420419

421420
<ProgressRing
@@ -424,16 +423,16 @@
424423
Height="20"
425424
HorizontalAlignment="Center"
426425
VerticalAlignment="Center"
427-
x:Load="{x:Bind OngoingTasksViewModel.HasAnyItemInProgress, Mode=OneWay}"
426+
x:Load="{x:Bind StatusCenterViewModel.HasAnyItemInProgress, Mode=OneWay}"
428427
Background="{ThemeResource SubtleFillColorSecondaryBrush}"
429428
IsIndeterminate="False"
430-
Value="{x:Bind OngoingTasksViewModel.AverageOperationProgressValue, Mode=OneWay}" />
429+
Value="{x:Bind StatusCenterViewModel.AverageOperationProgressValue, Mode=OneWay}" />
431430

432431
<InfoBadge
433432
x:Name="StatusInfoBadge"
434433
HorizontalAlignment="Right"
435434
VerticalAlignment="Top"
436-
Value="{x:Bind OngoingTasksViewModel.InfoBadgeValue, Mode=OneWay}" />
435+
Value="{x:Bind StatusCenterViewModel.InfoBadgeValue, Mode=OneWay}" />
437436

438437
</Grid>
439438

@@ -514,11 +513,11 @@
514513
x:Name="SettingsButton"
515514
AccessKey="I"
516515
AutomationProperties.AutomationId="SettingsButton"
517-
AutomationProperties.Name="{x:Bind ViewModel.Commands.OpenSettings.Label, Mode=OneWay}"
518-
Command="{x:Bind ViewModel.Commands.OpenSettings, Mode=OneWay}"
516+
AutomationProperties.Name="{x:Bind Commands.OpenSettings.Label, Mode=OneWay}"
517+
Command="{x:Bind Commands.OpenSettings, Mode=OneWay}"
519518
Style="{StaticResource AddressToolbarButtonStyle}"
520519
ToolTipService.Placement="Bottom"
521-
ToolTipService.ToolTip="{x:Bind ViewModel.Commands.OpenSettings.LabelWithHotKey, Mode=OneWay}"
520+
ToolTipService.ToolTip="{x:Bind Commands.OpenSettings.LabelWithHotKey, Mode=OneWay}"
522521
Visibility="{x:Bind ShowSettingsButton, Mode=OneWay}">
523522
<AnimatedIcon x:Name="SettingAnimatedIcon" Height="16">
524523
<AnimatedIcon.Source>
@@ -545,13 +544,13 @@
545544
<VisualStateGroup>
546545
<VisualState x:Name="SearchBoxAlwaysShown">
547546
<VisualState.StateTriggers>
548-
<triggers:IsEqualStateTrigger Value="{x:Bind ShowSearchBox, Mode=OneWay}" To="True" />
547+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind ShowSearchBox, Mode=OneWay}" To="True" />
549548
</VisualState.StateTriggers>
550549
</VisualState>
551550

552551
<VisualState x:Name="SearchBoxCollapse">
553552
<VisualState.StateTriggers>
554-
<triggers:IsEqualStateTrigger Value="{x:Bind ShowSearchBox, Mode=OneWay}" To="False" />
553+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind ShowSearchBox, Mode=OneWay}" To="False" />
555554
</VisualState.StateTriggers>
556555
<VisualState.Setters>
557556
<Setter Target="ShowSearchButton.Visibility" Value="Visible" />
@@ -563,43 +562,31 @@
563562
<VisualStateGroup>
564563
<VisualState x:Name="TasksSuccess">
565564
<VisualState.StateTriggers>
566-
<triggers:CompareStateTrigger
567-
Comparison="Equal"
568-
Value="{x:Bind OngoingTasksViewModel.InfoBadgeState, Mode=OneWay}"
569-
To="0" />
565+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind StatusCenterViewModel.InfoBadgeState, Mode=OneWay}" To="0" />
570566
</VisualState.StateTriggers>
571567
<VisualState.Setters>
572568
<Setter Target="StatusInfoBadge.Style" Value="{StaticResource SuccessIconInfoBadgeStyle}" />
573569
</VisualState.Setters>
574570
</VisualState>
575571
<VisualState x:Name="TasksOnging">
576572
<VisualState.StateTriggers>
577-
<triggers:CompareStateTrigger
578-
Comparison="Equal"
579-
Value="{x:Bind OngoingTasksViewModel.InfoBadgeState, Mode=OneWay}"
580-
To="1" />
573+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind StatusCenterViewModel.InfoBadgeState, Mode=OneWay}" To="1" />
581574
</VisualState.StateTriggers>
582575
<VisualState.Setters>
583576
<Setter Target="StatusInfoBadge.Style" Value="{StaticResource AttentionValueInfoBadgeStyle}" />
584577
</VisualState.Setters>
585578
</VisualState>
586579
<VisualState x:Name="TasksFailure">
587580
<VisualState.StateTriggers>
588-
<triggers:CompareStateTrigger
589-
Comparison="Equal"
590-
Value="{x:Bind OngoingTasksViewModel.InfoBadgeState, Mode=OneWay}"
591-
To="2" />
581+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind StatusCenterViewModel.InfoBadgeState, Mode=OneWay}" To="2" />
592582
</VisualState.StateTriggers>
593583
<VisualState.Setters>
594584
<Setter Target="StatusInfoBadge.Style" Value="{StaticResource CriticalValueInfoBadgeStyle}" />
595585
</VisualState.Setters>
596586
</VisualState>
597587
<VisualState x:Name="TasksCompletionWithFailure">
598588
<VisualState.StateTriggers>
599-
<triggers:CompareStateTrigger
600-
Comparison="Equal"
601-
Value="{x:Bind OngoingTasksViewModel.InfoBadgeState, Mode=OneWay}"
602-
To="3" />
589+
<wcttriggers:IsEqualStateTrigger Value="{x:Bind StatusCenterViewModel.InfoBadgeState, Mode=OneWay}" To="3" />
603590
</VisualState.StateTriggers>
604591
<VisualState.Setters>
605592
<Setter Target="StatusInfoBadge.Style" Value="{StaticResource CriticalIconInfoBadgeStyle}" />
@@ -609,4 +596,4 @@
609596
</VisualStateManager.VisualStateGroups>
610597
</Grid>
611598

612-
</UserControl>
599+
</UserControl>

0 commit comments

Comments
 (0)