New feature #406 : Right Click Menu for Stop-Light Icon (Chardonnay UI)

This commit is contained in:
Robert McRackan 2022-12-13 15:32:33 -05:00
parent d1528a095b
commit 5ca0d2a399
6 changed files with 131 additions and 61 deletions

View File

@ -1,4 +1,5 @@
using Avalonia.Media;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using System;
using System.Threading;
@ -16,5 +17,23 @@ namespace LibationAvalonia
return brush;
return defaultBrush;
}
public static Window GetParentWindow(this IControl control)
{
Window window = null;
var p = control.Parent;
while (p != null)
{
if (p is Window)
{
window = (Window)p;
break;
}
p = p.Parent;
}
return window;
}
}
}

View File

@ -26,7 +26,7 @@ namespace LibationAvalonia.ViewModels
[Browsable(false)] public string LongDescription { get; protected set; }
[Browsable(false)] public abstract DateTime DateAdded { get; }
[Browsable(false)] public int ListIndex { get; set; }
[Browsable(false)] protected Book Book => LibraryBook.Book;
[Browsable(false)] public Book Book => LibraryBook.Book;
#region Model properties exposed to the view

View File

@ -53,7 +53,7 @@ namespace LibationAvalonia.ViewModels
/// <summary>
/// Call when there's been a change to the library
/// </summary>
public async Task DisplayBooks(List<LibraryBook> dbBooks)
public async Task DisplayBooksAsync(List<LibraryBook> dbBooks)
{
try
{

View File

@ -54,7 +54,7 @@ namespace LibationAvalonia.Views
{
this.LibraryLoaded += MainWindow_LibraryLoaded;
LibraryCommands.LibrarySizeChanged += async (_, _) => await _viewModel.ProductsDisplay.DisplayBooks(DbContexts.GetLibrary_Flat_NoTracking(includeParents: true));
LibraryCommands.LibrarySizeChanged += async (_, _) => await _viewModel.ProductsDisplay.DisplayBooksAsync(DbContexts.GetLibrary_Flat_NoTracking(includeParents: true));
Closing += (_, _) => this.SaveSizeAndLocation(Configuration.Instance);
}
Opened += MainWindow_Opened;
@ -178,7 +178,7 @@ namespace LibationAvalonia.Views
if (QuickFilters.UseDefault)
await performFilter(QuickFilters.Filters.FirstOrDefault());
await _viewModel.ProductsDisplay.DisplayBooks(dbBooks);
await _viewModel.ProductsDisplay.DisplayBooksAsync(dbBooks);
}
private void InitializeComponent()

View File

@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ApplicationServices;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using DataLayer;
using FileLiberator;
using LibationFileManager;
using LibationAvalonia.ViewModels;
using LibationAvalonia.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using LibationAvalonia.Controls;
using LibationAvalonia.Dialogs;
using LibationAvalonia.ViewModels;
using LibationFileManager;
namespace LibationAvalonia.Views
{
@ -41,7 +42,7 @@ namespace LibationAvalonia.Views
};
var pdvm = new ProductsDisplayViewModel();
pdvm.DisplayBooks(sampleEntries);
pdvm.DisplayBooksAsync(sampleEntries);
DataContext = pdvm;
return;
@ -74,11 +75,64 @@ namespace LibationAvalonia.Views
#region Cell Context Menu
public void ProductsGrid_CellContextMenuStripNeeded(object sender, DataGridCellContextMenuStripNeededEventArgs args)
{
if (args.Column.SortMemberPath == "Liberate")
{
// stop light
if (args.Column.SortMemberPath == "Liberate")
{
var entry = args.GridEntry;
}
if (entry.IsSeries)
return;
var setDownloadMenuItem = new MenuItem()
{
Header = "_Set Download status to 'Downloaded'",
IsEnabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.Liberated
};
setDownloadMenuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.Liberated);
var setNotDownloadMenuItem = new MenuItem()
{
Header = "_Set Download status to 'Not Downloaded'",
IsEnabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.NotLiberated
};
setNotDownloadMenuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.NotLiberated);
var removeMenuItem = new MenuItem() { Header = "_Remove from library" };
removeMenuItem.Click += (_, __) => LibraryCommands.RemoveBook(entry.AudibleProductId);
var locateFileMenuItem = new MenuItem() { Header = "_Locate file..." };
locateFileMenuItem.Click += async (_, __) =>
{
try
{
var openFileDialog = new OpenFileDialog()
{
Title = $"Locate the audio file for '{entry.Book.Title}'",
Filters = new() { new() { Name = "All files (*.*)", Extensions = new() { "|*.*" } } },
AllowMultiple= false
};
var filePaths = await openFileDialog.ShowAsync(this.GetParentWindow());
var filePath = filePaths.SingleOrDefault();
if (!string.IsNullOrWhiteSpace(filePath))
FilePathCache.Insert(entry.AudibleProductId, filePath);
}
catch (Exception ex)
{
var msg = "Error saving book's location";
await MessageBox.ShowAdminAlert(null, msg, msg, ex);
}
};
args.ContextMenuItems.AddRange(new[]
{
setDownloadMenuItem,
setNotDownloadMenuItem,
removeMenuItem,
locateFileMenuItem
});
}
else
{
// any non-stop light column

View File

@ -136,55 +136,52 @@ namespace LibationWinForms.GridView
if (entry.IsSeries)
return;
var stopLightContextMenu = new ContextMenuStrip();
e.ContextMenuStrip = stopLightContextMenu;
var setDownloadMenuItem = new ToolStripMenuItem()
{
var menuItem = new ToolStripMenuItem()
{
Text = "Set Download status to 'Downloaded'",
Enabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.Liberated
};
menuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.Liberated);
stopLightContextMenu.Items.Add(menuItem);
}
Text = "Set Download status to 'Downloaded'",
Enabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.Liberated
};
setDownloadMenuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.Liberated);
var setNotDownloadMenuItem = new ToolStripMenuItem()
{
var menuItem = new ToolStripMenuItem()
Text = "Set Download status to 'Not Downloaded'",
Enabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.NotLiberated
};
setNotDownloadMenuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.NotLiberated);
var removeMenuItem = new ToolStripMenuItem() { Text = "Remove from library" };
removeMenuItem.Click += (_, __) => LibraryCommands.RemoveBook(entry.AudibleProductId);
var locateFileMenuItem = new ToolStripMenuItem() { Text = "Locate file..." };
locateFileMenuItem.Click += (_, __) =>
{
try
{
Text = "Set Download status to 'Not Downloaded'",
Enabled = entry.Book.UserDefinedItem.BookStatus != LiberatedStatus.NotLiberated
};
menuItem.Click += (_, __) => entry.Book.UpdateBookStatus(LiberatedStatus.NotLiberated);
stopLightContextMenu.Items.Add(menuItem);
}
{
var menuItem = new ToolStripMenuItem() { Text = "Remove from library" };
menuItem.Click += (_, __) => LibraryCommands.RemoveBook(entry.AudibleProductId);
stopLightContextMenu.Items.Add(menuItem);
}
{
var menuItem = new ToolStripMenuItem() { Text = "Locate file..." };
menuItem.Click += (_, __) =>
{
try
{
var openFileDialog = new OpenFileDialog
{
Title = $"Locate the audiofile for '{entry.Book.Title}'",
Filter = "All files (*.*)|*.*",
FilterIndex = 1
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
FilePathCache.Insert(entry.AudibleProductId, openFileDialog.FileName);
}
catch (Exception ex)
{
var msg = "Error saving book's location";
MessageBoxLib.ShowAdminAlert(this, msg, msg, ex);
}
};
stopLightContextMenu.Items.Add(menuItem);
}
}
var openFileDialog = new OpenFileDialog
{
Title = $"Locate the audio file for '{entry.Book.Title}'",
Filter = "All files (*.*)|*.*",
FilterIndex = 1
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
FilePathCache.Insert(entry.AudibleProductId, openFileDialog.FileName);
}
catch (Exception ex)
{
var msg = "Error saving book's location";
MessageBoxLib.ShowAdminAlert(this, msg, msg, ex);
}
};
var stopLightContextMenu = new ContextMenuStrip();
stopLightContextMenu.Items.Add(setDownloadMenuItem);
stopLightContextMenu.Items.Add(setNotDownloadMenuItem);
stopLightContextMenu.Items.Add(removeMenuItem);
stopLightContextMenu.Items.Add(locateFileMenuItem);
e.ContextMenuStrip = stopLightContextMenu;
}
private GridEntry getGridEntry(int rowIndex) => gridEntryDataGridView.GetBoundItem<GridEntry>(rowIndex);