Add file picker and DemoType file support

This commit is contained in:
Jaime Bernardo 2024-09-18 12:10:11 +01:00
parent b540b4bd86
commit 87297ec32c
4 changed files with 94 additions and 9 deletions

View File

@ -71,6 +71,9 @@
<tkcontrols:SettingsCard x:Uid="ZoomIt_DemoType_Shortcut" HeaderIcon="{ui:FontIcon Glyph=&#xEDA7;}">
<controls:ShortcutControl MinWidth="{StaticResource SettingActionControlMinWidth}" HotkeySettings="{x:Bind Path=ViewModel.DemoTypeToggleKey, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard x:Uid="ZoomIt_DemoType_File" Description="{x:Bind ViewModel.DemoTypeFile, Mode=OneWay}">
<Button x:Uid="ZoomIt_DemoType_File_BrowseButton" Command="{x:Bind ViewModel.SelectDemoTypeFileCommand, Mode=OneWay}"/>
</tkcontrols:SettingsCard>
</controls:SettingsGroup>
<controls:SettingsGroup x:Uid="ZoomIt_BreakGroup" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<tkcontrols:SettingsCard x:Uid="ZoomIt_Break_Shortcut" HeaderIcon="{ui:FontIcon Glyph=&#xEDA7;}">

View File

@ -2,6 +2,8 @@
// 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.Runtime.InteropServices;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.ViewModels;
@ -13,10 +15,40 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
private ZoomItViewModel ViewModel { get; set; }
private const int MaxPath = 260; // ZoomIt doesn't support LONG_PATHS. We need to change it here once it does.
private static string PickFileDialog(string filter, string title)
{
// this code was changed to solve the problem with WinUI3 that prevents to select a file
// while running elevated, when the issue is solved in WinUI3 it should be changed back
OpenFileName openFileName = new OpenFileName();
openFileName.StructSize = Marshal.SizeOf(openFileName);
openFileName.Filter = filter;
// make buffer double MAX_PATH since it can use 2 chars per char.
openFileName.File = new string(new char[MaxPath * 2]);
openFileName.MaxFile = openFileName.File.Length;
openFileName.FileTitle = new string(new char[MaxPath * 2]);
openFileName.MaxFileTitle = openFileName.FileTitle.Length;
openFileName.InitialDir = null;
openFileName.Title = title;
openFileName.DefExt = null;
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(App.GetSettingsWindow());
openFileName.Hwnd = windowHandle;
bool result = NativeMethods.GetOpenFileName(openFileName);
if (result)
{
return openFileName.File;
}
return null;
}
public ZoomItPage()
{
var settingsUtils = new SettingsUtils();
ViewModel = new ZoomItViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage);
ViewModel = new ZoomItViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, PickFileDialog);
DataContext = ViewModel;
InitializeComponent();
}

View File

@ -4385,6 +4385,18 @@ When you reach the end of the file, ZoomIt will reload the file and start at the
<data name="ZoomIt_DemoType_Shortcut.Header" xml:space="preserve">
<value>Demo Type Toggle Hotkey</value>
</data>
<data name="ZoomIt_DemoType_File.Header" xml:space="preserve">
<value>Input file</value>
</data>
<data name="ZoomIt_DemoType_File_BrowseButton.Content" xml:space="preserve">
<value>Browse</value>
</data>
<data name="ZoomIt_DemoType_File_Picker_Dialog_Title" xml:space="preserve">
<value>Specify DemoType file...</value>
</data>
<data name="FilePicker_AllFilesFilter" xml:space="preserve">
<value>All Files</value>
</data>
<data name="ZoomIt_BreakGroup.Header" xml:space="preserve">
<value>Break</value>
</data>

View File

@ -8,10 +8,13 @@ using System.Runtime.CompilerServices;
using System.Text.Json;
using AllExperiments;
using global::PowerToys.GPOWrapper;
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 Microsoft.Windows.ApplicationModel.Resources;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
@ -25,13 +28,17 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private Func<string, int> SendConfigMSG { get; }
private Func<string, string, string> PickFileDialog { get; }
public ButtonClickCommand SelectDemoTypeFileCommand { get; set; }
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
MaxDepth = 0,
IncludeFields = true,
};
public ZoomItViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
public ZoomItViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string, string> pickFileDialog)
{
ArgumentNullException.ThrowIfNull(settingsUtils);
@ -49,6 +56,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
// set the callback functions value to handle outgoing IPC message for the enabled value.
SendConfigMSG = ipcMSGCallBackFunc;
// set the callback for when we need the user to pick a file.
PickFileDialog = pickFileDialog;
SelectDemoTypeFileCommand = new ButtonClickCommand(SelectDemoTypeFileAction);
}
private void InitializeEnabledValue()
@ -237,18 +249,44 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public string DemoTypeFile
{
get => _zoomItSettings.Properties.DemoTypeFile.Value;
set
{
if (_zoomItSettings.Properties.DemoTypeFile.Value != value)
{
_zoomItSettings.Properties.DemoTypeFile.Value = value;
OnPropertyChanged(nameof(DemoTypeFile));
NotifySettingsChanged();
}
}
}
private void NotifySettingsChanged()
{
global::PowerToys.ZoomItSettingsInterop.ZoomItSettings.SaveSettingsJson(
JsonSerializer.Serialize(_zoomItSettings));
}
// Using InvariantCulture as this is an IPC message
/*SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
ZoomItSettings.ModuleName,
JsonSerializer.Serialize(_zoomItSettings)));*/
private void SelectDemoTypeFileAction()
{
// TODO: Localize
try
{
ResourceLoader resourceLoader = ResourceLoaderInstance.ResourceLoader;
string title = resourceLoader.GetString("ZoomIt_DemoType_File_Picker_Dialog_Title");
string allFilesFilter = resourceLoader.GetString("FilePicker_AllFilesFilter");
string pickedFile = PickFileDialog($"{allFilesFilter}\0*.*\0\0", title);
if (pickedFile != null)
{
DemoTypeFile = pickedFile;
}
}
catch (Exception ex)
{
Logger.LogError("Error picking Demo Type file.", ex);
}
}
public void RefreshEnabledState()