From 3def602e8b7b6f45003cb95f7a74979118ae0e4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= jaakko.keranen@iki.fi
Date: Sun, 13 Dec 2020 13:08:05 +0200
Subject: [PATCH 1/1] macOS: Disable page navigation while editing text
The Cmd+Left/Right keys used both for page navigation (by default) and for moving the cursor inside text editor. The input widget keys should take precedence.
src/macos.h | 2 ++
src/macos.m | 77 +++++++++++++++++++++++++++++++++++---------
src/ui/inputwidget.c | 18 +++++++++++
3 files changed, 82 insertions(+), 15 deletions(-)
diff --git a/src/macos.h b/src/macos.h
index f948a0db..a59351e6 100644
--- a/src/macos.h
+++ b/src/macos.h
@@ -33,4 +33,6 @@ void registerURLHandler_MacOS (void);
void setupApplication_MacOS (void);
void insertMenuItems_MacOS (const char *menuLabel, int atIndex, const iMenuItem *items, size_t count);
void enableMenu_MacOS (const char *menuLabel, iBool enable);
+void enableMenuItem_MacOS (const char *menuItemCommand, iBool enable);
+void enableMenuItemsByKey_MacOS (int key, int kmods, iBool enable);
void handleCommand_MacOS (const char *cmd);
diff --git a/src/macos.m b/src/macos.m
index c8793eac..4e214923 100644
--- a/src/macos.m
+++ b/src/macos.m
@@ -367,40 +367,86 @@ void enableMenu_MacOS(const char *menuLabel, iBool enable) {
[label release];
}
-static void setShortcut_NSMenuItem_(NSMenuItem *item, int key, int kmods) {
+void enableMenuItem_MacOS(const char *menuItemCommand, iBool enable) {
NSMenu *menu = mainMenuItem.submenu;
if (menu) {
for (NSMenuItem *menuItem in menu.itemArray) {
NSString *command = [myDel commandForItem:menuItem];
if (command) {
if (!iCmpStr([command cStringUsingEncoding:NSUTF8StringEncoding],
menuItemCommand)) {
[menuItem setEnabled:enable];
return;
}
}
}
}
+}
+static iString *composeKeyEquivalent_(int key, int kmods, NSEventModifierFlags *modMask) {
if (key == SDLK_LEFT) {
appendChar_String(&str, 0x2190);
appendChar_String(str, 0x2190);
}
else if (key == SDLK_RIGHT) {
appendChar_String(&str, 0x2192);
appendChar_String(str, 0x2192);
}
else if (key == SDLK_UP) {
appendChar_String(&str, 0x2191);
appendChar_String(str, 0x2191);
}
else if (key == SDLK_DOWN) {
appendChar_String(&str, 0x2193);
appendChar_String(str, 0x2193);
}
else if (key) {
appendChar_String(&str, key);
appendChar_String(str, key);
}
if (kmods & KMOD_GUI) {
modMask |= NSEventModifierFlagCommand;
*modMask |= NSEventModifierFlagCommand;
}
if (kmods & KMOD_ALT) {
modMask |= NSEventModifierFlagOption;
*modMask |= NSEventModifierFlagOption;
}
if (kmods & KMOD_CTRL) {
modMask |= NSEventModifierFlagControl;
*modMask |= NSEventModifierFlagControl;
}
if (kmods & KMOD_SHIFT) {
modMask |= NSEventModifierFlagShift;
*modMask |= NSEventModifierFlagShift;
}
+}
+void enableMenuItemsByKey_MacOS(int key, int kmods, iBool enable) {
NSMenu *menu = mainMenuItem.submenu;
if (menu) {
for (NSMenuItem *menuItem in menu.itemArray) {
if (menuItem.keyEquivalentModifierMask == modMask &&
!iCmpStr([menuItem.keyEquivalent cStringUsingEncoding:NSUTF8StringEncoding],
cstr_String(keyEquiv))) {
[menuItem setEnabled:enable];
}
}
}
+}
+static void setShortcut_NSMenuItem_(NSMenuItem *item, int key, int kmods) {
[item setKeyEquivalentModifierMask:modMask];
}
void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem *items, size_t count) {
@@ -412,6 +458,7 @@ void insertMenuItems_MacOS(const char *menuLabel, int atIndex, const iMenuItem *
keyEquivalent:@""
atIndex:atIndex];
NSMenu *menu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:menuLabel]];
for (size_t i = 0; i < count; ++i) {
const char *label = items[i].label;
if (label[0] == '\r') {
diff --git a/src/ui/inputwidget.c b/src/ui/inputwidget.c
index 59608151..c4c7475e 100644
--- a/src/ui/inputwidget.c
+++ b/src/ui/inputwidget.c
@@ -30,9 +30,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include <SDL_clipboard.h>
#include <SDL_timer.h>
+#if defined (iPlatformApple)
+# include "macos.h"
+#endif
static const int refreshInterval_InputWidget_ = 256;
static const size_t maxUndo_InputWidget_ = 64;
+static void enableEditorKeysInMenus_(iBool enable) {
+#if defined (iPlatformApple)
+#else
+#endif
+}
iDeclareType(InputUndo)
struct Impl_InputUndo {
@@ -114,6 +127,9 @@ void init_InputWidget(iInputWidget *d, size_t maxLen) {
}
void deinit_InputWidget(iInputWidget *d) {
enableEditorKeysInMenus_(iTrue);
delete_TextBuf(d->buffered);
clearUndo_InputWidget_(d);
deinit_Array(&d->undoStack);
@@ -279,6 +295,7 @@ void begin_InputWidget(iInputWidget *d) {
else {
iZap(d->mark);
}
}
void end_InputWidget(iInputWidget *d, iBool accept) {
@@ -287,6 +304,7 @@ void end_InputWidget(iInputWidget *d, iBool accept) {
/* Was not active. */
return;
}
if (!accept) {
setCopy_Array(&d->text, &d->oldText);
}
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).