Skip to content

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

:art: Stabilize mobile bars while scrolling https://github.com/siyuan-note/siyuan/issues/14542

Daniel485d08b6bad2
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index 063cbc6bf3..5375cb769c 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -4,6 +4,7 @@
body {
--mobile-bottom-bar-offset: calc(48px + env(safe-area-inset-bottom));
+ --mobile-editor-bottom-offset: calc(48px + env(safe-area-inset-bottom));
}
body.mobile-chrome--hidden {
@@ -12,6 +13,7 @@ body.mobile-chrome--hidden {
body.mobile-keyboard--open {
--mobile-bottom-bar-offset: 0px;
+ --mobile-editor-bottom-offset: 0px;
}
.block__popover {
@@ -274,7 +276,7 @@ body.mobile-keyboard--open {
min-height: 48px;
padding: 0 max(4px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
flex-shrink: 0;
- transition: margin-bottom 180ms cubic-bezier(.2, 0, 0, 1), opacity 180ms cubic-bezier(.2, 0, 0, 1), transform 180ms cubic-bezier(.2, 0, 0, 1), visibility 180ms;
+ transition: margin-bottom 240ms cubic-bezier(.2, 0, 0, 1), opacity 240ms cubic-bezier(.2, 0, 0, 1), transform 240ms cubic-bezier(.2, 0, 0, 1), visibility 240ms;
}
.mobile-bottom-bar {
@@ -290,7 +292,7 @@ body.mobile-keyboard--open {
padding: 0 max(4px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(4px, env(safe-area-inset-left));
border-top: .5px solid var(--b3-theme-background-light);
background-color: var(--b3-theme-background);
- transition: opacity 180ms cubic-bezier(.2, 0, 0, 1), transform 180ms cubic-bezier(.2, 0, 0, 1), visibility 180ms;
+ transition: opacity 240ms cubic-bezier(.2, 0, 0, 1), transform 240ms cubic-bezier(.2, 0, 0, 1), visibility 240ms;
-webkit-user-select: none;
user-select: none;
@@ -411,7 +413,7 @@ body.mobile-keyboard--open {
#editor {
box-sizing: border-box;
- padding-bottom: var(--mobile-bottom-bar-offset);
+ padding-bottom: var(--mobile-editor-bottom-offset);
}
@media (prefers-reduced-motion: reduce) {
diff --git a/app/src/mobile/util/mobileBars.ts b/app/src/mobile/util/mobileBars.ts
index e5cd47b7d9..fd899c27cb 100644
--- a/app/src/mobile/util/mobileBars.ts
+++ b/app/src/mobile/util/mobileBars.ts
@@ -5,6 +5,7 @@ import {
} from "./mobileBarsState";
const PANEL_IDS = ["sidebar", "menu", "model"];
+const MOBILE_BARS_TRANSITION_FALLBACK = 320;
let barsState = createMobileBarsState();
let scrollElement: HTMLElement | undefined;
@@ -13,6 +14,9 @@ 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 initialized = false;
const hasTextSelection = () => {
@@ -35,9 +39,58 @@ const isPanelOpen = () => PANEL_IDS.some((id) => {
return Boolean(element?.style.transform);
});
+const clearBarsTransitionWatch = () => {
+ clearTimeout(barsTransitionTimeout);
+ cancelAnimationFrame(barsTransitionFrame);
+ barsTransitionTimeout = 0;
+ barsTransitionFrame = 0;
+ if (barsTransitionEndHandler) {
+ document.getElementById("mobileTopBar")?.removeEventListener("transitionend", barsTransitionEndHandler);
+ barsTransitionEndHandler = undefined;
+ }
+};
+
+const finishBarsTransition = () => {
+ clearBarsTransitionWatch();
+ if (!barsState.barsTransitioning) {
+ return;
+ }
+ barsState = reduceMobileBarsState(barsState, {
+ type: "set-bars-transitioning",
+ active: false,
+ scrollTop: scrollElement?.scrollTop,
+ });
+};
+
+const startBarsTransition = () => {
+ clearBarsTransitionWatch();
+ barsState = reduceMobileBarsState(barsState, {
+ type: "set-bars-transitioning",
+ active: true,
+ scrollTop: scrollElement?.scrollTop,
+ });
+ const topbarElement = document.getElementById("mobileTopBar");
+ if (!topbarElement || window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
+ barsTransitionFrame = requestAnimationFrame(() => {
+ barsTransitionFrame = requestAnimationFrame(finishBarsTransition);
+ });
+ return;
+ }
+ barsTransitionEndHandler = (event: TransitionEvent) => {
+ if (event.target === topbarElement && event.propertyName === "margin-bottom") {
+ finishBarsTransition();
+ }
+ };
+ topbarElement.addEventListener("transitionend", barsTransitionEndHandler);
+ barsTransitionTimeout = window.setTimeout(finishBarsTransition, MOBILE_BARS_TRANSITION_FALLBACK);
+};
+
const renderMobileBars = () => {
const visibility = getMobileBarsVisibility(barsState);
const immersive = !visibility.readingBarsVisible && !visibility.editingBarVisible;
+ 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);
@@ -100,7 +153,7 @@ const onScroll = () => {
}
updatePanelState();
updateSelectionState();
- if (barsState.selecting || barsState.programmaticScrolling) {
+ if (barsState.selecting || barsState.programmaticScrolling || barsState.barsTransitioning) {
barsState = reduceMobileBarsState(barsState, {
type: "scroll",
scrollTop: scrollElement.scrollTop,
@@ -151,11 +204,13 @@ 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 6f448d625e..9641ba910f 100644
--- a/app/src/mobile/util/mobileBarsState.test.ts
+++ b/app/src/mobile/util/mobileBarsState.test.ts
@@ -22,13 +22,13 @@ const update = (state: IMobileBarsState, action: MobileBarsAction) => {
describe("mobile bars state", () => {
it("uses asymmetric default thresholds to avoid flicker", () => {
let state = createMobileBarsState();
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 31});
+ state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 63});
assert.equal(state.readingBarsVisible, true);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 32});
+ state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 64});
assert.equal(state.readingBarsVisible, false);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 17});
+ state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 33});
assert.equal(state.readingBarsVisible, false);
- state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 16});
+ state = reduceMobileBarsState(state, {type: "scroll", scrollTop: 32});
assert.equal(state.readingBarsVisible, true);
});
@@ -189,11 +189,39 @@ describe("mobile bars state", () => {
assert.equal(state.readingBarsVisible, true);
});
+ it("rebases layout-driven scrolling while the bars transition", () => {
+ 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 51d969d512..95ffef0aa5 100644
--- a/app/src/mobile/util/mobileBarsState.ts
+++ b/app/src/mobile/util/mobileBarsState.ts
@@ -12,6 +12,7 @@ export interface IMobileBarsState {
selecting: boolean,
panelOpen: boolean,
programmaticScrolling: boolean,
+ barsTransitioning: boolean,
scrollTop: number,
scrollDirection?: MobileBarsScrollDirection,
scrollDistance: number,
@@ -45,14 +46,18 @@ 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: 32,
- showThreshold: 16,
+ hideThreshold: 64,
+ showThreshold: 32,
topThreshold: 8,
};
@@ -82,12 +87,13 @@ export const createMobileBarsState = (scrollTop = 0): IMobileBarsState => ({
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;
+ return state.editing || state.selecting || state.panelOpen || state.programmaticScrolling || state.barsTransitioning;
};
export const getMobileBarsVisibility = (state: IMobileBarsState): IMobileBarsVisibility => ({
@@ -149,6 +155,13 @@ export const reduceMobileBarsState = (
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)) {