added toggles for text remappings

This commit is contained in:
Zach Teutsch
2026-02-10 00:03:58 -05:00
parent d4507379ee
commit dc7502451c
4 changed files with 168 additions and 170 deletions

View File

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

View File

@@ -247,7 +247,10 @@
Grid.Column="1"
Orientation="Horizontal"
Spacing="8">
<!-- 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

@@ -381,30 +381,8 @@ namespace KeyboardManagerEditorUI.Pages
break;
case EditingItem.ItemType.TextMapping:
if (originalKeys.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(originalKeys[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyToTextMapping(originalKey);
}
}
else
{
string originalKeysString = string.Join(";", originalKeys.Select(k => _mappingService.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
deleted = _mappingService.DeleteShortcutMapping(originalKeysString, _editingItem.IsAllApps ? string.Empty : _editingItem.AppName ?? string.Empty);
}
if (deleted)
{
_mappingService.SaveSettings();
}
break;
case EditingItem.ItemType.ProgramShortcut:
if (_editingItem.Item is ProgramShortcut programShortcut)
default:
if (_editingItem.Item is IToggleableShortcut shortcut)
{
if (originalKeys.Count == 1)
{
@@ -425,39 +403,9 @@ namespace KeyboardManagerEditorUI.Pages
_mappingService.SaveSettings();
}
if (!string.IsNullOrEmpty(programShortcut.Id))
if (!string.IsNullOrEmpty(shortcut.Id))
{
SettingsManager.RemoveShortcutKeyMappingFromSettings(programShortcut.Id);
}
}
break;
case EditingItem.ItemType.UrlShortcut:
if (_editingItem.Item is URLShortcut urlShortcut)
{
if (originalKeys.Count == 1)
{
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);
}
if (deleted)
{
_mappingService.SaveSettings();
}
if (!string.IsNullOrEmpty(urlShortcut.Id))
{
SettingsManager.RemoveShortcutKeyMappingFromSettings(urlShortcut.Id);
SettingsManager.RemoveShortcutKeyMappingFromSettings(shortcut.Id);
}
}
@@ -501,10 +449,21 @@ namespace KeyboardManagerEditorUI.Pages
int originalKey = _mappingService!.GetKeyCodeFromName(triggerKeys[0]);
if (originalKey != 0)
{
ShortcutKeyMapping shortcutKeyMapping = new ShortcutKeyMapping()
{
OperationType = ShortcutOperationType.RemapText,
OriginalKeys = originalKey.ToString(CultureInfo.InvariantCulture),
TargetKeys = textContent,
TargetText = textContent,
TargetApp = isAppSpecific ? appName : string.Empty,
};
bool saved = _mappingService.AddSingleKeyToTextMapping(originalKey, textContent);
if (saved)
{
return _mappingService.SaveSettings();
_mappingService.SaveSettings();
SettingsManager.AddShortcutKeyMappingToSettings(shortcutKeyMapping);
return true;
}
}
}
@@ -514,6 +473,16 @@ namespace KeyboardManagerEditorUI.Pages
string originalKeysString = string.Join(";", triggerKeys.Select(k => _mappingService!.GetKeyCodeFromName(k).ToString(CultureInfo.InvariantCulture)));
bool saved;
ShortcutKeyMapping shortcutKeyMapping = new ShortcutKeyMapping()
{
OperationType = ShortcutOperationType.RemapText,
OriginalKeys = originalKeysString,
TargetKeys = textContent,
TargetText = textContent,
TargetApp = isAppSpecific ? appName : string.Empty,
};
if (isAppSpecific && !string.IsNullOrEmpty(appName))
{
saved = _mappingService!.AddShortcutMapping(originalKeysString, textContent, appName, ShortcutOperationType.RemapText);
@@ -525,7 +494,9 @@ namespace KeyboardManagerEditorUI.Pages
if (saved)
{
return _mappingService.SaveSettings();
_mappingService.SaveSettings();
SettingsManager.AddShortcutKeyMappingToSettings(shortcutKeyMapping);
return true;
}
}
@@ -637,94 +608,33 @@ namespace KeyboardManagerEditorUI.Pages
break;
case TextMapping textMapping:
{
bool deleted = false;
if (textMapping.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(textMapping.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyToTextMapping(originalKey);
}
}
else
{
string originalKeys = string.Join(";", textMapping.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys, textMapping.IsAllApps ? string.Empty : textMapping.AppName ?? string.Empty);
}
if (deleted)
{
_mappingService.SaveSettings();
LoadTextMappings();
}
else
{
Logger.LogWarning($"Failed to delete text mapping: {string.Join("+", textMapping.Shortcut)}");
}
}
break;
case ProgramShortcut programShortcut:
{
bool deleted = false;
if (programShortcut.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(programShortcut.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyMapping(originalKey);
}
}
else
{
string originalKeys = string.Join(";", programShortcut.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys);
}
if (deleted)
{
_mappingService.SaveSettings();
}
SettingsManager.RemoveShortcutKeyMappingFromSettings(programShortcut.Id);
LoadProgramShortcuts();
}
break;
case URLShortcut urlShortcut:
{
bool deleted = false;
if (urlShortcut.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(urlShortcut.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyMapping(originalKey);
}
}
else
{
string originalKeys = string.Join(";", urlShortcut.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys);
}
if (deleted)
{
_mappingService.SaveSettings();
}
SettingsManager.RemoveShortcutKeyMappingFromSettings(urlShortcut.Id);
LoadUrlShortcuts();
}
break;
default:
Logger.LogWarning($"Unknown DataContext type for delete: {menuFlyoutItem.Tag?.GetType().Name ?? "null"}");
if (menuFlyoutItem.Tag is IToggleableShortcut shortcut)
{
bool deleted = false;
if (shortcut.Shortcut.Count == 1)
{
int originalKey = _mappingService.GetKeyCodeFromName(shortcut.Shortcut[0]);
if (originalKey != 0)
{
deleted = _mappingService.DeleteSingleKeyMapping(originalKey) || _mappingService.DeleteSingleKeyToTextMapping(originalKey);
}
}
else
{
string originalKeys = string.Join(";", shortcut.Shortcut.Select(k => _mappingService.GetKeyCodeFromName(k)));
deleted = _mappingService.DeleteShortcutMapping(originalKeys);
}
if (deleted)
{
_mappingService.SaveSettings();
}
SettingsManager.RemoveShortcutKeyMappingFromSettings(shortcut.Id);
LoadAllMappings();
}
break;
}
}
@@ -748,8 +658,16 @@ namespace KeyboardManagerEditorUI.Pages
{
bool saved = false;
ShortcutKeyMapping shortcutKeyMapping = SettingsManager.EditorSettings.ShortcutSettingsDictionary[shortcut.Id].Shortcut;
saved = _mappingService.AddShorcutMapping(shortcutKeyMapping);
if (shortcut.Shortcut.Count == 1)
{
saved = _mappingService.AddSingleKeyToTextMapping(
_mappingService.GetKeyCodeFromName(shortcut.Shortcut[0]),
shortcutKeyMapping.TargetText);
}
else
{
saved = shortcutKeyMapping.OperationType == ShortcutOperationType.RemapText ? _mappingService!.AddShortcutMapping(shortcutKeyMapping.OriginalKeys, shortcutKeyMapping.TargetText, operationType: ShortcutOperationType.RemapText) : _mappingService.AddShorcutMapping(shortcutKeyMapping);
}
if (saved)
{
@@ -781,14 +699,9 @@ namespace KeyboardManagerEditorUI.Pages
if (deleted)
{
shortcut.IsActive = false;
SettingsManager.ToggleShortcutKeyMappingActiveState(shortcut.Id);
_mappingService.SaveSettings();
}
else
{
toggleSwitch.IsOn = true;
}
LoadAllMappings();
}
@@ -887,22 +800,10 @@ namespace KeyboardManagerEditorUI.Pages
TextMappings.Clear();
// Load key-to-text mappings
var keyToTextMappings = _mappingService.GetKeyToTextMappings();
foreach (var mapping in keyToTextMappings)
{
TextMappings.Add(new TextMapping
{
Shortcut = new List<string> { _mappingService.GetKeyDisplayName(mapping.OriginalKey) },
Text = mapping.TargetText,
IsAllApps = true,
AppName = string.Empty,
});
}
// Load shortcut-to-text mappings
foreach (var mapping in _mappingService.GetShortcutMappingsByType(ShortcutOperationType.RemapText))
foreach (var shortcutSettings in SettingsManager.GetShortcutSettingsByOperationType(ShortcutOperationType.RemapText))
{
ShortcutKeyMapping mapping = shortcutSettings.Shortcut;
string[] originalKeyCodes = mapping.OriginalKeys.Split(';');
var originalKeyNames = new List<string>();
foreach (var keyCode in originalKeyCodes)
@@ -919,6 +820,8 @@ namespace KeyboardManagerEditorUI.Pages
Text = mapping.TargetText,
IsAllApps = string.IsNullOrEmpty(mapping.TargetApp),
AppName = string.IsNullOrEmpty(mapping.TargetApp) ? string.Empty : mapping.TargetApp,
Id = shortcutSettings.Id,
IsActive = shortcutSettings.IsActive,
});
}
}

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
@@ -85,6 +86,7 @@ namespace KeyboardManagerEditorUI.Settings
private static EditorSettings CreateSettingsFromKeyboardManagerService()
{
EditorSettings settings = new EditorSettings();
foreach (ShortcutKeyMapping mapping in _mappingService!.GetShortcutMappings())
{
string guid = Guid.NewGuid().ToString();
@@ -107,6 +109,39 @@ namespace KeyboardManagerEditorUI.Settings
}
}
// Handle single key to text mappings
var keyToTextMappings = _mappingService.GetKeyToTextMappings();
foreach (var mapping in keyToTextMappings)
{
// Create a ShortcutKeyMapping representation for single key to text mappings
var shortcutMapping = new ShortcutKeyMapping
{
OperationType = ShortcutOperationType.RemapText,
OriginalKeys = _mappingService.GetKeyDisplayName(mapping.OriginalKey),
TargetKeys = _mappingService.GetKeyDisplayName(mapping.OriginalKey),
TargetText = mapping.TargetText,
};
string guid = Guid.NewGuid().ToString();
ShortcutSettings shortcutSettings = new ShortcutSettings
{
Id = guid,
Shortcut = shortcutMapping,
IsActive = true,
};
settings.ShortcutSettingsDictionary[guid] = shortcutSettings;
if (settings.ShortcutsByOperationType.TryGetValue(ShortcutOperationType.RemapText, out List<string>? value))
{
value.Add(guid);
}
else
{
settings.ShortcutsByOperationType[ShortcutOperationType.RemapText] = new List<string> { guid };
}
}
return settings;
}
@@ -119,10 +154,11 @@ namespace KeyboardManagerEditorUI.Settings
return;
}
// Handle shortcut mappings (RunProgram, OpenUri, RemapShortcut, RemapText shortcuts)
List<ShortcutKeyMapping> shortcutKeyMappings = _mappingService.GetShortcutMappings();
foreach (ShortcutKeyMapping mapping in shortcutKeyMappings)
{
if (!EditorSettings.ShortcutSettingsDictionary.Values.Any(s => s.Shortcut.Equals(mapping)))
if (!EditorSettings.ShortcutSettingsDictionary.Values.Any(s => s.Shortcut.OriginalKeys == mapping.OriginalKeys))
{
shortcutSettingsChanged = true;
string guid = Guid.NewGuid().ToString();
@@ -144,9 +180,63 @@ namespace KeyboardManagerEditorUI.Settings
}
}
// Handle single key to text mappings
var keyToTextMappings = _mappingService.GetKeyToTextMappings();
foreach (var mapping in keyToTextMappings)
{
// Create a ShortcutKeyMapping representation for single key to text mappings
var shortcutMapping = new ShortcutKeyMapping
{
OperationType = ShortcutOperationType.RemapText,
OriginalKeys = mapping.OriginalKey.ToString(CultureInfo.InvariantCulture),
TargetKeys = mapping.TargetText,
TargetText = mapping.TargetText,
};
if (!EditorSettings.ShortcutSettingsDictionary.Values.Any(s => s.Shortcut.OriginalKeys == shortcutMapping.OriginalKeys))
{
shortcutSettingsChanged = true;
string guid = Guid.NewGuid().ToString();
ShortcutSettings shortcutSettings = new ShortcutSettings
{
Id = guid,
Shortcut = shortcutMapping,
IsActive = true,
};
EditorSettings.ShortcutSettingsDictionary[guid] = shortcutSettings;
if (EditorSettings.ShortcutsByOperationType.TryGetValue(ShortcutOperationType.RemapText, out List<string>? value))
{
value.Add(guid);
}
else
{
EditorSettings.ShortcutsByOperationType[ShortcutOperationType.RemapText] = new List<string> { guid };
}
}
}
// Mark as inactive any settings that no longer exist in the mapping service
foreach (ShortcutSettings shortcutSettings in EditorSettings.ShortcutSettingsDictionary.Values.ToList())
{
if (!shortcutKeyMappings.Any(m => m.Equals(shortcutSettings.Shortcut)))
bool foundInService = false;
if (shortcutSettings.Shortcut.OperationType == ShortcutOperationType.RemapText &&
!string.IsNullOrEmpty(shortcutSettings.Shortcut.OriginalKeys) &&
shortcutSettings.Shortcut.OriginalKeys.Split(';').Length == 1)
{
if (int.TryParse(shortcutSettings.Shortcut.OriginalKeys, out int keyCode))
{
foundInService = keyToTextMappings.Any(m =>
m.OriginalKey == keyCode &&
m.TargetText == shortcutSettings.Shortcut.TargetText);
}
}
else if (shortcutKeyMappings.Any(m => m.OriginalKeys == shortcutSettings.Shortcut.OriginalKeys))
{
foundInService = true;
}
if (!foundInService)
{
shortcutSettingsChanged = true;
shortcutSettings.IsActive = false;