Skip to content

Commit

Permalink
feat: update all bar components to new label parser (glzr-io#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger authored Sep 17, 2023
1 parent b19c72d commit 6ec79d2
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 189 deletions.
10 changes: 2 additions & 8 deletions GlazeWM.Bar/Components/BindingModeComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
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"
xmlns:components="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}" />
<components:LabelComponent DataContext="{Binding Label}" />
</UserControl>
33 changes: 26 additions & 7 deletions GlazeWM.Bar/Components/BindingModeComponentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Windows.Threading;
using GlazeWM.Domain.Common.Events;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.UserConfigs;
Expand All @@ -11,7 +11,7 @@ namespace GlazeWM.Bar.Components
{
public class BindingModeComponentViewModel : ComponentViewModel
{
private Dispatcher _dispatcher => _parentViewModel.Dispatcher;
private readonly BindingModeComponentConfig _config;
private readonly Bus _bus = ServiceLocator.GetRequiredService<Bus>();
private readonly ContainerService _containerService =
ServiceLocator.GetRequiredService<ContainerService>();
Expand All @@ -27,17 +27,36 @@ public class BindingModeComponentViewModel : ComponentViewModel
public override string Visibility =>
ActiveBindingMode is null ? "Collapsed" : "Visible";

private LabelViewModel _label;
public LabelViewModel Label
{
get => _label;
protected set => SetField(ref _label, value);
}

public BindingModeComponentViewModel(
BarViewModel parentViewModel,
BindingModeComponentConfig config) : base(parentViewModel, config)
{
_bus.Events.OfType<BindingModeChangedEvent>().Subscribe(_ =>
_dispatcher.Invoke(() =>
_config = config;

_bus.Events.OfType<BindingModeChangedEvent>()
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe((@event) =>
{
OnPropertyChanged(nameof(Visibility));
OnPropertyChanged(nameof(ActiveBindingMode));
})
);
Label = CreateLabel(@event.NewBindingMode);
});
}

private LabelViewModel CreateLabel(string bindingMode)
{
var variableDictionary = new Dictionary<string, Func<string>>()
{
{ "binding_mode", () => bindingMode }
};

return XamlHelper.ParseLabel(_config.Label, variableDictionary, this);
}
}
}
10 changes: 2 additions & 8 deletions GlazeWM.Bar/Components/ClockComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
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"
xmlns:components="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 FormattedTime}" />
<components:LabelComponent DataContext="{Binding Label}" />
</UserControl>
56 changes: 37 additions & 19 deletions GlazeWM.Bar/Components/ClockComponentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reactive.Linq;
using System.Text;
Expand All @@ -8,13 +9,43 @@ namespace GlazeWM.Bar.Components
{
public class ClockComponentViewModel : ComponentViewModel
{
private ClockComponentConfig _config => _componentConfig as ClockComponentConfig;
private string _timeFormatting => FormatCalendarWeek(_config.TimeFormatting);
private readonly ClockComponentConfig _config;
private LabelViewModel _label;
public LabelViewModel Label
{
get => _label;
protected set => SetField(ref _label, value);
}

public ClockComponentViewModel(
BarViewModel parentViewModel,
ClockComponentConfig config) : base(parentViewModel, config)
{
_config = config;

// Update the displayed time every second.
var updateInterval = TimeSpan.FromSeconds(1);

Observable
.Interval(updateInterval)
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => Label = CreateLabel());
}

private LabelViewModel CreateLabel()
{
var timeFormatting = FormatCalendarWeek(_config.TimeFormatting);

/// <summary>
/// Format the current time with the user's formatting config.
/// </summary>
public string FormattedTime => DateTime.Now.ToString(_timeFormatting, CultureInfo.InvariantCulture);
// Format the current time with the user's formatting config.
var formattedTime = DateTime.Now.ToString(timeFormatting, CultureInfo.InvariantCulture);

var variableDictionary = new Dictionary<string, Func<string>>()
{
{ "formatted_time", () => formattedTime }
};

return XamlHelper.ParseLabel(_config.Label, variableDictionary, this);
}

private static string FormatCalendarWeek(string timeFormat)
{
Expand Down Expand Up @@ -59,18 +90,5 @@ private static string FormatCalendarWeek(string timeFormat)
}
return result.ToString();
}

public ClockComponentViewModel(
BarViewModel parentViewModel,
ClockComponentConfig config) : base(parentViewModel, config)
{
// Update the displayed time every second.
var updateInterval = TimeSpan.FromSeconds(1);

Observable
.Interval(updateInterval)
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => OnPropertyChanged(nameof(FormattedTime)));
}
}
}
1 change: 0 additions & 1 deletion GlazeWM.Bar/Components/LabelComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<TextBlock
Text="{Binding Text}"
VerticalAlignment="Center"
Background="{Binding Background}"
Foreground="{Binding Foreground}"
FontFamily="{Binding FontFamily}"
FontWeight="{Binding FontWeight}"
Expand Down
71 changes: 0 additions & 71 deletions GlazeWM.Bar/Components/LabelSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,23 @@ namespace GlazeWM.Bar.Components
public class LabelSpan
{
public string Text { get; }
public string Background { get; }
public string Foreground { get; }
public string FontFamily { get; }
public string FontWeight { get; }
public string FontSize { get; }

public LabelSpan(
string text,
string background,
string foreground,
string fontFamily,
string fontWeight,
string fontSize)
{
Text = text;
Background = background;
Foreground = foreground;
FontFamily = fontFamily;
FontWeight = fontWeight;
FontSize = fontSize;
}
}
}

/// Usage:
/**
public class BatteryComponentViewModel : ComponentViewModel
{
private readonly BatteryComponentConfig _config;
private LabelViewModel _label;
public string Label
{
get => _label;
protected set => SetField(ref _label, value);
}
public BatteryComponentViewModel(...) : base(parentViewModel, config)
{
_batteryComponentConfig = config;
Label = XamlHelper.ParseLabel(config.Label, CreateVariableDict());
Observable
.Interval(TimeSpan.FromSeconds(3))
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => {
Label.UpdateVariables(CreateVariableDict());
OnPropertyChanged(nameof(Label));
});
}
}
*/

/// Alternatively:
/**
public class BatteryComponentViewModel : ComponentViewModel
{
private readonly BatteryComponentConfig _config;
private LabelViewModel _label;
public string Label
{
get => _label;
protected set => SetField(ref _label, value);
}
public BatteryComponentViewModel(...) : base(parentViewModel, config)
{
_batteryComponentConfig = config;
Label = XamlHelper.ParseLabel(config.Label, GetVariableDict());
Observable
.Interval(TimeSpan.FromSeconds(3))
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe(_ => {
Label.UpdateVariables();
OnPropertyChanged(nameof(Label));
});
}
public Dictionary<string, Action<string>> GetVariableDict()
{
return new()
{
{ "battery_level", () => GetBatteryLevel() }
}
}
}
*/
25 changes: 15 additions & 10 deletions GlazeWM.Bar/Components/SystemTrayComponent.xaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
<UserControl
x:Class="GlazeWM.Bar.Components.SystemTrayComponent"
x:Name="_workspacesComponent"
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"
xmlns:components="clr-namespace:GlazeWM.Bar.Components"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<StackPanel Orientation="Horizontal">
<Button
Background="{Binding Background}"
Foreground="{Binding Foreground}"
FontFamily="{Binding FontFamily}"
Content="{Binding ExpandCollapseText}"
FontWeight="{Binding FontWeight}"
FontSize="{Binding FontSize}"
Command="{Binding ToggleShowAllIconsCommand}"
BorderThickness="0"
Cursor="Hand" />
<components:LabelComponent
DataContext="{Binding Label}"
MouseEnter="OnLabelHoverEnter"
MouseLeave="OnLabelHoverLeave">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:CallMethodAction
MethodName="ToggleShowAllIcons"
TargetObject="{Binding Path=DataContext, ElementName=_workspacesComponent, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</components:LabelComponent>
<Border
x:Name="NotifyIconBorder"
ToolTip="{Binding Path=TrayIcon.Title}"
Expand Down
11 changes: 11 additions & 0 deletions GlazeWM.Bar/Components/SystemTrayComponent.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Controls;
using System.Windows.Input;

namespace GlazeWM.Bar.Components
{
Expand All @@ -11,5 +12,15 @@ public SystemTrayComponent()
{
InitializeComponent();
}

public void OnLabelHoverEnter(object sender, MouseEventArgs e)
{
Cursor = Cursors.Hand;
}

public void OnLabelHoverLeave(object sender, MouseEventArgs e)
{
Cursor = Cursors.Arrow;
}
}
}
12 changes: 6 additions & 6 deletions GlazeWM.Bar/Components/SystemTrayComponentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using GlazeWM.Bar.Common;
Expand All @@ -16,8 +18,6 @@ public class SystemTrayComponentViewModel : ComponentViewModel
private readonly ShellManager _shellManager =
ServiceLocator.GetRequiredService<ShellManager>();

public ICommand ToggleShowAllIconsCommand => new RelayCommand(ToggleShowAllIcons);

private bool _isExpanded = true;
public bool IsExpanded
{
Expand All @@ -29,9 +29,9 @@ public bool IsExpanded
}
}

public string ExpandCollapseText => IsExpanded
? _config.LabelCollapseText
: _config.LabelExpandText;
public LabelViewModel Label => IsExpanded
? XamlHelper.ParseLabel(_config.LabelCollapseText, new Dictionary<string, Func<string>>(), this)
: XamlHelper.ParseLabel(_config.LabelExpandText, new Dictionary<string, Func<string>>(), this);

public ObservableCollection<NotifyIconViewModel> PinnedTrayIcons =>
new(_pinnedTrayIcons);
Expand Down Expand Up @@ -64,7 +64,7 @@ public SystemTrayComponentViewModel(
public void ToggleShowAllIcons()
{
IsExpanded = !IsExpanded;
OnPropertyChanged(nameof(ExpandCollapseText));
OnPropertyChanged(nameof(Label));
}
}
}
10 changes: 2 additions & 8 deletions GlazeWM.Bar/Components/TilingDirectionComponent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
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"
xmlns:components="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 TilingDirectionString}" />
<components:LabelComponent DataContext="{Binding Label}" />
</UserControl>
Loading

0 comments on commit 6ec79d2

Please sign in to comment.