Skip to content

Commit 9e7c4f4

Browse files
committed
Implements basic Light/Dark themes request in #7
1 parent 2cd7505 commit 9e7c4f4

File tree

11 files changed

+280
-80
lines changed

11 files changed

+280
-80
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Dataset Tag is a free and open-source tool designed to make the process of image
3030
- **Memory Feature**: Remembers both the tags in each category and the tags used for each image, allowing for easy edits later.
3131
- **Replicability**: It ofers the option of copying the trigger words and/or the selected tags using the system's cache memory, and pasting them on other selected images (easy copy/paste of tags among images).
3232
- **Draggable Panel Columns**: Each column has a grid splitter, which can be used to resize them in any ratio you desire.
33+
- **Theme**: Light and Dark theme support.
3334

3435
## Compiling from Source
3536

src/DatasetTag/App.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Application.Styles>
66
<SimpleTheme />
77
<!--<FluentTheme Mode="Light"/>-->
8+
<StyleInclude Source="/Common/Styles/Light.xaml"/>
89
<StyleInclude Source="/Common/Styles/TextBox.axaml"/>
910
<StyleInclude Source="/Common/Styles/ScrollBar.axaml"/>
1011
<StyleInclude Source="/Common/Styles/ScrollViewer.axaml"/>

src/DatasetTag/App.axaml.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1+
#region ========================================================================= USING =====================================================================================
12
using Avalonia;
23
using Avalonia.Controls.ApplicationLifetimes;
34
using Avalonia.Markup.Xaml;
5+
#endregion
46

5-
namespace DatasetTag
7+
namespace DatasetTag;
8+
9+
public partial class App : Application
610
{
7-
public partial class App : Application
8-
{
9-
public override void Initialize()
10-
{
11-
AvaloniaXamlLoader.Load(this);
12-
}
11+
#region ================================================================== FIELD MEMBERS ================================================================================
12+
public static StyleManager? styles;
13+
#endregion
1314

14-
public override void OnFrameworkInitializationCompleted()
15-
{
16-
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
17-
{
18-
desktop.MainWindow = new MainWindow();
19-
}
15+
#region ===================================================================== METHODS ===================================================================================
16+
/// <summary>
17+
/// Initializes the application by loading XAML etc.
18+
/// </summary>
19+
public override void Initialize()
20+
{
21+
AvaloniaXamlLoader.Load(this);
22+
}
2023

21-
base.OnFrameworkInitializationCompleted();
22-
}
24+
/// <summary>
25+
/// Framework initialization code
26+
/// </summary>
27+
public override void OnFrameworkInitializationCompleted()
28+
{
29+
styles = new StyleManager(this);
30+
MainWindow.ThemeChanged += styles.SwitchThemeByIndex;
31+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
32+
desktop.MainWindow = new MainWindow();
33+
base.OnFrameworkInitializationCompleted();
2334
}
35+
#endregion
2436
}

src/DatasetTag/Common/Enums/Themes.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace DatasetTag.Common.Enums;
2+
3+
/// <summary>
4+
/// Enumeration for the application's themes
5+
/// </summary>
6+
/// <remarks>
7+
/// Creation Date: 25th of July, 2021
8+
/// </remarks>
9+
public enum Themes
10+
{
11+
Dark,
12+
Light
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Styles xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:sys="clr-namespace:System;assembly=netstandard">
4+
5+
<Style>
6+
<Style.Resources>
7+
<Color x:Key="WindowBackground">#232323</Color>
8+
<Color x:Key="ImagesPanelBackground">#333333</Color>
9+
<Color x:Key="TagsPanelBackground">#353535</Color>
10+
<Color x:Key="TagsBackground">#606060</Color>
11+
<Color x:Key="SelectedTagsPanelBackground">#373737</Color>
12+
<Color x:Key="TextForeground">#FFFFFF</Color>
13+
</Style.Resources>
14+
</Style>
15+
</Styles>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Styles xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:sys="clr-namespace:System;assembly=netstandard">
4+
5+
<Style>
6+
<Style.Resources>
7+
<Color x:Key="WindowBackground">#D8D8D8</Color>
8+
<Color x:Key="ImagesPanelBackground">#ADD8E6</Color>
9+
<Color x:Key="TagsPanelBackground">#E0FFFF</Color>
10+
<Color x:Key="TagsBackground">#A9A9A9</Color>
11+
<Color x:Key="SelectedTagsPanelBackground">#87CEFA</Color>
12+
<Color x:Key="TextForeground">#000000</Color>
13+
</Style.Resources>
14+
</Style>
15+
</Styles>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#region ========================================================================= USING =====================================================================================
2+
using System;
3+
using Avalonia;
4+
using Avalonia.Markup.Xaml.Styling;
5+
using DatasetTag.Common.Enums;
6+
#endregion
7+
8+
namespace DatasetTag;
9+
10+
/// <summary>
11+
/// Class for managing the application's styles
12+
/// </summary>
13+
/// <remarks>
14+
/// Creation Date: 25th of July, 2021
15+
/// </remarks>
16+
public class StyleManager
17+
{
18+
#region ================================================================== FIELD MEMBERS ================================================================================
19+
private readonly Application application;
20+
private readonly StyleInclude darkStyle = CreateStyle("avares://DatasetTag/Common/Styles/Dark.xaml");
21+
private readonly StyleInclude lightStyle = CreateStyle("avares://DatasetTag/Common/Styles/Light.xaml");
22+
#endregion
23+
24+
#region ==================================================================== PROPERTIES =================================================================================
25+
public Themes CurrentTheme { get; private set; } = Themes.Light;
26+
#endregion
27+
28+
#region ====================================================================== CTOR =====================================================================================
29+
/// <summary>
30+
/// Overload C-tor
31+
/// </summary>
32+
/// <param name="application">The application for which to manage the style</param>
33+
public StyleManager(Application application)
34+
{
35+
this.application = application;
36+
// safe guard
37+
if (application.Styles.Count == 0)
38+
application.Styles.Add(darkStyle);
39+
else
40+
application.Styles[1] = darkStyle;
41+
}
42+
#endregion
43+
44+
#region ===================================================================== METHODS ===================================================================================
45+
/// <summary>
46+
/// Switches the current theme with the oposite theme
47+
/// </summary>
48+
public void SwitchTheme()
49+
{
50+
SetTheme(CurrentTheme switch
51+
{
52+
Themes.Dark => Themes.Light,
53+
Themes.Light => Themes.Dark,
54+
_ => throw new ArgumentOutOfRangeException(nameof(CurrentTheme))
55+
});
56+
}
57+
58+
/// <summary>
59+
/// Switches the curent theme to a theme whose index is equal to <paramref name="themeIndex"/>
60+
/// </summary>
61+
/// <param name="themeIndex">The index of the theme to set</param>
62+
public void SwitchThemeByIndex(int themeIndex)
63+
{
64+
application.Styles[1] = themeIndex == 0 ? darkStyle : lightStyle;
65+
CurrentTheme = themeIndex == 0 ? Themes.Dark : Themes.Light;
66+
}
67+
68+
/// <summary>
69+
/// Sets the currently used theme
70+
/// </summary>
71+
/// <param name="theme">The theme to be set</param>
72+
public void SetTheme(Themes theme)
73+
{
74+
// change the first style in the main window styles section, and the main window instantly refreshes
75+
// (invoke only from the UI thread!)
76+
application.Styles[1] = theme switch
77+
{
78+
Themes.Dark => lightStyle,
79+
Themes.Light => darkStyle,
80+
_ => throw new ArgumentOutOfRangeException(nameof(theme))
81+
};
82+
CurrentTheme = theme;
83+
}
84+
85+
/// <summary>
86+
/// Creates the style used for theming
87+
/// </summary>
88+
/// <param name="url">The url of the theme file to be used</param>
89+
/// <returns>The <see cref="StyleInclude"/> containing the styles inside the theme identified by <paramref name="url"/></returns>
90+
private static StyleInclude CreateStyle(string url)
91+
{
92+
return new StyleInclude(new Uri("resm:Styles?assembly=DatasetTag"))
93+
{
94+
Source = new Uri(url)
95+
};
96+
}
97+
#endregion
98+
}

src/DatasetTag/DatasetTag.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@
3434
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3535
</None>
3636
</ItemGroup>
37+
38+
<ItemGroup>
39+
<AvaloniaResource Include="Common\Styles\Dark.xaml" />
40+
<AvaloniaResource Include="Common\Styles\Light.xaml" />
41+
</ItemGroup>
3742
</Project>

0 commit comments

Comments
 (0)