Lagrange [work/v1.10]

macOS: Disable page navigation while editing text

=> 3def602e8b7b6f45003cb95f7a74979118ae0e4a

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) {
-    iString str;
-    init_String(&str);
+void enableMenuItem_MacOS(const char *menuItemCommand, iBool enable) {
+    NSApplication *app = [NSApplication sharedApplication];
+    NSMenu *appMenu = [app mainMenu];
+    MyDelegate *myDel = (MyDelegate *) app.delegate;
+    for (NSMenuItem *mainMenuItem in appMenu.itemArray) {
+        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) {
+    iString *str = new_String();
     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);
     }
-    NSEventModifierFlags modMask = 0;
+    *modMask = 0;
     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;
     }
+    return str;
+}
+
+void enableMenuItemsByKey_MacOS(int key, int kmods, iBool enable) {
+    NSApplication *app = [NSApplication sharedApplication];
+    NSMenu *appMenu = [app mainMenu];
+    NSEventModifierFlags modMask;
+    iString *keyEquiv = composeKeyEquivalent_(key, kmods, &modMask);
+    for (NSMenuItem *mainMenuItem in appMenu.itemArray) {
+        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];
+                }
+            }
+        }
+    }
+    delete_String(keyEquiv);
+}
+
+static void setShortcut_NSMenuItem_(NSMenuItem *item, int key, int kmods) {
+    NSEventModifierFlags modMask;
+    iString *str = composeKeyEquivalent_(key, kmods, &modMask);
     [item setKeyEquivalentModifierMask:modMask];
-    [item setKeyEquivalent:[NSString stringWithUTF8String:cstr_String(&str)]];
-    deinit_String(&str);
+    [item setKeyEquivalent:[NSString stringWithUTF8String:cstr_String(str)]];
+    delete_String(str);
 }
 
 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]];
+    [menu setAutoenablesItems:NO];
     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 
 #include 
 
+#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)
+    enableMenuItemsByKey_MacOS(SDLK_LEFT, KMOD_PRIMARY, enable);
+    enableMenuItemsByKey_MacOS(SDLK_RIGHT, KMOD_PRIMARY, enable);
+#else
+    iUnused(enable);
+#endif
+}
+
 iDeclareType(InputUndo)
 
 struct Impl_InputUndo {
@@ -114,6 +127,9 @@ void init_InputWidget(iInputWidget *d, size_t maxLen) {
 }
 
 void deinit_InputWidget(iInputWidget *d) {
+    if (isSelected_Widget(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);
     }
+    enableEditorKeysInMenus_(iFalse);
 }
 
 void end_InputWidget(iInputWidget *d, iBool accept) {
@@ -287,6 +304,7 @@ void end_InputWidget(iInputWidget *d, iBool accept) {
         /* Was not active. */
         return;
     }
+    enableEditorKeysInMenus_(iTrue);
     if (!accept) {
         setCopy_Array(&d->text, &d->oldText);
     }
Proxy Information
Original URL
gemini://git.skyjake.fi/lagrange/work%2Fv1.10/cdiff/3def602e8b7b6f45003cb95f7a74979118ae0e4a
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
28.299987 milliseconds
Gemini-to-HTML Time
0.392682 milliseconds

This content has been proxied by September (ba2dc).