From 9332bb8aff3cf2d1e242c155c150b51a3694fbc5 Mon Sep 17 00:00:00 2001 From: lars-berger Date: Sat, 28 Jan 2023 15:19:26 +0800 Subject: [PATCH] feat: truncate window title in bar component (#230) --- .../WindowTitleComponentViewModel.cs | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs b/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs index dd387f1e..4f7f96ae 100644 --- a/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs +++ b/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs @@ -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() + .Subscribe((@event) => UpdateTitle(@event.WindowHandle)); - if (focusedWindow != null && windowHdl != focusedWindow.Handle) - return; + _bus.Events.OfType() + .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; } } }