Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve mobile editor scrolling experience https://github.com/siyuan-note/siyuan/issues/14542

Danielb92aacdecc75
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index d8321c0df9..62918b8d0b 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -3,6 +3,8 @@
@use "../component/svg";
body {
+ --mobile-topbar-height: 48px;
+ --mobile-breadcrumb-height: 42px;
--mobile-bottom-bar-height: 48px;
--mobile-bottom-bar-gap: 8px;
--mobile-status-height: 24px;
@@ -11,10 +13,6 @@ body {
--mobile-bottom-bar-offset: calc(var(--mobile-bottom-bar-height) + var(--mobile-bottom-bar-bottom));
}
-body.mobile-chrome--hidden {
- --mobile-bottom-bar-offset: var(--mobile-bottom-bar-safe-area);
-}
-
body.mobile-keyboard--open {
--mobile-bottom-bar-offset: 0px;
}
@@ -295,18 +293,17 @@ body.mobile-keyboard--open {
}
.mobile-topbar {
- position: relative;
+ position: absolute;
+ top: env(safe-area-inset-top);
+ right: 0;
+ left: 0;
z-index: 3;
box-sizing: border-box;
- height: 48px;
- min-height: 48px;
+ height: var(--mobile-topbar-height);
+ min-height: var(--mobile-topbar-height);
padding: 0 max(4px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
- flex-shrink: 0;
- transition: transform 240ms cubic-bezier(.2, 0, 0, 1), visibility 240ms;
-
- > * {
- transition: opacity 160ms cubic-bezier(.2, 0, 0, 1), transform 240ms cubic-bezier(.2, 0, 0, 1);
- }
+ transform: translate3d(0, var(--mobile-bar-translate-y, 0), 0);
+ will-change: transform;
}
.mobile-bottom-bar {
@@ -326,7 +323,9 @@ body.mobile-keyboard--open {
border-radius: calc(var(--mobile-bottom-bar-height) / 2);
background-color: var(--b3-theme-background);
box-shadow: var(--b3-point-shadow);
- transition: opacity 240ms cubic-bezier(.2, 0, 0, 1), transform 240ms cubic-bezier(.2, 0, 0, 1), visibility 240ms;
+ opacity: var(--mobile-bar-opacity, 1);
+ transform: translate3d(0, var(--mobile-bar-translate-y, 0), 0);
+ will-change: opacity, transform;
-webkit-user-select: none;
user-select: none;
@@ -386,33 +385,18 @@ body.mobile-keyboard--open {
.mobile-chrome--hidden {
.mobile-topbar {
- transform: translateY(-100%);
visibility: hidden;
-
- > * {
- opacity: 0;
- transform: translateY(-4px);
- }
- }
-
- #editor {
- margin-bottom: -48px;
- transform: translateY(-48px);
}
.mobile-bottom-bar {
- opacity: 0;
pointer-events: none;
- transform: translateY(100%);
visibility: hidden;
}
}
-.mobile-chrome--transitioning #editor {
- margin-bottom: -48px;
-}
-
body.mobile-topbar--merged {
+ --mobile-topbar-height: 0px;
+
.mobile-topbar {
height: 0;
min-height: 0;
@@ -423,9 +407,8 @@ body.mobile-topbar--merged {
visibility: hidden;
}
- &.mobile-chrome--hidden #editor,
- &.mobile-chrome--transitioning #editor {
- margin-bottom: 0;
+ #editor > .protyle-breadcrumb {
+ top: 0;
transform: none;
}
}
@@ -434,14 +417,10 @@ body.mobile-topbar--merged {
.mobile-selection--active .mobile-bottom-bar {
opacity: 0;
pointer-events: none;
- transform: translateY(100%);
+ transform: translate3d(0, 100%, 0);
visibility: hidden;
}
-.mobile-keyboard--open .mobile-bottom-bar {
- transition: none;
-}
-
.mobile-bottom-bar-setting {
display: block;
@@ -482,15 +461,22 @@ body.mobile-topbar--merged {
#editor {
box-sizing: border-box;
- transition: transform 240ms cubic-bezier(.2, 0, 0, 1);
-}
-@media (prefers-reduced-motion: reduce) {
- .mobile-topbar,
- .mobile-topbar > *,
- #editor,
- .mobile-bottom-bar {
- transition: none;
+ > .protyle-breadcrumb {
+ position: absolute;
+ top: var(--mobile-topbar-height);
+ right: 0;
+ left: 0;
+ z-index: 3;
+ height: var(--mobile-breadcrumb-height);
+ transform: translate3d(0, var(--mobile-bar-translate-y, 0), 0);
+ will-change: transform;
+ }
+
+ > .protyle-content::before {
+ display: block;
+ height: calc(var(--mobile-topbar-height) + var(--mobile-breadcrumb-height));
+ content: "";
}
}
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index e2ad4dfb67..179a616429 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -57,6 +57,7 @@ import {appearanceConfigApi} from "../config/tabs/appearanceRuntime";
import {openByMobile} from "../editor/openLink";
import {initHarmonyTextSelectionMenu} from "../util/harmonyTextSelectionMenu";
import {updateMobileTopBarLayout} from "./util/mobileTopBar";
+import {showMobileBars} from "./util/mobileBars";
class App {
public plugins: import("../plugin").Plugin[] = [];
@@ -168,6 +169,7 @@ class App {
window.matchMedia("(orientation:portrait)").addEventListener("change", () => {
updateCardHV();
updateMobileTopBarLayout();
+ showMobileBars();
activeBlur();
});
fetchPost("/api/system/getConf", {}, async (confResponse) => {
diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.ts
index 8ccf6d0ab3..1935420356 100644
--- a/app/src/mobile/util/mobileBars.ts
+++ b/app/src/mobile/util/mobileBars.ts
@@ -1,11 +1,11 @@
import {
createMobileBarsState,
getMobileBarsVisibility,
+ MOBILE_BARS_SCROLL_OPTIONS,
reduceMobileBarsState,
} from "./mobileBarsState";
const PANEL_IDS = ["sidebar", "sidebarRight", "menu", "model"];
-const MOBILE_BARS_TRANSITION_FALLBACK = 320;
let barsState = createMobileBarsState();
let scrollElement: HTMLElement | undefined;
@@ -14,10 +14,6 @@ let panelObserver: MutationObserver | undefined;
let selectionObserver: MutationObserver | undefined;
let selectionFrame = 0;
let programmaticScrollTimeout = 0;
-let barsTransitionTimeout = 0;
-let barsTransitionFrame = 0;
-let barsTransitionEndHandler: ((event: TransitionEvent) => void) | undefined;
-let barsTransitionElement: HTMLElement | undefined;
let initialized = false;
const hasTextSelection = () => {
@@ -43,73 +39,27 @@ const isPanelOpen = () => PANEL_IDS.some((id) => {
return Boolean(element?.style.transform);
});
-const clearBarsTransitionWatch = () => {
- clearTimeout(barsTransitionTimeout);
- cancelAnimationFrame(barsTransitionFrame);
- document.body.classList.remove("mobile-chrome--transitioning");
- barsTransitionTimeout = 0;
- barsTransitionFrame = 0;
- if (barsTransitionEndHandler && barsTransitionElement) {
- barsTransitionElement.removeEventListener("transitionend", barsTransitionEndHandler);
- }
- barsTransitionEndHandler = undefined;
- barsTransitionElement = undefined;
-};
-
-const finishBarsTransition = () => {
- clearBarsTransitionWatch();
- if (!barsState.barsTransitioning) {
- return;
- }
- barsState = reduceMobileBarsState(barsState, {
- type: "set-bars-transitioning",
- active: false,
- scrollTop: scrollElement?.scrollTop,
- });
-};
-
-const startBarsTransition = () => {
- clearBarsTransitionWatch();
- document.body.classList.add("mobile-chrome--transitioning");
- barsState = reduceMobileBarsState(barsState, {
- type: "set-bars-transitioning",
- active: true,
- scrollTop: scrollElement?.scrollTop,
- });
- barsTransitionElement = document.body.classList.contains("mobile-topbar--merged") ?
- document.getElementById("mobileBottomBar") : document.getElementById("mobileTopBar");
- if (!barsTransitionElement || window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
- barsTransitionFrame = requestAnimationFrame(() => {
- barsTransitionFrame = requestAnimationFrame(finishBarsTransition);
- });
- return;
- }
- barsTransitionEndHandler = (event: TransitionEvent) => {
- if (event.target === barsTransitionElement && event.propertyName === "transform") {
- finishBarsTransition();
- }
- };
- barsTransitionElement.addEventListener("transitionend", barsTransitionEndHandler);
- barsTransitionTimeout = window.setTimeout(finishBarsTransition, MOBILE_BARS_TRANSITION_FALLBACK);
-};
-
const renderMobileBars = () => {
const visibility = getMobileBarsVisibility(barsState);
const immersive = !visibility.topbarVisible;
- if (document.body.classList.contains("mobile-chrome--hidden") !== immersive) {
- startBarsTransition();
- }
document.body.classList.toggle("mobile-chrome--hidden", immersive);
document.body.classList.toggle("mobile-keyboard--open", visibility.editingBarVisible);
document.body.classList.toggle("mobile-selection--active", barsState.selecting);
+ const progress = MOBILE_BARS_SCROLL_OPTIONS.maxOffset === 0 ? 0 :
+ barsState.readingBarsOffset / MOBILE_BARS_SCROLL_OPTIONS.maxOffset;
const topbarElement = document.getElementById("mobileTopBar");
if (topbarElement) {
+ topbarElement.style.setProperty("--mobile-bar-translate-y", `${0 - barsState.readingBarsOffset}px`);
topbarElement.toggleAttribute("inert", immersive);
topbarElement.setAttribute("aria-hidden", immersive ? "true" : "false");
}
+ const breadcrumbElement = document.querySelector<HTMLElement>("#editor > .protyle-breadcrumb");
+ breadcrumbElement?.style.setProperty("--mobile-bar-translate-y", `${0 - barsState.readingBarsOffset}px`);
const bottomBarElement = document.getElementById("mobileBottomBar");
if (bottomBarElement) {
+ bottomBarElement.style.setProperty("--mobile-bar-translate-y", `${progress * 100}%`);
+ bottomBarElement.style.setProperty("--mobile-bar-opacity", (1 - progress).toString());
bottomBarElement.toggleAttribute("inert", !visibility.bottomBarVisible);
bottomBarElement.setAttribute("aria-hidden", visibility.bottomBarVisible ? "false" : "true");
}
@@ -162,13 +112,6 @@ const onScroll = () => {
}
updatePanelState();
updateSelectionState();
- if (barsState.selecting || barsState.programmaticScrolling || barsState.barsTransitioning) {
- barsState = reduceMobileBarsState(barsState, {
- type: "scroll",
- scrollTop: scrollElement.scrollTop,
- });
- return;
- }
barsState = reduceMobileBarsState(barsState, {
type: "scroll",
scrollTop: scrollElement.scrollTop,
@@ -213,13 +156,11 @@ export const clearMobileBarsScroll = () => {
selectionObserver?.disconnect();
selectionObserver = undefined;
scrollElement = undefined;
- clearBarsTransitionWatch();
resetMobileBars(0);
};
export const resetMobileBars = (scrollTop = scrollElement?.scrollTop || 0) => {
clearTimeout(programmaticScrollTimeout);
- clearBarsTransitionWatch();
barsState = reduceMobileBarsState(barsState, {type: "document-changed", scrollTop});
renderMobileBars();
};
diff --git a/app/src/mobile/util/mobileBarsState.test.ts b/app/src/mobile/util/mobileBarsState.test.ts
index 0c2deb6b64..817fd10019 100644
--- a/app/src/mobile/util/mobileBarsState.test.ts
+++ b/app/src/mobile/util/mobileBarsState.test.ts
@@ -10,91 +10,78 @@ import {
} from "./mobileBarsState";
const scrollOptions: IMobileBarsScrollOptions = {
- hideThreshold: 30,
- showThreshold: 12,
- topThreshold: 4,
+ maxOffset: 30,
};
const update = (state: IMobileBarsState, action: MobileBarsAction) => {
return reduceMobileBarsState(state, action, scrollOptions);
};
+const getVisibility = (state: IMobileBarsState) => {
+ return getMobileBarsVisibility(state, scrollOptions);
+};
+
describe("mobile bars state", () => {
- it("uses asymmetric default thresholds to avoid flicker", () => {
+ it("moves the reading bars by the scroll distance and clamps the offset", () => {
let state = createMobileBarsState();
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 63});
- assert.equal(state.readingBarsVisible, true);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 64});
- assert.equal(state.readingBarsVisible, false);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 33});
- assert.equal(state.readingBarsVisible, false);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 32});
- assert.equal(state.readingBarsVisible, true);
- });
+ state = update(state, {type: "scroll", scrollTop: 12});
+ assert.equal(state.readingBarsOffset, 12);
- it("shows reading bars initially and supports explicit visibility changes", () => {
- let state = createMobileBarsState();
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: true,
- bottomBarVisible: true,
- editingBarVisible: false,
- scrollPaused: false,
- });
+ state = update(state, {type: "scroll", scrollTop: 35});
+ assert.equal(state.readingBarsOffset, 30);
- state = update(state, {type: "set-reading-bars", visible: false});
- assert.equal(state.readingBarsVisible, false);
- state = update(state, {type: "set-reading-bars", visible: true});
- assert.equal(state.readingBarsVisible, true);
- });
+ state = update(state, {type: "scroll", scrollTop: 60});
+ assert.equal(state.readingBarsOffset, 30);
- it("hides reading bars after cumulative downward movement", () => {
- let state = createMobileBarsState();
- state = update(state, {type: "scroll", scrollTop: 10});
- state = update(state, {type: "scroll", scrollTop: 24});
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollDistance, 24);
+ state = update(state, {type: "scroll", scrollTop: 51});
+ assert.equal(state.readingBarsOffset, 21);
- state = update(state, {type: "scroll", scrollTop: 31});
- assert.equal(state.readingBarsVisible, false);
- assert.equal(state.scrollDistance, 0);
+ state = update(state, {type: "scroll", scrollTop: 20});
+ assert.equal(state.readingBarsOffset, 0);
});
- it("restarts accumulation when the scroll direction changes", () => {
+ it("reacts immediately when the scroll direction changes", () => {
let state = createMobileBarsState();
- state = update(state, {type: "scroll", scrollTop: 20});
state = update(state, {type: "scroll", scrollTop: 18});
- state = update(state, {type: "scroll", scrollTop: 35});
+ state = update(state, {type: "scroll", scrollTop: 15});
+ assert.equal(state.readingBarsOffset, 15);
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollDirection, "down");
- assert.equal(state.scrollDistance, 17);
+ state = update(state, {type: "scroll", scrollTop: 19});
+ assert.equal(state.readingBarsOffset, 19);
});
- it("shows hidden reading bars after cumulative upward movement", () => {
- let state = createMobileBarsState(80);
+ it("never hides the reading bars beyond the available top scroll distance", () => {
+ let state = createMobileBarsState(8);
state = update(state, {type: "set-reading-bars", visible: false});
- state = update(state, {type: "scroll", scrollTop: 72});
- assert.equal(state.readingBarsVisible, false);
+ state = update(state, {type: "scroll", scrollTop: 8});
+ assert.equal(state.readingBarsOffset, 8);
- state = update(state, {type: "scroll", scrollTop: 66});
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollDistance, 0);
+ state = update(state, {type: "scroll", scrollTop: 0});
+ assert.equal(state.readingBarsOffset, 0);
});
- it("forces reading bars to show near the top", () => {
- let state = createMobileBarsState(80);
+ it("shows reading bars initially and supports explicit visibility changes", () => {
+ let state = createMobileBarsState();
+ assert.deepEqual(getVisibility(state), {
+ topbarVisible: true,
+ bottomBarVisible: true,
+ editingBarVisible: false,
+ scrollPaused: false,
+ });
+
state = update(state, {type: "set-reading-bars", visible: false});
- state = update(state, {type: "scroll", scrollTop: 4});
+ assert.equal(state.readingBarsOffset, 30);
+ assert.equal(getVisibility(state).topbarVisible, false);
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollDirection, undefined);
- assert.equal(state.scrollDistance, 0);
+ state = update(state, {type: "set-reading-bars", visible: true});
+ assert.equal(state.readingBarsOffset, 0);
+ assert.equal(getVisibility(state).topbarVisible, true);
});
- it("keeps the visible title state while editing", () => {
+ it("keeps the visible reading state while editing", () => {
let state = createMobileBarsState(100);
state = update(state, {type: "set-editing", active: true});
- assert.deepEqual(getMobileBarsVisibility(state), {
+ assert.deepEqual(getVisibility(state), {
topbarVisible: true,
bottomBarVisible: false,
editingBarVisible: true,
@@ -102,33 +89,22 @@ describe("mobile bars state", () => {
});
state = update(state, {type: "scroll", scrollTop: 180});
- assert.equal(state.readingBarsVisible, true);
+ assert.equal(state.readingBarsOffset, 0);
assert.equal(state.scrollTop, 180);
state = update(state, {type: "set-editing", active: false, scrollTop: 180});
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: true,
- bottomBarVisible: true,
- editingBarVisible: false,
- scrollPaused: false,
- });
+ assert.equal(getVisibility(state).bottomBarVisible, true);
});
- it("keeps the hidden title state while editing", () => {
+ it("keeps the hidden reading state while editing", () => {
let state = createMobileBarsState(100);
state = update(state, {type: "set-reading-bars", visible: false});
state = update(state, {type: "set-editing", active: true, scrollTop: 100});
+ state = update(state, {type: "scroll", scrollTop: 180});
+ assert.equal(state.readingBarsOffset, 30);
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: false,
- bottomBarVisible: false,
- editingBarVisible: true,
- scrollPaused: true,
- });
-
- state = update(state, {type: "set-editing", active: false, scrollTop: 100});
-
- assert.deepEqual(getMobileBarsVisibility(state), {
+ state = update(state, {type: "set-editing", active: false, scrollTop: 180});
+ assert.deepEqual(getVisibility(state), {
topbarVisible: false,
bottomBarVisible: false,
editingBarVisible: false,
@@ -136,126 +112,51 @@ describe("mobile bars state", () => {
});
});
- it("keeps the visible title state while selecting", () => {
+ it("preserves the reading state while selecting", () => {
let state = createMobileBarsState(100);
+ state = update(state, {type: "scroll", scrollTop: 115});
state = update(state, {type: "set-selecting", active: true});
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: true,
- bottomBarVisible: false,
- editingBarVisible: false,
- scrollPaused: true,
- });
-
state = update(state, {type: "scroll", scrollTop: 180});
- assert.equal(state.scrollDistance, 0);
- state = update(state, {type: "set-selecting", active: false, scrollTop: 180});
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: true,
- bottomBarVisible: true,
- editingBarVisible: false,
- scrollPaused: false,
- });
- });
+ assert.equal(state.readingBarsOffset, 15);
+ assert.equal(getVisibility(state).bottomBarVisible, false);
- it("keeps the hidden title state while selecting", () => {
- let state = createMobileBarsState(100);
- state = update(state, {type: "set-reading-bars", visible: false});
- state = update(state, {type: "set-selecting", active: true});
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: false,
- bottomBarVisible: false,
- editingBarVisible: false,
- scrollPaused: true,
- });
-
- state = update(state, {type: "set-selecting", active: false, scrollTop: 100});
- assert.deepEqual(getMobileBarsVisibility(state), {
- topbarVisible: false,
- bottomBarVisible: false,
- editingBarVisible: false,
- scrollPaused: false,
- });
+ state = update(state, {type: "set-selecting", active: false, scrollTop: 180});
+ state = update(state, {type: "scroll", scrollTop: 185});
+ assert.equal(state.readingBarsOffset, 20);
});
- it("preserves visible reading bars while a panel opens and closes", () => {
+ it("preserves the reading state while a panel opens and closes", () => {
let state = createMobileBarsState(100);
+ state = update(state, {type: "scroll", scrollTop: 120});
state = update(state, {type: "set-panel-open", open: true});
- assert.equal(getMobileBarsVisibility(state).topbarVisible, true);
- assert.equal(getMobileBarsVisibility(state).scrollPaused, true);
state = update(state, {type: "scroll", scrollTop: 200});
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollDistance, 0);
+ assert.equal(state.readingBarsOffset, 20);
state = update(state, {type: "set-panel-open", open: false, scrollTop: 200});
- assert.equal(getMobileBarsVisibility(state).topbarVisible, true);
- state = update(state, {type: "scroll", scrollTop: 220});
- assert.equal(state.readingBarsVisible, true);
- state = update(state, {type: "scroll", scrollTop: 231});
- assert.equal(state.readingBarsVisible, false);
+ state = update(state, {type: "scroll", scrollTop: 205});
+ assert.equal(state.readingBarsOffset, 25);
});
- it("preserves hidden reading bars while a panel opens and closes", () => {
+ it("rebases programmatic scrolling and shows the bars when it finishes at the top", () => {
let state = createMobileBarsState(100);
state = update(state, {type: "set-reading-bars", visible: false});
- state = update(state, {type: "set-panel-open", open: true, scrollTop: 0});
- assert.equal(getMobileBarsVisibility(state).topbarVisible, false);
- assert.equal(getMobileBarsVisibility(state).scrollPaused, true);
-
- state = update(state, {type: "set-panel-open", open: false, scrollTop: 0});
- assert.equal(getMobileBarsVisibility(state).topbarVisible, false);
- assert.equal(getMobileBarsVisibility(state).scrollPaused, false);
- });
-
- it("pauses programmatic scrolling and applies the top rule after resuming", () => {
- let state = createMobileBarsState(100);
state = update(state, {type: "set-programmatic-scrolling", active: true});
state = update(state, {type: "scroll", scrollTop: 260});
state = update(state, {type: "set-programmatic-scrolling", active: false, scrollTop: 260});
- state = update(state, {type: "scroll", scrollTop: 278});
- assert.equal(state.readingBarsVisible, true);
- state = update(state, {type: "scroll", scrollTop: 291});
- assert.equal(state.readingBarsVisible, false);
+ assert.equal(state.readingBarsOffset, 30);
state = update(state, {type: "set-programmatic-scrolling", active: true});
state = update(state, {type: "scroll", scrollTop: 0});
- assert.equal(state.readingBarsVisible, false);
state = update(state, {type: "set-programmatic-scrolling", active: false, scrollTop: 0});
- assert.equal(state.readingBarsVisible, true);
+ assert.equal(state.readingBarsOffset, 0);
});
- it("rebases layout-driven scrolling while the bars transition", () => {
+ it("resets transient state and the reading offset when the document changes", () => {
let state = createMobileBarsState(100);
- state = update(state, {type: "set-bars-transitioning", active: true, scrollTop: 100});
- state = update(state, {type: "scroll", scrollTop: 180});
- assert.equal(state.readingBarsVisible, true);
- assert.equal(state.scrollTop, 180);
-
- state = update(state, {type: "set-bars-transitioning", active: false, scrollTop: 180});
state = update(state, {type: "set-reading-bars", visible: false});
- state = update(state, {type: "set-bars-transitioning", active: true, scrollTop: 100});
- state = update(state, {type: "scroll", scrollTop: 40});
-
- assert.equal(state.readingBarsVisible, false);
- assert.equal(state.scrollTop, 40);
- assert.equal(state.scrollDirection, undefined);
- assert.equal(state.scrollDistance, 0);
- assert.equal(getMobileBarsVisibility(state).scrollPaused, true);
-
- state = update(state, {type: "set-bars-transitioning", active: false, scrollTop: 40});
- assert.equal(state.readingBarsVisible, false);
- assert.equal(getMobileBarsVisibility(state).scrollPaused, false);
- state = update(state, {type: "scroll", scrollTop: 34});
- assert.equal(state.readingBarsVisible, false);
- state = update(state, {type: "scroll", scrollTop: 28});
- assert.equal(state.readingBarsVisible, true);
- });
-
- it("resets transient state when the document changes", () => {
- let state = createMobileBarsState(100);
state = update(state, {type: "set-editing", active: true});
state = update(state, {type: "set-panel-open", open: true});
state = update(state, {type: "set-programmatic-scrolling", active: true});
- state = update(state, {type: "set-bars-transitioning", active: true});
state = update(state, {type: "document-changed", scrollTop: 12});
assert.deepEqual(state, createMobileBarsState(12));
diff --git a/app/src/mobile/util/mobileBarsState.ts b/app/src/mobile/util/mobileBarsState.ts
index a4ab3207ef..a36263a81c 100644
--- a/app/src/mobile/util/mobileBarsState.ts
+++ b/app/src/mobile/util/mobileBarsState.ts
@@ -1,21 +1,14 @@
-export type MobileBarsScrollDirection = "up" | "down";
-
export interface IMobileBarsScrollOptions {
- hideThreshold: number,
- showThreshold: number,
- topThreshold: number,
+ maxOffset: number,
}
export interface IMobileBarsState {
- readingBarsVisible: boolean,
+ readingBarsOffset: number,
editing: boolean,
selecting: boolean,
panelOpen: boolean,
programmaticScrolling: boolean,
- barsTransitioning: boolean,
scrollTop: number,
- scrollDirection?: MobileBarsScrollDirection,
- scrollDistance: number,
}
export interface IMobileBarsVisibility {
@@ -47,19 +40,13 @@ export type MobileBarsAction = {
type: "set-programmatic-scrolling",
active: boolean,
scrollTop?: number,
-} | {
- type: "set-bars-transitioning",
- active: boolean,
- scrollTop?: number,
} | {
type: "document-changed",
scrollTop?: number,
};
export const MOBILE_BARS_SCROLL_OPTIONS: Readonly<IMobileBarsScrollOptions> = {
- hideThreshold: 64,
- showThreshold: 32,
- topThreshold: 8,
+ maxOffset: 48,
};
const normalizeScrollTop = (scrollTop: number, fallback = 0) => {
@@ -67,42 +54,40 @@ const normalizeScrollTop = (scrollTop: number, fallback = 0) => {
};
const getScrollOptions = (options: Partial<IMobileBarsScrollOptions>) => ({
- hideThreshold: Number.isFinite(options.hideThreshold) ?
- Math.max(0, options.hideThreshold) : MOBILE_BARS_SCROLL_OPTIONS.hideThreshold,
- showThreshold: Number.isFinite(options.showThreshold) ?
- Math.max(0, options.showThreshold) : MOBILE_BARS_SCROLL_OPTIONS.showThreshold,
- topThreshold: Number.isFinite(options.topThreshold) ?
- Math.max(0, options.topThreshold) : MOBILE_BARS_SCROLL_OPTIONS.topThreshold,
+ maxOffset: Number.isFinite(options.maxOffset) ?
+ Math.max(0, options.maxOffset) : MOBILE_BARS_SCROLL_OPTIONS.maxOffset,
});
const resetScrollTracking = (state: IMobileBarsState, scrollTop = state.scrollTop): IMobileBarsState => ({
...state,
scrollTop: normalizeScrollTop(scrollTop, state.scrollTop),
- scrollDirection: undefined,
- scrollDistance: 0,
});
export const createMobileBarsState = (scrollTop = 0): IMobileBarsState => ({
- readingBarsVisible: true,
+ readingBarsOffset: 0,
editing: false,
selecting: false,
panelOpen: false,
programmaticScrolling: false,
- barsTransitioning: false,
scrollTop: normalizeScrollTop(scrollTop),
- scrollDistance: 0,
});
export const isMobileBarsScrollPaused = (state: IMobileBarsState) => {
- return state.editing || state.selecting || state.panelOpen || state.programmaticScrolling || state.barsTransitioning;
+ return state.editing || state.selecting || state.panelOpen || state.programmaticScrolling;
};
-export const getMobileBarsVisibility = (state: IMobileBarsState): IMobileBarsVisibility => ({
- topbarVisible: state.readingBarsVisible,
- bottomBarVisible: state.readingBarsVisible && !state.editing && !state.selecting,
- editingBarVisible: state.editing,
- scrollPaused: isMobileBarsScrollPaused(state),
-});
+export const getMobileBarsVisibility = (
+ state: IMobileBarsState,
+ options: Partial<IMobileBarsScrollOptions> = {},
+): IMobileBarsVisibility => {
+ const readingBarsVisible = state.readingBarsOffset < getScrollOptions(options).maxOffset;
+ return {
+ topbarVisible: readingBarsVisible,
+ bottomBarVisible: readingBarsVisible && !state.editing && !state.selecting,
+ editingBarVisible: state.editing,
+ scrollPaused: isMobileBarsScrollPaused(state),
+ };
+};
export const reduceMobileBarsState = (
state: IMobileBarsState,
@@ -118,7 +103,7 @@ export const reduceMobileBarsState = (
if (action.type === "set-reading-bars") {
return {
...resetScrollTracking(state),
- readingBarsVisible: action.visible,
+ readingBarsOffset: action.visible ? 0 : scrollOptions.maxOffset,
};
}
@@ -148,50 +133,20 @@ export const reduceMobileBarsState = (
...resetScrollTracking(state, action.scrollTop),
programmaticScrolling: action.active,
};
- if (!action.active && !nextState.editing && !nextState.panelOpen &&
- nextState.scrollTop <= scrollOptions.topThreshold) {
- nextState.readingBarsVisible = true;
+ if (!action.active && !nextState.editing && !nextState.panelOpen && nextState.scrollTop === 0) {
+ nextState.readingBarsOffset = 0;
}
return nextState;
}
- if (action.type === "set-bars-transitioning") {
- return {
- ...resetScrollTracking(state, action.scrollTop),
- barsTransitioning: action.active,
- };
- }
-
const scrollTop = normalizeScrollTop(action.scrollTop, state.scrollTop);
const nextState = resetScrollTracking(state, scrollTop);
if (isMobileBarsScrollPaused(state)) {
return nextState;
}
- if (scrollTop <= scrollOptions.topThreshold) {
- nextState.readingBarsVisible = true;
- return nextState;
- }
-
- const scrollDelta = scrollTop - state.scrollTop;
- if (scrollDelta === 0) {
- return state;
- }
-
- const scrollDirection: MobileBarsScrollDirection = scrollDelta > 0 ? "down" : "up";
- const canChangeVisibility = scrollDirection === "down" ? state.readingBarsVisible : !state.readingBarsVisible;
- if (!canChangeVisibility) {
- return nextState;
- }
-
- const scrollDistance = Math.abs(scrollDelta) +
- (state.scrollDirection === scrollDirection ? state.scrollDistance : 0);
- const threshold = scrollDirection === "down" ? scrollOptions.hideThreshold : scrollOptions.showThreshold;
- if (scrollDistance >= threshold) {
- nextState.readingBarsVisible = scrollDirection === "up";
- return nextState;
- }
- nextState.scrollDirection = scrollDirection;
- nextState.scrollDistance = scrollDistance;
+ const maxOffset = Math.min(scrollOptions.maxOffset, scrollTop);
+ nextState.readingBarsOffset = Math.min(maxOffset, Math.max(0,
+ state.readingBarsOffset + scrollTop - state.scrollTop));
return nextState;
};