Improve macos permission prompt

This commit is contained in:
crschnick 2023-02-12 21:09:53 +00:00
parent b851c1da11
commit ccce77a648
3 changed files with 50 additions and 18 deletions

View file

@ -99,15 +99,11 @@ public class AppWindowHelper {
}
}
public static Optional<ButtonType> showBlockingAlert(Consumer<Alert> c) {
public static Optional<ButtonType> showBlockingAlert(Alert a) {
AtomicReference<Optional<ButtonType>> result = new AtomicReference<>();
if (!Platform.isFxApplicationThread()) {
CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(() -> {
Alert a = AppWindowHelper.createEmptyAlert();
AppFont.normal(a.getDialogPane());
c.accept(a);
result.set(a.showAndWait());
latch.countDown();
});
@ -116,10 +112,6 @@ public class AppWindowHelper {
} catch (InterruptedException ignored) {
}
} else {
Alert a = createEmptyAlert();
AppFont.normal(a.getDialogPane());
c.accept(a);
Button button = (Button) a.getDialogPane().lookupButton(ButtonType.OK);
if (button != null) {
button.getStyleClass().add("ok-button");
@ -130,6 +122,13 @@ public class AppWindowHelper {
return result.get();
}
public static Optional<ButtonType> showBlockingAlert(Consumer<Alert> c) {
Alert a = AppWindowHelper.createEmptyAlert();
AppFont.normal(a.getDialogPane());
c.accept(a);
return showBlockingAlert(a);
}
public static Alert createEmptyAlert() {
Alert alert = new Alert(Alert.AlertType.NONE);
setIcon(alert);

View file

@ -1,20 +1,51 @@
package io.xpipe.app.util;
import io.xpipe.app.core.AppWindowHelper;
import io.xpipe.core.store.ShellStore;
import io.xpipe.extension.I18n;
import io.xpipe.extension.util.ThreadHelper;
import javafx.application.Platform;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.Alert;
public class MacOsPermissions {
public static boolean waitForAccessibilityPermissions() throws Exception {
try (var pc = ShellStore.local().create().start()) {
while (true) {
var success = pc.executeBooleanSimpleCommand("osascript -e 'tell application \"System Events\" to keystroke \"t\"'");
if (success) {
return true;
}
private static Alert createAlert() {
var alert = AppWindowHelper.createEmptyAlert();
alert.setAlertType(Alert.AlertType.CONFIRMATION);
alert.setTitle(I18n.get("permissionsAlertTitle"));
alert.setHeaderText(I18n.get("permissionsAlertTitleHeader"));
alert.getDialogPane().setContent(AppWindowHelper.alertContentText(I18n.get("permissionsAlertTitleContent")));
alert.setAlertType(Alert.AlertType.CONFIRMATION);
return alert;
}
ThreadHelper.sleep(1000);
public static boolean waitForAccessibilityPermissions() throws Exception {
var alert = createAlert();
var state = new SimpleBooleanProperty(true);
try (var pc = ShellStore.local().create().start()) {
while (state.get()) {
var success = pc.executeBooleanSimpleCommand(
"osascript -e 'tell application \"System Events\" to keystroke \"t\"'");
if (success) {
Platform.runLater(() -> {
alert.close();
});
return true;
} else {
Platform.runLater(() -> {
var result = AppWindowHelper.showBlockingAlert(alert)
.map(buttonType -> buttonType.getButtonData().isDefaultButton())
.orElse(false);
if (!result) {
state.set(false);
}
});
ThreadHelper.sleep(1000);
}
}
}
return false;
}
}

View file

@ -224,7 +224,9 @@ public interface ExternalTerminalType extends PrefsChoiceValue {
@Override
public void launch(String name, String command) throws Exception {
MacOsPermissions.waitForAccessibilityPermissions();
if (!MacOsPermissions.waitForAccessibilityPermissions()) {
return;
}
try (ShellProcessControl pc = ShellStore.local().create().start()) {
var cmd = String.format(