Skip to content

Commit

Permalink
Turned safeAreaInsets into a JavaFX property
Browse files Browse the repository at this point in the history
  • Loading branch information
salmonb committed Aug 29, 2024
1 parent 634a932 commit 85d22b0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import elemental2.dom.*;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.*;
import javafx.collections.ObservableList;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
Expand Down Expand Up @@ -257,8 +256,19 @@ public Application getApplication() {
return application;
}

private ObjectProperty<Insets> safeAreaInsetsProperty = null;

@Override
public Insets getSafeAreaInsets() {
public ReadOnlyObjectProperty<Insets> safeAreaInsetsProperty() {
if (safeAreaInsetsProperty == null) {
safeAreaInsetsProperty = new SimpleObjectProperty<>(Insets.EMPTY);
FXProperties.runNowAndOnPropertiesChange(this::updateSafeAreaInsets,
getPrimaryStage().widthProperty(), getPrimaryStage().heightProperty());
}
return safeAreaInsetsProperty;
}

public void updateSafeAreaInsets() {
/* The following code is relying on this CSS rule present in [email protected]
:root {
--safe-area-inset-top: env(safe-area-inset-top);
Expand All @@ -272,11 +282,11 @@ public Insets getSafeAreaInsets() {
String right = computedStyle.getPropertyValue("--safe-area-inset-right");
String bottom = computedStyle.getPropertyValue("--safe-area-inset-bottom");
String left = computedStyle.getPropertyValue("--safe-area-inset-left");
return new Insets(
Numbers.doubleValue(Strings.removeSuffix(top, "px")),
Numbers.doubleValue(Strings.removeSuffix(right, "px")),
Numbers.doubleValue(Strings.removeSuffix(bottom, "px")),
Numbers.doubleValue(Strings.removeSuffix(left, "px"))
);
safeAreaInsetsProperty.set(new Insets(
Numbers.doubleValue(Strings.removeSuffix(top, "px")),
Numbers.doubleValue(Strings.removeSuffix(right, "px")),
Numbers.doubleValue(Strings.removeSuffix(bottom, "px")),
Numbers.doubleValue(Strings.removeSuffix(left, "px"))
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import dev.webfx.platform.util.function.Factory;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
Expand Down Expand Up @@ -122,7 +125,6 @@ public void start(Stage primaryStage) throws Exception {
application.start(primaryStage);
}
}

}

@Override
Expand All @@ -144,4 +146,11 @@ public double measureBaselineOffset(Font font) {
measurementText.setFont(font);
return measurementText.getBaselineOffset();
}

private final ReadOnlyObjectProperty<Insets> safeAreaInsetsProperty = new SimpleObjectProperty<>(Insets.EMPTY);

@Override
public ReadOnlyObjectProperty<Insets> safeAreaInsetsProperty() {
return safeAreaInsetsProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import dev.webfx.kit.launcher.spi.FastPixelReaderWriter;
import dev.webfx.kit.launcher.spi.WebFxKitLauncherProvider;
import dev.webfx.platform.console.Console;
import dev.webfx.platform.util.function.Factory;
import dev.webfx.platform.service.SingleServiceProvider;
import dev.webfx.platform.util.function.Factory;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
Expand Down Expand Up @@ -134,6 +135,10 @@ else if (webFxCssPath.contains(":"))
return "dev/webfx/kit/css/" + webFxCssPath;
}

public static ReadOnlyObjectProperty<Insets> safeAreaInsetsProperty() {
return getProvider().safeAreaInsetsProperty();
}

public static Insets getSafeAreaInsets() {
return getProvider().getSafeAreaInsets();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
Expand Down Expand Up @@ -97,7 +98,9 @@ default ObservableList<Font> loadingFonts() {
return FXCollections.emptyObservableList(); // Default implementation fpr synchronous font loading toolkits (such as OpenJFX)
}

ReadOnlyObjectProperty<Insets> safeAreaInsetsProperty();

default Insets getSafeAreaInsets() {
return Insets.EMPTY;
return safeAreaInsetsProperty().get();
}
}

0 comments on commit 85d22b0

Please sign in to comment.