Skip to content

Commit

Permalink
feat: added trimming to song title and artist name
Browse files Browse the repository at this point in the history
  • Loading branch information
AleGrz committed Dec 18, 2023
1 parent e6b09ee commit ee4ceba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions GlazeWM.Bar/Components/MusicComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
}
}
8 changes: 8 additions & 0 deletions GlazeWM.Domain/UserConfigs/MusicComponentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ public class MusicComponentConfig : BarComponentConfig
/// Formatted text to display when music is playing.
/// </summary>
public string LabelPlaying { get; set; } = "{song_title} - {artist_name}";
/// <summary>
/// The maximum length after which the song title will be truncated; if set to -1, the title will not be truncated.
/// </summary>
public int MaxTitleLength { get; set; } = -1;
/// <summary>
/// The maximum length after which the artist name will be truncated; if set to -1, the name will not be truncated.
/// </summary>
public int MaxArtistLength { get; set; } = -1;
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee4ceba

Please sign in to comment.