Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(editor): let the overview lane own the editor's vertical scrollbar (#1449)

黄贵波a0835715378a
diff --git a/e2e/overviewLaneSelection.spec.ts b/e2e/overviewLaneSelection.spec.ts
new file mode 100644
index 000000000..87eb037a6
--- /dev/null
+++ b/e2e/overviewLaneSelection.spec.ts
@@ -0,0 +1,28 @@
+import { expect, openFromTree, test } from "./fixtures";
+
+// Headless Chromium hides scrollbars by default, so nothing native sits at the editor's right
+// edge and the press reaches the overview lane. The tests in `overviewLane.spec.ts` keep a
+// scrollbar there instead.
+test("pressing the overview lane scrolls instead of starting a text selection", async ({ workbench: page }) => {
+ const editor = await openFromTree(page, "build.gradle.kts");
+ await editor.locator(".cm-content").click();
+ await page.keyboard.press("Meta+a");
+ await page.keyboard.insertText(Array.from({ length: 400 }, (_, i) => `val line${i} = ${i}`).join("\n"));
+ await page.keyboard.press("Escape");
+ const scroller = editor.locator(".cm-scroller");
+ await scroller.evaluate((s) => (s.scrollTop = 0));
+ const box = (await scroller.boundingBox())!;
+ // The lane replaces the native scrollbar: a press on it must stay a scrollbar press.
+ const x = box.x + box.width - 5;
+ const y = box.y + box.height - 40;
+ await page.mouse.move(x, y);
+ await page.mouse.down();
+ await page.mouse.move(x, y - 120, { steps: 8 });
+ await page.mouse.up();
+ const empty = await editor.locator(".cm-content").evaluate((el) => {
+ const view = (el as unknown as { cmTile: { view: { state: { selection: { main: { empty: boolean } } } } } }).cmTile
+ .view;
+ return view.state.selection.main.empty;
+ });
+ expect(empty).toBe(true);
+});
diff --git a/src/editor/core/cm/lanes.ts b/src/editor/core/cm/lanes.ts
index 68a2e6c37..0a89e1f60 100644
--- a/src/editor/core/cm/lanes.ts
+++ b/src/editor/core/cm/lanes.ts
@@ -65,8 +65,8 @@ class OverviewLane {
/** The last pointer position over the editor, to re-check the thumb when it scrolls. */
private pointer: { x: number; y: number } | null = null;
/**
- * The scrollbar thumb drawn over the marks: the native one sits under the lane, so a mark
- * the height of the lane (a new file) would hide it (#1014).
+ * The scrollbar thumb, drawn and dragged by the lane: the native scrollbar is hidden, so a
+ * press on this strip stays here instead of reaching the text layer as a text drag.
*/
private thumb: HTMLElement;
@@ -77,6 +77,7 @@ class OverviewLane {
this.thumb = document.createElement("div");
this.thumb.className = "cm-openidea-overviewThumb";
this.dom.addEventListener("mousedown", this.onMouseDown);
+ this.thumb.addEventListener("mousedown", this.onThumbMouseDown);
view.dom.addEventListener("mousemove", this.onPointerMove);
view.dom.addEventListener("mouseleave", this.onPointerLeave);
view.scrollDOM.addEventListener("scroll", this.onScroll, { passive: true });
@@ -175,8 +176,8 @@ class OverviewLane {
const thumb = scrollbarThumb(s.scrollTop, s.scrollHeight, s.clientHeight);
// The lane starts at the top of the editor, the scrollbar at the top of the scroller.
const offset = s.getBoundingClientRect().top - this.dom.getBoundingClientRect().top;
- // Inset by the thumb's 2px transparent border (global.css).
- return thumb && { top: thumb.top + offset + 2, height: thumb.bottom - thumb.top - 4 };
+ // The drawn thumb matches the geometry the drag maps onto, so the pointer keeps its grip.
+ return thumb && { top: thumb.top + offset, height: thumb.bottom - thumb.top };
},
write: (thumb) => {
this.thumb.style.display = thumb ? "" : "none";
@@ -188,8 +189,8 @@ class OverviewLane {
}
/**
- * Over the scrollbar thumb the marks let the pointer through, so the thumb can be dragged
- * even where a mark covers it (a new file is one mark the height of the lane, #752).
+ * Over the thumb the marks let the pointer through, so the thumb can be dragged even where a
+ * mark covers it (a new file is one mark the height of the lane, #752).
*/
private updateThumbPassThrough() {
const p = this.pointer;
@@ -220,6 +221,31 @@ class OverviewLane {
if (this.pointer) this.updateThumbPassThrough();
};
+ /** The thumb's own press: drag it, keeping the button state inside the lane. */
+ private onThumbMouseDown = (e: MouseEvent) => {
+ if (e.button !== 0) return;
+ e.preventDefault();
+ e.stopPropagation();
+ const s = this.view.scrollDOM;
+ const thumb = scrollbarThumb(s.scrollTop, s.scrollHeight, s.clientHeight);
+ if (!thumb) return;
+ const startY = e.clientY;
+ const startTop = s.scrollTop;
+ const travel = s.scrollHeight - s.clientHeight;
+ // The distance the thumb can move: the track minus the thumb itself.
+ const track = Math.max(1, s.clientHeight - (thumb.bottom - thumb.top));
+ const onMove = (ev: MouseEvent) => {
+ s.scrollTop = startTop + ((ev.clientY - startY) * travel) / track;
+ };
+ const onUp = () => {
+ window.removeEventListener("mousemove", onMove);
+ window.removeEventListener("mouseup", onUp);
+ this.updateThumbPassThrough();
+ };
+ window.addEventListener("mousemove", onMove);
+ window.addEventListener("mouseup", onUp);
+ };
+
private onMouseDown = (e: MouseEvent) => {
if (e.button !== 0) return;
const marks = [...this.dom.querySelectorAll<HTMLElement>(".cm-openidea-overviewMark")].map((el) => {
@@ -227,7 +253,17 @@ class OverviewLane {
return { top: r.top, bottom: r.bottom, pos: Number(el.dataset.pos) };
});
const hit = nearestMark(marks, e.clientY);
- if (!hit) return;
+ if (!hit) {
+ // Above / below the thumb pages the editor, like the native track it replaces.
+ const s = this.view.scrollDOM;
+ const thumb = scrollbarThumb(s.scrollTop, s.scrollHeight, s.clientHeight);
+ e.preventDefault();
+ if (!thumb) return;
+ const y = e.clientY - s.getBoundingClientRect().top;
+ if (y < thumb.top) s.scrollTop -= s.clientHeight;
+ else if (y > thumb.bottom) s.scrollTop += s.clientHeight;
+ return;
+ }
e.preventDefault();
const state = this.view.state;
const pos = Math.min(hit.pos, state.doc.length);
@@ -241,6 +277,7 @@ class OverviewLane {
destroy() {
this.dom.removeEventListener("mousedown", this.onMouseDown);
+ this.thumb.removeEventListener("mousedown", this.onThumbMouseDown);
this.view.dom.removeEventListener("mousemove", this.onPointerMove);
this.view.dom.removeEventListener("mouseleave", this.onPointerLeave);
this.view.scrollDOM.removeEventListener("scroll", this.onScroll);
diff --git a/src/editor/core/cm/theme.ts b/src/editor/core/cm/theme.ts
index 8bb129fae..909a80829 100644
--- a/src/editor/core/cm/theme.ts
+++ b/src/editor/core/cm/theme.ts
@@ -267,14 +267,14 @@ export const openideaTheme = EditorView.theme({
".cm-panels.cm-panels-top": { borderBottom: "1px solid var(--border)" },
".cm-panels.cm-panels-bottom": { borderTop: "1px solid var(--border)" },
- // Error stripe over the scrollbar (cm/lanes.ts)
+ // Error stripe and scrollbar lane (cm/lanes.ts)
".cm-openidea-overview": {
position: "absolute",
top: "0",
right: "0",
bottom: "0",
width: "12px",
- pointerEvents: "none",
+ cursor: "default",
zIndex: "3",
},
".cm-openidea-overviewMark": {
@@ -289,15 +289,17 @@ export const openideaTheme = EditorView.theme({
".cm-openidea-overviewMark-thin": { left: "4px", width: "4px", minHeight: "2px" },
// The scrollbar thumb wins over the marks under it (cm/lanes.ts, #752).
".cm-openidea-overview-onThumb .cm-openidea-overviewMark": { pointerEvents: "none" },
- // The lane draws the thumb over its marks (#1014); the native one only takes the drag.
- "&.cm-openidea-withOverview .cm-scroller::-webkit-scrollbar-thumb:vertical": { backgroundColor: "transparent" },
+ // The lane owns the scrollbar and its thumb (cm/lanes.ts): the native one is hidden so a press
+ // on the scrollbar can never reach the text layer and start a text selection.
+ "&.cm-openidea-withOverview .cm-scroller::-webkit-scrollbar:vertical": { width: "0" },
".cm-openidea-overviewThumb": {
position: "absolute",
right: "2px",
width: "6px",
borderRadius: "3px",
background: "var(--scrollbar-thumb)",
- pointerEvents: "none",
+ pointerEvents: "auto",
+ cursor: "default",
},
".cm-openidea-overview-onThumb .cm-openidea-overviewThumb": { background: "var(--scrollbar-thumb-hover)" },
".cm-openidea-overviewCaret": {
@@ -305,6 +307,7 @@ export const openideaTheme = EditorView.theme({
left: "2px",
right: "2px",
height: "2px",
+ pointerEvents: "none",
background: "color-mix(in srgb, var(--fg) 30%, transparent)",
},