Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): keep the launch bar visible after rotating (closes #10835)

Elian Doran90e4e981cc88
diff --git a/apps/client/src/widgets/containers/root_container.spec.ts b/apps/client/src/widgets/containers/root_container.spec.ts
new file mode 100644
index 0000000000..10cebc2e2e
--- /dev/null
+++ b/apps/client/src/widgets/containers/root_container.spec.ts
@@ -0,0 +1,70 @@
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import RootContainer from "./root_container";
+
+/**
+ * Stands in for `window.visualViewport`, which happy-dom does not implement. Only the parts the
+ * root container reads (`height`) and listens on (`resize`) are modelled.
+ */
+class FakeVisualViewport extends EventTarget {
+ height = 0;
+ width = 0;
+}
+
+describe("RootContainer", () => {
+ let visualViewport: FakeVisualViewport;
+
+ beforeEach(() => {
+ window.glob = { ...window.glob, device: "mobile" };
+ visualViewport = new FakeVisualViewport();
+ Object.defineProperty(window, "visualViewport", { value: visualViewport, configurable: true });
+ });
+
+ afterEach(() => {
+ Object.defineProperty(window, "visualViewport", { value: undefined, configurable: true });
+ window.glob = { ...window.glob, device: "desktop" };
+ });
+
+ /** Applies a new window/viewport geometry and lets the container react to it. */
+ function resizeTo({ width, height, viewportHeight }: { width: number; height: number; viewportHeight?: number }) {
+ Object.defineProperty(window, "innerWidth", { value: width, configurable: true });
+ Object.defineProperty(window, "innerHeight", { value: height, configurable: true });
+ visualViewport.width = width;
+ visualViewport.height = viewportHeight ?? height;
+ visualViewport.dispatchEvent(new Event("resize"));
+ }
+
+ function renderInPortrait() {
+ resizeTo({ width: 400, height: 800 });
+ const rootContainer = new RootContainer(false);
+ const $widget = rootContainer.render();
+ resizeTo({ width: 400, height: 800 });
+ return $widget;
+ }
+
+ it("does not report the virtual keyboard as opened after a portrait → landscape rotation", () => {
+ const $widget = renderInPortrait();
+ expect($widget.hasClass("virtual-keyboard-opened")).toBe(false);
+
+ // Rotating shrinks the window itself; the viewport still fills it, so no keyboard is up.
+ resizeTo({ width: 800, height: 400 });
+
+ expect($widget.hasClass("virtual-keyboard-opened")).toBe(false);
+ });
+
+ it("reports the virtual keyboard as opened while it covers part of the viewport", () => {
+ const $widget = renderInPortrait();
+
+ // Keyboard in portrait: the window keeps its height, the visual viewport shrinks.
+ resizeTo({ width: 400, height: 800, viewportHeight: 450 });
+ expect($widget.hasClass("virtual-keyboard-opened")).toBe(true);
+
+ resizeTo({ width: 400, height: 800 });
+ expect($widget.hasClass("virtual-keyboard-opened")).toBe(false);
+
+ // ... and the same once rotated to landscape.
+ resizeTo({ width: 800, height: 400 });
+ resizeTo({ width: 800, height: 400, viewportHeight: 200 });
+ expect($widget.hasClass("virtual-keyboard-opened")).toBe(true);
+ });
+});
diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.ts
index 314f1ec392..37cabc0766 100644
--- a/apps/client/src/widgets/containers/root_container.ts
+++ b/apps/client/src/widgets/containers/root_container.ts
@@ -31,12 +31,15 @@ const FONT_OPTIONS: OptionNames[] = [
*/
export default class RootContainer extends FlexContainer<BasicWidget> {
- private originalWindowHeight: number;
+ /** Window size the virtual keyboard detection compares against, per orientation. */
+ private baselineWindowHeight: number;
+ private baselineWindowWidth: number;
constructor(isHorizontalLayout: boolean) {
super(isHorizontalLayout ? "column" : "row");
- this.originalWindowHeight = window.innerHeight ?? 0;
+ this.baselineWindowHeight = window.innerHeight ?? 0;
+ this.baselineWindowWidth = window.innerWidth ?? 0;
this.id("root-widget");
this.css("height", "100dvh");
}
@@ -106,8 +109,16 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
}
#onMobileResize() {
+ // The virtual keyboard never changes the window width, so a width change means the device was
+ // rotated (or the window resized) and the stored height no longer describes this orientation.
+ // Keeping it would make the shorter landscape window look like an open keyboard (#10835).
+ if (window.innerWidth !== this.baselineWindowWidth) {
+ this.baselineWindowWidth = window.innerWidth;
+ this.baselineWindowHeight = window.innerHeight;
+ }
+
const viewportHeight = window.visualViewport?.height ?? window.innerHeight;
- const windowHeight = Math.max(window.innerHeight, this.originalWindowHeight); // inner height changes when keyboard is opened, we need to compare with the original height to detect it.
+ const windowHeight = Math.max(window.innerHeight, this.baselineWindowHeight); // inner height changes when keyboard is opened, we need to compare with the original height to detect it.
// If viewport is significantly smaller, keyboard is likely open
const isKeyboardOpened = windowHeight - viewportHeight > 150;