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 4 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
24 changes: 24 additions & 0 deletions AutoDarkModeApp/Helpers/EnumToIndexConverte.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();
}
}
280 changes: 271 additions & 9 deletions AutoDarkModeApp/ViewModels/WallpaperPickerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,216 @@
using AutoDarkModeApp.Contracts.Services;
using System.Data;
using System.Diagnostics;
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;
using Microsoft.UI.Xaml.Navigation;

namespace AutoDarkModeApp.ViewModels;

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

public enum WallpaperDisplayMode
{
Picture,
PictureMM,
SolidColor,
Spotlight
}

public enum WallpaperDisplayFlags
{
None = 0,
ShowImageSettings = 1,
ShowMonitorSettings = 2,
ShowFillingWaySettings = 4,
ShowColorSettings = 8,

// Predefined combinations
PictureMode = ShowImageSettings,
PictureMMMode = ShowImageSettings | ShowMonitorSettings | ShowFillingWaySettings,
SolidColorMode = ShowColorSettings,
SpotlightMode = None
}

public enum WallpaperFillingMode
{
Fill,
Fit,
Stretch
}

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

[ObservableProperty]
public partial ApplicationTheme SelectWallpaperThemeMode { get; set; }

[ObservableProperty]
public partial WallpaperDisplayMode CurrentDisplayMode { get; set; }

[ObservableProperty]
public partial WallpaperDisplayFlags CurrentDisplayFlags { 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]
private bool _isWallpaperSwitchEnabled;
public partial WallpaperFillingMode SelectWallpaperFillingMode { get; set; }

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

public ICommand PickFileCommand { get; }

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

SelectWallpaperThemeMode = App.Current.RequestedTheme;

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

LoadSettings();

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;
}
});
}

public void OnNaOnNavigatedFrom(NavigationEventArgs e) => StateUpdateHandler.OnConfigUpdate -= HandleConfigUpdate;

private void LoadSettings()
{
IsWallpaperSwitchEnabled = _builder.Config.WallpaperSwitch.Enabled;
if (App.Current.RequestedTheme == Microsoft.UI.Xaml.ApplicationTheme.Light)

SelectWallpaperFillingMode = _builder.Config.WallpaperSwitch.Component.Position switch
{
WallpaperPosition.Fill => WallpaperFillingMode.Fill,
WallpaperPosition.Fit => WallpaperFillingMode.Fit,
WallpaperPosition.Stretch => WallpaperFillingMode.Stretch,
_ => WallpaperFillingMode.Fill
};

CurrentDisplayFlags = WallpaperDisplayFlags.None;

// Generate a list with all installed Monitors, select the first one
List<MonitorSettings> monitors = _builder.Config.WallpaperSwitch.Component.Monitors;
var disconnected = new List<MonitorSettings>();
var 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();

foreach (var monitor in disconnected)
{
monitor.MonitorString = $"{"DisplayMonitorDisconnected".GetLocalized()} - {monitor.MonitorString}";
}

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

Monitors = monitors;
SelectMonitor = monitors.FirstOrDefault();

var currentType = SelectWallpaperThemeMode == ApplicationTheme.Light
? _builder.Config.WallpaperSwitch.Component.TypeLight
: _builder.Config.WallpaperSwitch.Component.TypeDark;

CurrentDisplayMode = currentType switch
{
WallpaperType.Global => WallpaperDisplayMode.Picture,
WallpaperType.Individual => WallpaperDisplayMode.PictureMM,
WallpaperType.SolidColor => WallpaperDisplayMode.SolidColor,
WallpaperType.Spotlight => WallpaperDisplayMode.Spotlight,
_ => WallpaperDisplayMode.Picture
};

CurrentDisplayFlags = currentType switch
{
WallpaperType.Global => WallpaperDisplayFlags.PictureMode,
WallpaperType.Individual => WallpaperDisplayFlags.PictureMMMode,
WallpaperType.SolidColor => WallpaperDisplayFlags.SolidColorMode,
WallpaperType.Spotlight => WallpaperDisplayFlags.SpotlightMode,
_ => WallpaperDisplayFlags.None
};

if (currentType == WallpaperType.Global)
{
GlobalWallpaperPath = SelectWallpaperThemeMode == ApplicationTheme.Light
? _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Light
: _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Dark;
}
else if (currentType == WallpaperType.Individual)
{
SelectIndexWallpaperMode = 0;
MonitorSettings monitorSettings = (SelectMonitor != null) ? (MonitorSettings)SelectMonitor : (MonitorSettings)new MonitorSettings() { MonitorString = "Waiting" };
GlobalWallpaperPath = SelectWallpaperThemeMode == ApplicationTheme.Light
? monitorSettings.LightThemeWallpaper
: monitorSettings.DarkThemeWallpaper;
}
else
{
SelectIndexWallpaperMode = 1;
GlobalWallpaperPath = SelectWallpaperThemeMode == ApplicationTheme.Light
? _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Light
: _builder.Config.WallpaperSwitch.Component.GlobalWallpaper.Dark;
}

SetColorButtonForeground = SelectWallpaperThemeMode == ApplicationTheme.Light
? new SolidColorBrush(_builder.Config.WallpaperSwitch.Component.SolidColors.Light.ToColor())
: new SolidColorBrush(_builder.Config.WallpaperSwitch.Component.SolidColors.Dark.ToColor());

_isLoaded = true;
}

private void HandleConfigUpdate(object sender, FileSystemEventArgs e)
Expand All @@ -73,7 +235,107 @@ partial void OnIsWallpaperSwitchEnabledChanged(bool value)
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickPage");
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickerPage");
}
}

partial void OnSelectWallpaperThemeModeChanged(ApplicationTheme value)
{
if (_isLoaded == true)
{
LoadSettings();
}
}

partial void OnCurrentDisplayModeChanged(WallpaperDisplayMode value)
{
if (SelectWallpaperThemeMode == ApplicationTheme.Light)
{
_builder.Config.WallpaperSwitch.Component.TypeLight = CurrentDisplayMode switch
{
WallpaperDisplayMode.Picture => WallpaperType.Global,
WallpaperDisplayMode.PictureMM => WallpaperType.Individual,
WallpaperDisplayMode.SolidColor => WallpaperType.SolidColor,
WallpaperDisplayMode.Spotlight => WallpaperType.Spotlight,
_ => WallpaperType.Unknown
};
}
else
{
_builder.Config.WallpaperSwitch.Component.TypeDark = CurrentDisplayMode switch
{
WallpaperDisplayMode.Picture => WallpaperType.Global,
WallpaperDisplayMode.PictureMM => WallpaperType.Individual,
WallpaperDisplayMode.SolidColor => WallpaperType.SolidColor,
WallpaperDisplayMode.Spotlight => WallpaperType.Spotlight,
_ => WallpaperType.Unknown
};
}
try
{
_builder.Save();
}
catch (Exception ex)
{
_errorService.ShowErrorMessage(ex, App.MainWindow.Content.XamlRoot, "WallpaperPickerPage");
}
}

partial void OnGlobalWallpaperPathChanged(string? value)
{
if (CurrentDisplayMode == WallpaperDisplayMode.Picture)
{
if (SelectWallpaperThemeMode == ApplicationTheme.Light)
{
_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 (CurrentDisplayMode == WallpaperDisplayMode.PictureMM)
{
MonitorSettings monitorSettings = (SelectMonitor != null) ? (MonitorSettings)SelectMonitor : (MonitorSettings)new object();
if (SelectWallpaperThemeMode == ApplicationTheme.Light)
{
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, "WallpaperPickerPage");
}
}

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