Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically update tray tooltip to display current color configuration #283

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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