Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(conversations): stand scroll buttons down where a widget has its own

oxoxDev38eb195efafa
diff --git a/app/src/features/conversations/tools/mcpUi/scrollAffordance.test.ts b/app/src/features/conversations/tools/mcpUi/scrollAffordance.test.ts
index 79421c2529..15715def8b 100644
--- a/app/src/features/conversations/tools/mcpUi/scrollAffordance.test.ts
+++ b/app/src/features/conversations/tools/mcpUi/scrollAffordance.test.ts
@@ -10,7 +10,28 @@ interface Box {
scrollLeft: number;
}
-function fakeLayout(el: Element, box: Box) {
+interface Rect {
+ left: number;
+ top: number;
+ width: number;
+ height: number;
+}
+
+const ROW_HTML =
+ '<div id="row" style="overflow-x:auto"><div>items</div></div>' +
+ '<div id="clip" style="overflow-x:hidden"><div>wide</div></div>';
+
+function setRect(el: Element, rect: () => Rect) {
+ Object.defineProperty(el, 'getBoundingClientRect', {
+ configurable: true,
+ value: () => {
+ const { left, top, width, height } = rect();
+ return { left, top, width, height, right: left + width, bottom: top + height };
+ },
+ });
+}
+
+function fakeLayout(el: Element, box: Box, left = 10) {
for (const key of Object.keys(box) as Array<keyof Box>) {
Object.defineProperty(el, key, {
configurable: true,
@@ -20,59 +41,60 @@ function fakeLayout(el: Element, box: Box) {
},
});
}
- Object.defineProperty(el, 'getBoundingClientRect', {
- value: () => ({
- left: 10,
- right: 10 + box.clientWidth,
- top: 20,
- width: box.clientWidth,
- height: 100,
- }),
- });
+ setRect(el, () => ({ left, top: 20, width: box.clientWidth, height: 100 }));
}
-async function mount() {
+const overflowingBox = (clientWidth = 300): Box => ({
+ scrollWidth: 1000,
+ clientWidth,
+ scrollHeight: 100,
+ clientHeight: 100,
+ scrollLeft: 0,
+});
+
+const settle = () => new Promise(resolve => setTimeout(resolve, 200));
+
+async function mount(html = ROW_HTML, layout?: (document: Document) => void) {
const frame = window.document.createElement('iframe');
window.document.body.appendChild(frame);
const win = frame.contentWindow as Window & typeof globalThis;
const document = win.document;
- document.body.innerHTML =
- '<div id="row" style="overflow-x:auto"><div>items</div></div>' +
- '<div id="clip" style="overflow-x:hidden"><div>wide</div></div>';
+ document.body.innerHTML = html;
const row = document.getElementById('row')!;
- const box: Box = {
- scrollWidth: 1000,
- clientWidth: 300,
- scrollHeight: 100,
- clientHeight: 100,
- scrollLeft: 0,
- };
+ const box = overflowingBox();
fakeLayout(row, box);
- fakeLayout(document.getElementById('clip')!, { ...box });
+ const clip = document.getElementById('clip');
+ if (clip) fakeLayout(clip, overflowingBox());
+ layout?.(document);
const scrollBy = vi.fn();
Object.defineProperty(row, 'scrollBy', { value: scrollBy });
new Function('window', 'document', SCROLL_AFFORDANCE_SOURCE)(win, document);
- await new Promise(resolve => setTimeout(resolve, 200));
+ await settle();
const button = (label: string) =>
- document.querySelector(`button[aria-label="${label}"]`) as HTMLButtonElement;
+ document.querySelector(`button[aria-label="${label}"]`) as HTMLButtonElement | null;
const scrolled = () => row.dispatchEvent(new win.Event('scroll'));
- return { win, document, row, box, scrollBy, button, scrolled };
+ const wheel = (deltaY: number) => {
+ const event = new win.WheelEvent('wheel', { deltaY, cancelable: true });
+ row.dispatchEvent(event);
+ return event.defaultPrevented;
+ };
+ return { win, document, row, box, scrollBy, button, scrolled, wheel };
}
describe('scroll affordance', () => {
it('adds buttons only to horizontally scrolling regions', async () => {
const { document, button } = await mount();
expect(document.querySelectorAll('button')).toHaveLength(2);
- expect(button('Scroll left').style.display).toBe('none');
- expect(button('Scroll right').style.display).toBe('flex');
- expect(button('Scroll right').type).toBe('button');
+ expect(button('Scroll left')!.style.display).toBe('none');
+ expect(button('Scroll right')!.style.display).toBe('flex');
+ expect(button('Scroll right')!.type).toBe('button');
});
it('scrolls by most of the visible width', async () => {
const { button, scrollBy } = await mount();
- button('Scroll right').click();
+ button('Scroll right')!.click();
expect(scrollBy).toHaveBeenCalledWith({ left: 240, behavior: 'smooth' });
- button('Scroll left').click();
+ button('Scroll left')!.click();
expect(scrollBy).toHaveBeenLastCalledWith({ left: -240, behavior: 'smooth' });
});
@@ -80,23 +102,18 @@ describe('scroll affordance', () => {
const { box, button, scrolled } = await mount();
box.scrollLeft = 700;
scrolled();
- expect(button('Scroll left').style.display).toBe('flex');
- expect(button('Scroll right').style.display).toBe('none');
+ expect(button('Scroll left')!.style.display).toBe('flex');
+ expect(button('Scroll right')!.style.display).toBe('none');
box.scrollWidth = 302;
box.scrollLeft = 0;
scrolled();
- expect(button('Scroll left').style.display).toBe('none');
- expect(button('Scroll right').style.display).toBe('none');
+ expect(button('Scroll left')!.style.display).toBe('none');
+ expect(button('Scroll right')!.style.display).toBe('none');
});
it('turns vertical wheel into horizontal scroll until the end', async () => {
- const { win, row, box } = await mount();
- const wheel = (deltaY: number) => {
- const event = new win.WheelEvent('wheel', { deltaY, cancelable: true });
- row.dispatchEvent(event);
- return event.defaultPrevented;
- };
+ const { box, wheel } = await mount();
expect(wheel(100)).toBe(true);
expect(box.scrollLeft).toBe(100);
@@ -107,4 +124,70 @@ describe('scroll affordance', () => {
box.scrollLeft = 0;
expect(wheel(-100)).toBe(false);
});
+
+ it('stands down when the widget has its own button at the row edge', async () => {
+ const { document, wheel, box } = await mount(
+ '<section><div id="row" style="overflow-x:auto"><div>items</div></div>' +
+ '<div id="native">&gt;</div></section>',
+ doc => {
+ const native = doc.getElementById('native')!;
+ native.style.cursor = 'pointer';
+ setRect(native, () => ({ left: 290, top: 50, width: 32, height: 32 }));
+ }
+ );
+ expect(document.querySelectorAll('button')).toHaveLength(0);
+ expect(wheel(100)).toBe(false);
+ expect(box.scrollLeft).toBe(0);
+ });
+
+ it('removes its buttons once native navigation appears', async () => {
+ const { document, win } = await mount(
+ '<section><div><div id="row" style="overflow-x:auto"><div>items</div></div></div></section>'
+ );
+ expect(document.querySelectorAll('button')).toHaveLength(2);
+
+ const native = document.createElement('button');
+ native.setAttribute('aria-label', 'Next slide');
+ setRect(native, () => ({ left: 260, top: 0, width: 24, height: 24 }));
+ document.querySelector('section')!.appendChild(native);
+ await new Promise(resolve => win.setTimeout(resolve, 200));
+
+ expect(document.querySelector('button[aria-label="Scroll right"]')).toBeNull();
+ expect(document.querySelectorAll('button')).toHaveLength(1);
+ });
+
+ it('ignores navigation controls that belong to another part of the page', async () => {
+ const { document } = await mount(
+ '<section><div id="row" style="overflow-x:auto"><div>items</div></div>' +
+ '<button id="far" aria-label="Next page">Next</button></section>',
+ doc =>
+ setRect(doc.getElementById('far')!, () => ({ left: 10, top: 600, width: 60, height: 24 }))
+ );
+ expect(document.querySelectorAll('button[aria-label^="Scroll"]')).toHaveLength(2);
+ });
+
+ it('ignores controls whose names only contain a navigation word', async () => {
+ const { document } = await mount(
+ '<section><div id="row" style="overflow-x:auto"><div>items</div></div>' +
+ '<button id="add" class="bg-background feedback">Add</button></section>',
+ doc =>
+ setRect(doc.getElementById('add')!, () => ({ left: 120, top: 0, width: 40, height: 20 }))
+ );
+ expect(document.querySelectorAll('button[aria-label^="Scroll"]')).toHaveLength(2);
+ });
+
+ it('gives buttons to the outer row only, and none to a narrow scroller', async () => {
+ const { document, scrollBy, button } = await mount(
+ '<div id="row" style="overflow-x:auto"><div id="inner" style="overflow-x:auto">' +
+ '<div>gallery</div></div></div>' +
+ '<div id="small" style="overflow-x:auto"><div>chips</div></div>',
+ doc => {
+ fakeLayout(doc.getElementById('inner')!, overflowingBox(260), 20);
+ fakeLayout(doc.getElementById('small')!, overflowingBox(200), 10);
+ }
+ );
+ expect(document.querySelectorAll('button')).toHaveLength(2);
+ button('Scroll right')!.click();
+ expect(scrollBy).toHaveBeenCalledWith({ left: 240, behavior: 'smooth' });
+ });
});
diff --git a/app/src/features/conversations/tools/mcpUi/scrollAffordance.ts b/app/src/features/conversations/tools/mcpUi/scrollAffordance.ts
index 0076ee0524..3c921d6d1d 100644
--- a/app/src/features/conversations/tools/mcpUi/scrollAffordance.ts
+++ b/app/src/features/conversations/tools/mcpUi/scrollAffordance.ts
@@ -3,6 +3,10 @@
* vertical wheel mapped onto them. Injected into every widget document; it
* never moves the widget's own nodes, it overlays fixed-position buttons that
* follow each scroller.
+ *
+ * Only the outermost scrolling row of a region gets buttons, never one
+ * narrower than `MIN_WIDTH`, and none while the widget shows its own
+ * navigation next to the row.
*/
export const SCROLL_AFFORDANCE_SOURCE = `(function () {
'use strict';
@@ -11,8 +15,14 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
var PREFIX = 'ohsa-';
var SIZE = 28;
var SLACK = 4;
+ var MIN_WIDTH = 240;
+ var EDGE = 72;
+ var HEADER = 64;
+ var ANCESTOR_LEVELS = 3;
+ var MAX_CONTROLS = 600;
+ var NAV_WORDS = ['next', 'prev', 'previous', 'forward', 'back', 'scroll', 'arrow', 'chevron',
+ 'left', 'right', 'carousel', 'slide'];
var tracked = [];
- var seen = typeof WeakSet === 'function' ? new WeakSet() : null;
var layer = null;
var timer = null;
var CHEVRON_LEFT = '<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M10 3 5 8l5 5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
@@ -21,11 +31,14 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
function overflowing(el) {
return el.scrollWidth > el.clientWidth + SLACK;
}
- function scrollsHorizontally(el) {
- if (!el || el.nodeType !== 1 || el.className === PREFIX + 'layer') return false;
- var style = window.getComputedStyle(el);
- var x = style.overflowX;
- return (x === 'auto' || x === 'scroll') && overflowing(el);
+ function inLayer(node) {
+ return !!layer && layer.contains(node);
+ }
+ function isCandidate(el) {
+ if (!el || el.nodeType !== 1 || inLayer(el)) return false;
+ var x = window.getComputedStyle(el).overflowX;
+ if ((x !== 'auto' && x !== 'scroll') || !overflowing(el)) return false;
+ return el.getBoundingClientRect().width >= MIN_WIDTH;
}
function atStart(el) {
return el.scrollLeft <= 0;
@@ -33,6 +46,51 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
function atEnd(el) {
return el.scrollLeft + el.clientWidth >= el.scrollWidth - 1;
}
+ function clickable(node) {
+ var tag = node.tagName;
+ if (tag === 'BUTTON' || tag === 'A') return true;
+ if (node.getAttribute('role') === 'button') return true;
+ return window.getComputedStyle(node).cursor === 'pointer';
+ }
+ function looksLikeNav(node) {
+ var text = [node.getAttribute('aria-label'), node.getAttribute('title'), node.getAttribute('class')]
+ .join(' ')
+ .replace(/([a-z])([A-Z])/g, '$1 $2')
+ .toLowerCase()
+ .split(/[^a-z]+/);
+ for (var i = 0; i < text.length; i++) {
+ if (NAV_WORDS.indexOf(text[i]) !== -1) return true;
+ }
+ return false;
+ }
+ function nearEdge(control, row) {
+ if (control.bottom <= row.top || control.top >= row.bottom) return false;
+ var nearLeft = control.left < row.left + EDGE && control.right > row.left - EDGE;
+ var nearRight = control.right > row.right - EDGE && control.left < row.right + EDGE;
+ return nearLeft || nearRight;
+ }
+ function nearRow(control, row) {
+ if (control.bottom <= row.top - HEADER || control.top >= row.bottom + SLACK * 4) return false;
+ return control.right > row.left - EDGE && control.left < row.right + EDGE;
+ }
+ function hasNativeNav(el) {
+ var top = el;
+ for (var level = 0; level < ANCESTOR_LEVELS && top.parentElement; level++) {
+ top = top.parentElement;
+ }
+ if (top === el) return false;
+ var row = el.getBoundingClientRect();
+ var nodes = top.getElementsByTagName('*');
+ var limit = Math.min(nodes.length, MAX_CONTROLS);
+ for (var i = 0; i < limit; i++) {
+ var node = nodes[i];
+ if (node === el || el.contains(node) || inLayer(node) || !clickable(node)) continue;
+ var rect = node.getBoundingClientRect();
+ if (rect.width <= 0 || rect.height <= 0) continue;
+ if (nearEdge(rect, row) || (looksLikeNav(node) && nearRow(rect, row))) return true;
+ }
+ return false;
+ }
function ensureLayer() {
if (layer && layer.isConnected) return layer;
if (!document.body) return null;
@@ -63,6 +121,7 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
return button;
}
function place(entry) {
+ if (entry.suppressed) return;
var el = entry.el;
var rect = el.getBoundingClientRect();
var visible = el.isConnected && overflowing(el) && rect.width > SIZE * 2 && rect.height > 0;
@@ -74,57 +133,81 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
entry.left.style.display = visible && !atStart(el) ? 'flex' : 'none';
entry.right.style.display = visible && !atEnd(el) ? 'flex' : 'none';
}
- function update() {
- for (var i = tracked.length - 1; i >= 0; i--) {
- var entry = tracked[i];
- if (!entry.el.isConnected) {
- entry.left.remove();
- entry.right.remove();
- tracked.splice(i, 1);
- continue;
- }
- place(entry);
+ function setSuppressed(entry, suppressed) {
+ if (entry.suppressed === suppressed) return;
+ entry.suppressed = suppressed;
+ if (suppressed) {
+ entry.left.remove();
+ entry.right.remove();
+ return;
}
+ var host = ensureLayer();
+ if (!host) return;
+ host.appendChild(entry.left);
+ host.appendChild(entry.right);
}
- function onWheel(event) {
- var el = event.currentTarget;
- if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
- if (el.scrollHeight > el.clientHeight + 1) return;
- if (event.deltaY > 0 ? atEnd(el) : atStart(el)) return;
- el.scrollLeft += event.deltaY;
- event.preventDefault();
+ function untrack(entry) {
+ entry.left.remove();
+ entry.right.remove();
+ entry.el.removeEventListener('scroll', entry.onScroll);
+ entry.el.removeEventListener('wheel', entry.onWheel);
+ if (entry.observer) entry.observer.disconnect();
+ tracked.splice(tracked.indexOf(entry), 1);
}
function track(el) {
- var host = ensureLayer();
- if (!host) return;
- if (seen) seen.add(el);
- else el.setAttribute('data-' + PREFIX + 'tracked', '');
+ if (!ensureLayer()) return;
var entry = {
el: el,
+ suppressed: true,
left: makeButton('Scroll left', CHEVRON_LEFT, -1, el),
- right: makeButton('Scroll right', CHEVRON_RIGHT, 1, el)
+ right: makeButton('Scroll right', CHEVRON_RIGHT, 1, el),
+ observer: null
};
- host.appendChild(entry.left);
- host.appendChild(entry.right);
- el.addEventListener('scroll', function () { place(entry); }, { passive: true });
- el.addEventListener('wheel', onWheel, { passive: false });
+ entry.onScroll = function () { place(entry); };
+ entry.onWheel = function (event) {
+ if (entry.suppressed) return;
+ if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
+ if (el.scrollHeight > el.clientHeight + 1) return;
+ if (event.deltaY > 0 ? atEnd(el) : atStart(el)) return;
+ el.scrollLeft += event.deltaY;
+ event.preventDefault();
+ };
+ el.addEventListener('scroll', entry.onScroll, { passive: true });
+ el.addEventListener('wheel', entry.onWheel, { passive: false });
if (typeof ResizeObserver === 'function') {
- new ResizeObserver(function () { place(entry); }).observe(el);
+ entry.observer = new ResizeObserver(entry.onScroll);
+ entry.observer.observe(el);
}
tracked.push(entry);
- place(entry);
}
- function isTracked(el) {
- return seen ? seen.has(el) : el.hasAttribute('data-' + PREFIX + 'tracked');
+ function update() {
+ for (var i = tracked.length - 1; i >= 0; i--) {
+ if (!tracked[i].el.isConnected) {
+ untrack(tracked[i]);
+ continue;
+ }
+ place(tracked[i]);
+ }
+ }
+ function adopt(el) {
+ for (var i = tracked.length - 1; i >= 0; i--) {
+ var other = tracked[i].el;
+ if (other === el || other.contains(el)) return;
+ if (el.contains(other)) untrack(tracked[i]);
+ }
+ track(el);
}
function scan() {
timer = null;
if (!document.body) return;
var all = document.body.getElementsByTagName('*');
+ var found = [];
for (var i = 0; i < all.length; i++) {
- var el = all[i];
- if (layer && layer.contains(el)) continue;
- if (!isTracked(el) && scrollsHorizontally(el)) track(el);
+ if (isCandidate(all[i])) found.push(all[i]);
+ }
+ for (var j = 0; j < found.length; j++) adopt(found[j]);
+ for (var k = 0; k < tracked.length; k++) {
+ setSuppressed(tracked[k], hasNativeNav(tracked[k].el));
}
update();
}
@@ -139,7 +222,9 @@ export const SCROLL_AFFORDANCE_SOURCE = `(function () {
if (typeof MutationObserver === 'function') {
new MutationObserver(schedule).observe(document.documentElement, {
childList: true,
- subtree: true
+ subtree: true,
+ attributes: true,
+ attributeFilter: ['class', 'hidden']
});
}
if (typeof ResizeObserver === 'function') {