Skip to content

Feature: Mainly for updates of Personalization #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
5 changes: 5 additions & 0 deletions AutoDarkModeApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutoDarkModeApp.Services;
using AutoDarkModeApp.ViewModels;
using AutoDarkModeApp.Views;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -72,6 +73,10 @@ public App()
services.AddSingleton<IFileService, FileService>();

// Views and ViewModels
services.AddTransient<CursorsViewModel>();
services.AddTransient<CursorsPage>();
services.AddTransient<ColorizationViewModel>();
services.AddTransient<ColorizationPage>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<AboutViewModel>();
Expand Down
2 changes: 2 additions & 0 deletions AutoDarkModeApp/AutoDarkModeApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>Properties\PublishProfiles\win-$(Platform).pubxml</PublishProfile>
<LangVersion>Preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWinUI>true</UseWinUI>
Expand Down Expand Up @@ -40,6 +41,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250310001" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="3.0.0" />
<PackageReference Include="System.Drawing.Common" Version="9.0.4" />
<PackageReference Include="WinUIEx" Version="2.5.1" />
</ItemGroup>

Expand Down
24 changes: 24 additions & 0 deletions AutoDarkModeApp/Helpers/EnumToIndexConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.UI.Xaml.Data;

namespace AutoDarkModeApp.Helpers;

public class EnumToIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null) return 0;
return (int)value;
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (value is int index)
{
if (Enum.IsDefined(targetType, index))
{
return Enum.ToObject(targetType, index);
}
}
return 0;
}
}
35 changes: 35 additions & 0 deletions AutoDarkModeApp/Helpers/FlagsToVisibilityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Diagnostics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;

namespace AutoDarkModeApp.Helpers;

public class FlagsToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null || parameter == null)
return Visibility.Collapsed;

try
{
var inputValue = (Enum)value;
var targetFlag = parameter is string strParam
? (Enum)Enum.Parse(inputValue.GetType(), strParam)
: (Enum)Enum.ToObject(inputValue.GetType(), parameter);

return inputValue.HasFlag(targetFlag)
? Visibility.Visible
: Visibility.Collapsed;
}
catch
{
return Visibility.Collapsed;
}
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
}
2 changes: 2 additions & 0 deletions AutoDarkModeApp/Services/PageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public PageService()
Configure<AboutViewModel, AboutPage>();
Configure<SettingsViewModel, SettingsPage>();
Configure<WallpaperPickerViewModel, WallpaperPickerPage>();
Configure<ColorizationViewModel, ColorizationPage>();
Configure<CursorsViewModel, CursorsPage>();
}

public Type GetPageType(string key)
Expand Down
88 changes: 88 additions & 0 deletions AutoDarkModeApp/Utils/Handlers/CursorCollectionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#region copyright
// Copyright (C) 2022 Auto Dark Mode
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#endregion
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoDarkModeApp.Utils.Handlers;

public static class CursorCollectionHandler
{
public static List<string> GetCursors()
{
using var cursorsKeyUser = Registry.CurrentUser.OpenSubKey(@"Control Panel\Cursors\Schemes");
using var cursorsKeySystem = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cursors\Schemes");
List<string> cursors = new();

var userCursors = cursorsKeyUser?.GetValueNames();
if (userCursors != null)
{
cursors.AddRange(userCursors.ToArray().ToList());
}
var systemCursors = cursorsKeySystem?.GetValueNames();
if (systemCursors != null) cursors.AddRange(systemCursors);

return cursors;
}

public static string GetCurrentCursorScheme()
{
using var cursorsKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Cursors");
return (string)cursorsKey.GetValue("");
}

public static string[] GetCursorScheme(string name)
{

using var cursorsKeyUser = Registry.CurrentUser.OpenSubKey(@"Control Panel\Cursors\Schemes");
using var cursorsKeySystem = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Control Panel\Cursors\Schemes");

List<string> cursorsUser = new();
List<string> cursorsSystem = new();

var cursorsUserRaw = cursorsKeyUser?.GetValueNames();
if (cursorsUserRaw != null)
{
cursorsUser = cursorsUserRaw.ToArray().ToList();
}

var cursorsSystemRaw = cursorsKeySystem?.GetValueNames();
if (cursorsSystemRaw != null)
{
cursorsSystem = cursorsSystemRaw.ToArray().ToList();
}

var userTheme = cursorsUser.Where(x => x == name).FirstOrDefault();
var systemTheme = cursorsSystem.Where(x => x == name).FirstOrDefault();
string[] cursorsList = { };

if (userTheme != null)
{
cursorsList = ((string)cursorsKeyUser.GetValue(userTheme)).Split(",");
}
else if (systemTheme != null)
{
cursorsList = ((string)cursorsKeySystem.GetValue(systemTheme)).Split(",");
}

return cursorsList;
}
}
168 changes: 168 additions & 0 deletions AutoDarkModeApp/ViewModels/ColorizationViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System.Diagnostics;
using AutoDarkModeApp.Contracts.Services;
using AutoDarkModeApp.Utils.Handlers;
using AutoDarkModeLib;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.WinUI.Helpers;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;

namespace AutoDarkModeApp.ViewModels;

public partial class ColorizationViewModel : ObservableRecipient
{
private readonly AdmConfigBuilder _builder = AdmConfigBuilder.Instance();
private readonly Microsoft.UI.Dispatching.DispatcherQueue _dispatcherQueue;
private readonly IErrorService _errorService;

public enum ThemeColorMode
{
Automatic,
Manual,
}

[ObservableProperty]
public partial bool IsColorizationEnabled { get; set; }

[ObservableProperty]
public partial ThemeColorMode LightThemeMode { get; set; }

[ObservableProperty]
public partial ThemeColorMode DarkThemeMode { get; set; }

[ObservableProperty]
public partial bool IsLightThemeSettingsCardEnabled { get; set; }

[ObservableProperty]
public partial bool IsDarkThemeSettingsCardEnabled { get; set; }

[ObservableProperty]
public partial SolidColorBrush? LightModeColorPreviewBorderBackground { get; set; }

[ObservableProperty]
public partial SolidColorBrush? DarkModeColorPreviewBorderBackground { get; set; }

public ColorizationViewModel(IErrorService errorService)
{
_dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
_errorService = errorService;

try
{
_builder.Load();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "ColorizationPage");
}

LoadSettings();

StateUpdateHandler.OnConfigUpdate += HandleConfigUpdate;
StateUpdateHandler.StartConfigWatcher();
}

private void LoadSettings()
{
IsColorizationEnabled = _builder.Config.ColorizationSwitch.Enabled;
if (_builder.Config.ColorizationSwitch.Component.LightAutoColorization)
{
LightThemeMode = ThemeColorMode.Automatic;
IsLightThemeSettingsCardEnabled = false;
}
else
{
LightThemeMode = ThemeColorMode.Manual;
IsLightThemeSettingsCardEnabled = true;
}
if (_builder.Config.ColorizationSwitch.Component.DarkAutoColorization)
{
DarkThemeMode = ThemeColorMode.Automatic;
IsDarkThemeSettingsCardEnabled = false;
}
else
{
DarkThemeMode = ThemeColorMode.Manual;
IsDarkThemeSettingsCardEnabled = true;
}
LightModeColorPreviewBorderBackground = new SolidColorBrush(_builder.Config.ColorizationSwitch.Component.LightHex.ToColor());
DarkModeColorPreviewBorderBackground = new SolidColorBrush(_builder.Config.ColorizationSwitch.Component.DarkHex.ToColor());
}

private void HandleConfigUpdate(object sender, FileSystemEventArgs e)
{
_dispatcherQueue.TryEnqueue(() =>
{
StateUpdateHandler.StopConfigWatcher();

_builder.Load();
LoadSettings();

StateUpdateHandler.StartConfigWatcher();
});
}

partial void OnIsColorizationEnabledChanged(bool value)
{
_builder.Config.ColorizationSwitch.Enabled = value;
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "ColorizationPage");
}
}

partial void OnLightThemeModeChanged(ThemeColorMode value)
{
if (value == ThemeColorMode.Automatic)
{
_builder.Config.ColorizationSwitch.Component.LightAutoColorization = true;
IsLightThemeSettingsCardEnabled = false;
Debug.WriteLine(IsLightThemeSettingsCardEnabled);
}
else
{
_builder.Config.ColorizationSwitch.Component.LightAutoColorization = false;
IsLightThemeSettingsCardEnabled = true;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "ColorizationPage");
}
}

partial void OnDarkThemeModeChanged(ThemeColorMode value)
{
if (value == ThemeColorMode.Automatic)
{
_builder.Config.ColorizationSwitch.Component.DarkAutoColorization = true;
IsDarkThemeSettingsCardEnabled = false;
}
else
{
_builder.Config.ColorizationSwitch.Component.DarkAutoColorization = false;
IsDarkThemeSettingsCardEnabled = true;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "ColorizationPage");
}
}

internal void OnViewModelNavigatedFrom(NavigationEventArgs e)
{
StateUpdateHandler.OnConfigUpdate -= HandleConfigUpdate;
StateUpdateHandler.StopConfigWatcher();
}
}
Loading