Improve macos permission prompt

This commit is contained in:
crschnick 2023-02-12 21:09:53 +00:00
parent 642d5c44ec
commit 7d2c328fa0
2 changed files with 47 additions and 17 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;
}
}