Skip to content

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

feat(bar): component for showing battery status (#204)

Aryaman Marathe0fb15e3db155
diff --git a/GlazeWM.Bar/BarViewModel.cs b/GlazeWM.Bar/BarViewModel.cs
index 5c0c8f24..6b6253ed 100644
--- a/GlazeWM.Bar/BarViewModel.cs
+++ b/GlazeWM.Bar/BarViewModel.cs
@@ -43,6 +43,7 @@ namespace GlazeWM.Bar
{
return componentConfigs.ConvertAll<ComponentViewModel>(config => config switch
{
+ BatteryComponentConfig bsc => new BatteryComponentViewModel(this, bsc),
BindingModeComponentConfig bmc => new BindingModeComponentViewModel(this, bmc),
ClockComponentConfig ccc => new ClockComponentViewModel(this, ccc),
TextComponentConfig tcc => new TextComponentViewModel(this, tcc),
diff --git a/GlazeWM.Bar/Components/BatteryComponent.xaml b/GlazeWM.Bar/Components/BatteryComponent.xaml
new file mode 100644
index 00000000..afee2a5d
--- /dev/null
+++ b/GlazeWM.Bar/Components/BatteryComponent.xaml
@@ -0,0 +1,16 @@
+<UserControl
+ x:Class="GlazeWM.Bar.Components.BatteryComponent"
+ 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"
+ mc:Ignorable="d">
+ <TextBlock
+ VerticalAlignment="Center"
+ Foreground="{Binding Foreground}"
+ FontFamily="{Binding FontFamily}"
+ FontWeight="{Binding FontWeight}"
+ FontSize="{Binding FontSize}"
+ Text="{Binding FormattedPowerStatus}" />
+</UserControl>
diff --git a/GlazeWM.Bar/Components/BatteryComponent.xaml.cs b/GlazeWM.Bar/Components/BatteryComponent.xaml.cs
new file mode 100644
index 00000000..14f94f65
--- /dev/null
+++ b/GlazeWM.Bar/Components/BatteryComponent.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace GlazeWM.Bar.Components
+{
+ /// <summary>
+ /// Interaction logic for BatteryComponent.xaml
+ /// </summary>
+ public partial class BatteryComponent : UserControl
+ {
+ public BatteryComponent()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/GlazeWM.Bar/Components/BatteryComponentViewModel.cs b/GlazeWM.Bar/Components/BatteryComponentViewModel.cs
new file mode 100644
index 00000000..c6b40925
--- /dev/null
+++ b/GlazeWM.Bar/Components/BatteryComponentViewModel.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Reactive.Linq;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Infrastructure.WindowsApi;
+
+namespace GlazeWM.Bar.Components
+{
+ public class BatteryComponentViewModel : ComponentViewModel
+ {
+
+ private readonly BatteryComponentConfig _batteryComponentConfig;
+
+ /// <summary>
+ /// Format the current power status with the user's formatting config.
+ /// </summary>
+ public string FormattedPowerStatus => FormatLabel();
+
+ private string FormatLabel()
+ {
+ WindowsApiService.GetSystemPowerStatus(out WindowsApiService.SYSTEM_POWER_STATUS ps);
+ var batteryLevel = ps.BatteryLifePercent.ToString();
+
+ // display the battery level as a 100% if no dedicated battery is available on the device
+ if (ps.BatteryFlag == 128)
+ {
+ return " 100% ";
+ }
+
+ if (ps.ACLineStatus == 1)
+ {
+ return _batteryComponentConfig.Charging.Replace("{battery_level}", batteryLevel);
+ }
+ else if (ps.SystemStatusFlag == 1)
+ {
+ return _batteryComponentConfig.PowerSaver.Replace("{battery_level}", batteryLevel);
+ }
+ else
+ {
+ return _batteryComponentConfig.Draining.Replace("{battery_level}", batteryLevel);
+ }
+ }
+
+ public BatteryComponentViewModel(
+ BarViewModel parentViewModel,
+ BatteryComponentConfig config) : base(parentViewModel, config)
+ {
+ _batteryComponentConfig = config;
+
+ Observable.Interval(TimeSpan.FromSeconds(3))
+ .Subscribe(_ => OnPropertyChanged(nameof(FormattedPowerStatus)));
+ }
+ }
+
+}
diff --git a/GlazeWM.Bar/Components/ComponentPortal.xaml b/GlazeWM.Bar/Components/ComponentPortal.xaml
index 86760657..70016b0c 100644
--- a/GlazeWM.Bar/Components/ComponentPortal.xaml
+++ b/GlazeWM.Bar/Components/ComponentPortal.xaml
@@ -16,6 +16,9 @@
<Grid Margin="{Binding Padding}">
<ContentPresenter Content="{Binding .}">
<ContentPresenter.Resources>
+ <DataTemplate DataType="{x:Type components:BatteryComponentViewModel}">
+ <components:BatteryComponent />
+ </DataTemplate>
<DataTemplate DataType="{x:Type components:BindingModeComponentViewModel}">
<components:BindingModeComponent />
</DataTemplate>
diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
index b6a4baef..50bd975a 100644
--- a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Text.Json;
using System.Text.Json.Serialization;
@@ -18,6 +18,11 @@ namespace GlazeWM.Domain.UserConfigs
return typeDiscriminator switch
{
+ "battery" =>
+ JsonSerializer.Deserialize<BatteryComponentConfig>(
+ jsonObject.RootElement.ToString(),
+ options
+ ),
"binding mode" =>
JsonSerializer.Deserialize<BindingModeComponentConfig>(
jsonObject.RootElement.ToString(),
diff --git a/GlazeWM.Domain/UserConfigs/BatteryComponentConfig.cs b/GlazeWM.Domain/UserConfigs/BatteryComponentConfig.cs
new file mode 100644
index 00000000..b033c6e6
--- /dev/null
+++ b/GlazeWM.Domain/UserConfigs/BatteryComponentConfig.cs
@@ -0,0 +1,28 @@
+namespace GlazeWM.Domain.UserConfigs
+{
+ /// <summary>
+ /// A component to display information about the battery of the device, namely the battery level and
+ /// whether the device is connected to power or is in power saving mode.
+ ///
+ /// There are 3 labels that are used depending on the situation, and the variable
+ /// <c>{battery_level}</c> is replaced with the current battery level of the device.
+ ///
+ /// When the device does not have a dedicated battery, the battery level is displayed
+ /// as 100% at all times.
+ /// </summary>
+ public class BatteryComponentConfig : BarComponentConfig
+ {
+ /// <summary>
+ /// Formatted text to display when the device is draining battery power.
+ /// </summary>
+ public string Draining { get; set; } = " {battery_level}% ";
+ /// <summary>
+ /// Formatted text to display when the device is in power saving mode.
+ /// </summary>
+ public string PowerSaver { get; set; } = " {battery_level}% (power saver) ";
+ /// <summary>
+ /// Formatted text to display when the device is connected to power.
+ /// </summary>
+ public string Charging { get; set; } = " {battery_level}% (charging) ";
+ }
+}
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
index a4af651f..49562c91 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
@@ -418,5 +418,18 @@ namespace GlazeWM.Infrastructure.WindowsApi
[DllImport("User32.dll")]
public static extern IntPtr MonitorFromPoint(Point pt, MonitorFromPointFlags dwFlags);
+
+ [DllImport("kernel32.dll")]
+ public static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS lpSystemPowerStatus);
+
+ public struct SYSTEM_POWER_STATUS
+ {
+ public byte ACLineStatus;
+ public byte BatteryFlag;
+ public byte BatteryLifePercent;
+ public byte SystemStatusFlag;
+ public uint BatteryLifeTime;
+ public uint BatteryFullLifeTime;
+ }
}
}
diff --git a/README.md b/README.md
index d414e2e8..14c2a319 100644
--- a/README.md
+++ b/README.md
@@ -233,6 +233,24 @@ Additionally supported format specifiers:
time_formatting: "hh:mm tt ddd MMM d"
```
+### Bar Component: Battery
+
+The battery component displays the system's battery level in percent.
+There are three labels available that can be customized:
+- `draining`: used when the system is draining battery power(i.e. not charging).
+- `power_saver`: used when the system is on power saving mode.
+- `charging`: used when the system is connected to power.
+
+`{battery_level}` is a variable which is replaced by the actual battery level when the label is displayed.
+
+**Example usage:**
+```yaml
+- type: "battery"
+ draining: " {battery_level}% remaining "
+ power_saver: " {battery_level}% (power saver) "
+ charging: " {battery_level}% (charging) "
+```
+
## Window rules
Commands can be run when a window is initially launched. This can be used to assign an app to a specific workspace or to always start an app in floating mode.