Add ViewModels for ActionBar

This commit is contained in:
Kristen Schau
2024-09-18 17:40:52 -04:00
parent b7a4706e49
commit 95c880ca21
4 changed files with 69 additions and 24 deletions

View File

@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Mvvm.ComponentModel;
namespace Microsoft.CmdPal.UI.ViewModels;
public partial class ActionBarContextItemViewModel : ObservableObject
{
////private ICommand _command;
[ObservableProperty]
private string _name = "Placeholder"; // Command.Name;
////private IconDataType Icon => Command.Icon;
[ObservableProperty]
private bool _canInvoke = true; // Command != null && Command is IInvokableCommand or IPage;
// TODO: do we want the icon here or get it over in the UI project?
////[ObservableProperty]
////private IconElement IcoElement => Microsoft.Terminal.UI.IconPathConverter.IconMUX(Icon.Icon);
public ActionBarContextItemViewModel(string name, bool canInvoke)
{
Name = name;
CanInvoke = canInvoke;
}
}

View File

@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Microsoft.CmdPal.UI.ViewModels;
public partial class ActionBarViewModel : ObservableObject
{
[ObservableProperty]
private string _actionName = string.Empty;
[ObservableProperty]
private bool _moreCommandsAvailable = false;
[ObservableProperty]
private ObservableCollection<ActionBarContextItemViewModel> _contextActions = [];
public ActionBarViewModel()
{
// Just for fun
ActionName = "My Action";
MoreCommandsAvailable = true;
ContextActions.Add(new ActionBarContextItemViewModel("Action1", true));
ContextActions.Add(new ActionBarContextItemViewModel("Action2", true));
}
}

View File

@@ -7,7 +7,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
Loaded="ActionBar_Loaded"
xmlns:viewmodels="using:Microsoft.CmdPal.UI.ViewModels"
mc:Ignorable="d">
<UserControl.Resources>
@@ -15,7 +15,7 @@
<converters:StringVisibilityConverter x:Key="StringNotEmptyToVisibilityConverter" EmptyValue="Collapsed" NotEmptyValue="Visible" />
<!-- Template for actions in the mode actions dropdown button -->
<DataTemplate x:Key="ContextMenuViewModelTemplate">
<DataTemplate x:Key="ContextMenuViewModelTemplate" x:DataType="viewmodels:ActionBarContextItemViewModel">
<ListViewItem KeyDown="ActionListViewItem_KeyDown" Tapped="ActionListViewItem_Tapped">
<Grid ColumnSpacing="8">
<Grid.ColumnDefinitions>
@@ -24,6 +24,7 @@
</Grid.ColumnDefinitions>
<Viewbox Width="16" Height="16">
<!-- TODO bind to icon -->
<ContentControl
Grid.Column="0"
Width="24"
@@ -31,7 +32,7 @@
<SymbolIcon Symbol="Emoji" />
</ContentControl>
</Viewbox>
<TextBlock Grid.Column="1" Text="Placeholder" />
<TextBlock Grid.Column="1" Text="{x:Bind Name}" />
</Grid>
</ListViewItem>
</DataTemplate>
@@ -70,12 +71,12 @@
BorderThickness="1"
CornerRadius="4"
Orientation="Horizontal"
Visibility="{x:Bind ActionName, Converter={StaticResource StringNotEmptyToVisibilityConverter}, Mode=OneWay}">
Visibility="{x:Bind ViewModel.ActionName, Converter={StaticResource StringNotEmptyToVisibilityConverter}, Mode=OneWay}">
<TextBlock
FontSize="11"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind ActionName, Mode=OneWay}" />
Text="{x:Bind ViewModel.ActionName, Mode=OneWay}" />
<FontIcon
Margin="4,0,0,0"
VerticalAlignment="Center"
@@ -91,14 +92,14 @@
VerticalAlignment="Center"
Content="Actions"
FontSize="12"
Visibility="{x:Bind MoreCommandsAvailable, Mode=OneWay}">
Visibility="{x:Bind ViewModel.MoreCommandsAvailable, Mode=OneWay}">
<SplitButton.Flyout>
<Flyout Placement="TopEdgeAlignedRight">
<ListView
x:Name="ActionsDropdown"
Margin="-12"
ItemTemplate="{StaticResource ContextMenuViewModelTemplate}"
ItemsSource="{x:Bind _contextActions, Mode=OneWay}"
ItemsSource="{x:Bind ViewModel.ContextActions, Mode=OneWay}"
Style="{StaticResource NoAnimationsPlease}" />
</Flyout>
</SplitButton.Flyout>

View File

@@ -2,23 +2,15 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.CmdPal.UI.ViewModels;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace Microsoft.CmdPal.UI.Controls;
[ObservableObject]
public sealed partial class ActionBar : UserControl
{
[ObservableProperty]
private string _actionName = string.Empty;
[ObservableProperty]
private bool _moreCommandsAvailable = false;
private ObservableCollection<string> _contextActions = [];
public ActionBarViewModel ViewModel { get; set; } = new();
public ActionBar()
{
@@ -34,11 +26,4 @@ public sealed partial class ActionBar : UserControl
{
// TODO
}
private void ActionBar_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
// TODO Just in here for testing
ActionName = "My Action";
MoreCommandsAvailable = true;
}
}