Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

fix: keep composer menus inside viewport (#688)

Joshua Franceaae338f786ce
diff --git a/plugins/web-ui/src/composer.ts b/plugins/web-ui/src/composer.ts
index 0ad48362a..5e28600d1 100644
--- a/plugins/web-ui/src/composer.ts
+++ b/plugins/web-ui/src/composer.ts
@@ -978,6 +978,7 @@ export function createComposerSurface(ctx: ConvCtx): ComposerSurface {
@input=${(e: InputEvent) => {
composerState.menuQuery = (e.currentTarget as HTMLInputElement).value;
ctx.chat.drawActiveChat();
+ requestAnimationFrame(() => placeComposerMenu(args.kind));
}}
/>
</label>`
@@ -1016,16 +1017,37 @@ export function createComposerSurface(ctx: ConvCtx): ComposerSurface {
`;
}
+ function placeComposerMenu(kind: ComposerMenu): void {
+ const popover = ctx.chat.state.host?.querySelector<HTMLElement>(`#composer-${kind}-menu`);
+ const anchor = popover?.parentElement;
+ if (!popover || !anchor) return;
+ const viewport = window.visualViewport;
+ const viewportTop = viewport?.offsetTop ?? 0;
+ const viewportBottom = viewportTop + (viewport?.height ?? window.innerHeight);
+ const anchorRect = anchor.getBoundingClientRect();
+ const margin = 12;
+ const gap = 8;
+ const availableAbove = Math.max(0, anchorRect.top - viewportTop - margin - gap);
+ const availableBelow = Math.max(0, viewportBottom - anchorRect.bottom - margin - gap);
+ const placeBelow = availableBelow > availableAbove;
+ const availableHeight = placeBelow ? availableBelow : availableAbove;
+ popover.classList.toggle("drop-down", placeBelow);
+ popover.style.setProperty("--menu-available-height", `${Math.floor(availableHeight)}px`);
+ }
+
function toggleComposerMenu(e: Event, kind: ComposerMenu): void {
e.stopPropagation();
const opening = composerState.openMenu !== kind;
composerState.openMenu = opening ? kind : null;
composerState.menuQuery = "";
ctx.chat.drawActiveChat();
- if (opening && kind === "model") {
- requestAnimationFrame(() =>
- ctx.chat.state.host?.querySelector<HTMLInputElement>(".model-control .menu-search input")?.focus(),
- );
+ if (opening) {
+ requestAnimationFrame(() => {
+ placeComposerMenu(kind);
+ if (kind !== "model") return;
+ ctx.chat.state.host?.querySelector<HTMLInputElement>(".model-control .menu-search input")?.focus();
+ requestAnimationFrame(() => placeComposerMenu(kind));
+ });
}
}
diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.css
index 1db8f58b5..16b1b53f0 100644
--- a/plugins/web-ui/src/shell.css
+++ b/plugins/web-ui/src/shell.css
@@ -2605,7 +2605,7 @@ a.chat-row-open {
z-index: 30;
width: 230px;
max-width: calc(100vw - 36px);
- max-height: min(320px, 52vh);
+ max-height: min(320px, var(--menu-available-height, 52vh));
overflow-y: auto;
padding: 6px;
border: 1px solid var(--border);
@@ -2620,6 +2620,10 @@ a.chat-row-open {
left: auto;
width: 230px;
}
+.menu-popover.drop-down {
+ top: calc(100% + 8px);
+ bottom: auto;
+}
.menu-title {
padding: 6px 8px 5px;
font-size: 11px;
@@ -4596,7 +4600,7 @@ a.chat-row-open {
width: min(286px, calc(100vw - 30px));
}
.menu-popover {
- max-height: 42vh;
+ max-height: min(320px, var(--menu-available-height, 42vh));
}
.message-bubble {
max-width: 94%;
diff --git a/plugins/web-ui/test/composer-source.test.ts b/plugins/web-ui/test/composer-source.test.ts
index 27e125ca8..cf7ecb296 100644
--- a/plugins/web-ui/test/composer-source.test.ts
+++ b/plugins/web-ui/test/composer-source.test.ts
@@ -87,3 +87,10 @@ test("long model menus stay searchable and show the selected effort", () => {
assert.match(composer, /option\.groupLabel/);
assert.match(composer, /suffix: `· \$\{effortLabel\(composerState\.effortLevel\)}`/);
});
+
+test("composer menus stay inside the visible viewport", () => {
+ assert.match(composer, /function placeComposerMenu\(kind: ComposerMenu\)/);
+ assert.match(composer, /window\.visualViewport/);
+ assert.match(composer, /--menu-available-height/);
+ assert.match(composer, /classList\.toggle\("drop-down", placeBelow\)/);
+});