Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(bar): component for displaying active window title (#207)

blam93ccc5e9d597
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index 9e29f795..5c0c8f24 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -48,6 +48,7 @@ namespace GlazeWM.Bar
TextComponentConfig tcc => new TextComponentViewModel(this, tcc),
TilingDirectionComponentConfig tdc => new TilingDirectionComponentViewModel(this, tdc),
WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),
+ WindowTitleComponentConfig wtcc => new WindowTitleComponentViewModel(this, wtcc),
_ => throw new ArgumentOutOfRangeException(nameof(config)),
});
}
diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xaml
index 2cf75358..86760657 100644
--- a/GlazeWM.Bar/Components/ComponentPortal.xaml
+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml
@@ -25,6 +25,9 @@
<DataTemplate DataType="{x:Type components:TextComponentViewModel}">
<components:TextComponent />
</DataTemplate>
+ <DataTemplate DataType="{x:Type components:WindowTitleComponentViewModel}">
+ <components:WindowTitleComponent />
+ </DataTemplate>
<DataTemplate DataType="{x:Type components:TilingDirectionComponentViewModel}">
<components:TilingDirectionComponent />
</DataTemplate>
diff --git a/GlazeWM.Bar/Components/WindowTitleComponent.xaml b/GlazeWM.Bar/Components/WindowTitleComponent.xaml
new file mode 100644
index 00000000..a199a6b6
--- /dev/null
+++ b/GlazeWM.Bar/Components/WindowTitleComponent.xaml
@@ -0,0 +1,16 @@
+<UserControl
+ x:Class="GlazeWM.Bar.Components.WindowTitleComponent"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:local="clr-namespace:GlazeWM.Bar.Components"
+ mc:Ignorable="d">
+ <TextBlock
+ VerticalAlignment="Center"
+ Foreground="{Binding Foreground}"
+ FontFamily="{Binding FontFamily}"
+ FontWeight="{Binding FontWeight}"
+ FontSize="{Binding FontSize}"
+ Text="{Binding FocusedWindowTitle}" />
+</UserControl>
diff --git a/GlazeWM.Bar/Components/WindowTitleComponent.xaml.cs b/GlazeWM.Bar/Components/WindowTitleComponent.xaml.cs
new file mode 100644
index 00000000..176c2fea
--- /dev/null
+++ b/GlazeWM.Bar/Components/WindowTitleComponent.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace GlazeWM.Bar.Components
+{
+ /// <summary>
+ /// Interaction logic for WindowTitleComponent.xaml
+ /// </summary>
+ public partial class WindowTitleComponent : UserControl
+ {
+ public WindowTitleComponent()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs b/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs
new file mode 100644
index 00000000..6f4a473e
--- /dev/null
+++ b/GlazeWM.Bar/Components/WindowTitleComponentViewModel.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Reactive.Linq;
+using GlazeWM.Domain.Containers;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Domain.Windows;
+using GlazeWM.Infrastructure;
+using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Common.Events;
+
+namespace GlazeWM.Bar.Components
+{
+ public class WindowTitleComponentViewModel : ComponentViewModel
+ {
+ private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();
+ private readonly ContainerService _containerService =
+ ServiceLocator.GetRequiredService<ContainerService>();
+
+ public string _focusedWindowTitle = string.Empty;
+ public string FocusedWindowTitle
+ {
+ get => _focusedWindowTitle;
+ set
+ {
+ _focusedWindowTitle = value;
+ OnPropertyChanged(nameof(FocusedWindowTitle));
+ }
+ }
+
+ public WindowTitleComponentViewModel(
+ BarViewModel parentViewModel,
+ WindowTitleComponentConfig config) : base(parentViewModel, config)
+ {
+ Action<IntPtr> processTitleChange = (windowHdl) =>
+ {
+ var focusedWindow = _containerService.FocusedContainer as Window;
+
+ if (focusedWindow != null && windowHdl != focusedWindow.Handle)
+ return;
+
+ FocusedWindowTitle = focusedWindow?.Title ?? string.Empty;
+ };
+
+ _bus.Events.Where(
+ (@event) => @event is WindowFocusedEvent or WindowTitleChangedEvent
+ ).Subscribe(e =>
+ {
+ dynamic d = e;
+ processTitleChange(d.WindowHandle);
+ });
+ }
+ }
+}
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yaml
index 04b5b1eb..aa8ec81b 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/sample-config.yaml
@@ -20,6 +20,8 @@ bar:
focused_workspace_background: "#ffffff33"
displayed_workspace_background: "#00000033"
default_workspace_background: "transparent"
+ components_center:
+ - type: "window title"
components_right:
- type: "tiling direction"
background: "#ffffff33"
@@ -34,7 +36,7 @@ bar:
# https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
# "w" Calendarweek (ISO 8601 week) 1..53
# "ww" CalendarWeek (ISO 8601 week) 01..53
- time_formatting: "hh:mm tt ddd MMM d"
+ time_formatting: "hh:mm tt ddd MMM d"
margin: "0 0 0 10px"
workspaces:
diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
index 5c687cb2..b6a4baef 100644
--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
@@ -38,6 +38,11 @@ namespace GlazeWM.Domain.UserConfigs
jsonObject.RootElement.ToString(),
options
),
+ "window title" =>
+ JsonSerializer.Deserialize<WindowTitleComponentConfig>(
+ jsonObject.RootElement.ToString(),
+ options
+ ),
"workspaces" =>
JsonSerializer.Deserialize<WorkspacesComponentConfig>(
jsonObject.RootElement.ToString(),
diff --git a/GlazeWM.Domain/UserConfigs/WindowTitleComponentConfig.cs b/GlazeWM.Domain/UserConfigs/WindowTitleComponentConfig.cs
new file mode 100644
index 00000000..f9044cbe
--- /dev/null
+++ b/GlazeWM.Domain/UserConfigs/WindowTitleComponentConfig.cs
@@ -0,0 +1,6 @@
+namespace GlazeWM.Domain.UserConfigs
+{
+ public class WindowTitleComponentConfig : BarComponentConfig
+ {
+ }
+}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs
new file mode 100644
index 00000000..c0dc68f3
--- /dev/null
+++ b/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs
@@ -0,0 +1,15 @@
+using System;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Infrastructure.Common.Events
+{
+ public class WindowTitleChangedEvent : Event
+ {
+ public IntPtr WindowHandle { get; }
+
+ public WindowTitleChangedEvent(IntPtr windowHandle)
+ {
+ WindowHandle = windowHandle;
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/Enums/EventConstant.cs b/GlazeWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
index 24d5383b..7dc3fabc 100644
--- a/GlazeWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/Enums/EventConstant.cs
@@ -13,5 +13,6 @@
EVENT_OBJECT_SHOW = 0x8002,
EVENT_OBJECT_HIDE = 0x8003,
EVENT_OBJECT_LOCATIONCHANGE = 0x800B,
+ EVENT_OBJECT_NAMECHANGE = 0x800C,
}
}
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs b/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
index c9ae0274..ed687276 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
@@ -29,6 +29,7 @@ namespace GlazeWM.Infrastructure.WindowsApi
SetWinEventHook(EventConstant.EVENT_SYSTEM_MINIMIZESTART, EventConstant.EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, _hookProc, 0, 0, 0);
SetWinEventHook(EventConstant.EVENT_SYSTEM_MOVESIZEEND, EventConstant.EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, _hookProc, 0, 0, 0);
SetWinEventHook(EventConstant.EVENT_SYSTEM_FOREGROUND, EventConstant.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _hookProc, 0, 0, 0);
+ SetWinEventHook(EventConstant.EVENT_OBJECT_LOCATIONCHANGE, EventConstant.EVENT_OBJECT_NAMECHANGE, IntPtr.Zero, _hookProc, 0, 0, 0);
}
private void WindowEventHookProc(
@@ -56,6 +57,7 @@ namespace GlazeWM.Infrastructure.WindowsApi
EventConstant.EVENT_SYSTEM_MOVESIZEEND => new WindowMovedOrResizedEvent(hwnd),
EventConstant.EVENT_OBJECT_DESTROY => new WindowDestroyedEvent(hwnd),
EventConstant.EVENT_OBJECT_SHOW => new WindowShownEvent(hwnd),
+ EventConstant.EVENT_OBJECT_NAMECHANGE => new WindowTitleChangedEvent(hwnd),
EventConstant.EVENT_OBJECT_HIDE => new WindowHiddenEvent(hwnd),
_ => null,
};