Skip to content

Commit

Permalink
Add Cancel, Error, Remove, and Open Folder items to task queue contex…
Browse files Browse the repository at this point in the history
…t menu
  • Loading branch information
ScrubN committed Jan 5, 2024
1 parent a3477ff commit 0eea513
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
24 changes: 24 additions & 0 deletions TwitchDownloaderWPF/PageQueue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" CornerRadius="8" Margin="8,8,8,8" Padding="4" Background="{DynamicResource AppElementBackground}" BorderBrush="{DynamicResource AppElementBorder}">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Header="Cancel" Click="MenuItemCancelTask_Click" IsEnabled="{Binding CanCancel}" Foreground="{DynamicResource AppText}">
<MenuItem.Icon>
<fa:SvgAwesome Icon="Solid_StopCircle" Foreground="{DynamicResource AppText}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Error" Click="MenuItemTaskError_Click" Visibility="{Binding Exception.Visibility}" Foreground="{DynamicResource AppText}">
<MenuItem.Icon>
<fa:SvgAwesome Icon="Solid_ExclamationTriangle" Foreground="{DynamicResource AppText}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Remove" Click="MenuItemRemoveTask_Click" Foreground="{DynamicResource AppText}">
<MenuItem.Icon>
<fa:SvgAwesome Icon="Solid_Times" Foreground="{DynamicResource AppText}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Open folder" Click="MenuItemOpenTaskFolder_Click" Foreground="{DynamicResource AppText}">
<MenuItem.Icon>
<fa:SvgAwesome Icon="Solid_FolderOpen" Foreground="{DynamicResource AppText}" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Border.ContextMenu>
<Grid Margin="1,1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
Expand Down
68 changes: 63 additions & 5 deletions TwitchDownloaderWPF/PageQueue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TwitchDownloaderWPF.TwitchTasks;
using TwitchDownloaderWPF.Properties;
using System.Diagnostics;
using System.IO;

namespace TwitchDownloaderWPF
{
Expand Down Expand Up @@ -206,14 +207,25 @@ private void BtnCancelTask_Click(object sender, RoutedEventArgs e)
return;
}

cancelButton.IsEnabled = false;
CancelTask(task);
}

if (task.Status is TwitchTaskStatus.Failed or TwitchTaskStatus.Canceled or TwitchTaskStatus.Finished)
private void MenuItemCancelTask_Click(object sender, RoutedEventArgs e)
{
if (sender is not MenuItem { DataContext: ITwitchTask task })
{
return;
}

task.Cancel();
CancelTask(task);
}

private static void CancelTask(ITwitchTask task)
{
if (task.CanCancel)
{
task.Cancel();
}
}

private void BtnTaskError_Click(object sender, RoutedEventArgs e)
Expand All @@ -223,14 +235,29 @@ private void BtnTaskError_Click(object sender, RoutedEventArgs e)
return;
}

TwitchTaskException taskException = task.Exception;
ShowTaskException(task);
}

private void MenuItemTaskError_Click(object sender, RoutedEventArgs e)
{
if (sender is not MenuItem { DataContext: ITwitchTask task })
{
return;
}

ShowTaskException(task);
}

private static void ShowTaskException(ITwitchTask task)
{
var taskException = task.Exception;

if (taskException?.Exception == null)
{
return;
}

string errorMessage = taskException.Exception.Message;
var errorMessage = taskException.Exception.Message;
if (Settings.Default.VerboseErrors)
{
errorMessage = taskException.Exception.ToString();
Expand All @@ -246,6 +273,21 @@ private void BtnRemoveTask_Click(object sender, RoutedEventArgs e)
return;
}

RemoveTask(task);
}

private void MenuItemRemoveTask_Click(object sender, RoutedEventArgs e)
{
if (sender is not MenuItem { DataContext: ITwitchTask task })
{
return;
}

RemoveTask(task);
}

private static void RemoveTask(ITwitchTask task)
{
if (task.CanRun() || task.Status is TwitchTaskStatus.Running or TwitchTaskStatus.Waiting)
{
MessageBox.Show(Translations.Strings.CancelTaskBeforeRemoving, Translations.Strings.TaskCouldNotBeRemoved, MessageBoxButton.OK, MessageBoxImage.Information);
Expand All @@ -257,5 +299,21 @@ private void BtnRemoveTask_Click(object sender, RoutedEventArgs e)
MessageBox.Show(Translations.Strings.TaskCouldNotBeRemoved, Translations.Strings.UnknownErrorOccurred, MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void MenuItemOpenTaskFolder_Click(object sender, RoutedEventArgs e)
{
if (sender is not MenuItem { DataContext: ITwitchTask task })
{
return;
}

var outputFolder = Path.GetDirectoryName(task.OutputFile);
if (!Directory.Exists(outputFolder))
{
return;
}

Process.Start(new ProcessStartInfo(outputFolder) { UseShellExecute = true });
}
}
}

0 comments on commit 0eea513

Please sign in to comment.