Skip to content

Commit

Permalink
Dark mode (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithSilver authored May 14, 2024
1 parent 0dd6505 commit bf98eb1
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 73 deletions.
100 changes: 79 additions & 21 deletions LightBulb/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dialogHostAvalonia="clr-namespace:DialogHostAvalonia;assembly=DialogHost.Avalonia"
xmlns:framework="clr-namespace:LightBulb.Framework"
xmlns:sys="using:System"
xmlns:materialAssists="clr-namespace:Material.Styles.Assists;assembly=Material.Styles"
xmlns:materialControls="clr-namespace:Material.Styles.Controls;assembly=Material.Styles"
xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:materialStyles="clr-namespace:Material.Styles.Themes;assembly=Material.Styles"
RequestedThemeVariant="Light">
xmlns:materialStyles="clr-namespace:Material.Styles.Themes;assembly=Material.Styles">
<Application.DataTemplates>
<framework:ViewManager />
</Application.DataTemplates>

<Application.Styles>
<Style>
<Style.Resources>
Expand Down Expand Up @@ -79,19 +81,62 @@
</Style>
</Style>

<!-- Apply Slider Accent class, since Material doesn't do it itself -->
<Style Selector="Slider.accent">
<Style Selector="^ Border#PART_InactiveState">
<Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>
<Style Selector="^ Border#PART_Indicator">
<Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>
<Style Selector="^ Border#PART_HoverEffect">
<Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>

<Style Selector="^ Border#PART_ThumbGrip">
<Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>
</Style>

<!-- Text box -->
<Style Selector="TextBox">
<Setter Property="FontSize" Value="14" />
</Style>

<!-- Apply TextBox Accent class, since material doesn't do it itself -->
<Style Selector="TextBox.accent">
<Setter Property="SelectionBrush" Value="{DynamicResource MaterialSecondaryMidBrush}" />
<Setter Property="SelectionForegroundBrush" Value="{DynamicResource MaterialSecondaryForegroundBrush}" />
<Setter Property="CaretBrush" Value="{DynamicResource MaterialSecondaryMidBrush}" />
<Style Selector="^ materialControls|MaterialUnderline">
<Setter Property="ActiveBrush" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>
</Style>

<!-- Toggle button -->
<Style Selector="ToggleButton">
<Setter Property="TextElement.FontWeight" Value="Medium" />
</Style>

<!-- Combo box -->
<Style Selector="ComboBox">
<Style Selector="^ /template/ Panel#PART_RootPanel">
<Setter Property="Height" Value="22" />
</Style>
</Style>

<!-- Apply ComboBox Accent class, since material doesn't do it itself -->
<Style Selector="ComboBox.accent">
<Style Selector="^ materialControls|MaterialUnderline">
<Setter Property="ActiveBrush" Value="{DynamicResource MaterialSecondaryMidBrush}" />
</Style>
</Style>

<!-- Toggle switch -->
<Style Selector="ToggleSwitch">
<Setter Property="materialAssists:ToggleSwitchAssist.SwitchThumbOffBackground" Value="{DynamicResource MaterialBackgroundBrush}" />
<!-- Apply custom style for disabled toggles, because Material's default is bad.-->
<!-- Tracked in https://github.com/AvaloniaCommunity/Material.Avalonia/issues/379 -->
<Setter Property="materialAssists:ToggleSwitchAssist.SwitchThumbOffBackground" Value="{DynamicResource ToggleBackgroundBrush}" />
</Style>

<!-- Tooltip -->
Expand All @@ -104,25 +149,38 @@
</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>

<Style Selector="^ /template/ Panel#PART_TextContainer">
<Setter Property="Margin" Value="0" />
<ResourceDictionary>
<!-- 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>
</Style>
</Style>
</Styles>
</ControlTheme>
</Styles>
</ControlTheme>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<!-- Custom style for disabled toggles -->
<SolidColorBrush x:Key="ToggleBackgroundBrush" Color="#FFFFFF"></SolidColorBrush>
<sys:Boolean x:Key="UseAccentControls">false</sys:Boolean>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<!-- Custom style for disabled toggles -->
<SolidColorBrush x:Key="ToggleBackgroundBrush" Color="#8E8E8E"></SolidColorBrush>
<sys:Boolean x:Key="UseAccentControls">true</sys:Boolean>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>

<!-- Tray icon -->
Expand Down
64 changes: 58 additions & 6 deletions LightBulb/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Threading;
using LightBulb.Framework;
using LightBulb.Models;
using LightBulb.Services;
using LightBulb.Utils;
using LightBulb.Utils.Extensions;
Expand All @@ -26,6 +28,7 @@ public class App : Application, IDisposable

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

public App()
{
Expand Down Expand Up @@ -56,6 +59,22 @@ public App()

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

// Load settings
_settingsService.Load();

_settingsService.WatchProperty(
o => o.Theme,
() =>
{
if (PlatformSettings is IPlatformSettings settings)
{
SetupTheme(settings.GetColorValues());
}
},
false
);
}

public override void Initialize()
Expand Down Expand Up @@ -95,12 +114,45 @@ public override void OnFrameworkInitializationCompleted()

base.OnFrameworkInitializationCompleted();

// Set custom theme colors
this.LocateMaterialTheme<MaterialThemeBase>().CurrentTheme = Theme.Create(
Theme.Light,
Color.Parse("#343838"),
Color.Parse("#F9A825")
);
if (PlatformSettings is IPlatformSettings settings)
{
settings.ColorValuesChanged += PlatformSettings_ColorValuesChanged;
SetupTheme(settings.GetColorValues());
}
}

private void PlatformSettings_ColorValuesChanged(object? sender, PlatformColorValues colors)
{
SetupTheme(colors);
}

private void SetupTheme(PlatformColorValues colors)
{
var themeMode = _settingsService.Theme;
if (themeMode == ThemeMode.System)
{
themeMode =
colors.ThemeVariant == PlatformThemeVariant.Dark ? ThemeMode.Dark : ThemeMode.Light;
}

if (themeMode == ThemeMode.Dark)
{
RequestedThemeVariant = Avalonia.Styling.ThemeVariant.Dark;
this.LocateMaterialTheme<MaterialThemeBase>().CurrentTheme = Theme.Create(
Theme.Dark,
Color.Parse("#202222"),
Color.Parse("#F9A825")
);
}
else
{
RequestedThemeVariant = Avalonia.Styling.ThemeVariant.Light;
this.LocateMaterialTheme<MaterialThemeBase>().CurrentTheme = Theme.Create(
Theme.Light,
Color.Parse("#343838"),
Color.Parse("#F9A825")
);
}
}

private void TrayIcon_OnClicked(object? sender, EventArgs args) => this.TryFocusMainWindow();
Expand Down
2 changes: 1 addition & 1 deletion LightBulb/LightBulb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Deorcify" Version="1.0.2" PrivateAssets="all" />
<PackageReference Include="DialogHost.Avalonia" Version="0.7.7" />
<PackageReference Include="DotnetRuntimeBootstrapper" Version="2.5.3" PrivateAssets="all" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
<PackageReference Include="Material.Avalonia" Version="3.5.0" />
<PackageReference Include="Material.Avalonia" Version="3.6.0" />
<PackageReference Include="Material.Icons.Avalonia" Version="2.1.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Onova" Version="2.6.11" />
Expand Down
22 changes: 22 additions & 0 deletions LightBulb/Models/ThemeMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace LightBulb.Models;

/// <summary>
/// Describes the application's theme mode.
/// </summary>
public enum ThemeMode
{
/// <summary>
/// Use the light theme
/// </summary>
Light,

/// <summary>
/// Use the dark theme
/// </summary>
Dark,

/// <summary>
/// Use whichever theme is specified by system settings
/// </summary>
System
}
3 changes: 3 additions & 0 deletions LightBulb/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public partial class SettingsService() : SettingsBase(GetFilePath())
[property: JsonIgnore] // comes from registry
private bool _isAutoStartEnabled;

[ObservableProperty]
private ThemeMode _theme = ThemeMode.System;

[ObservableProperty]
private bool _isAutoUpdateEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using LightBulb.Services;
using System;
using System.Collections.Generic;
using LightBulb.Models;
using LightBulb.Services;

namespace LightBulb.ViewModels.Components.Settings;

Expand All @@ -11,6 +14,14 @@ public bool IsAutoStartEnabled
set => SettingsService.IsAutoStartEnabled = value;
}

public IReadOnlyList<ThemeMode> ThemeArray { get; } = Enum.GetValues<ThemeMode>();

public ThemeMode Theme
{
get => SettingsService.Theme;
set => SettingsService.Theme = value;
}

public bool IsAutoUpdateEnabled
{
get => SettingsService.IsAutoUpdateEnabled;
Expand Down
3 changes: 0 additions & 3 deletions LightBulb/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ Click LEARN MORE to find ways that you can help.
[RelayCommand]
private async Task InitializeAsync()
{
// Load settings
settingsService.Load();

await FinalizePendingUpdateAsync();
await ShowGammaRangePromptAsync();
await ShowFirstTimeExperienceMessageAsync();
Expand Down
Loading

0 comments on commit bf98eb1

Please sign in to comment.