Skip to content

Commit

Permalink
Dynamically update tray tooltip to display current color configuration (
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Apr 8, 2024
1 parent 6202f5b commit e4f61e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
42 changes: 40 additions & 2 deletions LightBulb/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Threading;
using LightBulb.Framework;
using LightBulb.Services;
using LightBulb.Utils;
using LightBulb.Utils.Extensions;
using LightBulb.ViewModels;
using LightBulb.ViewModels.Components;
Expand All @@ -18,6 +22,8 @@ namespace LightBulb;

public class App : Application, IDisposable
{
private readonly DisposableCollector _eventRoot = new();

private readonly ServiceProvider _services;
private readonly MainViewModel _mainViewModel;

Expand Down Expand Up @@ -52,7 +58,35 @@ public App()
_mainViewModel = _services.GetRequiredService<ViewModelManager>().CreateMainViewModel();
}

public override void Initialize() => AvaloniaXamlLoader.Load(this);
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);

// Tray icon does not support binding so we use this hack to update its tooltip
_eventRoot.Add(
_mainViewModel.Dashboard.WatchProperties(
[o => o.IsActive, o => o.CurrentConfiguration],
() =>
{
var status =
_mainViewModel.Dashboard.CurrentConfiguration.Temperature.ToString("F0")
+ " / "
+ _mainViewModel.Dashboard.CurrentConfiguration.Brightness.ToString("P0");
var tooltip =
"LightBulb"
+ Environment.NewLine
+ (_mainViewModel.Dashboard.IsActive ? status : "Disabled");
Dispatcher.UIThread.Invoke(() =>
{
if (TrayIcon.GetIcons(this)?.FirstOrDefault() is { } trayIcon)
trayIcon.ToolTipText = tooltip;
});
}
)
);
}

public override void OnFrameworkInitializationCompleted()
{
Expand Down Expand Up @@ -120,5 +154,9 @@ private void DisableTemporarily1MinuteMenuItem_OnClick(object? sender, EventArgs
private void ExitMenuItem_OnClick(object? sender, EventArgs args) =>
ApplicationLifetime?.TryShutdown();

public void Dispose() => _services.Dispose();
public void Dispose()
{
_eventRoot.Dispose();
_services.Dispose();
}
}
4 changes: 2 additions & 2 deletions LightBulb/ViewModels/Components/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
_eventRoot.Dispose();

_updateInstantTimer.Dispose();
_updateConfigurationTimer.Dispose();
_updateIsPausedTimer.Dispose();

_eventRoot.Dispose();

_enableAfterDelayRegistration?.Dispose();
}

Expand Down

0 comments on commit e4f61e5

Please sign in to comment.