Repository Public
1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/check-shortcuts.py4# Description: Verifies that no key combination is declared twice in the menu bar of SP Mail (macOS menus = iPadOS hardware-keyboard5# commands): the app's own commands (MailCommands, ComposeFormatMenu, SystemCommands), the commands SwiftUI adds (sidebar, toolbar,6# inspector, standard Edit / File / Window items) and the system-wide combinations macOS keeps for itself. A combination may be shared7# by two items only when they are mutually exclusive by construction: `composer != nil ? … : nil` and `composer == nil ? … : nil`.8#9# Usage: Scripts/check-shortcuts.py exits 1 and lists the clashes when there are any10import os, re, sys11ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))12FILES = ["Sources/Shared/Commands.swift", "Sources/Shared/Compose/FormatBar.swift", "Sources/Shared/System/SystemShare.swift", "Sources/macOS/SPMailApp.swift"]13ORDER = ["control", "option", "shift", "command"]14SYMBOL = {"control": "⌃", "option": "⌥", "shift": "⇧", "command": "⌘"}1516def combo(key, mods):17 return "".join(SYMBOL[m] for m in ORDER if m in mods) + key.upper()1819# What SwiftUI and macOS already own in an app with SidebarCommands, ToolbarCommands, InspectorCommands and the standard menus.20RESERVED = {21 "⌃⌘S": "Show / Hide Sidebar (SidebarCommands)", "⌥⌘T": "Show / Hide Toolbar (ToolbarCommands)", "⌃⌘I": "Show / Hide Inspector (InspectorCommands)",22 "⌃⌘F": "Enter Full Screen", "⌘Q": "Quit", "⌘W": "Close", "⌘M": "Minimize", "⌥⌘M": "Minimize All", "⌘H": "Hide", "⌥⌘H": "Hide Others", "⌘,": "Settings",23 "⌘Z": "Undo", "⇧⌘Z": "Redo", "⌘X": "Cut", "⌘C": "Copy", "⌘V": "Paste", "⌘A": "Select All", "⌥⇧⌘V": "Paste and Match Style",24 "⌥⌘D": "Show / Hide Dock (system-wide)", "⌃⌘D": "Look Up (system-wide)", "⌃⌘Q": "Lock Screen (system-wide)", "⌃⌘SPACE": "Character Viewer (system-wide)",25 "⌥⌘ESCAPE": "Force Quit (system-wide)", "⇧⌘3": "Screenshot (system-wide)", "⇧⌘4": "Screenshot (system-wide)", "⇧⌘5": "Screenshot (system-wide)", "⇧⌘Q": "Log Out (system-wide)",26 "⌘`": "Next Window", "⇧⌘/": "Help",27}28KEYNAMES = {".downArrow": "↓", ".upArrow": "↑", ".delete": "⌫", ".return": "↩", ".escape": "ESCAPE", ".space": "SPACE"}29PATTERN = re.compile(r'(?:\.keyboardShortcut|KeyboardShortcut)\(\s*(?:"((?:\\.|[^"\\])+)"|(\.[A-Za-z]+)|KeyEquivalent\(Character\(String\(n \+ 1\)\)\))\s*(?:,\s*modifiers:\s*(\[[^\]]*\]|\.[a-z]+))?\s*\)')3031found = {} # combo -> [(file, line, scope, text)]32for rel in FILES:33 for n, line in enumerate(open(os.path.join(ROOT, rel), encoding="utf-8"), 1):34 code = line.split("//")[0] if line.strip().startswith("//") else line35 for m in PATTERN.finditer(code):36 if "defaultAction" in m.group(0) or "cancelAction" in m.group(0): continue37 mods = set(re.findall(r"\.(control|option|shift|command)", m.group(3) or ".command")) or {"command"}38 scope = "composer" if "composer != nil ?" in code else "no-composer" if "composer == nil ?" in code else "always"39 title = (re.search(r'Button\("([^"]+)"', code) or re.search(r'Button\(([a-zA-Z]+)\(', code))40 keys = [str(d) for d in range(1, 10)] if m.group(1) is None and m.group(2) is None else [KEYNAMES.get(m.group(2), m.group(2))] if m.group(2) else [m.group(1).replace("\\\\", "\\")]41 for k in keys:42 found.setdefault(combo(k, mods), []).append((rel, n, scope, title.group(1) if title else "?"))4344problems = []45for c, uses in sorted(found.items()):46 if c in RESERVED: problems += [f"{c} is taken by {RESERVED[c]}: {f}:{n} « {t} »" for f, n, _, t in uses]47 scopes = [s for _, _, s, _ in uses]48 clash = len(uses) > 1 and not (len(uses) == 2 and sorted(scopes) == ["composer", "no-composer"])49 if clash: problems.append(f"{c} is declared {len(uses)} times: " + "; ".join(f"{f}:{n} « {t} » [{s}]" for f, n, s, t in uses))50print(f"{sum(len(u) for u in found.values())} menu shortcuts, {len(found)} distinct combinations")51for p in problems: print(" clash:", p)52sys.exit(1 if problems else 0)53