Skip to content

Commit

Permalink
Adding delete and refresh actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Dec 23, 2021
1 parent b1576f5 commit faa4e1d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
13 changes: 13 additions & 0 deletions ColorKraken/CommandMixins.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Windows;

using Microsoft.Toolkit.Mvvm.Input;

namespace ColorKraken;

public static class CommandMixins
{
public static void RaiseCanExecuteChanged(this IRelayCommand command)
{
Application.Current.Dispatcher.Invoke(() => command.NotifyCanExecuteChanged());
}
}
42 changes: 28 additions & 14 deletions ColorKraken/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
</Grid.RowDefinitions>

<TextBlock Text="Enter the name for the new theme" />
<TextBox Text="{Binding Name}" materialDesign:HintAssist.Hint="Name" Grid.Row="1" />
<TextBox Text="{Binding Name}" materialDesign:HintAssist.Hint="Name" Grid.Row="1"
Margin="0,10"/>

<ComboBox materialDesign:HintAssist.Hint="Based on theme"
ItemsSource="{Binding Themes}"
SelectedItem="{Binding SelectedTheme}"
SelectedItem="{Binding SelectedTheme}"
Margin="0,5"
Grid.Row="2">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Theme}">
Expand Down Expand Up @@ -90,23 +92,35 @@
</Grid.RowDefinitions>

<materialDesign:ColorZone Mode="PrimaryLight">
<StackPanel Orientation="Horizontal">
<Button Content="{materialDesign:PackIcon Kind=FolderOpen}"
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="{materialDesign:PackIcon Kind=FolderOpen}"
Command="{Binding OpenThemeFolderCommand}"
ToolTip="Open theme folder"/>

<ComboBox ItemsSource="{Binding Themes}"
<ComboBox ItemsSource="{Binding Themes}"
SelectedItem="{Binding SelectedTheme}"
MinWidth="200" Margin="10,3">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Theme}">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="_New" Command="{Binding NewThemeCommand}"
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Theme}">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="_New" Command="{Binding NewThemeCommand}"
VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<Button Content="{materialDesign:PackIcon Kind=Delete}" Command="{Binding DeleteCommand}"
ToolTip="Delete current theme"/>
<Button Content="{materialDesign:PackIcon Kind=Refresh}" Command="{Binding RefreshCommand}"
ToolTip="Reload all themes" Margin="5,0"/>
</StackPanel>
</Grid>
</materialDesign:ColorZone>

<ScrollViewer Grid.Row="1">
Expand All @@ -124,7 +138,7 @@
</Style.Triggers>
</Style>
</ItemsControl.Style>

<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:ThemeCategory}">
<GroupBox Header="{Binding Name}" HorizontalContentAlignment="Stretch" Margin="1,2">
Expand Down
31 changes: 31 additions & 0 deletions ColorKraken/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class MainWindowViewModel : ObservableObject, IRecipient<BrushUpdated>
public SnackbarMessageQueue MessageQueue { get; } = new();

public AsyncRelayCommand NewThemeCommand { get; }
public IRelayCommand DeleteCommand { get; }
public IRelayCommand RefreshCommand { get; }
public IRelayCommand OpenThemeFolderCommand { get; }

public ObservableCollection<Theme> Themes { get; } = new();
Expand All @@ -59,6 +61,7 @@ public Theme? SelectedTheme
{
if (SetProperty(ref _selectedTheme, value))
{
DeleteCommand.RaiseCanExecuteChanged();
UndoStack.Clear();
Task.Run(async () =>
{
Expand All @@ -74,6 +77,8 @@ public MainWindowViewModel()

NewThemeCommand = new AsyncRelayCommand(NewTheme);
OpenThemeFolderCommand = new RelayCommand(OnOpenThemeFolder);
DeleteCommand = new RelayCommand(OnDelete, () => SelectedTheme != null && SelectedTheme.IsDefault == false);
RefreshCommand = new AsyncRelayCommand(OnRefresh);

BindingOperations.EnableCollectionSynchronization(Themes, new object());

Expand Down Expand Up @@ -342,6 +347,32 @@ private async void OnShowErrorDetails(string? details)
if (string.IsNullOrEmpty(details)) return;
await DialogHost.Show(new ErrorDetailsViewModel(details), "Root");
}

private void OnDelete()
{
//TODO: prompt
if (SelectedTheme is { } selectedTheme && selectedTheme.IsDefault == false)
{
try
{
File.Delete(selectedTheme.FilePath);
}
catch(Exception e)
{
ShowError($"Error deleting {Path.GetFileName(selectedTheme.FilePath)}", e.ToString());
return;
}
SelectedTheme = null;
Themes.Remove(selectedTheme);
}
}

public async Task OnRefresh()
{
SelectedTheme = null;
Themes.Clear();
await Task.Run(LoadThemes);
}
}

public record class ErrorDetailsViewModel(string Details) { }

0 comments on commit faa4e1d

Please sign in to comment.