Skip to content

Commit

Permalink
Merge branch 'master' into self-contained
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored May 15, 2024
2 parents c6372dd + 03218e8 commit 4b45e3e
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 135 deletions.
6 changes: 3 additions & 3 deletions LightBulb.Core.Tests/LightBulb.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion LightBulb.Core/LightBulb.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.28.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="JsonExtensions" Version="1.2.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion LightBulb.PlatformInterop/LightBulb.PlatformInterop.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.28.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions LightBulb.PlatformInterop/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public Timer(TimeSpan firstTickDelay, TimeSpan interval, Action tick)
public Timer(TimeSpan interval, Action callback)
: this(TimeSpan.Zero, interval, callback) { }

public TimeSpan Interval => _interval;

private void Tick()
{
// Prevent multiple reentry
Expand Down
77 changes: 56 additions & 21 deletions LightBulb/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,40 @@
xmlns:framework="clr-namespace:LightBulb.Framework"
xmlns:materialAssists="clr-namespace:Material.Styles.Assists;assembly=Material.Styles"
xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:materialStyles="clr-namespace:Material.Styles.Themes;assembly=Material.Styles">
xmlns:materialStyles="clr-namespace:Material.Styles.Themes;assembly=Material.Styles"
ActualThemeVariantChanged="Application_OnActualThemeVariantChanged">
<Application.DataTemplates>
<framework:ViewManager />
</Application.DataTemplates>

<Application.Styles>
<materialStyles:MaterialTheme />
<!-- This theme is used as a stub to pre-load default resources, the actual colors are set through code -->
<materialStyles:MaterialTheme
BaseTheme="Light"
PrimaryColor="Grey"
SecondaryColor="DeepOrange" />
<materialIcons:MaterialIconStyles />
<dialogHostAvalonia:DialogHostStyles />

<!-- Combo box -->
<Style Selector="ComboBox">
<Setter Property="FontSize" Value="14" />

<Style Selector="^ /template/ Panel#PART_RootPanel">
<Setter Property="Height" Value="22" />
</Style>

<Style Selector="^ /template/ ToggleButton">
<Style Selector="^:checked, ^:unchecked">
<Setter Property="Margin" Value="0" />
<Setter Property="CornerRadius" Value="0" />

<Style Selector="^ ContentPresenter#contentPresenter">
<Setter Property="Margin" Value="12,8" />
</Style>
</Style>
</Style>
</Style>

<!-- Dialog host -->
<Style Selector="dialogHostAvalonia|DialogHost">
<Setter Property="DialogMargin" Value="0" />
Expand Down Expand Up @@ -82,7 +106,7 @@

<!-- Toggle switch -->
<Style Selector="ToggleSwitch">
<Setter Property="materialAssists:ToggleSwitchAssist.SwitchThumbOffBackground" Value="{DynamicResource MaterialBackgroundBrush}" />
<Setter Property="materialAssists:ToggleSwitchAssist.SwitchThumbOffBackground" Value="{DynamicResource ToggleBackgroundBrush}" />
</Style>

<!-- Tooltip -->
Expand All @@ -95,25 +119,36 @@
</Application.Styles>

<Application.Resources>
<!-- Text box -->
<ControlTheme
x:Key="CompactTextBox"
BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="{x:Type TextBox}">
<Styles>
<Style Selector="TextBox">
<Setter Property="Height" Value="22" />

<Style Selector="^ /template/ Panel#PART_TextFieldPanel">
<Setter Property="MinHeight" Value="0" />
</Style>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="ToggleBackgroundBrush" Color="#FFFFFF" />
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="ToggleBackgroundBrush" Color="#8E8E8E" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<!-- Text box -->
<ControlTheme
x:Key="CompactTextBox"
BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="{x:Type TextBox}">
<Styles>
<Style Selector="TextBox">
<Setter Property="Height" Value="22" />

<Style Selector="^ /template/ Panel#PART_TextFieldPanel">
<Setter Property="MinHeight" Value="0" />
</Style>

<Style Selector="^ /template/ Panel#PART_TextContainer">
<Setter Property="Margin" Value="0" />
<Style Selector="^ /template/ Panel#PART_TextContainer">
<Setter Property="Margin" Value="0" />
</Style>
</Style>
</Style>
</Styles>
</ControlTheme>
</Styles>
</ControlTheme>
</ResourceDictionary>
</Application.Resources>

<!-- Tray icon -->
Expand Down
67 changes: 55 additions & 12 deletions LightBulb/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Threading;
using LightBulb.Framework;
using LightBulb.Services;
Expand All @@ -25,6 +26,7 @@ public class App : Application, IDisposable
private readonly DisposableCollector _eventRoot = new();

private readonly ServiceProvider _services;
private readonly SettingsService _settingsService;
private readonly MainViewModel _mainViewModel;

public App()
Expand Down Expand Up @@ -55,14 +57,29 @@ public App()
services.AddTransient<SettingsTabViewModelBase, LocationSettingsTabViewModel>();

_services = services.BuildServiceProvider(true);
_settingsService = _services.GetRequiredService<SettingsService>();
_mainViewModel = _services.GetRequiredService<ViewModelManager>().CreateMainViewModel();
}

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
// Re-initialize the theme when the user changes it
_eventRoot.Add(
_settingsService.WatchProperty(
o => o.Theme,
() =>
{
RequestedThemeVariant = _settingsService.Theme switch
{
ThemeVariant.Light => Avalonia.Styling.ThemeVariant.Light,
ThemeVariant.Dark => Avalonia.Styling.ThemeVariant.Dark,
_ => Avalonia.Styling.ThemeVariant.Default
};
InitializeTheme();
},
false
)
);

// Tray icon does not support binding so we use this hack to update its tooltip
// Tray icon does not support binding so we use this hack to synchronize its tooltip
_eventRoot.Add(
_mainViewModel.Dashboard.WatchProperties(
[o => o.IsActive, o => o.CurrentConfiguration],
Expand All @@ -83,26 +100,52 @@ public override void Initialize()
if (TrayIcon.GetIcons(this)?.FirstOrDefault() is { } trayIcon)
trayIcon.ToolTipText = tooltip;
});
}
},
false
)
);
}

public override void Initialize()
{
base.Initialize();

AvaloniaXamlLoader.Load(this);
}

private void InitializeTheme()
{
var actualTheme = RequestedThemeVariant?.Key switch
{
"Light" => PlatformThemeVariant.Light,
"Dark" => PlatformThemeVariant.Dark,
_ => PlatformSettings?.GetColorValues().ThemeVariant ?? PlatformThemeVariant.Light
};

this.LocateMaterialTheme<MaterialThemeBase>().CurrentTheme =
actualTheme == PlatformThemeVariant.Light
? Theme.Create(Theme.Light, Color.Parse("#343838"), Color.Parse("#F9A825"))
: Theme.Create(Theme.Dark, Color.Parse("#E8E8E8"), Color.Parse("#F9A825"));
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
desktopLifetime.MainWindow = new MainView { DataContext = _mainViewModel };

base.OnFrameworkInitializationCompleted();

// Set custom theme colors
this.LocateMaterialTheme<MaterialThemeBase>().CurrentTheme = Theme.Create(
Theme.Light,
Color.Parse("#343838"),
Color.Parse("#F9A825")
);
// Set up custom theme colors
InitializeTheme();

// Load settings
_settingsService.Load();
}

private void Application_OnActualThemeVariantChanged(object? sender, EventArgs args) =>
// Re-initialize the theme when the system theme changes
InitializeTheme();

private void TrayIcon_OnClicked(object? sender, EventArgs args) => this.TryFocusMainWindow();

private void ShowSettingsMenuItem_OnClick(object? sender, EventArgs args)
Expand Down
8 changes: 8 additions & 0 deletions LightBulb/Framework/ThemeVariant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LightBulb.Framework;

public enum ThemeVariant
{
System,
Light,
Dark
}
5 changes: 2 additions & 3 deletions LightBulb/LightBulb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>..\favicon.ico</ApplicationIcon>
</PropertyGroup>

Expand All @@ -20,10 +19,10 @@
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.10" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.10.6" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.10.9" />
<PackageReference Include="Cogwheel" Version="2.0.4" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.1" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="Deorcify" Version="1.0.2" PrivateAssets="all" />
<PackageReference Include="DialogHost.Avalonia" Version="0.7.7" />
<PackageReference Include="Material.Avalonia" Version="3.5.0" />
Expand Down
7 changes: 7 additions & 0 deletions LightBulb/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Cogwheel;
using CommunityToolkit.Mvvm.ComponentModel;
using LightBulb.Core;
using LightBulb.Framework;
using LightBulb.Models;
using LightBulb.PlatformInterop;
using LightBulb.Utils;
Expand Down Expand Up @@ -63,6 +64,9 @@ public partial class SettingsService() : SettingsBase(GetFilePath())
[ObservableProperty]
private double _configurationTransitionOffset;

[ObservableProperty]
private TimeSpan _configurationSmoothingMaxDuration = TimeSpan.FromSeconds(5);

// Location

[ObservableProperty]
Expand All @@ -81,6 +85,9 @@ public partial class SettingsService() : SettingsBase(GetFilePath())

// Advanced

[ObservableProperty]
private ThemeVariant _theme;

[ObservableProperty]
[property: JsonIgnore] // comes from registry
private bool _isAutoStartEnabled;
Expand Down
60 changes: 56 additions & 4 deletions LightBulb/ViewModels/Components/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public partial class DashboardViewModel : ViewModelBase
[ObservableProperty]
private ColorConfiguration _currentConfiguration = ColorConfiguration.Default;

private ColorConfiguration? _configurationSmoothingSource;
private ColorConfiguration? _configurationSmoothingTarget;

public DashboardViewModel(
SettingsService settingsService,
GammaService gammaService,
Expand Down Expand Up @@ -287,11 +290,60 @@ private void UpdateInstant()

private void UpdateConfiguration()
{
var isSmooth = _settingsService.IsConfigurationSmoothingEnabled && !IsCyclePreviewEnabled;
var isSmooth =
!IsCyclePreviewEnabled
&& CurrentConfiguration != TargetConfiguration
&& _settingsService.IsConfigurationSmoothingEnabled
&& _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds >= 0.1;

if (isSmooth)
{
// Check if the target configuration has changed since the last transition started
if (
_configurationSmoothingTarget != TargetConfiguration
|| _configurationSmoothingSource is null
)
{
_configurationSmoothingSource = CurrentConfiguration;
_configurationSmoothingTarget = TargetConfiguration;
}

var brightnessDelta = Math.Abs(
_configurationSmoothingTarget.Value.Brightness
- _configurationSmoothingSource.Value.Brightness
);

var brightnessStep = Math.Max(
brightnessDelta
/ _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds
* _updateConfigurationTimer.Interval.TotalSeconds,
0.08
);

var temperatureDelta = Math.Abs(
_configurationSmoothingTarget.Value.Temperature
- _configurationSmoothingSource.Value.Temperature
);

var temperatureStep = Math.Max(
temperatureDelta
/ _settingsService.ConfigurationSmoothingMaxDuration.TotalSeconds
* _updateConfigurationTimer.Interval.TotalSeconds,
30
);

CurrentConfiguration = isSmooth
? CurrentConfiguration.StepTo(TargetConfiguration, 30, 0.008)
: TargetConfiguration;
CurrentConfiguration = CurrentConfiguration.StepTo(
TargetConfiguration,
temperatureStep,
brightnessStep
);
}
else
{
CurrentConfiguration = TargetConfiguration;
_configurationSmoothingSource = null;
_configurationSmoothingTarget = null;
}

_gammaService.SetGamma(CurrentConfiguration);
}
Expand Down
Loading

0 comments on commit 4b45e3e

Please sign in to comment.