Skip to content

Commit f339a40

Browse files
committed
Keep screenshot key line stable while flipping
Preserve the focused screenshot control during a held primary press on either of its labels. This keeps the key line from painting page bindings between mousedown and the native label click. Cover both labels in the browser regression while retaining native click, keyboard, and scriptless behavior.
1 parent 1d1a92c commit f339a40

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎plugins/leaf/skills/leaf/packages/default/widgets/lf-shot.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* over the image drives one checkbox, so the pointer rests on the change and clicks
1616
* without aiming, and the two states swap under it.
1717
*
18-
* Everything that needs is native: the state is a checkbox, the swap a `:has(:checked)`
19-
* rule in the theme. No handler runs after the upgrade, so a serialized copy of this
20-
* page — DOM kept, script tags dropped — still flips, and a printed one stacks both
21-
* frames instead. A dragged slider would have survived neither.
18+
* Everything that drives the flip is native: the state is a checkbox, the swap a
19+
* `:has(:checked)` rule in the theme. A serialized copy of this page — DOM kept, script
20+
* tags dropped — still flips, and a printed one stacks both frames instead. A dragged
21+
* slider would have survived neither.
2222
*
2323
* Two kinds of word, and they are marked differently on purpose. Each frame's caption
2424
* names which state it holds, which is the widget's own word and the only thing telling
@@ -104,6 +104,16 @@ customElements.define(
104104
box.addEventListener("change", paintKeys);
105105

106106
this.append(flip, row);
107+
// A label transfers focus on click, after its mousedown can blur the checkbox.
108+
// During a repeated held press, the key line therefore painted the page's bindings
109+
// before restoring the screenshot bindings on mouseup. Preserve an existing focus
110+
// across either label; their native clicks still flip the box when the press completes.
111+
for (const target of box.labels) {
112+
target.addEventListener("mousedown", (event) => {
113+
if (event.button === 0 && document.activeElement === box)
114+
event.preventDefault();
115+
});
116+
}
107117
settle(this.register(shots));
108118
}
109119

‎tests/test_render_controls.py‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
flip_point,
9595
geometry,
9696
held_stale,
97+
key_line,
9798
leaf_page,
9899
live_url,
99100
live_watcher,
@@ -2861,13 +2862,45 @@ def test_a_shot_shows_one_frame_and_flips_between_them(browser, serve):
28612862
page.mouse.click(*at)
28622863
expect(page.locator('.lf-shotframe[data-lf-state="after"]')).to_be_visible()
28632864
assert shown_frames(page) == ["after"]
2864-
page.mouse.click(*at)
2865+
box = page.locator("lf-shot input[type=checkbox]")
2866+
expect(box).to_be_focused()
2867+
assert "show before" in key_line(page)
2868+
# The overlay is a label. It used to blur its checkbox on mousedown, leaving a human
2869+
# press long enough for the line to paint the page's bindings before mouseup restored
2870+
# the screenshot bindings. Hold it across two frames: its focus and line must not move.
2871+
page.mouse.down()
2872+
expect(box).to_be_focused()
2873+
assert "show before" in key_line(page)
2874+
page.mouse.up()
28652875
expect(page.locator('.lf-shotframe[data-lf-state="before"]')).to_be_visible()
28662876
assert shown_frames(page) == ["before"]
2877+
# The visible instruction is a second label for the same switch. A repeated press
2878+
# on its words has the same focus bargain as one on the image.
2879+
text_label = page.locator("lf-shot .lf-shotpick label")
2880+
label_bounds = text_label.bounding_box()
2881+
assert label_bounds is not None
2882+
text_at = (
2883+
label_bounds["x"] + label_bounds["width"] - 2,
2884+
label_bounds["y"] + label_bounds["height"] / 2,
2885+
)
2886+
assert text_label.evaluate(
2887+
"""(label, point) => {
2888+
const hit = document.elementFromPoint(...point);
2889+
return hit?.closest('label') === label && hit !== label.control;
2890+
}""",
2891+
text_at,
2892+
)
2893+
page.mouse.move(*text_at)
2894+
page.mouse.down()
2895+
expect(box).to_be_focused()
2896+
assert "show after" in key_line(page)
2897+
page.mouse.up()
2898+
expect(page.locator('.lf-shotframe[data-lf-state="after"]')).to_be_visible()
2899+
assert shown_frames(page) == ["after"]
28672900
# The keyboard's handle, which the label over the image cannot be.
2868-
page.locator("lf-shot input[type=checkbox]").focus()
2901+
box.focus()
28692902
page.keyboard.press(" ")
2870-
expect(page.locator('.lf-shotframe[data-lf-state="after"]')).to_be_visible()
2903+
expect(page.locator('.lf-shotframe[data-lf-state="before"]')).to_be_visible()
28712904
assert errors == []
28722905
page.close()
28732906

0 commit comments

Comments
 (0)