feat(library): simplify the library page and fit columns to their text
GridColumns widens text columns to the longest entry over all rows, since the lists are virtualized and the built-in auto width only sees visible rows. The page loses its license and attribution columns, gets Enter and Delete on the list, and is in German. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
b8f5a3e469
commit
d7388fbfc3
4 files changed
+121
-29
No files matched your search
@@ -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;
|
||||
|
||||
/// <summary>Sizes the text columns of a list so that every row can be read in full.</summary>
|
||||
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<GridViewColumn, object> 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<string, double> TextWidths = [];
|
||||
|
||||
/// <summary>
|
||||
/// Widens each column with a <see cref="GridViewColumn.DisplayMemberBinding"/> 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.
|
||||
/// </summary>
|
||||
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() ?? "";
|
||||
}
|
||||
}
|
||||
@@ -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">
|
||||
<Grid Margin="24">
|
||||
<Grid.RowDefinitions>
|
||||
@@ -11,26 +13,24 @@
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Title" Text="Library" />
|
||||
|
||||
<DockPanel Grid.Row="1" Margin="0,16,0,12" LastChildFill="True">
|
||||
<DockPanel Grid.Row="1" Margin="0,0,0,12" LastChildFill="True">
|
||||
<ui:Button x:Name="DeleteButton"
|
||||
DockPanel.Dock="Right"
|
||||
Margin="8,0,0,0"
|
||||
Content="Delete"
|
||||
Content="Löschen"
|
||||
Icon="{ui:SymbolIcon Delete24}"
|
||||
IsEnabled="False"
|
||||
Click="OnDelete" />
|
||||
<ui:Button x:Name="EditButton"
|
||||
DockPanel.Dock="Right"
|
||||
Margin="8,0,0,0"
|
||||
Content="Edit"
|
||||
Content="Bearbeiten"
|
||||
Icon="{ui:SymbolIcon Edit24}"
|
||||
IsEnabled="False"
|
||||
Click="OnEdit" />
|
||||
<ui:TextBox x:Name="SearchBox"
|
||||
AutomationProperties.Name="Search songs"
|
||||
PlaceholderText="Search title or artist"
|
||||
AutomationProperties.Name="Songs suchen"
|
||||
PlaceholderText="Titel oder Interpret suchen"
|
||||
Icon="{ui:SymbolIcon Search24}"
|
||||
TextChanged="OnSearchChanged" />
|
||||
</DockPanel>
|
||||
@@ -40,18 +40,17 @@
|
||||
SelectionMode="Single"
|
||||
AutomationProperties.Name="Songs"
|
||||
SelectionChanged="OnSelectionChanged"
|
||||
MouseDoubleClick="OnEdit">
|
||||
MouseDoubleClick="OnEdit"
|
||||
KeyDown="OnListKeyDown">
|
||||
<ui:ListView.View>
|
||||
<ui:GridView>
|
||||
<ui:GridViewColumn Width="260" Header="Title" DisplayMemberBinding="{Binding Title}" />
|
||||
<ui:GridViewColumn Width="200" Header="Artist" DisplayMemberBinding="{Binding Artist}" />
|
||||
<ui:GridViewColumn Width="80" Header="Duration">
|
||||
<ui:GridViewColumn Width="220" Header="Titel" DisplayMemberBinding="{Binding Title}" />
|
||||
<ui:GridViewColumn Width="160" Header="Interpret" DisplayMemberBinding="{Binding Artist}" />
|
||||
<ui:GridViewColumn Width="80" Header="Dauer">
|
||||
<ui:GridViewColumn.DisplayMemberBinding>
|
||||
<Binding Path="Duration" StringFormat="m\:ss" />
|
||||
</ui:GridViewColumn.DisplayMemberBinding>
|
||||
</ui:GridViewColumn>
|
||||
<ui:GridViewColumn Width="140" Header="License" DisplayMemberBinding="{Binding License}" />
|
||||
<ui:GridViewColumn Width="220" Header="Attribution" DisplayMemberBinding="{Binding Attribution}" />
|
||||
</ui:GridView>
|
||||
</ui:ListView.View>
|
||||
</ui:ListView>
|
||||
@@ -61,7 +60,7 @@
|
||||
Margin="0,48,0,0"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Top"
|
||||
Text="No songs found."
|
||||
Foreground="{DynamicResource TextFillColorSecondaryBrush}"
|
||||
Visibility="Collapsed" />
|
||||
</Grid>
|
||||
</Page>
|
||||
@@ -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)
|
||||
|
||||
@@ -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}">
|
||||
<StackPanel>
|
||||
<ui:TitleBar Title="Edit song" ShowMinimize="False" ShowMaximize="False" />
|
||||
<ui:TitleBar Title="Song bearbeiten" ShowMinimize="False" ShowMaximize="False" />
|
||||
|
||||
<StackPanel Margin="24,8,24,24">
|
||||
<Label Content="_Title" Target="{Binding ElementName=TitleBox}" />
|
||||
<Label Content="_Titel" Target="{Binding ElementName=TitleBox}" />
|
||||
<ui:TextBox x:Name="TitleBox" Margin="0,0,0,12" />
|
||||
|
||||
<Label Content="_Artist" Target="{Binding ElementName=ArtistBox}" />
|
||||
<Label Content="_Interpret" Target="{Binding ElementName=ArtistBox}" />
|
||||
<ui:TextBox x:Name="ArtistBox" Margin="0,0,0,12" />
|
||||
|
||||
<Label Content="_License" Target="{Binding ElementName=LicenseBox}" />
|
||||
<ui:TextBox x:Name="LicenseBox" Margin="0,0,0,12" PlaceholderText="Optional, e.g. CC BY 4.0" />
|
||||
<Label Content="_Lizenz" Target="{Binding ElementName=LicenseBox}" />
|
||||
<ui:TextBox x:Name="LicenseBox" Margin="0,0,0,12" PlaceholderText="Optional, z. B. CC BY 4.0" />
|
||||
|
||||
<Label Content="Attri_bution" Target="{Binding ElementName=AttributionBox}" />
|
||||
<Label Content="_Namensnennung" Target="{Binding ElementName=AttributionBox}" />
|
||||
<ui:TextBox x:Name="AttributionBox" PlaceholderText="Optional" />
|
||||
|
||||
<TextBlock x:Name="ErrorText"
|
||||
Margin="0,12,0,0"
|
||||
Foreground="{DynamicResource SystemFillColorCriticalBrush}"
|
||||
Text="Title and artist are required."
|
||||
Text="Titel und Interpret sind erforderlich."
|
||||
Visibility="Collapsed" />
|
||||
|
||||
<StackPanel Margin="0,24,0,0" HorizontalAlignment="Right" Orientation="Horizontal">
|
||||
<ui:Button Content="Save" Appearance="Primary" IsDefault="True" MinWidth="90" Click="OnSave" />
|
||||
<ui:Button Margin="8,0,0,0" Content="Cancel" IsCancel="True" MinWidth="90" />
|
||||
<ui:Button Content="Speichern" Appearance="Primary" IsDefault="True" MinWidth="90" Click="OnSave" />
|
||||
<ui:Button Margin="8,0,0,0" Content="Abbrechen" IsCancel="True" MinWidth="90" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
Reference in new issue
Block a user