Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Sep 16, 2023
1 parent b19c72d commit 7cf447b
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 177 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)));
}
}
}
68 changes: 0 additions & 68 deletions GlazeWM.Bar/Components/LabelSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,3 @@ public LabelSpan(
}
}
}

/// 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() }
}
}
}
*/
21 changes: 11 additions & 10 deletions GlazeWM.Bar/Components/SystemTrayComponent.xaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<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}">
<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: 7 additions & 4 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 Down Expand Up @@ -29,9 +31,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 @@ -63,8 +65,9 @@ public SystemTrayComponentViewModel(

public void ToggleShowAllIcons()
{
Debug.WriteLine("was called");
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>
45 changes: 28 additions & 17 deletions GlazeWM.Bar/Components/TilingDirectionComponentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Windows.Threading;
using GlazeWM.Domain.Common.Enums;
Expand All @@ -17,31 +18,41 @@ public class TilingDirectionComponentViewModel : ComponentViewModel
private readonly ContainerService _containerService =
ServiceLocator.GetRequiredService<ContainerService>();

private TilingDirectionComponentConfig _config => _componentConfig as TilingDirectionComponentConfig;
private readonly TilingDirectionComponentConfig _config;

private string LabelVertical => _config.LabelVertical;
private string LabelHorizontal => _config.LabelHorizontal;

/// <summary>
/// The tiling direction of the currently focused container. Can be null on app
/// startup when workspaces haven't been created yet.
/// </summary>
private TilingDirection? _tilingDirection =>
(_containerService.FocusedContainer as SplitContainer)?.TilingDirection ??
(_containerService.FocusedContainer.Parent as SplitContainer)?.TilingDirection;

public string TilingDirectionString =>
_tilingDirection == TilingDirection.Vertical ? LabelVertical : LabelHorizontal;
private LabelViewModel _label;
public LabelViewModel Label
{
get => _label;
protected set => SetField(ref _label, value);
}

public TilingDirectionComponentViewModel(
BarViewModel parentViewModel,
TilingDirectionComponentConfig config) : base(parentViewModel, config)
{
_config = config;

_bus.Events.Where(
(@event) => @event is TilingDirectionChangedEvent or FocusChangedEvent
).Subscribe((_) =>
_dispatcher.Invoke(() => OnPropertyChanged(nameof(TilingDirectionString)))
);
)
.TakeUntil(_parentViewModel.WindowClosing)
.Subscribe((_) => _dispatcher.Invoke(() => Label = CreateLabel()));
}

private LabelViewModel CreateLabel()
{
// The tiling direction of the currently focused container. Can be null on app
// startup when workspaces haven't been created yet.
var tilingDirection =
(_containerService.FocusedContainer as SplitContainer)?.TilingDirection ??
(_containerService.FocusedContainer.Parent as SplitContainer)?.TilingDirection;

var label = tilingDirection == TilingDirection.Vertical
? _config.LabelVertical
: _config.LabelHorizontal;

return XamlHelper.ParseLabel(label, new Dictionary<string, Func<string>>(), this);
}
}
}
Loading

0 comments on commit 7cf447b

Please sign in to comment.