diff --git a/src/Radiosender/Views/GridColumns.cs b/src/Radiosender/Views/GridColumns.cs new file mode 100644 index 0000000..095ffec --- /dev/null +++ b/src/Radiosender/Views/GridColumns.cs @@ -0,0 +1,83 @@ +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Media; + +namespace Radiosender.Views; + +/// Sizes the text columns of a list so that every row can be read in full. +public static class GridColumns +{ + // Cell padding on both sides plus a little air, so the text never touches the next column. + private const double Slack = 28; + + // Rows use this size when no row exists yet to ask. + private const double DefaultFontSize = 14; + + // The width from the XAML stays the minimum, so an empty or short list keeps its designed look. + private static readonly ConditionalWeakTable MinWidths = []; + + // Laying out text is the slow part and the same titles come up on every refresh, so widths are remembered. + // All lists share one font. shortcut: never trimmed; clear it if libraries reach hundreds of thousands of songs. + private static readonly Dictionary TextWidths = []; + + /// + /// Widens each column with a to its longest text over + /// all rows, not only the visible ones: the lists are virtualized, so the built-in auto width would miss + /// rows that are scrolled out of view. Columns with a cell template keep their width, and so do columns + /// with a string format: those show times, which always have the same length. + /// + public static void Fit(ListView list) + { + if (list.View is not GridView view) + return; + + // The rows have their own font size, so a realized row is asked rather than the list. + var row = list.ItemContainerGenerator.ContainerFromIndex(0) as Control; + var typeface = new Typeface(row?.FontFamily ?? list.FontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); + var fontSize = row?.FontSize ?? DefaultFontSize; + var pixelsPerDip = VisualTreeHelper.GetDpi(list).PixelsPerDip; + + foreach (var column in view.Columns) + { + if (column.DisplayMemberBinding is not Binding { StringFormat: null, Path.Path: { } path }) + continue; + var min = (double)MinWidths.GetValue(column, c => double.IsNaN(c.Width) ? 0d : c.Width); + var properties = path.Split('.'); + + var widest = 0d; + foreach (var item in list.Items) + { + var text = Text(item, properties); + if (text.Length == 0) + continue; + if (!TextWidths.TryGetValue(text, out var width)) + { + width = new FormattedText(text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, + typeface, fontSize, Brushes.Black, pixelsPerDip).WidthIncludingTrailingWhitespace; + TextWidths[text] = width; + } + widest = Math.Max(widest, width); + } + + var fitted = Math.Max(min, Math.Ceiling(widest + Slack)); + if (column.Width != fitted) // setting it triggers a layout pass + column.Width = fitted; + } + } + + // Follows a simple property path such as "Item.Title". A binding on a probe element would not do: + // it only resolves once the element is part of the window. + private static string Text(object? value, string[] properties) + { + foreach (var property in properties) + { + value = value?.GetType().GetProperty(property)?.GetValue(value); + if (value is null) + return ""; + } + return value?.ToString() ?? ""; + } +} diff --git a/src/Radiosender/Views/Pages/LibraryPage.xaml b/src/Radiosender/Views/Pages/LibraryPage.xaml index dcf4e56..8b38fa6 100644 --- a/src/Radiosender/Views/Pages/LibraryPage.xaml +++ b/src/Radiosender/Views/Pages/LibraryPage.xaml @@ -2,7 +2,9 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" - Title="Library" + Title="Bibliothek" + Foreground="{DynamicResource TextFillColorPrimaryBrush}" + ScrollViewer.CanContentScroll="False" Loaded="OnLoaded"> @@ -11,26 +13,24 @@ - - - + @@ -40,18 +40,17 @@ SelectionMode="Single" AutomationProperties.Name="Songs" SelectionChanged="OnSelectionChanged" - MouseDoubleClick="OnEdit"> + MouseDoubleClick="OnEdit" + KeyDown="OnListKeyDown"> - - - + + + - - @@ -61,7 +60,7 @@ Margin="0,48,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" - Text="No songs found." + Foreground="{DynamicResource TextFillColorSecondaryBrush}" Visibility="Collapsed" /> diff --git a/src/Radiosender/Views/Pages/LibraryPage.xaml.cs b/src/Radiosender/Views/Pages/LibraryPage.xaml.cs index 43a7857..ff45ae7 100644 --- a/src/Radiosender/Views/Pages/LibraryPage.xaml.cs +++ b/src/Radiosender/Views/Pages/LibraryPage.xaml.cs @@ -27,9 +27,19 @@ public partial class LibraryPage : Page { var songs = _controller.Search(SearchBox.Text); SongList.ItemsSource = songs; + GridColumns.Fit(SongList); + EmptyText.Text = string.IsNullOrWhiteSpace(SearchBox.Text) + ? "Noch keine Songs. Musik auf der Seite Import hinzufügen." + : "Kein Song passt zur Suche."; EmptyText.Visibility = songs.Count == 0 ? Visibility.Visible : Visibility.Collapsed; } + private void OnListKeyDown(object sender, System.Windows.Input.KeyEventArgs e) + { + if (e.Key == System.Windows.Input.Key.Enter) OnEdit(sender, e); + else if (e.Key == System.Windows.Input.Key.Delete) OnDelete(sender, e); + } + private void OnEdit(object sender, RoutedEventArgs e) { if (Selected is not { } song) @@ -48,10 +58,10 @@ public partial class LibraryPage : Page var result = await new MessageBox { - Title = "Delete song", - Content = $"Delete \"{song.Title}\" by {song.Artist} from the library?", - PrimaryButtonText = "Delete", - CloseButtonText = "Cancel", + Title = "Song löschen", + Content = $"\"{song.Title}\" von {song.Artist} aus der Bibliothek löschen?", + PrimaryButtonText = "Löschen", + CloseButtonText = "Abbrechen", }.ShowDialogAsync(); if (result != Wpf.Ui.Controls.MessageBoxResult.Primary) diff --git a/src/Radiosender/Views/SongEditWindow.xaml b/src/Radiosender/Views/SongEditWindow.xaml index f0537ea..c842a6a 100644 --- a/src/Radiosender/Views/SongEditWindow.xaml +++ b/src/Radiosender/Views/SongEditWindow.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" - Title="Edit song" + Title="Song bearbeiten" Width="460" SizeToContent="Height" ResizeMode="NoResize" ShowInTaskbar="False" @@ -11,30 +11,30 @@ WindowStartupLocation="CenterOwner" FocusManager.FocusedElement="{Binding ElementName=TitleBox}"> - + -