GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: invoke commands on click in text bar component (#136)
lars-bergerdbe62666fa58
diff --git a/GlazeWM.Bar/Components/TextComponent.xaml b/GlazeWM.Bar/Components/TextComponent.xamlindex 231357c5..2af22ac9 100644--- a/GlazeWM.Bar/Components/TextComponent.xaml+++ b/GlazeWM.Bar/Components/TextComponent.xaml@@ -12,5 +12,10 @@FontFamily="{Binding FontFamily}"FontWeight="{Binding FontWeight}"FontSize="{Binding FontSize}"- Text="{Binding Text}" />+ Text="{Binding Text}">+ <TextBlock.InputBindings>+ <MouseBinding Gesture="LeftClick" Command="{Binding LeftClickCommand}" />+ <MouseBinding Gesture="RightClick" Command="{Binding RightClickCommand}" />+ </TextBlock.InputBindings>+ </TextBlock></UserControl>diff --git a/GlazeWM.Bar/Components/TextComponentViewModel.cs b/GlazeWM.Bar/Components/TextComponentViewModel.csindex cc50faeb..8c1be571 100644--- a/GlazeWM.Bar/Components/TextComponentViewModel.cs+++ b/GlazeWM.Bar/Components/TextComponentViewModel.cs@@ -1,17 +1,57 @@+using System.Windows.Input;+using GlazeWM.Bar.Common;+using GlazeWM.Domain.Containers;using GlazeWM.Domain.UserConfigs;+using GlazeWM.Infrastructure;+using GlazeWM.Infrastructure.Bussing;+using Microsoft.Extensions.DependencyInjection;namespace GlazeWM.Bar.Components{public class TextComponentViewModel : ComponentViewModel{+ private readonly Bus _bus = ServiceLocator.Provider.GetRequiredService<Bus>();+ private readonly ContainerService _containerService =+ ServiceLocator.Provider.GetRequiredService<ContainerService>();+ private readonly CommandParsingService _commandParsingService =+ ServiceLocator.Provider.GetRequiredService<CommandParsingService>();private TextComponentConfig _config => _componentConfig as TextComponentConfig;public string Text => _config.Text;+ public ICommand LeftClickCommand => new RelayCommand(OnLeftClick);+ public ICommand RightClickCommand => new RelayCommand(OnRightClick);+public TextComponentViewModel(BarViewModel parentViewModel,TextComponentConfig config) : base(parentViewModel, config){}++ public void OnLeftClick()+ {+ InvokeCommand(_config.LeftClickCommand);+ }++ public void OnRightClick()+ {+ InvokeCommand(_config.RightClickCommand);+ }++ private void InvokeCommand(string commandString)+ {+ if (string.IsNullOrEmpty(commandString))+ return;++ var subjectContainer = _containerService.FocusedContainer;++ var parsedCommand = _commandParsingService.ParseCommand(+ commandString,+ subjectContainer+ );++ // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.+ _bus.Invoke((dynamic)parsedCommand);+ }}}diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.csindex 93fc4e90..780bc427 100644--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs@@ -157,10 +157,21 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers);}+ var textComponentCommands = componentConfigs+ .OfType<TextComponentConfig>()+ .SelectMany(+ (componentConfig) => new List<string> {+ componentConfig.LeftClickCommand,+ componentConfig.RightClickCommand+ }+ )+ .Where((commandString) => !string.IsNullOrEmpty(commandString));+var allCommandStrings = new List<string>()- .Concat(keybindingsConfig.SelectMany(keybinding => keybinding.CommandList))- .Concat(windowRulesConfig.SelectMany(windowRule => windowRule.CommandList))- .Select(commandString => CommandParsingService.FormatCommand(commandString));+ .Concat(keybindingsConfig.SelectMany((keybinding) => keybinding.CommandList))+ .Concat(windowRulesConfig.SelectMany((windowRule) => windowRule.CommandList))+ .Concat(textComponentCommands)+ .Select((commandString) => CommandParsingService.FormatCommand(commandString));foreach (var commandString in allCommandStrings)_commandParsingService.ValidateCommand(commandString);diff --git a/GlazeWM.Domain/UserConfigs/TextComponentConfig.cs b/GlazeWM.Domain/UserConfigs/TextComponentConfig.csindex 589d4c1a..9097595d 100644--- a/GlazeWM.Domain/UserConfigs/TextComponentConfig.cs+++ b/GlazeWM.Domain/UserConfigs/TextComponentConfig.cs@@ -6,5 +6,15 @@ namespace GlazeWM.Domain.UserConfigs/// Text to display./// </summary>public string Text { get; set; } = "Hello world!";++ /// <summary>+ /// Command to invoke on left-click.+ /// </summary>+ public string LeftClickCommand { get; set; }++ /// <summary>+ /// Command to invoke on right-click.+ /// </summary>+ public string RightClickCommand { get; set; }}}