Skip to content

Commit

Permalink
Safe inset stuff for Android (still WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
HorusGoul committed May 12, 2024
1 parent fa0b3c7 commit 691f960
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 44 deletions.
5 changes: 5 additions & 0 deletions apps/pwa/src/NativeBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const platformMethods: PlatformMethods = {
window.open("market://details?id=tk.horusgoul.valenciasquimicas");
}
},
getSafeInset: () => null,
};

const AtomNative = window.AtomNative || platformMethods;
Expand Down Expand Up @@ -44,6 +45,10 @@ class NativeBridge {
return !!window.AtomNative?.["rateApp"];
}

public getSafeInset() {
return this.exec("getSafeInset", [], []);
}

private exec<T extends keyof typeof AtomNative>(
methodName: T,
nativeArgs: Parameters<NonNullable<typeof AtomNative[T]>>,
Expand Down
1 change: 1 addition & 0 deletions apps/pwa/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface Window {
getDebugMode?: () => boolean;
getSystemLanguage?: () => string | undefined;
rateApp?: (openMarket?: boolean) => void;
getSafeInset?: () => string | null;
};
__DEBUG__?: boolean;
}
Expand Down
6 changes: 3 additions & 3 deletions apps/pwa/src/components/about/About.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
}

&__content {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-bottom: env(safe-area-inset-bottom);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);
padding-bottom: var(--safe-area-inset-bottom);

max-width: 1024px;
margin: 0 auto;
Expand Down
4 changes: 2 additions & 2 deletions apps/pwa/src/components/hub/Hub.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
flex-direction: column;
background-color: var(--backgroundColor);
color: var(--contentTextColor);
padding-bottom: env(safe-area-inset-bottom);
padding-bottom: var(--safe-area-inset-bottom);
}

.header {
display: flex;
align-items: center;
flex-direction: column;
width: 100%;
padding-top: env(safe-area-inset-top);
padding-top: var(--safe-area-inset-top);
position: relative;

&::before {
Expand Down
10 changes: 5 additions & 5 deletions apps/pwa/src/components/mass-calculator/MassCalculator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
display: flex;
align-items: center;
padding: 16px;
padding-left: calc(16px + env(safe-area-inset-left));
padding-right: calc(16px + env(safe-area-inset-right));
padding-left: calc(16px + var(--safe-area-inset-left));
padding-right: calc(16px + var(--safe-area-inset-right));
z-index: 2;

@include box-shadow($level: 1);
Expand Down Expand Up @@ -176,12 +176,12 @@

&__controls,
&__element-list {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);
}

&__element-list {
padding-bottom: env(safe-area-inset-bottom);
padding-bottom: var(--safe-area-inset-bottom);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@

&__current-question {
position: fixed;
left: calc(24px + env(safe-area-inset-left, 0px));
bottom: calc(32px + env(safe-area-inset-bottom, 0px));
left: calc(24px + var(--safe-area-inset-left, 0px));
bottom: calc(32px + var(--safe-area-inset-bottom, 0px));
height: 72px;
width: 72px;
z-index: 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
height: 100%;

&__content {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);

@include themify($themes) {
background-color: themed("cardBackgroundColor");
Expand Down
2 changes: 1 addition & 1 deletion apps/pwa/src/components/periodic-table/PeriodicTable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
width: 100%;
height: 100%;

padding-left: env(safe-area-inset-left);
padding-left: var(--safe-area-inset-left);

@include themify($themes) {
color: themed("contentTextColor");
Expand Down
8 changes: 4 additions & 4 deletions apps/pwa/src/components/search-view/SearchView.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
overflow-y: auto;
animation: slideIn 180ms ease-in forwards;

padding-top: env(safe-area-inset-top);
padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-top: var(--safe-area-inset-top);
padding-bottom: var(--safe-area-inset-bottom);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);

@media (prefers-reduced-motion) {
animation: none;
Expand Down
6 changes: 3 additions & 3 deletions apps/pwa/src/components/shared/navbar/Navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
display: flex;
align-items: center;

padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-top: env(safe-area-inset-top);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);
padding-top: var(--safe-area-inset-top);

@include themify($themes) {
@include overscroll-top($color: themed("primary"));
Expand Down
6 changes: 3 additions & 3 deletions apps/pwa/src/components/test-selection/TestSelection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
max-width: 1024px;
margin: 0 auto;

padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-bottom: var(--safe-area-inset-bottom);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
height: 100%;

&__content {
padding-left: env(safe-area-inset-left);
padding-right: env(safe-area-inset-right);
padding-left: var(--safe-area-inset-left);
padding-right: var(--safe-area-inset-right);

@include themify($themes) {
background-color: themed("cardBackgroundColor");
Expand Down
19 changes: 2 additions & 17 deletions apps/pwa/src/contexts/SafeAreaInsetContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { loadHybridSafeInsets } from "@/services/safe-inset";
import { createContext, useContext, useEffect, useState } from "react";

export interface SafeAreaInset {
Expand All @@ -7,23 +8,6 @@ export interface SafeAreaInset {
left: number;
}

document.documentElement.style.setProperty(
"--safe-area-inset-top",
"env(safe-area-inset-top, 0px)"
);
document.documentElement.style.setProperty(
"--safe-area-inset-right",
"env(safe-area-inset-right, 0px)"
);
document.documentElement.style.setProperty(
"--safe-area-inset-bottom",
"env(safe-area-inset-bottom, 0px)"
);
document.documentElement.style.setProperty(
"--safe-area-inset-left",
"env(safe-area-inset-left, 0px)"
);

export const SafeAreaInsetContext = createContext<SafeAreaInset>(
getSafeAreaInset()
);
Expand All @@ -42,6 +26,7 @@ export function SafeAreaInsetProvider({

useEffect(() => {
const listener = () => {
loadHybridSafeInsets();
setState(getSafeAreaInset());
};

Expand Down
2 changes: 2 additions & 0 deletions apps/pwa/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import { initSentry } from "@/services/sentry";
import { loadFlags } from "@/services/flags";
import App from "./components/App";
import invariant from "invariant";
import { loadHybridSafeInsets } from "@/services/safe-inset";

loadFlags();
initSentry();
loadHybridSafeInsets();

const rootElement = document.getElementById("root");

Expand Down
7 changes: 7 additions & 0 deletions apps/pwa/src/main.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
@import "./scss/themes.scss";
@import "./scss/theming.scss";

:root {
--safe-area-inset-top: env(safe-area-inset-top, 0px);
--safe-area-inset-right: env(safe-area-inset-right, 0px);
--safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
--safe-area-inset-left: env(safe-area-inset-left, 0px);
}

* {
box-sizing: border-box;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
Expand Down
46 changes: 46 additions & 0 deletions apps/pwa/src/services/safe-inset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import NativeBridge from "@/NativeBridge";
import { useFlagStore } from "./flags";

export function loadHybridSafeInsets() {
const flags = useFlagStore.getState();

if (!flags.isHybrid) {
return;
}

if (!flags.isAndroid) {
return;
}

const insetJson = NativeBridge.getSafeInset();
if (!insetJson) {
return;
}

const values = {
top: 0,
bottom: 0,
left: 0,
right: 0,
};

try {
const inset = JSON.parse(insetJson);
Object.assign(values, inset);

// Apply pixel ratio
values.top /= window.devicePixelRatio;
values.bottom /= window.devicePixelRatio;
values.left /= window.devicePixelRatio;
values.right /= window.devicePixelRatio;
} catch {
// noop
}

const root = document.documentElement;

root.style.setProperty("--safe-area-inset-top", `${values.top}px`);
root.style.setProperty("--safe-area-inset-right", `${values.right}px`);
root.style.setProperty("--safe-area-inset-bottom", `${values.bottom}px`);
root.style.setProperty("--safe-area-inset-left", `${values.left}px`);
}

0 comments on commit 691f960

Please sign in to comment.