Skip to content

Commit 2b4fb9e

Browse files
Yu Leng (from Dev Box)claude
andcommitted
[KBM] Fix Text Expand enable/disable toggle not working
EnableShortcut, DisableShortcut, and HandleShortcutDelete did not recognize ExpandMapping, falling through to incorrect single-key or multi-key branches. Add explicit ExpandMapping handling that calls AddExpandMapping/DeleteExpandMapping on the engine config. Also add DeleteExpandMapping C++ interop (matches by abbreviation + app name). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d1091b3 commit 2b4fb9e

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

src/modules/keyboardmanager/KeyboardManagerEditorLibraryWrapper/KeyboardManagerEditorLibraryWrapper.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,32 @@ bool GetShortcutRemapByType(void* config, int operationType, int index, Shortcut
696696
return true;
697697
}
698698

699+
bool DeleteExpandMapping(void* config, const wchar_t* abbreviation, const wchar_t* targetApp)
700+
{
701+
auto mappingConfig = static_cast<MappingConfiguration*>(config);
702+
if (!abbreviation)
703+
{
704+
return false;
705+
}
706+
707+
std::wstring abbrev = abbreviation;
708+
std::wstring app = targetApp ? targetApp : L"";
709+
710+
auto& mappings = mappingConfig->expandMappings;
711+
auto it = std::remove_if(mappings.begin(), mappings.end(), [&](const ExpandMapping& m) {
712+
return _wcsicmp(m.abbreviation.c_str(), abbrev.c_str()) == 0 &&
713+
_wcsicmp(m.appName.c_str(), app.c_str()) == 0;
714+
});
715+
716+
if (it != mappings.end())
717+
{
718+
mappings.erase(it, mappings.end());
719+
return true;
720+
}
721+
722+
return false;
723+
}
724+
699725
// Function to delete a shortcut remapping
700726
bool DeleteShortcutRemap(void* config, const wchar_t* originalKeys, const wchar_t* targetApp)
701727
{

src/modules/keyboardmanager/KeyboardManagerEditorLibraryWrapper/KeyboardManagerEditorLibraryWrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern "C"
8080
__declspec(dllexport) bool AreShortcutsEqual(const wchar_t* lShort, const wchar_t* rShort);
8181

8282
__declspec(dllexport) bool AddExpandMapping(void* config, const wchar_t* abbreviation, int triggerKey, const wchar_t* expandedText, const wchar_t* targetApp);
83+
__declspec(dllexport) bool DeleteExpandMapping(void* config, const wchar_t* abbreviation, const wchar_t* targetApp);
8384

8485
__declspec(dllexport) bool DeleteSingleKeyRemap(void* config, int originalKey);
8586
__declspec(dllexport) bool DeleteSingleKeyToTextRemap(void* config, int originalKey);

src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardManagerInterop.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ internal static extern bool AddExpandMapping(
9494
[MarshalAs(UnmanagedType.LPWStr)] string expandedText,
9595
[MarshalAs(UnmanagedType.LPWStr)] string targetApp);
9696

97+
[DllImport(DllName, CallingConvention = Convention, CharSet = CharSet.Unicode)]
98+
[return: MarshalAs(UnmanagedType.Bool)]
99+
internal static extern bool DeleteExpandMapping(
100+
IntPtr config,
101+
[MarshalAs(UnmanagedType.LPWStr)] string abbreviation,
102+
[MarshalAs(UnmanagedType.LPWStr)] string targetApp);
103+
97104
// Delete Mapping Functions
98105
[DllImport(DllName, CallingConvention = Convention)]
99106
[return: MarshalAs(UnmanagedType.Bool)]

src/modules/keyboardmanager/KeyboardManagerEditorUI/Interop/KeyboardMappingService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ public bool AddExpandMapping(string abbreviation, int triggerKey, string expande
243243
return KeyboardManagerInterop.AddExpandMapping(_configHandle, abbreviation, triggerKey, expandedText, targetApp);
244244
}
245245

246+
public bool DeleteExpandMapping(string abbreviation, string targetApp)
247+
{
248+
return KeyboardManagerInterop.DeleteExpandMapping(_configHandle, abbreviation, targetApp);
249+
}
250+
246251
public bool SaveSettings()
247252
{
248253
return KeyboardManagerInterop.SaveMappingSettings(_configHandle);

src/modules/keyboardmanager/KeyboardManagerEditorUI/Pages/MainPage.xaml.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,19 @@ private void HandleRemappingDelete(Remapping remapping)
691691

692692
private void HandleShortcutDelete(IToggleableShortcut shortcut)
693693
{
694+
if (shortcut is ExpandMapping)
695+
{
696+
ShortcutKeyMapping skm = SettingsManager.EditorSettings.ShortcutSettingsDictionary[shortcut.Id].Shortcut;
697+
if (shortcut.IsActive)
698+
{
699+
_mappingService!.DeleteExpandMapping(skm.OriginalKeys, skm.TargetApp);
700+
_mappingService.SaveSettings();
701+
}
702+
703+
SettingsManager.RemoveShortcutKeyMappingFromSettings(shortcut.Id);
704+
return;
705+
}
706+
694707
bool deleted = shortcut.Shortcut.Count == 1
695708
? DeleteSingleKeyToTextMapping(shortcut.Shortcut[0]) // Remapping has its own handler, single key will always be text mapping
696709
: DeleteMultiKeyShortcut(shortcut);
@@ -747,14 +760,34 @@ private void EnableShortcut(IToggleableShortcut shortcut)
747760
return;
748761
}
749762

763+
if (shortcut is ExpandMapping expandMapping)
764+
{
765+
ShortcutKeyMapping skm = SettingsManager.EditorSettings.ShortcutSettingsDictionary[shortcut.Id].Shortcut;
766+
int triggerKeyVk = KeyboardManagerInterop.GetKeyCodeFromName(skm.TargetKeys);
767+
if (triggerKeyVk == 0)
768+
{
769+
triggerKeyVk = 0x20;
770+
}
771+
772+
bool saved = _mappingService!.AddExpandMapping(skm.OriginalKeys, triggerKeyVk, skm.TargetText, skm.TargetApp);
773+
if (saved)
774+
{
775+
shortcut.IsActive = true;
776+
SettingsManager.ToggleShortcutKeyMappingActiveState(shortcut.Id);
777+
_mappingService.SaveSettings();
778+
}
779+
780+
return;
781+
}
782+
750783
ShortcutKeyMapping shortcutKeyMapping = SettingsManager.EditorSettings.ShortcutSettingsDictionary[shortcut.Id].Shortcut;
751-
bool saved = shortcut.Shortcut.Count == 1
784+
bool saved2 = shortcut.Shortcut.Count == 1
752785
? _mappingService!.AddSingleKeyToTextMapping(_mappingService.GetKeyCodeFromName(shortcut.Shortcut[0]), shortcutKeyMapping.TargetText)
753786
: shortcutKeyMapping.OperationType == ShortcutOperationType.RemapText
754787
? _mappingService!.AddShortcutMapping(shortcutKeyMapping.OriginalKeys, shortcutKeyMapping.TargetText, operationType: ShortcutOperationType.RemapText)
755788
: _mappingService!.AddShortcutMapping(shortcutKeyMapping);
756789

757-
if (saved)
790+
if (saved2)
758791
{
759792
shortcut.IsActive = true;
760793
SettingsManager.ToggleShortcutKeyMappingActiveState(shortcut.Id);
@@ -772,11 +805,25 @@ private void DisableShortcut(IToggleableShortcut shortcut)
772805
return;
773806
}
774807

775-
bool deleted = shortcut.Shortcut.Count == 1
808+
if (shortcut is ExpandMapping expandMapping)
809+
{
810+
ShortcutKeyMapping skm = SettingsManager.EditorSettings.ShortcutSettingsDictionary[shortcut.Id].Shortcut;
811+
bool deleted = _mappingService!.DeleteExpandMapping(skm.OriginalKeys, skm.TargetApp);
812+
if (deleted)
813+
{
814+
shortcut.IsActive = false;
815+
SettingsManager.ToggleShortcutKeyMappingActiveState(shortcut.Id);
816+
_mappingService.SaveSettings();
817+
}
818+
819+
return;
820+
}
821+
822+
bool deleted2 = shortcut.Shortcut.Count == 1
776823
? DeleteSingleKeyToTextMapping(shortcut.Shortcut[0])
777824
: DeleteMultiKeyMapping(shortcut.Shortcut, shortcut.AppName);
778825

779-
if (deleted)
826+
if (deleted2)
780827
{
781828
shortcut.IsActive = false;
782829
SettingsManager.ToggleShortcutKeyMappingActiveState(shortcut.Id);

0 commit comments

Comments
 (0)