Skip to content
Open
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
47 changes: 25 additions & 22 deletions src/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
Expand All @@ -14,12 +14,14 @@
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Threading;
using SourceGit.Models;

namespace SourceGit
{
public partial class App : Application
{
#region App Entry Point

[STAThread]
public static void Main(string[] args)
{
Expand Down Expand Up @@ -56,10 +58,7 @@ public static AppBuilder BuildAvaloniaApp()
builder.UsePlatformDetect();
builder.LogToTrace();
builder.WithInterFont();
builder.With(new FontManagerOptions()
{
DefaultFamilyName = "fonts:Inter#Inter"
});
builder.With(new FontManagerOptions() { DefaultFamilyName = "fonts:Inter#Inter" });
builder.ConfigureFonts(manager =>
{
var monospace = new EmbeddedFontCollection(
Expand All @@ -71,9 +70,11 @@ public static AppBuilder BuildAvaloniaApp()
Native.OS.SetupApp(builder);
return builder;
}

#endregion

#region Utility Functions

public static async Task<bool> AskConfirmAsync(string message, Models.ConfirmButtonType buttonType = Models.ConfirmButtonType.OkCancel)
{
if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner })
Expand Down Expand Up @@ -117,17 +118,21 @@ app.Resources[finalLocaleKey] is not ResourceDictionary targetLocale ||
app._activeLocale = targetLocale;
}

public static void SetTheme(string theme, string themeOverridesFile)
public static void SetTheme(ThemeOverrides theme)
{
if (Current is not App app)
return;

if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase))
string themeOverridesFile = string.Empty;

if (theme.Name.Equals("Light", StringComparison.OrdinalIgnoreCase))
app.RequestedThemeVariant = ThemeVariant.Light;
else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase))
else if (theme.Name.Equals("Dark", StringComparison.OrdinalIgnoreCase))
app.RequestedThemeVariant = ThemeVariant.Dark;
else
else if (theme.Name.Equals("Default", StringComparison.OrdinalIgnoreCase))
app.RequestedThemeVariant = ThemeVariant.Default;
else
themeOverridesFile = theme.FilePath;

if (app._themeOverrides != null)
{
Expand Down Expand Up @@ -234,16 +239,18 @@ public static void Quit(int exitCode)
else
Environment.Exit(exitCode);
}

#endregion

#region Overrides

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);

var pref = ViewModels.Preferences.Instance;
SetLocale(pref.Locale);
SetTheme(pref.Theme, pref.ThemeOverrides);
SetTheme(pref.Themes.FirstOrDefault(m => m.Name == pref.Theme) ?? new ThemeOverrides("Default"));
SetFonts(pref.DefaultFontFamily, pref.MonospaceFontFamily);
}

Expand Down Expand Up @@ -276,9 +283,11 @@ public override void OnFrameworkInitializationCompleted()
TryLaunchAsNormal(desktop);
}
}

#endregion

#region Launch Ways

private static bool TryLaunchAsRebaseTodoEditor(string[] args, out int exitCode)
{
exitCode = -1;
Expand Down Expand Up @@ -362,17 +371,11 @@ private bool TryLaunchAsFileHistoryViewer(IClassicDesktopStyleApplicationLifetim
var relativePath = Path.GetRelativePath(repo, fullPath).Replace('\\', '/');
if (File.Exists(fullPath))
{
desktop.MainWindow = new Views.FileHistories()
{
DataContext = new ViewModels.FileHistories(repo, relativePath)
};
desktop.MainWindow = new Views.FileHistories() { DataContext = new ViewModels.FileHistories(repo, relativePath) };
}
else if (Directory.Exists(fullPath))
{
desktop.MainWindow = new Views.DirHistories()
{
DataContext = new ViewModels.DirHistories(repo, relativePath.TrimEnd('/'))
};
desktop.MainWindow = new Views.DirHistories() { DataContext = new ViewModels.DirHistories(repo, relativePath.TrimEnd('/')) };
}
else
{
Expand Down Expand Up @@ -410,10 +413,7 @@ private bool TryLaunchAsBlameViewer(IClassicDesktopStyleApplicationLifetime desk
}

var relFile = Path.GetRelativePath(repo, file);
var viewer = new Views.Blame()
{
DataContext = new ViewModels.Blame(repo, relFile, head)
};
var viewer = new Views.Blame() { DataContext = new ViewModels.Blame(repo, relFile, head) };
desktop.MainWindow = viewer;
return true;
}
Expand Down Expand Up @@ -532,9 +532,11 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
Check4Update();
#endif
}

#endregion

#region Check for Updates

private void Check4Update(bool manually = false)
{
if (_launcher != null)
Expand Down Expand Up @@ -595,6 +597,7 @@ private void ShowSelfUpdateResult(object data)
// Ignore exceptions.
}
}

#endregion

private Models.IpcChannel _ipcChannel = null;
Expand Down
14 changes: 14 additions & 0 deletions src/Models/ThemeOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ namespace SourceGit.Models
{
public class ThemeOverrides
{
public ThemeOverrides()
{
}

public ThemeOverrides(string name)
{
Name = name;
}

public string Version { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string Url { get; set; }
public Dictionary<string, Color> BasicColors { get; set; } = new Dictionary<string, Color>();
public double GraphPenThickness { get; set; } = 2;
public double OpacityForNotMergedCommits { get; set; } = 0.5;
public List<Color> GraphColors { get; set; } = new List<Color>();
public string FilePath { get; set; }
}
}
48 changes: 38 additions & 10 deletions src/ViewModels/Preferences.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Avalonia.Collections;
using CommunityToolkit.Mvvm.ComponentModel;
using SourceGit.Models;

namespace SourceGit.ViewModels
{
Expand Down Expand Up @@ -41,26 +43,33 @@ public string Locale
}
}

public string Theme
[JsonIgnore]
public List<ThemeOverrides> Themes
{
get => _theme;
get => _themes;
set
{
if (SetProperty(ref _theme, value) && !_isLoading)
App.SetTheme(_theme, _themeOverrides);
SetProperty(ref _themes, value);
}
}

public string ThemeOverrides
[JsonIgnore]
public ThemeOverrides SelectedTheme
{
get => _themeOverrides;
get
{
return Themes.FirstOrDefault(m => m.Name == Theme) ?? new ThemeOverrides("Default");
}
set
{
if (SetProperty(ref _themeOverrides, value) && !_isLoading)
App.SetTheme(_theme, value);
if (SetProperty(ref _selectedTheme, value) && !_isLoading)
App.SetTheme(_selectedTheme);
Theme = _selectedTheme.Name;
}
}

public string Theme { get; set; }

public string DefaultFontFamily
{
get => _defaultFontFamily;
Expand Down Expand Up @@ -649,6 +658,25 @@ public void Save()

private static Preferences Load()
{
try
{
var themesPath = Path.Combine(Native.OS.DataDir, "themes");
var themeFiles = Directory.GetFiles(themesPath, "theme.json", SearchOption.AllDirectories);
foreach (var file in themeFiles)
{
using var stream = File.OpenRead(file);
var theme = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.ThemeOverrides);
if (theme == null)
continue;
theme.FilePath = file;
_themes.Add(theme);
}
}
catch
{
// ignored
}

var path = Path.Combine(Native.OS.DataDir, "preference.json");
if (!File.Exists(path))
return new Preferences();
Expand Down Expand Up @@ -791,8 +819,8 @@ private bool RemoveInvalidRepositoriesRecursive(List<RepositoryNode> collection)
private bool _isLoading = true;
private bool _isReadonly = true;
private string _locale = "en_US";
private string _theme = "Default";
private string _themeOverrides = string.Empty;
private ThemeOverrides _selectedTheme = new("Default");
private static List<ThemeOverrides> _themes = new() { new ThemeOverrides("Default"), new ThemeOverrides("Dark"), new ThemeOverrides("Light") };
private string _defaultFontFamily = string.Empty;
private string _monospaceFontFamily = string.Empty;
private double _defaultFontSize = 13;
Expand Down
59 changes: 25 additions & 34 deletions src/Views/Preferences.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,31 @@
Text="{DynamicResource Text.Preferences.Appearance.Theme}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<ComboBox Grid.Row="0" Grid.Column="1"
MinHeight="28"
Padding="8,0"
HorizontalAlignment="Stretch"
DisplayMemberBinding="{Binding Key, x:DataType=ThemeVariant}"
SelectedItem="{Binding Theme, Mode=TwoWay, Converter={x:Static c:StringConverters.ToTheme}}">
<ComboBox.Items>
<ThemeVariant>Default</ThemeVariant>
<ThemeVariant>Dark</ThemeVariant>
<ThemeVariant>Light</ThemeVariant>
</ComboBox.Items>
</ComboBox>

<Grid Grid.Row="0" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0" Grid.Row="0" MinHeight="28"
HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch"
Padding="8,0"
ItemsSource="{Binding Themes, Mode=OneWay}"
SelectedItem="{Binding SelectedTheme, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:ThemeOverrides}">
<TextBlock Text="{Binding Name, Mode=OneWay}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Grid.Column="1" Grid.Row="0" Classes="icon_button" Width="28" Height="28" Click="OpenThemeRepository">
<Path Width="14" Height="14" Data="{StaticResource Icons.Remotes}" Fill="{DynamicResource Brush.FG1}"/>
</Button>
<Button Grid.Column="2" Grid.Row="0" Classes="icon_button" Width="28" Height="28" Click="OpenThemeFolder">
<Path Width="14" Height="14" Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/>
</Button>
</Grid>

<TextBlock Grid.Row="1" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.DefaultFont}"
HorizontalAlignment="Right"
Expand Down Expand Up @@ -270,27 +282,6 @@
Value="{Binding EditorTabWidth, Mode=TwoWay}"/>
</Grid>

<TextBlock Grid.Row="5" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.ThemeOverrides}"
HorizontalAlignment="Right"
Margin="0,0,16,0"/>
<TextBox Grid.Row="5" Grid.Column="1"
Height="28"
CornerRadius="3"
Text="{Binding ThemeOverrides, Mode=TwoWay}">
<TextBox.InnerRightContent>
<StackPanel Orientation="Horizontal">
<Button Classes="icon_button" Width="28" Height="28" Click="SelectThemeOverrideFile">
<Path Width="16" Height="16" Data="{StaticResource Icons.Folder.Open}" Margin="0,2,0,0" Fill="{DynamicResource Brush.FG1}"/>
</Button>

<Button Classes="icon_button" Width="28" Height="28" Click="OpenThemeRepository">
<Path Width="14" Height="14" Data="{StaticResource Icons.Remotes}" Fill="{DynamicResource Brush.FG1}"/>
</Button>
</StackPanel>
</TextBox.InnerRightContent>
</TextBox>

<CheckBox Grid.Row="6" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseFixedTabWidth}"
Expand Down
Loading