From 0dd95163e46717ecb4e1dba4ee0d32f000768e3c Mon Sep 17 00:00:00 2001 From: Jeff Lord Date: Fri, 15 Dec 2023 22:17:05 -0500 Subject: [PATCH] Fixed settings screen --- .../common/KeyboardManagerConstants.h | 4 +- .../AppSpecificKeysDataModel.cs | 4 +- .../Settings.UI.Library/KeysDataModel.cs | 84 ++++++++++++++----- .../SettingsXAML/Views/DashboardPage.xaml | 10 +-- .../Views/KeyboardManagerPage.xaml | 10 +-- .../ViewModels/DashboardViewModel.cs | 1 + .../ViewModels/KeyboardManagerViewModel.cs | 4 +- 7 files changed, 82 insertions(+), 35 deletions(-) diff --git a/src/modules/keyboardmanager/common/KeyboardManagerConstants.h b/src/modules/keyboardmanager/common/KeyboardManagerConstants.h index 3f77d828f464..a0cc640e1146 100644 --- a/src/modules/keyboardmanager/common/KeyboardManagerConstants.h +++ b/src/modules/keyboardmanager/common/KeyboardManagerConstants.h @@ -62,10 +62,10 @@ namespace KeyboardManagerConstants inline const std::wstring ShortcutSecondKeyOfChordSettingName = L"secondKeyOfChord"; // Name of the property use to store openUri. - inline const std::wstring ShortcutOpenURI = L"openUri "; + inline const std::wstring ShortcutOpenURI = L"openUri"; // Name of the property use to store shortcutOperationType. - inline const std::wstring ShortcutOperationType = L"operationType "; + inline const std::wstring ShortcutOperationType = L"operationType"; // Name of the property use to store runProgramFilePath. //inline const std::wstring IsRunProgramSettingName = L"isRunProgram"; diff --git a/src/settings-ui/Settings.UI.Library/AppSpecificKeysDataModel.cs b/src/settings-ui/Settings.UI.Library/AppSpecificKeysDataModel.cs index 0f550598caa4..3b986bff7759 100644 --- a/src/settings-ui/Settings.UI.Library/AppSpecificKeysDataModel.cs +++ b/src/settings-ui/Settings.UI.Library/AppSpecificKeysDataModel.cs @@ -18,9 +18,9 @@ public class AppSpecificKeysDataModel : KeysDataModel return base.GetMappedOriginalKeys(); } - public new List GetMappedNewRemapKeys() + public new List GetMappedNewRemapKeys(int runProgramMaxLength) { - return base.GetMappedNewRemapKeys(); + return base.GetMappedNewRemapKeys(runProgramMaxLength); } public bool Compare(AppSpecificKeysDataModel arg) diff --git a/src/settings-ui/Settings.UI.Library/KeysDataModel.cs b/src/settings-ui/Settings.UI.Library/KeysDataModel.cs index 3f3605cb022b..4bd4e9ce0d6d 100644 --- a/src/settings-ui/Settings.UI.Library/KeysDataModel.cs +++ b/src/settings-ui/Settings.UI.Library/KeysDataModel.cs @@ -3,7 +3,9 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Management; using System.Text.Json; using System.Text.Json.Serialization; using Microsoft.PowerToys.Settings.UI.Library.Utilities; @@ -27,8 +29,11 @@ public class KeysDataModel [JsonPropertyName("runProgramArgs")] public string RunProgramArgs { get; set; } - [JsonPropertyName("isRunProgram")] - public bool IsRunProgram { get; set; } + [JsonPropertyName("openUri")] + public string OpenUri { get; set; } + + [JsonPropertyName("operationType")] + public int OperationType { get; set; } private static List MapKeys(string stringOfKeys) { @@ -49,7 +54,31 @@ public List GetMappedOriginalKeys() return MapKeys(OriginalKeys); } - public List GetMappedNewRemapKeys() + public bool IsRunProgram + { + get + { + return OperationType == 1; + } + } + + public bool IsOpenURI + { + get + { + return OperationType == 2; + } + } + + public bool IsOpenURIOrIsRunProgram + { + get + { + return IsOpenURI || IsRunProgram; + } + } + + public List GetMappedNewRemapKeys(int runProgramMaxLength) { if (IsRunProgram) { @@ -60,7 +89,26 @@ public List GetMappedNewRemapKeys() } else { - return new List { FormatFakeKeyForDisplay() }; + return new List { FormatFakeKeyForDisplay(runProgramMaxLength) }; + } + } + else if (IsOpenURI) + { + // we're going to just pretend this is a "key" if we have a RunProgramFilePath + if (string.IsNullOrEmpty(OpenUri)) + { + return new List(); + } + else + { + if (OpenUri.Length > runProgramMaxLength) + { + return new List { $"{OpenUri.Substring(0, runProgramMaxLength - 3)}..." }; + } + else + { + return new List { OpenUri }; + } } } @@ -72,28 +120,26 @@ public List GetMappedNewRemapKeys() // e.g.: c:\MyCool\PathIs\Long\software.exe myArg1 myArg2 myArg3 -> (something like) "...ng\software.exe myArg1..." // the idea is you get the most important part of the program to run and some of the args in case that the only thing thats different, // e.g: "...path\software.exe cool1.txt" and "...path\software.exe cool3.txt" - private string FormatFakeKeyForDisplay() + private string FormatFakeKeyForDisplay(int runProgramMaxLength) { // was going to use this: - // var fakeKey = Path.GetFileName(RunProgramFilePath); - // but I like this better: var fakeKey = RunProgramFilePath; - - if (fakeKey.Length > 15) + try + { + if (File.Exists(fakeKey)) + { + fakeKey = Path.GetFileName(RunProgramFilePath); + } + } + catch { - fakeKey = $"...{fakeKey.Substring(fakeKey.Length - 12)}"; } - if (!string.IsNullOrEmpty(RunProgramArgs)) + fakeKey = $"{fakeKey} {RunProgramArgs}".Trim(); + + if (fakeKey.Length > runProgramMaxLength) { - if (RunProgramArgs.Length > 10) - { - fakeKey = $"{fakeKey} {RunProgramArgs.Substring(0, 7)}..."; - } - else - { - fakeKey = $"{fakeKey} {RunProgramArgs}"; - } + fakeKey = $"{fakeKey.Substring(0, runProgramMaxLength - 3)}..."; } return fakeKey; diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/DashboardPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/DashboardPage.xaml index 4e92e4a2ec70..fe2e59cf8124 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/DashboardPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/DashboardPage.xaml @@ -156,7 +156,7 @@ + ItemsSource="{x:Bind GetMappedNewRemapKeys(15)}"> @@ -221,13 +221,13 @@ x:Uid="To" Margin="8,0,8,0" VerticalAlignment="Center" - Visibility="{x:Bind Path=IsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}" + Visibility="{x:Bind Path=IsOpenURIOrIsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}" Style="{StaticResource SecondaryIsEnabledTextBlockStyle}" /> + ItemsSource="{x:Bind GetMappedNewRemapKeys(15)}"> @@ -260,7 +260,7 @@ diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/KeyboardManagerPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/KeyboardManagerPage.xaml index 0def811ae2d1..4414bea61ce6 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/KeyboardManagerPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/KeyboardManagerPage.xaml @@ -110,7 +110,7 @@ x:Uid="KeyboardManager_RemappedTo" IsTabStop="False" ItemTemplate="{StaticResource RemappedKeyTemplate}" - ItemsSource="{x:Bind GetMappedNewRemapKeys()}"> + ItemsSource="{x:Bind GetMappedNewRemapKeys(50)}"> @@ -159,14 +159,14 @@ x:Uid="To" Margin="8,0,8,0" VerticalAlignment="Center" - Visibility="{x:Bind Path=IsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}" + Visibility="{x:Bind Path=IsOpenURIOrIsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}" Style="{StaticResource SecondaryIsEnabledTextBlockStyle}" /> @@ -175,7 +175,7 @@ x:Uid="KeyboardManager_RemappedTo" IsTabStop="False" ItemTemplate="{StaticResource RemappedKeyTemplate}" - ItemsSource="{x:Bind GetMappedNewRemapKeys()}"> + ItemsSource="{x:Bind GetMappedNewRemapKeys(50)}"> @@ -187,7 +187,7 @@ Padding="12,4,12,6" VerticalAlignment="Center" CornerRadius="12" - Visibility="{x:Bind Path=IsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}"> + Visibility="{x:Bind Path=IsOpenURIOrIsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}"> diff --git a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs index 954458a4c730..25f0e2e4269f 100644 --- a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.IO.Abstractions; using System.Linq; using System.Windows.Threading; diff --git a/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs index d59f406b4a4a..5ede81816015 100644 --- a/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs @@ -190,11 +190,11 @@ public static List CombineShortcutLists(List new AppSpecificKeysDataModel { OriginalKeys = x.OriginalKeys, NewRemapKeys = x.NewRemapKeys, NewRemapString = x.NewRemapString, TargetApp = allAppsDescription }).ToList(); + return globalShortcutList.ConvertAll(x => new AppSpecificKeysDataModel { OriginalKeys = x.OriginalKeys, NewRemapKeys = x.NewRemapKeys, NewRemapString = x.NewRemapString, RunProgramFilePath = x.RunProgramFilePath, OperationType = x.OperationType, OpenUri = x.OpenUri, RunProgramArgs = x.RunProgramArgs, TargetApp = allAppsDescription }).ToList(); } else { - return globalShortcutList.ConvertAll(x => new AppSpecificKeysDataModel { OriginalKeys = x.OriginalKeys, NewRemapKeys = x.NewRemapKeys, NewRemapString = x.NewRemapString, RunProgramFilePath = x.RunProgramFilePath, IsRunProgram = x.IsRunProgram, RunProgramArgs = x.RunProgramArgs, TargetApp = allAppsDescription }).Concat(appSpecificShortcutList).ToList(); + return globalShortcutList.ConvertAll(x => new AppSpecificKeysDataModel { OriginalKeys = x.OriginalKeys, NewRemapKeys = x.NewRemapKeys, NewRemapString = x.NewRemapString, RunProgramFilePath = x.RunProgramFilePath, OperationType = x.OperationType, OpenUri = x.OpenUri, RunProgramArgs = x.RunProgramArgs, TargetApp = allAppsDescription }).Concat(appSpecificShortcutList).ToList(); } }