Browse Source

Use Cmd for multi-select in File Manager on Mac

On macOS, Ctrl + click is a right click, so Ctrl could not be used to multi-select. The primary modifier is now Cmd on Mac and Ctrl elsewhere, in both the file selector and the File Manager.

- Add isMacOS, isPrimaryModifierKey and hasPrimaryModifier helpers, and read modifier state from the click and key events themselves.
- Reset stuck modifier flags on window blur (e.g. Cmd + Tab).
- Ctrl + click on a Mac now selects the target before the context menu opens.
- Add Finder-style shortcuts: Cmd + Backspace deletes, Cmd + Option + Backspace force deletes, and Cmd + Down opens the selection.
- Show Cmd glyphs in the shortcut hints.
- Ignore browser shortcuts and lone modifiers in type-to-search so they no longer clear the selection.
Toby Chui 1 month ago
parent
commit
43cd4cb685

+ 20 - 3
src/web/SystemAO/file_system/file_selector.html

@@ -1015,6 +1015,8 @@
             var historyIndex = -1;
             var ctrlDown = false;
             var shiftDown = false;
+            //Multi-select uses Cmd on a Mac (Ctrl + click is the right click there)
+            var isMacOS = /Mac/i.test(navigator.platform || navigator.userAgent);
             var lastClickedItemID = 0;
             var listenerUUID = "";
             var fileOptions = {};
@@ -2000,7 +2002,7 @@
             function selectThis(object, event){
                 var isDir = $(object).attr("isDir") == "true";
                 var filename = decodeURIComponent($(object).attr("filename"));
-                var useCtrl = (event != undefined && event.ctrlKey) || ctrlDown;
+                var useCtrl = (event != undefined && (isMacOS ? event.metaKey : event.ctrlKey)) || ctrlDown;
                 var useShift = (event != undefined && event.shiftKey) || shiftDown;
 
                 if (isDir && type == "file"){
@@ -3173,8 +3175,16 @@
             /* ------------------------------------------------------------------ */
             /*  Keyboard                                                           */
             /* ------------------------------------------------------------------ */
+            //Ctrl, or Cmd on a Mac (keyCode 91 / 93, or 224 in Firefox)
+            function isMultiSelectKey(event){
+                if (isMacOS){
+                    return event.key == "Meta" || event.which == 91 || event.which == 93 || event.which == 224;
+                }
+                return event.which == 17;
+            }
+
             $(window).on("keydown", function(event){
-                if (event.which == 17){
+                if (isMultiSelectKey(event)){
                     ctrlDown = true;
                 }else if (event.which == 16){
                     shiftDown = true;
@@ -3206,13 +3216,20 @@
             });
 
             $(window).on("keyup", function(event){
-                if (event.which == 17){
+                if (isMultiSelectKey(event)){
                     ctrlDown = false;
                 }else if (event.which == 16){
                     shiftDown = false;
                 }
             });
 
+            //A modifier released in another window (Cmd + Tab) never sends its
+            //keyup here, which would leave every later click multi-selecting
+            $(window).on("blur", function(){
+                ctrlDown = false;
+                shiftDown = false;
+            });
+
             //Clicking the empty area of the file list clears the selection
             $("#fileArea").on("click", function(event){
                 if ($(event.target).closest(".fileObject, .fsListHead").length == 0){

+ 1 - 1
src/web/SystemAO/file_system/js/explorer/dragdrop.js

@@ -15,7 +15,7 @@
 
     //Make fileObject draggable
     function onFileObjectDragStart(object, event){
-        if ((ctrlHold) &&  $(object).hasClass("selected") == false){
+        if ((ctrlHold || hasPrimaryModifier(event)) &&  $(object).hasClass("selected") == false){
             $(object).addClass("selected");
         }else if ($(object).hasClass("selected") == false){
             $(".fileObject.selected").removeClass("selected");

+ 73 - 5
src/web/SystemAO/file_system/js/explorer/keyboard.js

@@ -23,7 +23,14 @@ $(document).keydown(function(event){
         }
         return;
     }
-    if(event.which==17 || event.which == 91){
+
+    //Trust the modifier state on the event itself: the modifier's own keydown
+    //may have gone to another frame (e.g. the desktop around this window)
+    if (hasPrimaryModifier(event)){
+        ctrlHold = true;
+    }
+
+    if(isPrimaryModifierKey(event)){
         ctrlHold = true;
         updateCtrlDisplay();
     }else if (event.which == 16){
@@ -77,6 +84,15 @@ $(document).keydown(function(event){
     }else if (event.which == 46){
         //Delete
         deleteFile();
+    }else if (event.which == 8 && isMacOS && ctrlHold == true){
+        //Mac keyboards have no Delete key: Cmd + Backspace moves to trash and
+        //Cmd + Option + Backspace deletes immediately, as in Finder
+        event.preventDefault();
+        if (event.altKey){
+            forceDelete();
+        }else{
+            deleteFile();
+        }
     }else if (event.which == 38){
         //Up Arrow
         if ($(".popup:visible").length == 0){
@@ -93,7 +109,15 @@ $(document).keydown(function(event){
         //Down arrow
         if ($(".popup:visible").length == 0){
             //No popup visiable
-            selectNextFile(event);
+            if (isMacOS && ctrlHold == true){
+                //Cmd + Down opens the selection, as in Finder
+                event.preventDefault();
+                if ($(".fileObject.selected").length > 0){
+                    openViaButton();
+                }
+            }else{
+                selectNextFile(event);
+            }
         }
     }else if (event.which == 39){
         //Right arrow
@@ -121,7 +145,12 @@ $(document).keydown(function(event){
         $(".fileObject.selected").removeClass("selected");
 
     }else{
-        //Handle generic file search by consecutive typing
+        //Handle generic file search by consecutive typing. Only plain printable
+        //keys count: a browser shortcut (Cmd + R, Cmd + L ...) or a lone
+        //modifier must not wipe the selection or end up in the search buffer
+        if (event.key == undefined || event.key.length != 1 || event.ctrlKey || event.metaKey || event.altKey){
+            return;
+        }
         if (hotSearchTimer != null){
             clearTimeout(hotSearchTimer);
         }
@@ -143,11 +172,31 @@ $(document).keydown(function(event){
 });
 
 $(document).keyup(function(event){
-    if(event.which==17 || event.which == 91){
-        ctrlHold = false;
+    if(isPrimaryModifierKey(event)){
+        if (!stickyMultiSelect){
+            ctrlHold = false;
+        }
         updateCtrlDisplay();
     }else if (event.which == 16){
         shiftHold = false;
+        updateCtrlDisplay();
+    }
+});
+
+/*
+    A modifier released while this window is not focused never sends its keyup
+    here - most often Cmd + Tab on a Mac (or Alt + Tab), which would otherwise
+    leave every later click adding to the selection. Mobile multi-select also
+    lives in ctrlHold, so it is left alone.
+*/
+$(window).on("blur", function(){
+    if (isMobile){
+        return;
+    }
+    if (ctrlHold || shiftHold){
+        ctrlHold = false;
+        shiftHold = false;
+        updateCtrlDisplay();
     }
 });
 
@@ -338,3 +387,22 @@ function getFileObjectInTheSameCol(baseFileObject){
     return fileObjectInTheSameCol;
 
 }
+
+/*
+    Shortcut hints are written for Ctrl in the markup. On a Mac they read the
+    way the system menus do: Cmd glyph + key, and Cmd + Backspace for Delete.
+*/
+function applyPlatformShortcutLabels(){
+    if (!isMacOS){
+        return;
+    }
+    $(".fsMenuShortcut, #contextmenu .description").each(function(){
+        let label = $(this).text().trim();
+        if (label == "Del"){
+            $(this).text("\u2318\u232B");
+        }else if (/^Ctrl \+ /.test(label)){
+            $(this).text(label.replace(/^Ctrl \+ /, "\u2318"));
+        }
+    });
+}
+applyPlatformShortcutLabels();

+ 37 - 1
src/web/SystemAO/file_system/js/explorer/selection.js

@@ -41,6 +41,24 @@ function bindFileObjectEvents(){
             lastTapTime = isSecondTap ? 0 : now;
         }
 
+        /*
+            Read the modifiers off the click itself as well as the tracked
+            flags: Cmd on a Mac (Ctrl elsewhere) toggles, Shift selects a range.
+            The flags alone missed Cmd whenever its keydown went to another
+            frame or was reported under a keyCode not listed here.
+        */
+        if (!isMobile && !stickyMultiSelect){
+            //The click's own modifier state is authoritative, which also clears
+            //a flag left stuck by a keyup that never arrived. Off the Mac the
+            //Windows key (metaKey) has always doubled as Ctrl
+            let held = hasPrimaryModifier(evt) || (!isMacOS && evt.metaKey);
+            if (held != ctrlHold){
+                ctrlHold = held;
+                updateCtrlDisplay();
+            }
+        }
+        let shiftClick = shiftHold || (!isMobile && evt.shiftKey);
+
         if (ctrlHold == true){
             if ($(this).hasClass("selected")){
                 $(this).removeClass("selected");
@@ -72,7 +90,7 @@ function bindFileObjectEvents(){
                 openthis(this, evt);
                 return;
             }
-        }else if (shiftHold == true){
+        }else if (shiftClick){
             //Select everything in range lastClicked to this
             var thisFileID = $(this).attr("fileid");
             let start = parseInt(thisFileID);
@@ -222,6 +240,24 @@ function bindFileObjectEvents(){
             return;
         }
 
+        /*
+            Ctrl + click is the right click on a Mac, but it arrives as a left
+            button mousedown, so the right-click select above never ran and
+            the menu would act on the old selection. Select the target here,
+            the same way a right click does.
+        */
+        if ($(this).hasClass("selected") == false){
+            if (!hasPrimaryModifier(evt)){
+                $(".fileObject.selected").removeClass("selected");
+            }
+            $(this).addClass("selected");
+            lastClickedFileID = parseInt($(this).attr("fileid"));
+            updateSelectedObjectsCount();
+            if (propertiesView){
+                loadFileProperties($(this).attr("filepath"));
+            }
+        }
+
         //Show all options by defaults
         $("#contextmenu").find(".item").show();
         $("#contextmenu").find(".vroothide").show();

+ 27 - 0
src/web/SystemAO/file_system/js/explorer/state.js

@@ -166,6 +166,33 @@ let isChromium = window.chrome;
 let isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
 let isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
 let isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
+//iPadOS with a keyboard also reports itself as a Mac and uses Cmd the same way
+let isMacOS = /Mac/i.test(navigator.platform || navigator.userAgent);
+
+/*
+    The "primary" modifier is Ctrl everywhere except macOS, where it is Cmd.
+    Ctrl + click on a Mac is a right click, so Ctrl must not select there.
+
+    isPrimaryModifierKey answers "is this keydown / keyup the modifier itself"
+    (event.key first; the codes cover Cmd's per-browser keyCodes: 91 / 93 in
+    Chrome and Safari, 224 in Firefox). hasPrimaryModifier reads the modifier
+    state carried by any key or mouse event, which stays correct even when the
+    keydown landed in another frame or the keyup was lost to Cmd + Tab.
+*/
+function isPrimaryModifierKey(event){
+    if (isMacOS){
+        return event.key == "Meta" || event.which == 91 || event.which == 93 || event.which == 224;
+    }
+    //The Windows key has always doubled as Ctrl here
+    return event.key == "Control" || event.which == 17 || event.which == 91;
+}
+
+function hasPrimaryModifier(event){
+    if (event == undefined){
+        return false;
+    }
+    return isMacOS ? !!event.metaKey : !!event.ctrlKey;
+}
 
 //Bind onclicke events for fileObjects
 var lastClickedFileID = 0;