Skip to content

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

feat: binding mode bar component (#192)

lars-bergerde73c5a18176
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index e2bd95d0..7d49d129 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -43,9 +43,10 @@ namespace GlazeWM.Bar
{
return componentConfigs.ConvertAll<ComponentViewModel>(config => config switch
{
- WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),
+ BindingModeComponentConfig bmc => new BindingModeComponentViewModel(this, bmc),
ClockComponentConfig ccc => new ClockComponentViewModel(this, ccc),
TextComponentConfig tcc => new TextComponentViewModel(this, tcc),
+ WorkspacesComponentConfig wcc => new WorkspacesComponentViewModel(this, wcc),
_ => throw new ArgumentOutOfRangeException(nameof(config)),
});
}
diff --git a/GlazeWM.Bar/Components/BindingModeComponent.xaml b/GlazeWM.Bar/Components/BindingModeComponent.xaml
new file mode 100644
index 00000000..abcc4f21
--- /dev/null
+++ b/GlazeWM.Bar/Components/BindingModeComponent.xaml
@@ -0,0 +1,16 @@
+<UserControl
+ x:Class="GlazeWM.Bar.Components.BindingModeComponent"
+ 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 ActiveBindingMode}" />
+</UserControl>
diff --git a/GlazeWM.Bar/Components/BindingModeComponent.xaml.cs b/GlazeWM.Bar/Components/BindingModeComponent.xaml.cs
new file mode 100644
index 00000000..81b55b76
--- /dev/null
+++ b/GlazeWM.Bar/Components/BindingModeComponent.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace GlazeWM.Bar.Components
+{
+ /// <summary>
+ /// Interaction logic for BindingModeComponent.xaml
+ /// </summary>
+ public partial class BindingModeComponent : UserControl
+ {
+ public BindingModeComponent()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/BindingModeComponentViewModel.cs b/GlazeWM.Bar/Components/BindingModeComponentViewModel.cs
new file mode 100644
index 00000000..ced8d041
--- /dev/null
+++ b/GlazeWM.Bar/Components/BindingModeComponentViewModel.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Reactive.Linq;
+using System.Windows.Threading;
+using GlazeWM.Domain.Containers;
+using GlazeWM.Domain.Monitors.Events;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Infrastructure;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Bar.Components
+{
+ public class BindingModeComponentViewModel : ComponentViewModel
+ {
+ private Dispatcher _dispatcher => _parentViewModel.Dispatcher;
+ private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();
+ private readonly ContainerService _containerService =
+ ServiceLocator.GetRequiredService<ContainerService>();
+
+ /// <summary>
+ /// Name of the currently active binding mode (if one is active).
+ /// </summary>
+ public string ActiveBindingMode => _containerService.ActiveBindingMode;
+
+ /// <summary>
+ /// Hide component when no binding mode is active.
+ /// </summary>
+ public override string Visibility =>
+ ActiveBindingMode is null ? "Collapsed" : "Visible";
+
+ public BindingModeComponentViewModel(
+ BarViewModel parentViewModel,
+ BindingModeComponentConfig config) : base(parentViewModel, config)
+ {
+ _bus.Events.OfType<BindingModeChangedEvent>().Subscribe(_ =>
+ _dispatcher.Invoke(() =>
+ {
+ OnPropertyChanged(nameof(Visibility));
+ OnPropertyChanged(nameof(ActiveBindingMode));
+ })
+ );
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xaml
index b3a3290b..2542a4ee 100644
--- a/GlazeWM.Bar/Components/ComponentPortal.xaml
+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml
@@ -7,6 +7,7 @@
xmlns:components="clr-namespace:GlazeWM.Bar.Components"
mc:Ignorable="d">
<Grid
+ Visibility="{Binding Visibility}"
Margin="{Binding Margin}"
Background="{Binding Background}">
<Border
@@ -15,8 +16,8 @@
<Grid Margin="{Binding Padding}">
<ContentPresenter Content="{Binding .}">
<ContentPresenter.Resources>
- <DataTemplate DataType="{x:Type components:WorkspacesComponentViewModel}">
- <components:WorkspacesComponent />
+ <DataTemplate DataType="{x:Type components:BindingModeComponentViewModel}">
+ <components:BindingModeComponent />
</DataTemplate>
<DataTemplate DataType="{x:Type components:ClockComponentViewModel}">
<components:ClockComponent />
@@ -24,6 +25,9 @@
<DataTemplate DataType="{x:Type components:TextComponentViewModel}">
<components:TextComponent />
</DataTemplate>
+ <DataTemplate DataType="{x:Type components:WorkspacesComponentViewModel}">
+ <components:WorkspacesComponent />
+ </DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
diff --git a/GlazeWM.Bar/Components/ComponentViewModel.cs b/GlazeWM.Bar/Components/ComponentViewModel.cs
index 9a261649..e10568b5 100644
--- a/GlazeWM.Bar/Components/ComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/ComponentViewModel.cs
@@ -3,11 +3,12 @@ using GlazeWM.Domain.UserConfigs;
namespace GlazeWM.Bar.Components
{
- public class ComponentViewModel : ViewModelBase
+ public abstract class ComponentViewModel : ViewModelBase
{
protected readonly BarViewModel _parentViewModel;
protected readonly BarComponentConfig _componentConfig;
+ public virtual string Visibility { get; set; } = "Visible";
public string Background => XamlHelper.FormatColor(_componentConfig.Background);
public string Foreground =>
XamlHelper.FormatColor(_componentConfig.Foreground ?? _parentViewModel.Foreground);
@@ -22,7 +23,7 @@ namespace GlazeWM.Bar.Components
public string Padding => XamlHelper.FormatRectShorthand(_componentConfig.Padding);
public string Margin => XamlHelper.FormatRectShorthand(_componentConfig.Margin);
- public ComponentViewModel(BarViewModel parentViewModel, BarComponentConfig baseConfig)
+ protected ComponentViewModel(BarViewModel parentViewModel, BarComponentConfig baseConfig)
{
_parentViewModel = parentViewModel;
_componentConfig = baseConfig;
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yaml
index 99953c63..dc1fe42b 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/sample-config.yaml
@@ -17,6 +17,10 @@ bar:
displayed_workspace_background: "#42403e"
default_workspace_background: "#101010"
components_right:
+ - type: "binding mode"
+ background: "#ffffff33"
+ margin: "5 5 5 0"
+ padding: "0 5"
- type: "clock"
time_formatting: "hh:mm tt ddd MMM d"
diff --git a/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs b/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs
index 2e53c149..3bbade21 100644
--- a/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs
+++ b/GlazeWM.Domain/Common/CommandHandlers/SetBindingModeHandler.cs
@@ -1,4 +1,6 @@
using GlazeWM.Domain.Common.Commands;
+using GlazeWM.Domain.Containers;
+using GlazeWM.Domain.Monitors.Events;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Infrastructure.Bussing;
@@ -8,11 +10,16 @@ namespace GlazeWM.Domain.Common.CommandHandlers
public class SetBindingModeHandler : ICommandHandler<SetBindingModeCommand>
{
private readonly Bus _bus;
+ private readonly ContainerService _containerService;
private readonly UserConfigService _userConfigService;
- public SetBindingModeHandler(Bus bus, UserConfigService userConfigService)
+ public SetBindingModeHandler(
+ Bus bus,
+ ContainerService containerService,
+ UserConfigService userConfigService)
{
_bus = bus;
+ _containerService = containerService;
_userConfigService = userConfigService;
}
@@ -25,6 +32,10 @@ namespace GlazeWM.Domain.Common.CommandHandlers
{
var defaultKeybindings = _userConfigService.Keybindings;
_bus.Invoke(new RegisterKeybindingsCommand(defaultKeybindings));
+
+ _containerService.ActiveBindingMode = null;
+ _bus.Emit(new BindingModeChangedEvent(null));
+
return CommandResponse.Ok;
}
@@ -32,6 +43,9 @@ namespace GlazeWM.Domain.Common.CommandHandlers
var bindingMode = _userConfigService.GetBindingModeByName(bindingModeName);
_bus.Invoke(new RegisterKeybindingsCommand(bindingMode.Keybindings));
+ _containerService.ActiveBindingMode = bindingModeName;
+ _bus.Emit(new BindingModeChangedEvent(bindingModeName));
+
return CommandResponse.Ok;
}
}
diff --git a/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs b/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs
new file mode 100644
index 00000000..2e6b2e94
--- /dev/null
+++ b/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs
@@ -0,0 +1,14 @@
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Monitors.Events
+{
+ public class BindingModeChangedEvent : Event
+ {
+ public string BindingMode { get; }
+
+ public BindingModeChangedEvent(string bindingMode)
+ {
+ BindingMode = bindingMode;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Containers/ContainerService.cs b/GlazeWM.Domain/Containers/ContainerService.cs
index d9fbebce..b064d000 100644
--- a/GlazeWM.Domain/Containers/ContainerService.cs
+++ b/GlazeWM.Domain/Containers/ContainerService.cs
@@ -33,6 +33,11 @@ namespace GlazeWM.Domain.Containers
public FocusMode FocusMode => FocusedContainer is FloatingWindow ?
FocusMode.FLOATING : FocusMode.TILING;
+ /// <summary>
+ /// Name of the currently active binding mode (if one is active).
+ /// </summary>
+ public string ActiveBindingMode = null;
+
private readonly UserConfigService _userConfigService;
public ContainerService(UserConfigService userConfigService)
diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
index 889f758f..64c0db24 100644
--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
@@ -18,8 +18,8 @@ namespace GlazeWM.Domain.UserConfigs
return typeDiscriminator switch
{
- "workspaces" =>
- JsonSerializer.Deserialize<WorkspacesComponentConfig>(
+ "binding mode" =>
+ JsonSerializer.Deserialize<BindingModeComponentConfig>(
jsonObject.RootElement.ToString(),
options
),
@@ -33,6 +33,11 @@ namespace GlazeWM.Domain.UserConfigs
jsonObject.RootElement.ToString(),
options
),
+ "workspaces" =>
+ JsonSerializer.Deserialize<WorkspacesComponentConfig>(
+ jsonObject.RootElement.ToString(),
+ options
+ ),
_ => throw new ArgumentException($"Invalid component type '{typeDiscriminator}'."),
};
}
diff --git a/GlazeWM.Domain/UserConfigs/BindingModeComponentConfig.cs b/GlazeWM.Domain/UserConfigs/BindingModeComponentConfig.cs
new file mode 100644
index 00000000..4fd50f10
--- /dev/null
+++ b/GlazeWM.Domain/UserConfigs/BindingModeComponentConfig.cs
@@ -0,0 +1,6 @@
+namespace GlazeWM.Domain.UserConfigs
+{
+ public class BindingModeComponentConfig : BarComponentConfig
+ {
+ }
+}