Skip to content

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

refactor(bar): move out method for converting user config shorthands to `BarService`

Lars Berger4be642bae86f
diff --git a/GlazeWM.Bar/BarService.cs b/GlazeWM.Bar/BarService.cs
index 382d7898..0754e932 100644
--- a/GlazeWM.Bar/BarService.cs
+++ b/GlazeWM.Bar/BarService.cs
@@ -5,20 +5,17 @@ using System.Reactive.Linq;
using System;
using System.Threading;
using GlazeWM.Domain.UserConfigs;
+using System.Linq;
namespace GlazeWM.Bar
{
public class BarService
{
private Bus _bus;
- private WorkspaceService _workspaceService;
- private UserConfigService _userConfigService;
- public BarService(Bus bus, WorkspaceService workspaceService, UserConfigService userConfigService)
+ public BarService(Bus bus)
{
_bus = bus;
- _workspaceService = workspaceService;
- _userConfigService = userConfigService;
}
public void StartApp()
@@ -51,5 +48,24 @@ namespace GlazeWM.Bar
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
+
+ /// <summary>
+ /// Convert shorthand properties from user config (ie. `Padding`, `Margin`, and `BorderWidth`)
+ /// to be compatible with their equivalent XAML properties (ie. `Padding`, `Margin`, and
+ /// `BorderThickness`). Shorthand properties follow the 1-to-4 value syntax used in CSS.
+ /// </summary>
+ public string ShorthandToXamlProperty(string shorthand)
+ {
+ var shorthandParts = shorthand.Split(" ");
+
+ return shorthandParts.Count() switch
+ {
+ 1 => shorthand,
+ 2 => $"{shorthandParts[1]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[0]}",
+ 3 => $"{shorthandParts[1]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[2]}",
+ 4 => $"{shorthandParts[3]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[2]}",
+ _ => throw new ArgumentException(),
+ };
+ }
}
}
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index cdd4861e..678e7be0 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -16,14 +16,15 @@ namespace GlazeWM.Bar
{
public Dispatcher Dispatcher { get; set; }
public Monitor Monitor { get; set; }
+ private BarService _barService = ServiceLocator.Provider.GetRequiredService<BarService>();
private UserConfigService _userConfigService = ServiceLocator.Provider.GetRequiredService<UserConfigService>();
private BarConfig _barConfig => _userConfigService.UserConfig.Bar;
public string Background => _barConfig.Background;
public string FontFamily => _barConfig.FontFamily;
public string FontSize => _barConfig.FontSize;
public string BorderColor => _barConfig.BorderColor;
- public string BorderWidth => ShorthandToXamlProperty(_barConfig.BorderWidth);
- public string Padding => ShorthandToXamlProperty(_barConfig.Padding);
+ public string BorderWidth => _barService.ShorthandToXamlProperty(_barConfig.BorderWidth);
+ public string Padding => _barService.ShorthandToXamlProperty(_barConfig.Padding);
public double Opacity => _barConfig.Opacity;
public List<ComponentViewModel> ComponentsLeft =>
@@ -54,24 +55,5 @@ namespace GlazeWM.Bar
return viewModel;
}).ToList();
}
-
- /// <summary>
- /// Convert shorthand properties from user config (ie. `Padding`, `Margin`, and `BorderWidth`)
- /// to be compatible with their equivalent XAML properties (ie. `Padding`, `Margin`, and
- /// `BorderThickness`). Shorthand properties follow the 1-to-4 value syntax used in CSS.
- /// </summary>
- private string ShorthandToXamlProperty(string shorthand)
- {
- var shorthandParts = shorthand.Split(" ");
-
- return shorthandParts.Count() switch
- {
- 1 => shorthand,
- 2 => $"{shorthandParts[1]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[0]}",
- 3 => $"{shorthandParts[1]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[2]}",
- 4 => $"{shorthandParts[3]},{shorthandParts[0]},{shorthandParts[1]},{shorthandParts[2]}",
- _ => throw new ArgumentException(),
- };
- }
}
}