Skip to content

Commit

Permalink
style: simplify PlaylistView UI logic (#468)
Browse files Browse the repository at this point in the history
* simplify PlaylistView selection modes

* check if item can be moved up or down for commands

* fix missing condition check

* dont hide ItemMediaTypeIcon when in multi select mode
  • Loading branch information
huynhsontung authored Oct 17, 2024
1 parent e913a42 commit c680bc7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
16 changes: 12 additions & 4 deletions Screenbox.Core/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ partial void OnEnableMultiSelectChanged(bool value)

private static bool HasSelection(IList<object>? selectedItems) => selectedItems?.Count > 0;

private bool IsSelectedItemNotFirst(IList<object>? selectedItems) =>
selectedItems?.Count == 1 &&
Playlist.Items.Count > 0 && Playlist.Items[0] != selectedItems[0];

private bool IsSelectedItemNotLast(IList<object>? selectedItems) =>
selectedItems?.Count == 1 &&
Playlist.Items.Count > 0 && Playlist.Items[Playlist.Items.Count - 1] != selectedItems[0];

private bool IsItemNotFirst(MediaViewModel item) => Playlist.Items.Count > 0 && Playlist.Items[0] != item;

private bool IsItemNotLast(MediaViewModel item) => Playlist.Items.Count > 0 && Playlist.Items[Playlist.Items.Count - 1] != item;
Expand Down Expand Up @@ -134,10 +142,10 @@ private void PlayNext(MediaViewModel item)
Playlist.Items.Insert(Playlist.CurrentIndex + 1, new MediaViewModel(item));
}

[RelayCommand(CanExecute = nameof(HasSelection))]
[RelayCommand(CanExecute = nameof(IsSelectedItemNotFirst))]
private void MoveSelectedItemUp(IList<object>? selectedItems)
{
if (selectedItems == null || selectedItems.Count != 1) return;
if (selectedItems is not { Count: 1 }) return;
MediaViewModel item = (MediaViewModel)selectedItems[0];
MoveItemUp(item);

Expand All @@ -156,10 +164,10 @@ private void MoveItemUp(MediaViewModel item)
Playlist.Items.Insert(index - 1, item);
}

[RelayCommand(CanExecute = nameof(HasSelection))]
[RelayCommand(CanExecute = nameof(IsSelectedItemNotLast))]
private void MoveSelectedItemDown(IList<object>? selectedItems)
{
if (selectedItems == null || selectedItems.Count != 1) return;
if (selectedItems is not { Count: 1 }) return;
MediaViewModel item = (MediaViewModel)selectedItems[0];
MoveItemDown(item);

Expand Down
2 changes: 0 additions & 2 deletions Screenbox/Controls/MediaListViewItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,12 @@
<VisualState.Setters>
<Setter Target="RootGridTranslateTransform.X" Value="0" />
<Setter Target="PlayingIndicator.Opacity" Value="1" />
<Setter Target="ItemMediaTypeIcon.Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="MultiSelectEnabled">
<VisualState.Setters>
<Setter Target="RootGridTranslateTransform.X" Value="-32" />
<Setter Target="PlayingIndicator.Opacity" Value="0" />
<Setter Target="ItemMediaTypeIcon.Opacity" Value="0" />
</VisualState.Setters>
</VisualState>

Expand Down
40 changes: 12 additions & 28 deletions Screenbox/Controls/PlaylistView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:commands="using:Screenbox.Commands"
xmlns:controls="using:Screenbox.Controls"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:ctAnimations="using:CommunityToolkit.WinUI.Animations"
xmlns:ctConverters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down Expand Up @@ -206,23 +205,18 @@
<KeyboardAccelerator Key="Delete" />
</AppBarButton.KeyboardAccelerators>
</AppBarButton>

<CommandBar.SecondaryCommands>
<AppBarButton
x:Name="MoveUpButton"
Command="{x:Bind ViewModel.MoveSelectedItemUpCommand}"
CommandParameter="{x:Bind PlaylistListView.SelectedItems}"
Icon="{ui:FontIcon Glyph=&#xE74A;}"
IsEnabled="False"
Label="{strings:Resources Key=MoveUp}" />
<AppBarButton
x:Name="MoveDownButton"
Command="{x:Bind ViewModel.MoveSelectedItemDownCommand}"
CommandParameter="{x:Bind PlaylistListView.SelectedItems}"
Icon="{ui:FontIcon Glyph=&#xE74B;}"
IsEnabled="False"
Label="{strings:Resources Key=MoveDown}" />
</CommandBar.SecondaryCommands>
<AppBarButton
x:Name="MoveUpButton"
Command="{x:Bind ViewModel.MoveSelectedItemUpCommand}"
CommandParameter="{x:Bind PlaylistListView.SelectedItems}"
Icon="{ui:FontIcon Glyph=&#xE74A;}"
Label="{strings:Resources Key=MoveUp}" />
<AppBarButton
x:Name="MoveDownButton"
Command="{x:Bind ViewModel.MoveSelectedItemDownCommand}"
CommandParameter="{x:Bind PlaylistListView.SelectedItems}"
Icon="{ui:FontIcon Glyph=&#xE74B;}"
Label="{strings:Resources Key=MoveDown}" />

<CommandBar.Content>
<StackPanel Orientation="Horizontal">
Expand Down Expand Up @@ -351,16 +345,6 @@
<Setter Target="PlaylistListView.SelectionMode" Value="Multiple" />
</VisualState.Setters>
</VisualState>

<VisualState x:Name="MultipleSingleSelected">
<VisualState.Setters>
<Setter Target="SelectionCommandRow.Visibility" Value="Visible" />
<Setter Target="ActionButtonRow.Visibility" Value="Collapsed" />
<Setter Target="PlaylistListView.SelectionMode" Value="Multiple" />
<Setter Target="MoveUpButton.IsEnabled" Value="True" />
<Setter Target="MoveDownButton.IsEnabled" Value="True" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>

<VisualStateGroup x:Name="LayoutGroup">
Expand Down
3 changes: 1 addition & 2 deletions Screenbox/Controls/PlaylistView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ private void PlaylistListView_OnSelectionChanged(object sender, SelectionChanged
SelectionCheckBox.IsChecked = PlaylistListView.SelectedItems.Count == ViewModel.Playlist.Items.Count;
if (ViewModel.EnableMultiSelect)
{
VisualStateManager.GoToState(this,
PlaylistListView.SelectedItems.Count == 1 ? "MultipleSingleSelected" : "Multiple", true);
VisualStateManager.GoToState(this, "Multiple", true);
}

ViewModel.SelectionCount = PlaylistListView.SelectedItems.Count;
Expand Down

0 comments on commit c680bc7

Please sign in to comment.