diff --git a/.agents/skills/simple-words-design/README.md b/.agents/skills/simple-words-design/README.md
index aa95e1e..d8d48c6 100644
--- a/.agents/skills/simple-words-design/README.md
+++ b/.agents/skills/simple-words-design/README.md
@@ -180,8 +180,8 @@ The options page is fully opaque.
### Layout rules
-- **Floating button** is `position: fixed`, `z-index: 2147483647` (max), starts near the active editable field's lower-right edge, and can move to avoid button-like page controls.
- This is non-negotiable — it must clear every host page, including ones with their own floating UI.
+- **Floating button** is `position: fixed`, `z-index: 2147483647` (max), starts near the active editable field's lower-right edge, and can move when a candidate button center would sit over a button-like page control.
+ Keep this placement check lightweight so typing, selection, scroll, and caret movement stay responsive on heavy host pages.
Use a compact 24px `sw` glyph button for normal editors and a 20px version for short inputs.
The injected button and panel are visible only while the current domain is enabled and the active editor is connected, focused, and visible, including empty editors.
Hide them when the editor or any ancestor is `hidden`, `aria-hidden="true"`, or `display: none`, when the editor itself is `visibility: hidden` or `visibility: collapse`, inside a closed `dialog`, or inside closed `details` content.
diff --git a/.agents/skills/simple-words-design/ui_kits/extension/README.md b/.agents/skills/simple-words-design/ui_kits/extension/README.md
index 5dde15e..bddb764 100644
--- a/.agents/skills/simple-words-design/ui_kits/extension/README.md
+++ b/.agents/skills/simple-words-design/ui_kits/extension/README.md
@@ -6,7 +6,7 @@ Check the production extension files before treating this kit as pixel-faithful.
## Surfaces
1. **Floating button** (`FloatingButton.jsx`) - a prototype pill button with the older sparkles icon and label.
- Production now uses a compact `sw` glyph button that starts near the lower-right edge of focused editable fields and can move to avoid button-like page controls, so check the extension files before treating this component as pixel-faithful.
+ Production now uses a compact `sw` glyph button that starts near the lower-right edge of focused editable fields and can move when a candidate button center would sit over a button-like page control, so check the extension files before treating this component as pixel-faithful.
2. **Refine panel** (`RefinePanel.jsx`) - the floating panel that shows loading, setup or provider error guidance, rewritten draft states, and Open settings/Replace/Dismiss actions.
3. **Options page** (`OptionsPage.jsx`) - the settings page prototype with provider config, enabled domains, name field, and advanced system prompt.
4. **Host page** (`HostPage.jsx`) - a faked Gmail-like compose window, just so the floating button has something to attach to in `index.html`.
@@ -33,7 +33,7 @@ Preserve that visibility contract in production-facing work.
## Demo
`index.html` boots a fake Gmail compose window with a draft typed in.
-The prototype still positions its legacy pill below the textarea; production starts the compact `sw` button near the editor's lower-right edge and can move it to avoid button-like page controls.
+The prototype still positions its legacy pill below the textarea; production starts the compact `sw` button near the editor's lower-right edge and can move it when the candidate center would sit over a button-like page control.
Clicking the button opens the refine panel with a 1-second simulated "Refining..." state, then a rewritten draft.
Click "Replace draft" to swap the textarea content; click "Dismiss" to close.
There's also a tab switch at the top to jump to the **Options page** view.
diff --git a/src/content.ts b/src/content.ts
index 6ecd13a..d74936f 100644
--- a/src/content.ts
+++ b/src/content.ts
@@ -39,9 +39,6 @@ const CLICKABLE_AVOID_SELECTOR = [
'input[type="reset"]',
'summary'
].join(',')
-const CLICKABLE_AVOID_PROXIMITY = 80
-const CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP = 4
-
const BRAND_GLYPH_SVG = ``
const SPARKLES_SVG = ``
@@ -577,8 +574,7 @@ function collisionAwareButtonPositionNearEditor(options: {
const candidateRects = buttonCandidateRectsNearEditor(positionOptions)
for (const candidateRect of candidateRects) {
- const avoidRects = clickableAvoidRectsForCandidate(editor, candidateRect)
- if (!avoidRects.some((rect) => rectsOverlap(rect, candidateRect))) {
+ if (!candidateCenterCoversClickable(editor, candidateRect)) {
return candidateRect
}
}
@@ -586,127 +582,35 @@ function collisionAwareButtonPositionNearEditor(options: {
return candidateRects[0]
}
-function clickableAvoidRectsForCandidate(
+// Cheap collision check: hit-test a single point at the candidate's center.
+// This avoids the topmost clickable control sitting directly under the button
+// without the cost of scanning every pixel of the candidate rect, which made
+// repositioning sluggish on heavy pages (it runs on scroll, input, and caret
+// moves). The browser's hit-testing already skips hidden/non-painted elements.
+function candidateCenterCoversClickable(
editor: HTMLElement,
candidateRect: RectLike
-): DOMRect[] {
- const elementsFromPoint = document.elementsFromPoint?.bind(document)
-
- if (elementsFromPoint) {
- const seenElements = new Set()
-
- for (const element of elementsFromCandidateRect(
- candidateRect,
- elementsFromPoint
- )) {
- const clickableElement = element.closest(
- CLICKABLE_AVOID_SELECTOR
- )
-
- if (!clickableElement || seenElements.has(clickableElement)) {
- continue
- }
-
- seenElements.add(clickableElement)
-
- if (clickableElement === editor) {
- continue
- }
-
- if (clickableElement.contains(editor)) {
- continue
- }
-
- if (isInjectedUITarget(clickableElement)) {
- continue
- }
-
- const visibility = getComputedStyle(clickableElement).visibility
- if (visibility === 'hidden' || visibility === 'collapse') {
- continue
- }
-
- const rect = clickableElement.getBoundingClientRect()
- if (
- rect.width > 0 &&
- rect.height > 0 &&
- rectsOverlap(rect, candidateRect)
- ) {
- return [rect]
- }
- }
-
- return []
- }
-
- const proximityRect = expandedRect(candidateRect, CLICKABLE_AVOID_PROXIMITY)
-
- return Array.from(
- document.querySelectorAll(CLICKABLE_AVOID_SELECTOR)
- )
- .filter((element) => element !== editor)
- .filter((element) => !element.contains(editor))
- .filter((element) => !isInjectedUITarget(element))
- .filter((element) => {
- const visibility = getComputedStyle(element).visibility
- return visibility !== 'hidden' && visibility !== 'collapse'
- })
- .map((element) => element.getBoundingClientRect())
- .filter((rect) => rect.width > 0 && rect.height > 0)
- .filter((rect) => rectsOverlap(rect, proximityRect))
-}
-
-function* elementsFromCandidateRect(
- rect: RectLike,
- elementsFromPoint: (x: number, y: number) => Element[]
-): Iterable {
- const right = rect.right - 1
- const bottom = rect.bottom - 1
- const centerX = Math.floor(rect.left + rect.width / 2)
-
- for (let x = rect.left; x < rect.right; x += 1) {
- yield* elementsFromPoint(x, rect.top)
- yield* elementsFromPoint(x, bottom)
- }
-
- for (let y = rect.top + 1; y < bottom; y += 1) {
- yield* elementsFromPoint(rect.left, y)
- yield* elementsFromPoint(right, y)
- yield* elementsFromPoint(centerX, y)
- }
-
- for (
- let x = rect.left + CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP;
- x < right;
- x += CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP
- ) {
- if (x === centerX) {
+): boolean {
+ const centerX = candidateRect.left + candidateRect.width / 2
+ const centerY = candidateRect.top + candidateRect.height / 2
+ const elements = document.elementsFromPoint?.(centerX, centerY) ?? [
+ document.elementFromPoint?.(centerX, centerY)
+ ]
+
+ for (const element of elements) {
+ if (!(element instanceof Element) || isInjectedUITarget(element)) {
continue
}
- for (
- let y = rect.top + CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP;
- y < bottom;
- y += CLICKABLE_AVOID_INTERIOR_SAMPLE_STEP
- ) {
- yield* elementsFromPoint(x, y)
+ const clickable = element.closest(CLICKABLE_AVOID_SELECTOR)
+ if (!clickable || clickable === editor || clickable.contains(editor)) {
+ return false
}
- }
-}
-function expandedRect(rect: RectLike, amount: number): DOMRect {
- return new DOMRect(
- rect.left - amount,
- rect.top - amount,
- rect.width + amount * 2,
- rect.height + amount * 2
- )
-}
+ return !isInjectedUITarget(clickable)
+ }
-function rectsOverlap(a: RectLike, b: RectLike): boolean {
- return (
- a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top
- )
+ return false
}
function buttonSizeForEditor(rect: DOMRect): number {
diff --git a/test/content.test.ts b/test/content.test.ts
index 30897d3..39ff68d 100644
--- a/test/content.test.ts
+++ b/test/content.test.ts
@@ -9,6 +9,7 @@ describe('content script button visibility', () => {
document.getElementById('simplewords-panel')?.remove()
vi.restoreAllMocks()
Reflect.deleteProperty(document, 'elementsFromPoint')
+ Reflect.deleteProperty(document, 'elementFromPoint')
Reflect.deleteProperty(globalThis, 'chrome')
resetExtensionContextForTests()
})
@@ -86,7 +87,7 @@ describe('content script button visibility', () => {
expect(button.innerHTML).toContain('font-size="48"')
})
- test('moves the Simple Words button away from overlapping page buttons', () => {
+ test('moves the Simple Words button away from a page button under its default position', () => {
document.body.innerHTML =
''
@@ -103,86 +104,7 @@ describe('content script button visibility', () => {
y: 100,
toJSON: () => null
})
- vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue({
- top: 160,
- left: 260,
- right: 310,
- bottom: 210,
- width: 50,
- height: 50,
- x: 260,
- y: 160,
- toJSON: () => null
- })
-
- editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
-
- const button = document.getElementById(
- 'simplewords-button'
- ) as HTMLButtonElement
- expect(button.style.top).toBe('108px')
- expect(button.style.left).toBe('268px')
- })
-
- test('does not avoid invisible page buttons near the editor', () => {
- document.body.innerHTML =
- ''
-
- const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const sendButton = document.querySelector('button') as HTMLButtonElement
- vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
- top: 100,
- left: 100,
- right: 300,
- bottom: 200,
- width: 200,
- height: 100,
- x: 100,
- y: 100,
- toJSON: () => null
- })
- vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue({
- top: 160,
- left: 260,
- right: 310,
- bottom: 210,
- width: 50,
- height: 50,
- x: 260,
- y: 160,
- toJSON: () => null
- })
-
- editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
-
- const button = document.getElementById(
- 'simplewords-button'
- ) as HTMLButtonElement
- expect(button.style.top).toBe('168px')
- expect(button.style.left).toBe('268px')
- })
-
- test('does not measure distant page buttons while positioning', () => {
- document.body.innerHTML =
- ''
-
- const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const nearButton = document.querySelector(
- '[data-near]'
- ) as HTMLButtonElement
- const farButton = document.querySelector('[data-far]') as HTMLButtonElement
- vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
- top: 100,
- left: 100,
- right: 300,
- bottom: 200,
- width: 200,
- height: 100,
- x: 100,
- y: 100,
- toJSON: () => null
- })
- const nearRect = {
+ const sendRect = {
top: 160,
left: 260,
right: 310,
@@ -193,62 +115,16 @@ describe('content script button visibility', () => {
y: 160,
toJSON: () => null
}
- vi.spyOn(nearButton, 'getBoundingClientRect').mockReturnValue(nearRect)
- const farRectSpy = vi.spyOn(farButton, 'getBoundingClientRect')
- Object.defineProperty(document, 'elementsFromPoint', {
- configurable: true,
- value: () => []
- })
- vi.spyOn(document, 'elementsFromPoint').mockReturnValue([nearButton])
-
- editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
-
- expect(farRectSpy).not.toHaveBeenCalled()
- const button = document.getElementById(
- 'simplewords-button'
- ) as HTMLButtonElement
- expect(button.style.top).toBe('108px')
- expect(button.style.left).toBe('268px')
- })
-
- test('avoids page buttons that partially overlap a candidate between sampled points', () => {
- document.body.innerHTML =
- ''
-
- const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const sendButton = document.querySelector('button') as HTMLButtonElement
- vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
- top: 100,
- left: 100,
- right: 300,
- bottom: 200,
- width: 200,
- height: 100,
- x: 100,
- y: 100,
- toJSON: () => null
- })
- const sendRect = {
- top: 170,
- left: 282,
- right: 302,
- bottom: 178,
- width: 20,
- height: 8,
- x: 282,
- y: 170,
- toJSON: () => null
- }
vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect)
- Object.defineProperty(document, 'elementsFromPoint', {
+ Object.defineProperty(document, 'elementFromPoint', {
configurable: true,
value: (x: number, y: number) =>
x >= sendRect.left &&
x < sendRect.right &&
y >= sendRect.top &&
y < sendRect.bottom
- ? [sendButton]
- : []
+ ? sendButton
+ : null
})
editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
@@ -260,59 +136,7 @@ describe('content script button visibility', () => {
expect(button.style.left).toBe('268px')
})
- test('stops overlap hit testing after finding a clear button position', () => {
- document.body.innerHTML =
- ''
-
- const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const sendButton = document.querySelector('button') as HTMLButtonElement
- vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
- top: 100,
- left: 100,
- right: 300,
- bottom: 200,
- width: 200,
- height: 100,
- x: 100,
- y: 100,
- toJSON: () => null
- })
- const sendRect = {
- top: 170,
- left: 282,
- right: 302,
- bottom: 178,
- width: 20,
- height: 8,
- x: 282,
- y: 170,
- toJSON: () => null
- }
- vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect)
- const elementsFromPoint = vi.fn((x: number, y: number) =>
- x >= sendRect.left &&
- x < sendRect.right &&
- y >= sendRect.top &&
- y < sendRect.bottom
- ? [sendButton]
- : []
- )
- Object.defineProperty(document, 'elementsFromPoint', {
- configurable: true,
- value: elementsFromPoint
- })
-
- editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
-
- expect(elementsFromPoint).toHaveBeenCalledTimes(187)
- const button = document.getElementById(
- 'simplewords-button'
- ) as HTMLButtonElement
- expect(button.style.top).toBe('108px')
- expect(button.style.left).toBe('268px')
- })
-
- test('short-circuits overlap hit testing for blocked button positions', () => {
+ test('looks past the visible Simple Words button when checking for covered page buttons', () => {
document.body.innerHTML =
''
@@ -341,22 +165,28 @@ describe('content script button visibility', () => {
toJSON: () => null
}
vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect)
- const elementsFromPoint = vi.fn((x: number, y: number) =>
+ const hitsDefaultCandidate = (x: number, y: number) =>
x >= sendRect.left &&
x < sendRect.right &&
y >= sendRect.top &&
y < sendRect.bottom
- ? [sendButton]
- : []
- )
+ Object.defineProperty(document, 'elementFromPoint', {
+ configurable: true,
+ value: (x: number, y: number) => {
+ const button = document.getElementById('simplewords-button')
+ return button && hitsDefaultCandidate(x, y) ? button : null
+ }
+ })
Object.defineProperty(document, 'elementsFromPoint', {
configurable: true,
- value: elementsFromPoint
+ value: (x: number, y: number) => {
+ const button = document.getElementById('simplewords-button')
+ return button && hitsDefaultCandidate(x, y) ? [button, sendButton] : []
+ }
})
editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
- expect(elementsFromPoint).toHaveBeenCalledTimes(135)
const button = document.getElementById(
'simplewords-button'
) as HTMLButtonElement
@@ -364,9 +194,9 @@ describe('content script button visibility', () => {
expect(button.style.left).toBe('268px')
})
- test('avoids small page buttons that overlap between sampled rows and columns', () => {
+ test('does not avoid an invisible page button under its default position', () => {
document.body.innerHTML =
- ''
+ ''
const editor = document.querySelector('textarea') as HTMLTextAreaElement
const sendButton = document.querySelector('button') as HTMLButtonElement
@@ -382,26 +212,29 @@ describe('content script button visibility', () => {
toJSON: () => null
})
const sendRect = {
- top: 170,
- left: 278,
- right: 282,
- bottom: 174,
- width: 4,
- height: 4,
- x: 278,
- y: 170,
+ top: 160,
+ left: 260,
+ right: 310,
+ bottom: 210,
+ width: 50,
+ height: 50,
+ x: 260,
+ y: 160,
toJSON: () => null
}
vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect)
- Object.defineProperty(document, 'elementsFromPoint', {
+ // Real hit-testing never returns a visibility:hidden element, so the
+ // mock mirrors that: the hidden button is invisible to elementFromPoint.
+ Object.defineProperty(document, 'elementFromPoint', {
configurable: true,
value: (x: number, y: number) =>
+ getComputedStyle(sendButton).visibility !== 'hidden' &&
x >= sendRect.left &&
x < sendRect.right &&
y >= sendRect.top &&
y < sendRect.bottom
- ? [sendButton]
- : []
+ ? sendButton
+ : null
})
editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
@@ -409,16 +242,16 @@ describe('content script button visibility', () => {
const button = document.getElementById(
'simplewords-button'
) as HTMLButtonElement
- expect(button.style.top).toBe('108px')
+ expect(button.style.top).toBe('168px')
expect(button.style.left).toBe('268px')
})
- test('avoids small page buttons fully inside a candidate away from sampled lines', () => {
+ test('does not avoid a non-clickable focusable container under its default position', () => {
document.body.innerHTML =
- ''
+ 'Focusable wrapper
'
const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const sendButton = document.querySelector('button') as HTMLButtonElement
+ const focusableWrapper = document.querySelector('div') as HTMLDivElement
vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
top: 100,
left: 100,
@@ -430,27 +263,29 @@ describe('content script button visibility', () => {
y: 100,
toJSON: () => null
})
- const sendRect = {
- top: 176,
- left: 276,
- right: 280,
- bottom: 180,
- width: 4,
- height: 4,
- x: 276,
- y: 176,
+ const wrapperRect = {
+ top: 160,
+ left: 260,
+ right: 310,
+ bottom: 210,
+ width: 50,
+ height: 50,
+ x: 260,
+ y: 160,
toJSON: () => null
}
- vi.spyOn(sendButton, 'getBoundingClientRect').mockReturnValue(sendRect)
- Object.defineProperty(document, 'elementsFromPoint', {
+ vi.spyOn(focusableWrapper, 'getBoundingClientRect').mockReturnValue(
+ wrapperRect
+ )
+ Object.defineProperty(document, 'elementFromPoint', {
configurable: true,
value: (x: number, y: number) =>
- x >= sendRect.left &&
- x < sendRect.right &&
- y >= sendRect.top &&
- y < sendRect.bottom
- ? [sendButton]
- : []
+ x >= wrapperRect.left &&
+ x < wrapperRect.right &&
+ y >= wrapperRect.top &&
+ y < wrapperRect.bottom
+ ? focusableWrapper
+ : null
})
editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
@@ -458,16 +293,16 @@ describe('content script button visibility', () => {
const button = document.getElementById(
'simplewords-button'
) as HTMLButtonElement
- expect(button.style.top).toBe('108px')
+ expect(button.style.top).toBe('168px')
expect(button.style.left).toBe('268px')
})
- test('does not avoid broad tabindex containers near the editor', () => {
+ test('checks candidate centers in order, stops at the first clear spot, and never measures page controls', () => {
document.body.innerHTML =
- 'Focusable wrapper
'
+ ''
const editor = document.querySelector('textarea') as HTMLTextAreaElement
- const focusableWrapper = document.querySelector('div') as HTMLDivElement
+ const sendButton = document.querySelector('button') as HTMLButtonElement
vi.spyOn(editor, 'getBoundingClientRect').mockReturnValue({
top: 100,
left: 100,
@@ -479,24 +314,43 @@ describe('content script button visibility', () => {
y: 100,
toJSON: () => null
})
- vi.spyOn(focusableWrapper, 'getBoundingClientRect').mockReturnValue({
- top: 160,
- left: 260,
- right: 310,
- bottom: 210,
- width: 50,
- height: 50,
- x: 260,
- y: 160,
+ // Exactly covers the default (lower-right) candidate, so the first center
+ // is blocked and the second is clear.
+ const sendRect = {
+ top: 168,
+ left: 268,
+ right: 292,
+ bottom: 192,
+ width: 24,
+ height: 24,
+ x: 268,
+ y: 168,
toJSON: () => null
+ }
+ const sendRectSpy = vi
+ .spyOn(sendButton, 'getBoundingClientRect')
+ .mockReturnValue(sendRect)
+ const elementFromPoint = vi.fn((x: number, y: number) =>
+ x >= sendRect.left &&
+ x < sendRect.right &&
+ y >= sendRect.top &&
+ y < sendRect.bottom
+ ? sendButton
+ : null
+ )
+ Object.defineProperty(document, 'elementFromPoint', {
+ configurable: true,
+ value: elementFromPoint
})
editor.dispatchEvent(new FocusEvent('focusin', { bubbles: true }))
+ expect(elementFromPoint).toHaveBeenCalledTimes(2)
+ expect(sendRectSpy).not.toHaveBeenCalled()
const button = document.getElementById(
'simplewords-button'
) as HTMLButtonElement
- expect(button.style.top).toBe('168px')
+ expect(button.style.top).toBe('108px')
expect(button.style.left).toBe('268px')
})