diff --git a/README.md b/README.md index eb057b3..e3d5b3d 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,12 @@ installer per platform: a DMG for macOS (Apple silicon), an `.exe` for Windows x `.deb` for Linux x64, plus portable archives for Windows and Linux. They bundle their own Java runtime. See [packaging/README.md](packaging/README.md) for how releases are built. +On macOS, the toolbar highlights **Access** the first time you run each version. +Open it for shortcuts to macOS settings for protected files, local networking, the Phone +page firewall, app automation, accessibility, and screen recording. Grant access in +macOS as needed. **Done** clears the highlight for that version, and **Later** keeps the +reminder. You can reopen the dialog any time. + ## Run from source diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/settings/AppSettings.java b/app/src/main/java/io/github/ghosthack/microhm/app/settings/AppSettings.java index 33d04e8..8fe9ac9 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/settings/AppSettings.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/settings/AppSettings.java @@ -218,6 +218,15 @@ public static void saveToolbarLabels(boolean on) { save(p -> p.setProperty("toolbar.labels", on ? "on" : "off")); } + /** Records which app versions the user has reviewed in the macOS permissions dialog. */ + public static boolean macPermissionsReviewed(String version) { + return Boolean.parseBoolean(load().getProperty("mac.permissions-reviewed." + version)); + } + + public static void saveMacPermissionsReviewed(String version) { + save(p -> p.setProperty("mac.permissions-reviewed." + version, "true")); + } + /** * Whether the toolbar shows its Page Up/Down controls: * {@code toolbar.page-buttons=on|off}. Off (unset, or anything but {@code on} — diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/ui/LucidIcon.java b/app/src/main/java/io/github/ghosthack/microhm/app/ui/LucidIcon.java index 1fbd9a7..1e85d37 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/ui/LucidIcon.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/ui/LucidIcon.java @@ -23,6 +23,10 @@ */ final class LucidIcon implements Icon { + static LucidIcon shield(int size) { + return new LucidIcon("M12 2 20 5v7a11 11 0 0 1-8 10 11 11 0 0 1-8-10V5z", size); + } + /** The kotlin-mosaic {@code Icon.Screensaver} crescent, path data verbatim. */ static LucidIcon moon(int size) { return new LucidIcon("M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z", size); diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialog.java b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialog.java new file mode 100644 index 0000000..63ff2f4 --- /dev/null +++ b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialog.java @@ -0,0 +1,187 @@ +package io.github.ghosthack.microhm.app.ui; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; +import java.awt.GridBagConstraints; +import java.awt.Window; +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +/** Shortcuts to the macOS settings that affect sessions and the Phone page. */ +final class MacPermissionsDialog { + + private static final String SETTINGS_SCHEME = "x-apple.systempreferences:"; + private static final String PRIVACY_SETTINGS = "com.apple.settings.PrivacySecurity.extension"; + private static final String NETWORK_SETTINGS = "com.apple.Network-Settings.extension"; + private static final String LEGACY_SECURITY = "com.apple.preference.security"; + + enum Pane { + FILES("Files & Folders", "Use projects in protected folders such as Desktop or Documents.", + "Privacy_FilesAndFolders"), + DISK("Full Disk Access", "Add the app if your workflow needs broader access to protected files.", + "Privacy_AllFiles"), + LOCAL_NETWORK("Local Network", "Let a session connect to devices on your local network.", + "Privacy_LocalNetwork"), + FIREWALL("Firewall", "Allow incoming connections for the Phone page if the firewall blocks them.", + "Firewall"), + AUTOMATION("Automation", "Allow a tool in a session to control specific apps through Apple Events.", + "Privacy_Automation"), + ACCESSIBILITY("Accessibility", "Let a UI automation tool interact with other apps.", + "Privacy_Accessibility"), + SCREEN_RECORDING("Screen & System Audio Recording", "Allow a tool to capture the Mac screen or system audio.", + "Privacy_ScreenCapture"); + + private final String title; + private final String description; + private final String anchor; + + Pane(String title, String description, String anchor) { + this.title = title; + this.description = description; + this.anchor = anchor; + } + + String title(int macVersion) { + // Sequoia (15) renamed the pane. Sonoma (14) still calls it Screen Recording. + return this == SCREEN_RECORDING && macVersion < 15 ? "Screen Recording" : title; + } + + String settingsUrl(int macVersion) { + String section = macVersion < 13 ? LEGACY_SECURITY + : this == FIREWALL ? NETWORK_SETTINGS : PRIVACY_SETTINGS; + return SETTINGS_SCHEME + section + "?" + anchor; + } + + String settingsPath(int macVersion) { + if (this == FIREWALL) { + return macVersion < 13 ? "Security & Privacy › Firewall › Firewall Options" + : "Network › Firewall › Options"; + } + String section = macVersion < 13 ? "Security & Privacy › Privacy" : "Privacy & Security"; + return section + " › " + title(macVersion); + } + } + + /** Returns true only when the user marks this version's review done. */ + static boolean show(Window parent, boolean reviewPending) { + int macVersion = macMajorVersion(); + String settingsName = macVersion < 13 ? "System Preferences" : "System Settings"; + JDialog dialog = DialogChrome.newDialog(parent, "macOS Permissions"); + DialogChrome.Form form = DialogChrome.form(); + JLabel guidance = new JLabel(html(450, "Grant the access your workflow needs in " + settingsName + + ". Some entries appear after you use a feature that requests access. " + + "Use the app named in the macOS prompt.")); + guidance.setBorder(BorderFactory.createEmptyBorder(0, 0, 8, 0)); + addFullWidth(form, guidance); + JLabel path = new JLabel(html(450, "Some changes take effect after you quit and reopen the app.")); + for (Pane pane : Pane.values()) { + if (pane == Pane.LOCAL_NETWORK && macVersion < 15) { + continue; + } + String title = pane.title(macVersion); + JLabel details = new JLabel(html(325, "" + title.replace("&", "&") + "
" + pane.description)); + JButton open = new JButton("Open Settings"); + open.getAccessibleContext().setAccessibleName("Open " + title + " settings"); + String settingsPath = pane.settingsPath(macVersion); + open.setToolTipText(settingsPath); + open.addActionListener(e -> { + path.setText(html(450, "If needed, choose " + settingsPath.replace("&", "&") + ".")); + openSettings(dialog, pane.settingsUrl(macVersion), settingsName, settingsPath); + }); + int row = form.reserveRow(); + form.constraints.gridy = row; + form.constraints.gridx = 0; + form.constraints.gridwidth = 1; + form.constraints.weightx = 1; + form.panel.add(details, form.constraints); + form.constraints.gridx = 1; + form.constraints.weightx = 0; + form.panel.add(open, form.constraints); + } + path.setBorder(BorderFactory.createEmptyBorder(7, 0, 0, 0)); + addFullWidth(form, path); + + boolean[] reviewed = {false}; + JButton close = new JButton(reviewPending ? "Later" : "Close"); + close.addActionListener(e -> dialog.dispose()); + JButton done = new JButton("Done"); + done.addActionListener(e -> { + reviewed[0] = true; + dialog.dispose(); + }); + DialogChrome.layout(dialog, form.panel, + reviewPending ? DialogChrome.buttons(close, done) : DialogChrome.buttons(close)); + DialogChrome.show(dialog, parent, reviewPending ? done : close, close); + return reviewed[0]; + } + + private static void addFullWidth(DialogChrome.Form form, JLabel label) { + GridBagConstraints constraints = form.constraints; + constraints.gridy = form.reserveRow(); + constraints.gridx = 0; + constraints.gridwidth = 2; + constraints.weightx = 1; + form.panel.add(label, constraints); + } + + private static String html(int width, String text) { + return "" + text + ""; + } + + private static int macMajorVersion() { + try { + return Integer.parseInt(System.getProperty("os.version", "").split("\\.", 2)[0]); + } catch (NumberFormatException e) { + return Integer.MAX_VALUE; // use current System Settings paths if the version is unavailable + } + } + + private static void openSettings(JDialog dialog, String url, String settingsName, String settingsPath) { + Thread.startVirtualThread(() -> { + try { + if (open(url) || open("-b", "com.apple.systempreferences")) { + return; + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + SwingUtilities.invokeLater(() -> { + if (dialog.isShowing()) { + JOptionPane.showMessageDialog(dialog, + "Open " + settingsName + " and choose " + settingsPath + ".", + "Could not open " + settingsName, JOptionPane.WARNING_MESSAGE); + } + }); + }); + } + + private static boolean open(String... arguments) throws InterruptedException { + String[] command = new String[arguments.length + 1]; + command[0] = "/usr/bin/open"; + System.arraycopy(arguments, 0, command, 1, arguments.length); + Process process; + try { + process = new ProcessBuilder(command) + .redirectOutput(ProcessBuilder.Redirect.DISCARD) + .redirectError(ProcessBuilder.Redirect.DISCARD) + .start(); + } catch (IOException e) { + return false; + } + try { + return process.waitFor(3, TimeUnit.SECONDS) && process.exitValue() == 0; + } finally { + if (process.isAlive()) { + process.destroyForcibly(); + } + } + } + + private MacPermissionsDialog() { + } +} diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacWindowAppearance.java b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacWindowAppearance.java index e95e6f7..a3055ab 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacWindowAppearance.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MacWindowAppearance.java @@ -54,7 +54,7 @@ static boolean isMac(String osName) { return osName != null && osName.toLowerCase(Locale.ROOT).contains("mac"); } - private static boolean isMac() { + static boolean isMac() { return isMac(System.getProperty("os.name", "")); } diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java index 642691e..3531934 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java @@ -261,6 +261,7 @@ public String getToolTipText(MouseEvent event) { private final JButton removeDeadButton = new JButton(); private final JButton themeButton = new JButton(); private final JButton usageButton = new JButton(); + private final JButton macPermissionsButton = new JButton(); private final JButton settingsButton = new JButton(); private final JButton sortButton = new JButton(); private final JButton filterButton = new JButton(); @@ -283,6 +284,10 @@ public String getToolTipText(MouseEvent event) { * also what {@code MicroHarnessManagerApp} set up the initial LaF from. */ private AppSettings.UiTheme uiTheme = AppSettings.uiTheme(); + private final boolean mac = MacWindowAppearance.isMac(); + private final String appVersion = mac ? MainWindow.class.getPackage().getImplementationVersion() : null; + private boolean permissionsReviewPending = appVersion != null && !AppSettings.macPermissionsReviewed(appVersion); + /** Whether toolbar buttons carry text next to their glyph (the settings dialog's * toggle); tooltips carry the long form either way. */ private boolean toolbarLabels = AppSettings.toolbarLabels(); @@ -542,6 +547,12 @@ public void mouseReleased(MouseEvent e) { usageButton.setToolTipText("Show harness usage and limits"); usageButton.addActionListener(e -> openUsageLimitsDialog()); addControlButton(toolBar, usageButton); + if (mac) { + configureControlButton(macPermissionsButton); + macPermissionsButton.setIcon(LucidIcon.shield(16)); + macPermissionsButton.addActionListener(e -> openMacPermissionsDialog()); + addControlButton(toolBar, macPermissionsButton); + } settingsButton.setIcon(LucidIcon.settings(16)); settingsButton.setToolTipText("Settings"); settingsButton.addActionListener(e -> openSettingsDialog()); @@ -2578,6 +2589,15 @@ private void resumeSession(SidebarEntry entry) { // Settings dialog // ------------------------------------------------------------------ + private void openMacPermissionsDialog() { + if (MacPermissionsDialog.show(this, permissionsReviewPending)) { + permissionsReviewPending = false; + applyMacPermissionsButtonFace(); + applyToolbarBackground(); + settingsExecutor.execute(() -> AppSettings.saveMacPermissionsReviewed(appVersion)); + } + } + /** * Opens the settings dialog and, on Save, persists the choices (off the EDT — see * {@link #toggleTheme}) and applies what can be applied immediately: attached @@ -2720,6 +2740,9 @@ private void applyToolbarButtonBackground(Container container, boolean outline) if (child instanceof JButton button) { Color background = toolbarMatchesSidebar ? sidebarList.getBackground() : UIManager.getColor("Button.background"); + if (button == macPermissionsButton && permissionsReviewPending) { + background = uiTheme == AppSettings.UiTheme.LIGHT ? new Color(0xd6ebff) : new Color(0x193c59); + } Map style = new LinkedHashMap<>(); if (outline || toolbarMatchesSidebar) { Color border = toolbarBorderColor(background); @@ -2849,6 +2872,9 @@ private void applyToolbarLabels() { batchCloseButton.setText(toolbarLabels ? "Batch close…" : null); removeDeadButton.setText(toolbarLabels ? "Remove dead…" : null); usageButton.setText(toolbarLabels ? "Usage" : null); + if (mac) { + applyMacPermissionsButtonFace(); + } settingsButton.setText(toolbarLabels ? "Settings" : null); applyPageButtonsVisibility(); applyStopLadderVisibility(); @@ -2858,6 +2884,13 @@ private void applyToolbarLabels() { applyThemeButtonFace(); } + private void applyMacPermissionsButtonFace() { + macPermissionsButton.setText(toolbarLabels ? "Access" : null); + String description = permissionsReviewPending ? "Review macOS permissions for this version" : "macOS Permissions"; + macPermissionsButton.setToolTipText(description); + macPermissionsButton.getAccessibleContext().setAccessibleName(description); + } + /** Hides the optional page-button group cleanly, without leaving two separators. */ private void applyPageButtonsVisibility() { pageButtonsGroup.setVisible(toolbarPageButtons); diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/settings/AppSettingsTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/settings/AppSettingsTest.java index f1fb59d..34bc377 100644 --- a/app/src/test/java/io/github/ghosthack/microhm/app/settings/AppSettingsTest.java +++ b/app/src/test/java/io/github/ghosthack/microhm/app/settings/AppSettingsTest.java @@ -49,6 +49,21 @@ private void settings(String content) throws IOException { Files.writeString(dir.resolve("settings.properties"), content); } + @Test + void macPermissionsReviewPersistsPerVersionAlongsideExistingSettings() throws IOException { + settings("toolbar.labels=off\n"); + assertFalse(AppSettings.macPermissionsReviewed("1.2.0")); + + AppSettings.saveMacPermissionsReviewed("1.2.0"); + assertTrue(AppSettings.macPermissionsReviewed("1.2.0")); + assertFalse(AppSettings.macPermissionsReviewed("1.3.0")); + + AppSettings.saveMacPermissionsReviewed("1.3.0"); + assertTrue(AppSettings.macPermissionsReviewed("1.2.0")); + assertTrue(AppSettings.macPermissionsReviewed("1.3.0")); + assertFalse(AppSettings.toolbarLabels()); + } + @Test void concurrentSavesOfDifferentKeysNeitherLoseAChangeNorLeaveTempFiles() throws Exception { for (int round = 0; round < 50; round++) { diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialogTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialogTest.java new file mode 100644 index 0000000..c7c2973 --- /dev/null +++ b/app/src/test/java/io/github/ghosthack/microhm/app/ui/MacPermissionsDialogTest.java @@ -0,0 +1,15 @@ +package io.github.ghosthack.microhm.app.ui; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +final class MacPermissionsDialogTest { + + @Test + void screenRecordingKeepsTheSonomaNameThroughMacOS14() { + MacPermissionsDialog.Pane screen = MacPermissionsDialog.Pane.SCREEN_RECORDING; + assertEquals("Privacy & Security › Screen Recording", screen.settingsPath(14)); + assertEquals("Privacy & Security › Screen & System Audio Recording", screen.settingsPath(15)); + } +}