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 1 commit
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
335 changes: 329 additions & 6 deletions AutoDarkModeApp/ViewModels/WallpaperPickerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
using AutoDarkModeApp.Contracts.Services;
using System.Windows.Input;
using AutoDarkModeApp.Contracts.Services;
using AutoDarkModeApp.Helpers;
using AutoDarkModeApp.Utils.Handlers;
using AutoDarkModeLib;
using AutoDarkModeLib.ComponentSettings.Base;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;

namespace AutoDarkModeApp.ViewModels;

Expand All @@ -12,10 +19,51 @@ public partial class WallpaperPickerViewModel : ObservableRecipient
private readonly IErrorService _errorService;

[ObservableProperty]
private bool _isWallpaperSwitchEnabled;
public partial bool IsWallpaperSwitchEnabled { get; set; }

[ObservableProperty]
private int _selectIndexWallpaperMode;
public partial int SelectIndexWallpaperMode { get; set; }

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

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

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

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

[ObservableProperty]
public partial Visibility ImageSettingsCardVisibility { get; set; }

[ObservableProperty]
public partial Visibility MonitorSettingsCardVisibility { get; set; }

[ObservableProperty]
public partial Visibility WallpaperFillingWaySettingsCardVisibility { get; set; }

[ObservableProperty]
public partial Visibility ColorSettingsCardVisibility { get; set; }

[ObservableProperty]
public partial string? GlobalWallpaperPath { get; set; }

[ObservableProperty]
public partial object? SelectMonitor { get; set; }

[ObservableProperty]
public partial List<MonitorSettings>? Monitors { get; set; }

[ObservableProperty]
public partial int SelectIndexWallpaperFillingWay { get; set; }

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

public ICommand PickFileCommand { get; }

public WallpaperPickerViewModel(IErrorService errorService)
{
Expand All @@ -36,21 +84,153 @@ public WallpaperPickerViewModel(IErrorService errorService)

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

PickFileCommand = new RelayCommand(async () =>
{
var openPicker = new Windows.Storage.Pickers.FileOpenPicker()
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg", ".jpeg", ".bmp", ".dib", ".png", ".jff", ".jpe", ".gif", ".tif", ".tiff", ".wdp", ".heic", ".heif", ".heics", ".heifs", ".hif", ".avci", ".avcs", ".avif", ".avifs", ".jxr", ".jxl" }
};
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow);
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd);
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
GlobalWallpaperPath = file.Path;
}
});
}

private void LoadSettings()
{
IsWallpaperSwitchEnabled = _builder.Config.WallpaperSwitch.Enabled;
if (App.Current.RequestedTheme == Microsoft.UI.Xaml.ApplicationTheme.Light)
SelectIndexWallpaperFillingWay = _builder.Config.WallpaperSwitch.Component.Position switch
{
WallpaperPosition.Fill => 0,
WallpaperPosition.Fit => 1,
WallpaperPosition.Stretch => 2,
_ => 0
};

ResetAllOptions();

// Generate a list with all installed Monitors, select the first one
List<MonitorSettings> monitors = _builder.Config.WallpaperSwitch.Component.Monitors;
List<MonitorSettings> disconnected = new();
List<MonitorSettings> connected = monitors.Where(m =>
{
// Preload tostring to avoid dropdown opening lag
m.ToString();
// Return monitors connecte to system connected monitors
if (m.Connected)
{
return true;
}
disconnected.Add(m);
return false;
}).ToList();

disconnected.ForEach(m =>
{
SelectIndexWallpaperMode = 0;
m.MonitorString = $"{"DisplayMonitorDisconnected".GetLocalized()} - {m.MonitorString}";
});

monitors.Clear();
monitors.AddRange(connected);
monitors.AddRange(disconnected);
Monitors = monitors;

if (Monitors.Count > 0)
{
SelectMonitor = Monitors[0];
}

if (SelectIndexWallpaperMode == 0)
{
switch (_builder.Config.WallpaperSwitch.Component.TypeLight)
{
case WallpaperType.Global:
IsPictureType = true;
ImageSettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.Individual:
IsPictureMMType = true;
ImageSettingsCardVisibility = Visibility.Visible;
MonitorSettingsCardVisibility = Visibility.Visible;
WallpaperFillingWaySettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.SolidColor:
IsSolidColorType = true;
ColorSettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.Spotlight:
IsSpotlightType = true;
break;
}

if (IsPictureType == true)
{
GlobalWallpaperPath = _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Light;
}
else if (IsPictureMMType == true)
{
MonitorSettings monitorSettings = (SelectMonitor != null) ? (MonitorSettings)SelectMonitor : (MonitorSettings)new object();
GlobalWallpaperPath = monitorSettings.LightThemeWallpaper;
}

SetColorButtonForeground = new SolidColorBrush(_builder.Config.WallpaperSwitch.Component.SolidColors.Light.ToColor());
}
else
{
SelectIndexWallpaperMode = 1;
switch (_builder.Config.WallpaperSwitch.Component.TypeDark)
{
case WallpaperType.Global:
IsPictureType = true;
ImageSettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.Individual:
IsPictureMMType = true;
ImageSettingsCardVisibility = Visibility.Visible;
MonitorSettingsCardVisibility = Visibility.Visible;
WallpaperFillingWaySettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.SolidColor:
IsSolidColorType = true;
ColorSettingsCardVisibility = Visibility.Visible;
break;
case WallpaperType.Spotlight:
IsSpotlightType = true;
break;
}

if (IsPictureType == true)
{
GlobalWallpaperPath = _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Dark;
}
else if (IsPictureMMType == true)
{
MonitorSettings monitorSettings = (SelectMonitor != null) ? (MonitorSettings)SelectMonitor : (MonitorSettings)new object();
GlobalWallpaperPath = monitorSettings.DarkThemeWallpaper;
}

SetColorButtonForeground = new SolidColorBrush(_builder.Config.WallpaperSwitch.Component.SolidColors.Dark.ToColor());
}
}

private void ResetAllOptions()
{
IsPictureType = false;
IsPictureMMType = false;
IsSolidColorType = false;
IsSpotlightType = false;
ImageSettingsCardVisibility = Visibility.Collapsed;
MonitorSettingsCardVisibility = Visibility.Collapsed;
WallpaperFillingWaySettingsCardVisibility = Visibility.Collapsed;
ColorSettingsCardVisibility = Visibility.Collapsed;
}

private void HandleConfigUpdate(object sender, FileSystemEventArgs e)
{
_dispatcherQueue.TryEnqueue(() =>
Expand All @@ -76,4 +256,147 @@ partial void OnIsWallpaperSwitchEnabledChanged(bool value)
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnSelectIndexWallpaperModeChanged(int value)
{
LoadSettings();
}

partial void OnIsPictureTypeChanged(bool value)
{
if (SelectIndexWallpaperMode == 0 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.Global;
}
else if (SelectIndexWallpaperMode == 1 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.Global;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnIsPictureMMTypeChanged(bool value)
{
if (SelectIndexWallpaperMode == 0 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.Individual;
}
else if (SelectIndexWallpaperMode == 1 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.Individual;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnIsSolidColorTypeChanged(bool value)
{
if (SelectIndexWallpaperMode == 0 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.SolidColor;
}
else if (SelectIndexWallpaperMode == 1 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.SolidColor;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnIsSpotlightTypeChanged(bool value)
{
if (SelectIndexWallpaperMode == 0 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.Spotlight;
}
else if (SelectIndexWallpaperMode == 1 && value == true)
{
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.Spotlight;
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnGlobalWallpaperPathChanged(string? value)
{
if (IsPictureType == true)
{
if (SelectIndexWallpaperMode == 0)
{
_builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Light = value;
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.Global;
}
else
{
_builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Dark = value;
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.Global;
}
}
else if (IsPictureMMType == true)
{
MonitorSettings monitorSettings = (SelectMonitor != null) ? (MonitorSettings)SelectMonitor : (MonitorSettings)new object();
if (SelectIndexWallpaperMode == 0)
{
monitorSettings.LightThemeWallpaper = value;
_builder.Config.WallpaperSwitch.Component.TypeLight = WallpaperType.Individual;
}
else
{
monitorSettings.DarkThemeWallpaper = value;
_builder.Config.WallpaperSwitch.Component.TypeDark = WallpaperType.Individual;
}
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}

partial void OnSelectIndexWallpaperFillingWayChanged(int value)
{
_builder.Config.WallpaperSwitch.Component.Position = value switch
{
0 => WallpaperPosition.Fill,
1 => WallpaperPosition.Fit,
2 => WallpaperPosition.Stretch,
_ => WallpaperPosition.Fill
};
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
}
}
}
Loading