2024-09-19 09:12:24 -07:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using Common.UI;
|
|
|
|
|
|
using global::PowerToys.GPOWrapper;
|
|
|
|
|
|
using ManagedCommon;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Helpers;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
|
|
|
|
|
|
using Windows.ApplicationModel.VoiceCommands;
|
|
|
|
|
|
using Windows.System;
|
|
|
|
|
|
using static Microsoft.PowerToys.Settings.UI.Helpers.ShellGetFolder;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|
|
|
|
|
{
|
|
|
|
|
|
public class NewPlusViewModel : Observable
|
|
|
|
|
|
{
|
|
|
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ISettingsUtils _settingsUtils;
|
|
|
|
|
|
|
|
|
|
|
|
private NewPlusSettings Settings { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private const string ModuleName = NewPlusSettings.ModuleName;
|
|
|
|
|
|
|
|
|
|
|
|
public NewPlusViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
|
|
|
|
|
|
{
|
|
|
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
|
|
|
|
|
|
|
|
|
|
|
// To obtain the general settings configurations of PowerToys Settings.
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(settingsRepository);
|
|
|
|
|
|
|
|
|
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
|
|
|
|
|
|
|
|
|
|
|
Settings = LoadSettings(settingsUtils);
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize properties
|
|
|
|
|
|
_hideFileExtension = Settings.HideFileExtension;
|
|
|
|
|
|
_hideStartingDigits = Settings.HideStartingDigits;
|
|
|
|
|
|
_templateLocation = Settings.TemplateLocation;
|
|
|
|
|
|
InitializeEnabledValue();
|
2024-09-24 17:33:01 +02:00
|
|
|
|
InitializeGpoValues();
|
2024-09-19 09:12:24 -07:00
|
|
|
|
|
|
|
|
|
|
// set the callback functions value to handle outgoing IPC message.
|
|
|
|
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitializeEnabledValue()
|
|
|
|
|
|
{
|
|
|
|
|
|
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredNewPlusEnabledValue();
|
|
|
|
|
|
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the enabled state from GPO.
|
|
|
|
|
|
_enabledStateIsGPOConfigured = true;
|
|
|
|
|
|
_isNewPlusEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_isNewPlusEnabled = GeneralSettingsConfig.Enabled.NewPlus;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-24 17:33:01 +02:00
|
|
|
|
private void InitializeGpoValues()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Policy for hide file extension setting
|
|
|
|
|
|
_hideFileExtensionGpoRuleConfiguration = GPOWrapper.GetConfiguredNewPlusHideTemplateFilenameExtensionValue();
|
|
|
|
|
|
_hideFileExtensionIsGPOConfigured = _hideFileExtensionGpoRuleConfiguration == GpoRuleConfigured.Disabled || _hideFileExtensionGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-19 09:12:24 -07:00
|
|
|
|
public bool IsEnabled
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _isNewPlusEnabled;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isNewPlusEnabled != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_isNewPlusEnabled = value;
|
|
|
|
|
|
|
|
|
|
|
|
GeneralSettingsConfig.Enabled.NewPlus = value;
|
|
|
|
|
|
OnPropertyChanged(nameof(IsEnabled));
|
2024-09-24 17:33:01 +02:00
|
|
|
|
OnPropertyChanged(nameof(IsHideFileExtSettingsCardEnabled));
|
|
|
|
|
|
OnPropertyChanged(nameof(IsHideFileExtSettingGPOConfigured));
|
2024-09-19 09:12:24 -07:00
|
|
|
|
|
|
|
|
|
|
OutGoingGeneralSettings outgoingMessage = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
|
|
|
|
|
SendConfigMSG(outgoingMessage.ToString());
|
|
|
|
|
|
|
|
|
|
|
|
NotifySettingsChanged();
|
|
|
|
|
|
|
|
|
|
|
|
if (_isNewPlusEnabled == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
CopyTemplateExamples(_templateLocation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsWin10OrLower
|
|
|
|
|
|
{
|
|
|
|
|
|
get => !OSVersionHelper.IsWindows11();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string TemplateLocation
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _templateLocation;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_templateLocation != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_templateLocation = value;
|
|
|
|
|
|
Settings.TemplateLocation = value;
|
|
|
|
|
|
OnPropertyChanged(nameof(TemplateLocation));
|
|
|
|
|
|
|
|
|
|
|
|
NotifySettingsChanged();
|
|
|
|
|
|
|
|
|
|
|
|
SaveSettingsToJson();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool HideFileExtension
|
|
|
|
|
|
{
|
2024-09-24 17:33:01 +02:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_hideFileExtensionIsGPOConfigured)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _hideFileExtensionGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _hideFileExtension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-19 09:12:24 -07:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2024-09-24 17:33:01 +02:00
|
|
|
|
if (_hideFileExtension != value && !_hideFileExtensionIsGPOConfigured)
|
2024-09-19 09:12:24 -07:00
|
|
|
|
{
|
|
|
|
|
|
_hideFileExtension = value;
|
|
|
|
|
|
Settings.HideFileExtension = value;
|
|
|
|
|
|
OnPropertyChanged(nameof(HideFileExtension));
|
|
|
|
|
|
|
|
|
|
|
|
NotifySettingsChanged();
|
|
|
|
|
|
|
|
|
|
|
|
SaveSettingsToJson();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-24 17:33:01 +02:00
|
|
|
|
public bool IsHideFileExtSettingsCardEnabled => _isNewPlusEnabled && !_hideFileExtensionIsGPOConfigured;
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsHideFileExtSettingGPOConfigured => _isNewPlusEnabled && _hideFileExtensionIsGPOConfigured;
|
|
|
|
|
|
|
2024-09-19 09:12:24 -07:00
|
|
|
|
public bool HideStartingDigits
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _hideStartingDigits;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_hideStartingDigits != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_hideStartingDigits = value;
|
|
|
|
|
|
Settings.HideStartingDigits = value;
|
|
|
|
|
|
OnPropertyChanged(nameof(HideStartingDigits));
|
|
|
|
|
|
|
|
|
|
|
|
NotifySettingsChanged();
|
|
|
|
|
|
|
|
|
|
|
|
SaveSettingsToJson();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsEnabledGpoConfigured
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _enabledStateIsGPOConfigured;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ButtonClickCommand OpenCurrentNewTemplateFolder => new ButtonClickCommand(OpenNewTemplateFolder);
|
|
|
|
|
|
|
|
|
|
|
|
public ButtonClickCommand PickAnotherNewTemplateFolder => new ButtonClickCommand(PickNewTemplateFolder);
|
|
|
|
|
|
|
|
|
|
|
|
private void NotifySettingsChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Using InvariantCulture as this is an IPC message
|
|
|
|
|
|
SendConfigMSG(
|
|
|
|
|
|
string.Format(
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
|
|
|
|
|
ModuleName,
|
|
|
|
|
|
JsonSerializer.Serialize(Settings)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Func<string, int> SendConfigMSG { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public static NewPlusSettings LoadSettings(ISettingsUtils settingsUtils)
|
|
|
|
|
|
{
|
|
|
|
|
|
NewPlusSettings settings = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-09-23 14:28:39 +02:00
|
|
|
|
settings = settingsUtils.GetSettingsOrDefault<NewPlusSettings>(NewPlusSettings.ModuleName);
|
2024-09-19 09:12:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.LogError($"Exception encountered while reading {NewPlusSettings.ModuleName} settings.", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void CopyTemplateExamples(string templateLocation)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(templateLocation))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(templateLocation);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Directory.GetFiles(templateLocation).Length == 0 && Directory.GetDirectories(templateLocation).Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// No files in templateLocation directory
|
|
|
|
|
|
// Copy over examples files from <Program Files>\PowerToys\WinUI3Apps\Assets\NewPlus\Templates
|
|
|
|
|
|
var example_templates = Path.Combine(Helper.GetPowerToysInstallationWinUI3AppsAssetsFolder(), "NewPlus", "Templates");
|
|
|
|
|
|
Helper.CopyDirectory(example_templates, templateLocation, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool _isNewPlusEnabled;
|
|
|
|
|
|
private string _templateLocation;
|
|
|
|
|
|
private bool _hideFileExtension;
|
|
|
|
|
|
private bool _hideStartingDigits;
|
|
|
|
|
|
|
2024-09-24 17:33:01 +02:00
|
|
|
|
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
|
|
|
|
|
private bool _enabledStateIsGPOConfigured;
|
|
|
|
|
|
private GpoRuleConfigured _hideFileExtensionGpoRuleConfiguration;
|
|
|
|
|
|
private bool _hideFileExtensionIsGPOConfigured;
|
|
|
|
|
|
|
2024-09-19 09:12:24 -07:00
|
|
|
|
public void RefreshEnabledState()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeEnabledValue();
|
|
|
|
|
|
OnPropertyChanged(nameof(IsEnabled));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OpenNewTemplateFolder()
|
|
|
|
|
|
{
|
|
|
|
|
|
var process = new ProcessStartInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
FileName = _templateLocation,
|
|
|
|
|
|
UseShellExecute = true,
|
|
|
|
|
|
};
|
|
|
|
|
|
Process.Start(process);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void PickNewTemplateFolder()
|
|
|
|
|
|
{
|
|
|
|
|
|
var newPath = await PickFolderDialog();
|
2024-09-25 15:29:08 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(newPath))
|
2024-09-19 09:12:24 -07:00
|
|
|
|
{
|
|
|
|
|
|
TemplateLocation = newPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<string> PickFolderDialog()
|
|
|
|
|
|
{
|
|
|
|
|
|
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.GetSettingsWindow());
|
2024-09-25 15:29:08 +02:00
|
|
|
|
return await Task.FromResult(GetFolderDialogWithFlags(hwnd, FolderDialogFlags._BIF_NEWDIALOGSTYLE));
|
2024-09-19 09:12:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SaveSettingsToJson()
|
|
|
|
|
|
{
|
|
|
|
|
|
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|