add url toggles

This commit is contained in:
Zach Teutsch
2026-02-04 01:51:54 -05:00
parent 3bd39876fc
commit 7b73e7a059
8 changed files with 85 additions and 49 deletions

View File

@@ -0,0 +1,21 @@
// 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KeyboardManagerEditorUI.Helpers
{
internal interface IToggleableShortcut
{
public List<string> Shortcut { get; set; }
bool IsActive { get; set; }
string Id { get; set; }
}
}

View File

@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace KeyboardManagerEditorUI.Helpers
{
public class ProgramShortcut
public class ProgramShortcut : IToggleableShortcut
{
public List<string> Shortcut { get; set; } = new List<string>();

View File

@@ -12,7 +12,7 @@ namespace KeyboardManagerEditorUI.Helpers
{
public class TextMapping
{
public List<string> Keys { get; set; } = new List<string>();
public List<string> Shortcut { get; set; } = new List<string>();
public string Text { get; set; } = string.Empty;

View File

@@ -10,12 +10,14 @@ using System.Threading.Tasks;
namespace KeyboardManagerEditorUI.Helpers
{
public class URLShortcut
public class URLShortcut : IToggleableShortcut
{
public List<string> Shortcut { get; set; } = new List<string>();
public string URL { get; set; } = string.Empty;
public bool IsActive { get; set; } = true;
public string Id { get; set; } = string.Empty;
}
}

View File

@@ -208,7 +208,7 @@
<Rectangle Style="{StaticResource ItemDividerStyle}" />
<StackPanel Orientation="Horizontal" Spacing="8">
<ItemsControl VerticalAlignment="Center" ItemsSource="{x:Bind Keys}">
<ItemsControl VerticalAlignment="Center" ItemsSource="{x:Bind Shortcut}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Spacing="4" />
@@ -322,7 +322,7 @@
<ToggleSwitch
IsOn="{x:Bind IsActive}"
Style="{StaticResource RightAlignedCompactToggleSwitchStyle}"
Toggled="ProgramToggleSwitch_Toggled" />
Toggled="ToggleSwitch_Toggled" />
<Button
VerticalAlignment="Center"
Content="{ui:FontIcon Glyph=&#xE712;,
@@ -395,7 +395,10 @@
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<!-- TO DO: Add ToggleSwitch -->
<ToggleSwitch
IsOn="{x:Bind IsActive}"
Style="{StaticResource RightAlignedCompactToggleSwitchStyle}"
Toggled="ToggleSwitch_Toggled" />
<Button
VerticalAlignment="Center"
Content="{ui:FontIcon Glyph=&#xE712;,

View File

@@ -138,13 +138,13 @@ namespace KeyboardManagerEditorUI.Pages
{
Type = EditingItem.ItemType.TextMapping,
Item = textMapping,
OriginalTriggerKeys = textMapping.Keys.ToList(),
OriginalTriggerKeys = textMapping.Shortcut.ToList(),
AppName = textMapping.AppName,
IsAllApps = textMapping.IsAllApps,
};
UnifiedMappingControl.Reset();
UnifiedMappingControl.SetTriggerKeys(textMapping.Keys.ToList());
UnifiedMappingControl.SetTriggerKeys(textMapping.Shortcut.ToList());
UnifiedMappingControl.SetActionType(UnifiedMappingControl.ActionType.Text);
UnifiedMappingControl.SetTextContent(textMapping.Text);
UnifiedMappingControl.SetAppSpecific(!textMapping.IsAllApps, textMapping.AppName);
@@ -434,23 +434,31 @@ namespace KeyboardManagerEditorUI.Pages
break;
case EditingItem.ItemType.UrlShortcut:
if (originalKeys.Count == 1)
if (_editingItem.Item is URLShortcut urlShortcut)
{
int originalKey = _mappingService.GetKeyCodeFromName(originalKeys[0]);
if (originalKey != 0)
if (originalKeys.Count == 1)
{
deleted = _mappingService.DeleteSingleKeyMapping(originalKey);
int originalKey = _mappingService.GetKeyCodeFromName(originalKeys[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyMapping(originalKey);
}
}
else
{
string originalKeysString = string.Join(";", originalKeys.Select(k => _mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
deleted = _mappingService.DeleteShortcutMapping(originalKeysString);
}
}
else
{
string originalKeysString = string.Join(";", originalKeys.Select(k => _mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
deleted = _mappingService.DeleteShortcutMapping(originalKeysString);
}
if (deleted)
{
_mappingService.SaveSettings();
if (deleted)
{
_mappingService.SaveSettings();
}
if (!string.IsNullOrEmpty(urlShortcut.Id))
{
SettingsManager.RemoveShortcutKeyMappingFromSettings(urlShortcut.Id);
}
}
break;
@@ -547,7 +555,9 @@ namespace KeyboardManagerEditorUI.Pages
if (saved)
{
return _mappingService.SaveSettings();
_mappingService.SaveSettings();
SettingsManager.AddShortcutKeyMappingToSettings(shortcutKeyMapping);
return true;
}
return false;
@@ -630,9 +640,9 @@ namespace KeyboardManagerEditorUI.Pages
case TextMapping textMapping:
{
bool deleted = false;
if (textMapping.Keys.Count == 1)
if (textMapping.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(textMapping.Keys[0]);
int originalKey = _mappingService.GetKeyCodeFromName(textMapping.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyToTextMapping(originalKey);
@@ -640,7 +650,7 @@ namespace KeyboardManagerEditorUI.Pages
}
else
{
string originalKeys = string.Join(";", textMapping.Keys.Select(k => _mappingService.GetKeyCodeFromName(k)));
string originalKeys = string.Join(";", textMapping.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys, textMapping.IsAllApps ? string.Empty : textMapping.AppName ?? string.Empty);
}
@@ -651,7 +661,7 @@ namespace KeyboardManagerEditorUI.Pages
}
else
{
Logger.LogWarning($"Failed to delete text mapping: {string.Join("+", textMapping.Keys)}");
Logger.LogWarning($"Failed to delete text mapping: {string.Join("+", textMapping.Shortcut)}");
}
}
@@ -705,12 +715,10 @@ namespace KeyboardManagerEditorUI.Pages
if (deleted)
{
_mappingService.SaveSettings();
LoadUrlShortcuts();
}
else
{
Logger.LogWarning($"Failed to delete URL shortcut: {string.Join("+", urlShortcut.Shortcut)}");
}
SettingsManager.RemoveShortcutKeyMappingFromSettings(urlShortcut.Id);
LoadUrlShortcuts();
}
break;
@@ -730,9 +738,9 @@ namespace KeyboardManagerEditorUI.Pages
#region Toggle Switch Handlers
private void ProgramToggleSwitch_Toggled(object sender, RoutedEventArgs e)
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
if (sender is ToggleSwitch toggleSwitch && toggleSwitch.DataContext is ProgramShortcut shortcut && _mappingService != null)
if (sender is ToggleSwitch toggleSwitch && toggleSwitch.DataContext is IToggleableShortcut shortcut && _mappingService != null)
{
try
{
@@ -782,7 +790,7 @@ namespace KeyboardManagerEditorUI.Pages
toggleSwitch.IsOn = true;
}
LoadProgramShortcuts();
LoadAllMappings();
}
}
catch (Exception ex)
@@ -885,7 +893,7 @@ namespace KeyboardManagerEditorUI.Pages
{
TextMappings.Add(new TextMapping
{
Keys = new List<string> { _mappingService.GetKeyDisplayName(mapping.OriginalKey) },
Shortcut = new List<string> { _mappingService.GetKeyDisplayName(mapping.OriginalKey) },
Text = mapping.TargetText,
IsAllApps = true,
AppName = string.Empty,
@@ -907,7 +915,7 @@ namespace KeyboardManagerEditorUI.Pages
TextMappings.Add(new TextMapping
{
Keys = originalKeyNames,
Shortcut = originalKeyNames,
Text = mapping.TargetText,
IsAllApps = string.IsNullOrEmpty(mapping.TargetApp),
AppName = string.IsNullOrEmpty(mapping.TargetApp) ? string.Empty : mapping.TargetApp,
@@ -957,9 +965,9 @@ namespace KeyboardManagerEditorUI.Pages
UrlShortcuts.Clear();
foreach (var mapping in _mappingService.GetShortcutMappingsByType(ShortcutOperationType.OpenUri))
foreach (var shortcutSettings in SettingsManager.GetShortcutSettingsByOperationType(ShortcutOperationType.OpenUri))
{
string[] originalKeyCodes = mapping.OriginalKeys.Split(';');
string[] originalKeyCodes = shortcutSettings.Shortcut.OriginalKeys.Split(';');
var originalKeyNames = new List<string>();
foreach (var keyCode in originalKeyCodes)
{
@@ -972,7 +980,9 @@ namespace KeyboardManagerEditorUI.Pages
UrlShortcuts.Add(new URLShortcut
{
Shortcut = originalKeyNames,
URL = mapping.UriToOpen,
URL = shortcutSettings.Shortcut.UriToOpen,
Id = shortcutSettings.Id,
IsActive = shortcutSettings.IsActive,
});
}
}

View File

@@ -112,7 +112,7 @@
<ItemsControl
Grid.Column="0"
VerticalAlignment="Center"
ItemsSource="{x:Bind Keys}">
ItemsSource="{x:Bind Shortcut}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Spacing="4" />

View File

@@ -102,7 +102,7 @@ namespace KeyboardManagerEditorUI.Pages
{
TextMappings.Add(new TextMapping
{
Keys = new List<string> { _mappingService.GetKeyDisplayName(mapping.OriginalKey) },
Shortcut = new List<string> { _mappingService.GetKeyDisplayName(mapping.OriginalKey) },
Text = mapping.TargetText,
IsAllApps = true,
AppName = string.Empty,
@@ -124,7 +124,7 @@ namespace KeyboardManagerEditorUI.Pages
TextMappings.Add(new TextMapping
{
Keys = originalKeyNames,
Shortcut = originalKeyNames,
Text = mapping.TargetText,
IsAllApps = string.IsNullOrEmpty(mapping.TargetApp),
AppName = string.IsNullOrEmpty(mapping.TargetApp) ? string.Empty : mapping.TargetApp,
@@ -158,7 +158,7 @@ namespace KeyboardManagerEditorUI.Pages
_isEditMode = true;
_editingMapping = selectedMapping;
TextInputControl.SetShortcutKeys(selectedMapping.Keys);
TextInputControl.SetShortcutKeys(selectedMapping.Shortcut);
TextInputControl.SetTextContent(selectedMapping.Text);
TextInputControl.SetAppSpecific(!selectedMapping.IsAllApps, selectedMapping.AppName);
@@ -207,9 +207,9 @@ namespace KeyboardManagerEditorUI.Pages
// Delete existing mapping if in edit mode
if (_isEditMode && _editingMapping != null)
{
if (_editingMapping.Keys.Count == 1)
if (_editingMapping.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(_editingMapping.Keys[0]);
int originalKey = _mappingService.GetKeyCodeFromName(_editingMapping.Shortcut[0]);
if (originalKey != 0)
{
_mappingService.DeleteSingleKeyToTextMapping(originalKey);
@@ -217,7 +217,7 @@ namespace KeyboardManagerEditorUI.Pages
}
else
{
string originalKeys = string.Join(";", _editingMapping.Keys.Select(k => _mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
string originalKeys = string.Join(";", _editingMapping.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
_mappingService.DeleteShortcutMapping(originalKeys, _editingMapping.IsAllApps ? string.Empty : _editingMapping.AppName);
}
}
@@ -270,10 +270,10 @@ namespace KeyboardManagerEditorUI.Pages
try
{
bool deleted = false;
if (mapping.Keys.Count == 1)
if (mapping.Shortcut.Count == 1)
{
// Single key mapping
int originalKey = _mappingService.GetKeyCodeFromName(mapping.Keys[0]);
int originalKey = _mappingService.GetKeyCodeFromName(mapping.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyToTextMapping(originalKey);
@@ -282,7 +282,7 @@ namespace KeyboardManagerEditorUI.Pages
else
{
// Shortcut mapping
string originalKeys = string.Join(";", mapping.Keys.Select(k => _mappingService.GetKeyCodeFromName(k)));
string originalKeys = string.Join(";", mapping.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys, mapping.IsAllApps ? string.Empty : mapping.AppName);
}