Skip to content

Commit

Permalink
Merge pull request #62 from XanatosX/feature/add-theme-switching
Browse files Browse the repository at this point in the history
Add Theme Switching
  • Loading branch information
XanatosX authored Feb 16, 2023
2 parents 52c245b + 1f7fda3 commit 7e69a42
Show file tree
Hide file tree
Showing 27 changed files with 688 additions and 45 deletions.
6 changes: 1 addition & 5 deletions src/ModularToolManager/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
xmlns:local="using:ModularToolManager"
x:Class="ModularToolManager.App"
xmlns:p="clr-namespace:ModularToolManager.Properties">
<!--
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
-->

<Application.Styles>
<FluentTheme Mode="Dark"/>
<StyleInclude Source="avares://ModularToolManager/Resources/Icons.axaml"/>
<StyleInclude Source="avares://ModularToolManager/Resources/LinkButtonStyle.axaml"/>
</Application.Styles>

<!-- Custom control styles -->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
1 change: 1 addition & 0 deletions src/ModularToolManager/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ModularToolManager.Models.Messages;
using ModularToolManager.Services.IO;
using ModularToolManager.Services.Settings;
using ModularToolManager.Services.Ui;
using ModularToolManager.ViewModels;
using ModularToolManager.Views;
using ModularToolManagerModel.Services.IO;
Expand Down
90 changes: 90 additions & 0 deletions src/ModularToolManager/Converters/Serialization/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Avalonia.Media;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ModularToolManager.Converters.Serialization;

/// <summary>
/// Converter class to get avalonia colors correctly laoded/saved into a json
/// </summary>
internal class ColorConverter : JsonConverter<Color>
{
/// <summary>
/// The current color mode for reading
/// </summary>
internal enum CurrentColorMode
{
Unknown,
Alpha,
Red,
Green,
Blue
}

/// <inheritdoc/>
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{

byte alpha = 0;
byte red = 0;
byte green = 0;
byte blue = 0;
CurrentColorMode currentColorMode = CurrentColorMode.Unknown;


while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
currentColorMode = reader.GetString() switch
{
"A" => CurrentColorMode.Alpha,
"R" => CurrentColorMode.Red,
"G" => CurrentColorMode.Green,
"B" => CurrentColorMode.Blue,
_ => CurrentColorMode.Unknown,
};
}
if (reader.TokenType == JsonTokenType.Number)
{
byte number = reader.GetByte();
switch (currentColorMode)
{
case CurrentColorMode.Unknown:
break;
case CurrentColorMode.Alpha:
alpha = number;
break;
case CurrentColorMode.Red:
red = number;
break;
case CurrentColorMode.Green:
green = number;
break;
case CurrentColorMode.Blue:
blue = number;
break;
default: break;
}
}
}

return new Color(alpha, red, green, blue);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("A", value.A);
writer.WriteNumber("R", value.R);
writer.WriteNumber("G", value.G);
writer.WriteNumber("B", value.B);
writer.WriteEndObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ public static IServiceCollection AddServices(this IServiceCollection collection)
.AddSingleton<IViewModelLocatorService, ViewModelLocator>()
.AddSingleton<IDependencyResolverService, MicrosoftDepdencyResolverService>()
.AddSingleton<ISettingsService, SerializedSettingsService>()
.AddTransient<ViewLocator>()
.AddSingleton<PluginSettingViewModelService>()
.AddSingleton<IThemeService, AvaloniaThemeService>()
.AddTransient<IImageService, ImageService>()
.AddTransient<ResourceReaderService>()
.AddTransient<GetApplicationInformationService>();
.AddTransient<GetApplicationInformationService>()
.AddTransient<ViewLocator>();
}
}
3 changes: 3 additions & 0 deletions src/ModularToolManager/Models/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace ModularToolManager.Models;
/// </summary>
public class ApplicationSettings
{
[JsonPropertyName("selected_theme")]
public int SelectedThemeId { get; set; }

/// <summary>
/// Should the application be always on top of all others
/// </summary>
Expand Down
65 changes: 65 additions & 0 deletions src/ModularToolManager/Models/ApplicationStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Avalonia.Media;
using Avalonia.Themes.Fluent;
using System.Text.Json.Serialization;

namespace ModularToolManager.Models;

/// <summary>
/// Style information for the application
/// </summary>
public class ApplicationStyle
{
/// <summary>
/// The id of the style
/// </summary>
[JsonPropertyName("id")]
public int Id { get; init; }

/// <summary>
/// IS this a dark or light style variant
/// </summary>
[JsonPropertyName("mode")]
public FluentThemeMode Mode { get; init; }

/// <summary>
/// The name of the translation key which is getting used for the name
/// </summary>
[JsonPropertyName("name_translation_key")]
public string? NameTranslationKey { get; init; }

/// <summary>
/// The name of the translation key which is getting used for the description
/// </summary>
[JsonPropertyName("description_translation_key")]
public string? DescriptionTranslationKey { get; init; }

/// <summary>
/// The translated name for the application style
/// </summary>
[JsonIgnore]
public string? Name { get; set; }

/// <summary>
/// The translated description for the application style
/// </summary>
[JsonIgnore]
public string? Description { get; set; }

/// <summary>
/// The tint color for the material
/// </summary>
[JsonPropertyName("tint_color")]
public Color? TintColor { get; init; }

/// <summary>
/// The opacity of the material
/// </summary>
[JsonPropertyName("material_opacity")]
public float MaterialOpacity { get; init; }

/// <summary>
/// The opacity of the tint
/// </summary>
[JsonPropertyName("tint_opacity")]
public float TintOpacity { get; init; }
}
17 changes: 17 additions & 0 deletions src/ModularToolManager/Models/Messages/ApplicationThemeUpdated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CommunityToolkit.Mvvm.Messaging.Messages;

namespace ModularToolManager.Models.Messages;

/// <summary>
/// Mesage that the application theme got changed
/// </summary>
internal class ApplicationThemeUpdated : ValueChangedMessage<int>
{
/// <summary>
/// Create a new instance of this class
/// </summary>
/// <param name="value">The theme id the application got updated to</param>
public ApplicationThemeUpdated(int value) : base(value)
{
}
}
13 changes: 13 additions & 0 deletions src/ModularToolManager/Models/ModalWindowInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace ModularToolManager.Models;

/// <summary>
/// Record to save internal data for showing a modal
/// </summary>
/// <param name="ThemeId">The id of the theme to use</param>
/// <param name="Title"> The title to show on the modal</param>
/// <param name="modalContent">The content of the modal</param>
/// <param name="IconName">The name of the icon to load</param>
/// <param name="CanResize">Can the modal be resized</param>
public record ModalWindowInformation(int ThemeId, string Title, ObservableObject modalContent, string? IconName, bool CanResize = true);
2 changes: 1 addition & 1 deletion src/ModularToolManager/Models/ShowWindowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ namespace ModularToolManager.Models;
/// <param name="ModalContent">The content of the modal</param>
/// <param name="StartupLocation">The location where the modal should be positioned on startup</param>
///
public record ShowWindowModel(string Title, string? ImagePath, ObservableObject? ModalContent, WindowStartupLocation StartupLocation);
public record ShowWindowModel(string Title, string? ImagePath, ObservableObject? ModalContent, WindowStartupLocation StartupLocation, bool CanResize = true);
4 changes: 3 additions & 1 deletion src/ModularToolManager/ModularToolManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
<None Remove=".gitignore" />
<None Remove="Resources\buildInStyles.json" />
<None Remove="Resources\dependencies.json" />
<None Remove="Resources\hotkeys.json" />
<None Remove="Resources\LICENSE" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\buildInStyles.json" />
<EmbeddedResource Include="Resources\dependencies.json" />
<EmbeddedResource Include="Resources\hotkeys.json" />
<EmbeddedResource Include="Resources\LICENSE" />
Expand All @@ -21,7 +23,7 @@
<PackageReference Include="Avalonia.Desktop" Version="0.10.18" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.18" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
Expand Down
36 changes: 36 additions & 0 deletions src/ModularToolManager/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/ModularToolManager/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
<data name="AllPlugins_Header_Settings" xml:space="preserve">
<value>Einstellungen</value>
</data>
<data name="App_Dark_Theme" xml:space="preserve">
<value>Dunkler Modus</value>
</data>
<data name="App_Dark_Theme_Description" xml:space="preserve">
<value>Dunkler Modus für die Applikation</value>
</data>
<data name="App_Light_Theme" xml:space="preserve">
<value>Heller Modus</value>
</data>
<data name="App_Light_Theme_Description" xml:space="preserve">
<value>Heller Modus für die Applikation</value>
</data>
<data name="Button_Open_Project" xml:space="preserve">
<value>Projekt Website im Browser öffnen</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions src/ModularToolManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@
<data name="Application_Name" xml:space="preserve">
<value>Modular Tool Manager</value>
</data>
<data name="App_Dark_Theme" xml:space="preserve">
<value>Dark mode</value>
</data>
<data name="App_Dark_Theme_Description" xml:space="preserve">
<value>Dark theme for the application</value>
</data>
<data name="App_Light_Theme" xml:space="preserve">
<value>Light mode</value>
</data>
<data name="App_Light_Theme_Description" xml:space="preserve">
<value>Light theme for the application</value>
</data>
<data name="Button_Open_Project" xml:space="preserve">
<value>Open project website</value>
</data>
Expand Down
30 changes: 30 additions & 0 deletions src/ModularToolManager/Resources/buildInStyles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": 0,
"mode": 1,
"name_translation_key": "App_Dark_Theme",
"description_translation_key": "App_Dark_Theme_Description",
"tint_color": {
"A": 255,
"R": 0,
"G": 0,
"B": 0
},
"material_opacity": 0.65,
"tint_opacity": 1
},
{
"id": 1,
"mode": 0,
"name_translation_key": "App_Light_Theme",
"description_translation_key": "App_Light_Theme_Description",
"tint_color": {
"A": 255,
"R": 255,
"G": 255,
"B": 255
},
"material_opacity": 0.65,
"tint_opacity": 1
}
]
Loading

0 comments on commit 7e69a42

Please sign in to comment.