From ee4cebafd0c00a31cf4c8a0a2bc90ca83fb309b3 Mon Sep 17 00:00:00 2001 From: AleGrz Date: Mon, 18 Dec 2023 20:33:33 +0100 Subject: [PATCH] feat: added trimming to song title and artist name --- .../Components/MusicComponentViewModel.cs | 18 +++++++++++++----- .../UserConfigs/MusicComponentConfig.cs | 8 ++++++++ README.md | 2 ++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/GlazeWM.Bar/Components/MusicComponentViewModel.cs b/GlazeWM.Bar/Components/MusicComponentViewModel.cs index 39bc76c76..77b0ea1f3 100644 --- a/GlazeWM.Bar/Components/MusicComponentViewModel.cs +++ b/GlazeWM.Bar/Components/MusicComponentViewModel.cs @@ -91,17 +91,19 @@ private LabelViewModel CreateLabel() foreach (var session in songSessionDict) { - if(GetLabel(session.Value.MusicStatus) == _config.LabelPaused && label != _config.LabelPlaying) + var title = Truncate(session.Value.SongTitle, _config.MaxTitleLength); + var artist = Truncate(session.Value.ArtistName, _config.MaxArtistLength); + if (GetLabel(session.Value.MusicStatus) == _config.LabelPaused && label != _config.LabelPlaying) { label = _config.LabelPaused; - variableDictionary["song_title"] = () => session.Value.SongTitle; - variableDictionary["artist_name"] = () => session.Value.ArtistName; + variableDictionary["song_title"] = () => title; + variableDictionary["artist_name"] = () => artist; } else if (GetLabel(session.Value.MusicStatus) == _config.LabelPlaying) { label = _config.LabelPlaying; - variableDictionary["song_title"] = () => session.Value.SongTitle; - variableDictionary["artist_name"] = () => session.Value.ArtistName; + variableDictionary["song_title"] = () => title; + variableDictionary["artist_name"] = () => artist; } } return XamlHelper.ParseLabel( @@ -110,5 +112,11 @@ private LabelViewModel CreateLabel() this ); } + public static string Truncate(string value, int maxLength, string truncationSuffix = "…") + { + return value?.Length > maxLength && maxLength >= 0 + ? string.Concat(value.AsSpan(0, maxLength), truncationSuffix) + : value; + } } } diff --git a/GlazeWM.Domain/UserConfigs/MusicComponentConfig.cs b/GlazeWM.Domain/UserConfigs/MusicComponentConfig.cs index 962b2db24..c7164ebe4 100644 --- a/GlazeWM.Domain/UserConfigs/MusicComponentConfig.cs +++ b/GlazeWM.Domain/UserConfigs/MusicComponentConfig.cs @@ -14,5 +14,13 @@ public class MusicComponentConfig : BarComponentConfig /// Formatted text to display when music is playing. /// public string LabelPlaying { get; set; } = "{song_title} - {artist_name}"; + /// + /// The maximum length after which the song title will be truncated; if set to -1, the title will not be truncated. + /// + public int MaxTitleLength { get; set; } = -1; + /// + /// The maximum length after which the artist name will be truncated; if set to -1, the name will not be truncated. + /// + public int MaxArtistLength { get; set; } = -1; } } diff --git a/README.md b/README.md index 3ee8b2131..394bc7bac 100644 --- a/README.md +++ b/README.md @@ -453,6 +453,8 @@ Displays currently playing music. label_not_playing: "" label_paused: "{song_title} - {artist_name}" label_playing: "{song_title} - {artist_name} ▶" + max_title_length: 20 + max_artist_length: 20 ``` ## Mixing font properties within a label