Skip to content

Commit

Permalink
feat: truncate window title in bar component (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Jan 28, 2023
1 parent 6d3905f commit 9332bb8
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,30 @@ public WindowTitleComponentViewModel(
BarViewModel parentViewModel,
WindowTitleComponentConfig config) : base(parentViewModel, config)
{
void processTitleChange(IntPtr windowHdl)
{
var focusedWindow = _containerService.FocusedContainer as Window;
_bus.Events.OfType<WindowFocusedEvent>()
.Subscribe((@event) => UpdateTitle(@event.WindowHandle));

if (focusedWindow != null && windowHdl != focusedWindow.Handle)
return;
_bus.Events.OfType<WindowTitleChangedEvent>()
.Subscribe((@event) => UpdateTitle(@event.WindowHandle));
}

FocusedWindowTitle = focusedWindow?.Title ?? string.Empty;
}
private void UpdateTitle(IntPtr windowHandle)
{
var focusedWindow = _containerService.FocusedContainer as Window;

_bus.Events.Where(
(@event) => @event is WindowFocusedEvent or WindowTitleChangedEvent
).Subscribe(e =>
{
dynamic d = e;
processTitleChange(d.WindowHandle);
});
if (focusedWindow != null && windowHandle != focusedWindow.Handle)
return;

// TODO: Make truncate max length configurable from config.
var windowTitle = focusedWindow?.Title ?? string.Empty;
FocusedWindowTitle = Truncate(windowTitle, 60);
}

public static string Truncate(string value, int maxLength, string truncationSuffix = "")
{
return value?.Length > maxLength
? string.Concat(value.AsSpan(0, maxLength), truncationSuffix)
: value;
}
}
}

0 comments on commit 9332bb8

Please sign in to comment.