Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add click commands to text file component #531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion GlazeWM.Bar/Components/ImageComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GlazeWM.Bar.Components"
mc:Ignorable="d">
<Image Source="{Binding Source}" />
<Image Source="{Binding Source}">
</Image>
</UserControl>
4 changes: 4 additions & 0 deletions GlazeWM.Bar/Components/TextFileComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
FontWeight="{Binding FontWeight}"
FontSize="{Binding FontSize}"
Text="{Binding Text}">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding LeftClickCommand}" />
<MouseBinding Gesture="RightClick" Command="{Binding RightClickCommand}" />
</TextBlock.InputBindings>
</TextBlock>
</UserControl>
37 changes: 37 additions & 0 deletions GlazeWM.Bar/Components/TextFileComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
using GlazeWM.Domain.UserConfigs.Events;
using GlazeWM.Infrastructure;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Domain.Containers;
using System.Windows.Input;
using GlazeWM.Bar.Common;

namespace GlazeWM.Bar.Components
{
public class TextFileComponentViewModel : ComponentViewModel
{
private readonly TextFileComponentConfig _baseConfig;
private readonly IDisposable _disposable;
private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();
private readonly ContainerService _containerService =
ServiceLocator.GetRequiredService<ContainerService>();
private readonly CommandParsingService _commandParsingService =
ServiceLocator.GetRequiredService<CommandParsingService>();

public string Text { get; set; } = "Loading...";

public ICommand LeftClickCommand => new RelayCommand(OnLeftClick);
public ICommand RightClickCommand => new RelayCommand(OnRightClick);

public FileSystemWatcher Watcher { get; }

public TextFileComponentViewModel(BarViewModel parentViewModel, TextFileComponentConfig baseConfig) : base(parentViewModel, baseConfig)
Expand All @@ -35,6 +46,32 @@ public TextFileComponentViewModel(BarViewModel parentViewModel, TextFileComponen
Update();
}

public void OnLeftClick()
{
InvokeCommand(_baseConfig.LeftClickCommand);
}

public void OnRightClick()
{
InvokeCommand(_baseConfig.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);
}

protected override void Dispose(bool disposing)
{
if (disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,21 @@ windowRuleConfig.MatchProcessName is not null ||
)
.Where((commandString) => !string.IsNullOrEmpty(commandString));

var textFileComponentCommands = componentConfigs
.OfType<TextFileComponentConfig>()
.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))
.Concat(textComponentCommands)
.Concat(textFileComponentCommands)
.Select((commandString) => CommandParsingService.FormatCommand(commandString));

foreach (var commandString in allCommandStrings)
Expand Down
10 changes: 10 additions & 0 deletions GlazeWM.Domain/UserConfigs/TextFileComponentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@ namespace GlazeWM.Domain.UserConfigs
public class TextFileComponentConfig : BarComponentConfig
{
public string FilePath { get; set; }

/// <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; }
}
}
Loading