mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(cli-rs): stop holding keys once a JS dialog's first tree shows
The ds-3 wait held typed keys until a node of a just-opened dialog took the focus, or for 500 ms. A dialog with no field, or one that focuses nothing, so held Enter and every other key for the full 500 ms, which is visible lag. The wait now ends as soon as the dialog shows its first tree from the plugin, once the focus that tree asked for has applied. It still ends when the mount ends or after 500 ms. The pipe's placeholder, which stands in before the JS host sends a tree, is marked `stand_in` on the view so it does not count as that first tree.
This commit is contained in:
@@ -181,6 +181,8 @@ export default Plugin.define({
|
||||
run: () =>
|
||||
context.ui.dialog.show(() => <input id="ahead" focused onInput={(value: string) => log({ ahead: value })} />),
|
||||
},
|
||||
// A key that opens a dialog of its own with nothing to focus, which the user types past at once (big-boi ds-3).
|
||||
{ id: "v2probe.plain", bind: "f3", run: () => context.ui.dialog.show(() => <text>Plain dialog</text>) },
|
||||
{ id: "v2probe.relayer", run: () => context.keymap.layer(() => ({ commands: [{ id: "v2probe.relayered", run() {} }] })) },
|
||||
{
|
||||
id: "v2probe.color",
|
||||
|
||||
@@ -74,9 +74,11 @@ pub struct Owner {
|
||||
dialog: Cell<bool>,
|
||||
/// Brings a widget of the mount's tree into view in the scrolls of the tree that hold it.
|
||||
reveal: RefCell<Option<Reveal>>,
|
||||
/// What waits for a node of the mount to take the focus, or for the mount to end: input typed while a dialog
|
||||
/// surface opened goes on then (big-boi ds-3).
|
||||
/// What waits for the mount's first tree of the plugin's, and the focus it asked for with it, or for the mount's
|
||||
/// end: input typed while a dialog surface opened goes on then (big-boi ds-3).
|
||||
focus_waiters: RefCell<Vec<Box<dyn FnOnce()>>>,
|
||||
/// The mount showed a tree of the plugin's, not only the pipe's stand-in.
|
||||
viewed: Cell<bool>,
|
||||
/// When a dialog surface's mount began: input waits for its focus only that soon after ([`OPENING`]).
|
||||
born: Cell<Option<std::time::Instant>>,
|
||||
layers: RefCell<Vec<Disposer>>,
|
||||
@@ -292,6 +294,7 @@ impl Owner {
|
||||
dialog: Cell::new(false),
|
||||
reveal: RefCell::default(),
|
||||
focus_waiters: RefCell::default(),
|
||||
viewed: Cell::new(false),
|
||||
born: Cell::new(None),
|
||||
layers: RefCell::default(),
|
||||
mode: RefCell::default(),
|
||||
@@ -347,7 +350,8 @@ impl Owner {
|
||||
!self.ids.borrow().is_empty()
|
||||
}
|
||||
|
||||
/// Runs `then` once a node of the mount takes the focus, or once the mount ends.
|
||||
/// Runs `then` once a node of the mount takes the focus, a view of the plugin's tree settled the focus it asked
|
||||
/// for, or the mount ends.
|
||||
pub(crate) fn when_focused(&self, then: Box<dyn FnOnce()>) {
|
||||
self.focus_waiters.borrow_mut().push(then);
|
||||
}
|
||||
@@ -724,7 +728,14 @@ impl Owner {
|
||||
/// mount's view that comes later never applies it (big-boi's G2 of step B).
|
||||
/// A focus the user moved meanwhile, a click or the host's own, is dropped too (carbon c2b-2): the user's choice
|
||||
/// stands.
|
||||
pub(crate) fn settle_pending(&self, host: &dyn Host) {
|
||||
/// `shows`: the view is the plugin's tree, not the pipe's stand-in before its first.
|
||||
pub(crate) fn settle_pending(&self, host: &dyn Host, shows: bool) {
|
||||
// The plugin's tree is here, and the focus it asked for settles now: input that waits for it goes on, after
|
||||
// (its waiters go after the turn). The pipe's stand-in before the first tree is no answer.
|
||||
if shows {
|
||||
self.viewed.set(true);
|
||||
self.focused_now();
|
||||
}
|
||||
let Some(Pending { id, from }) = self.pending.borrow_mut().take() else { return };
|
||||
if !self.has(&id) {
|
||||
log::warn!("plugin {}: {id:?} is not a node of its mount's next view", self.plugin);
|
||||
@@ -1141,15 +1152,15 @@ impl Router {
|
||||
self.offered(host)
|
||||
}
|
||||
|
||||
/// The mount of a plugin's dialog surface on top, opened less than [`OPENING`] ago, whose nodes have not taken the
|
||||
/// focus yet: its first tree and focus may still come (the JS host's next revisions).
|
||||
/// The mount of a plugin's dialog surface on top, opened less than [`OPENING`] ago, that has shown no tree of the
|
||||
/// plugin's yet: its tree and the focus it asks for come with the JS host's next revisions.
|
||||
pub(crate) fn opening(&self, host: &dyn Host) -> Option<Rc<Owner>> {
|
||||
let owners: Vec<Rc<Owner>> = self.owners.borrow().iter().filter_map(Weak::upgrade).collect();
|
||||
owners.into_iter().rev().find(|owner| {
|
||||
owner.alive()
|
||||
&& owner.born.get().is_some_and(|born| born.elapsed() < OPENING)
|
||||
&& !owner.viewed.get()
|
||||
&& owner.on_top(host)
|
||||
&& owner.focused().node.is_none()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -2121,7 +2121,7 @@ impl Viewer for Remote {
|
||||
(viewing.give)(handle, bytes.0);
|
||||
}
|
||||
reply.released.into_iter().for_each(|handle| (viewing.release)(handle));
|
||||
Some(Viewed { node: Some(reply.node), memo: Some(reply.memo) })
|
||||
Some(Viewed { node: Some(reply.node), memo: Some(reply.memo), stand_in: false })
|
||||
}
|
||||
|
||||
fn draws(&self) -> bool {
|
||||
|
||||
@@ -1374,7 +1374,7 @@ impl surface::Viewer for Viewer {
|
||||
let mount = mounts.get_mut(id)?;
|
||||
mount.shown = true;
|
||||
let height = self.heights.borrow().get(&id.key).copied().unwrap_or(1);
|
||||
return Some(Viewed { node: Some(placeholder(height)), memo: Some(0) });
|
||||
return Some(Viewed { node: Some(placeholder(height)), memo: Some(0), stand_in: true });
|
||||
}
|
||||
// The height it was laid out at, not the input's, which stands in the screen's before a solve (review carbon-7).
|
||||
if let (true, Some(laid)) = (real, viewing.laid) {
|
||||
@@ -1392,7 +1392,7 @@ impl surface::Viewer for Viewer {
|
||||
drop(mounts);
|
||||
let error = "the host asked for a whole tree".to_owned();
|
||||
guest.send(Down::Refused { id: guest.id().to_owned(), mount: id.clone(), rev, adopted, error }, None);
|
||||
return Some(Viewed { node: None, memo: None });
|
||||
return Some(Viewed { node: None, memo: None, stand_in: false });
|
||||
}
|
||||
if let Some(tree) = mount.pending.take() {
|
||||
let tree = *tree;
|
||||
@@ -1447,15 +1447,15 @@ impl surface::Viewer for Viewer {
|
||||
viewer.settle(&id, true);
|
||||
}
|
||||
});
|
||||
return Some(Viewed { node: Some(node), memo: Some(tree.memo) });
|
||||
return Some(Viewed { node: Some(node), memo: Some(tree.memo), stand_in: false });
|
||||
}
|
||||
if !mount.shown {
|
||||
mount.shown = true;
|
||||
let height = self.heights.borrow().get(&id.key).copied().unwrap_or(1);
|
||||
let _ = host;
|
||||
return Some(Viewed { node: Some(placeholder(height)), memo: Some(0) });
|
||||
return Some(Viewed { node: Some(placeholder(height)), memo: Some(0), stand_in: true });
|
||||
}
|
||||
Some(Viewed { node: None, memo: None })
|
||||
Some(Viewed { node: None, memo: None, stand_in: false })
|
||||
}
|
||||
|
||||
fn waits_for_room(&self) -> bool {
|
||||
|
||||
@@ -801,8 +801,9 @@ struct Hold {
|
||||
input: VecDeque<(Held, Past)>,
|
||||
/// Input put back now, which the intercepts let through.
|
||||
replaying: bool,
|
||||
/// A plugin's dialog surface just opened and none of its nodes took the focus yet: the input waits for that focus
|
||||
/// (or the mount's end, or [`crate::interact::OPENING`]), so what the user typed as it opened is its field's
|
||||
/// A plugin's dialog surface just opened and showed no tree of the plugin's yet: the input waits for that tree and
|
||||
/// the focus it asks for (or the mount's end, or [`crate::interact::OPENING`]), so what the user typed as it opened
|
||||
/// is its field's
|
||||
/// (big-boi ds-3). The number tells this wait from an earlier one, and the timer is its deadline.
|
||||
opening: Option<(u64, TimerId)>,
|
||||
openings: u64,
|
||||
@@ -1074,6 +1075,13 @@ impl Scripts {
|
||||
self.waiting.borrow().len()
|
||||
}
|
||||
|
||||
/// Whether input waits for a call, its effects, or a dialog that opened.
|
||||
#[doc(hidden)]
|
||||
pub fn holds_input(&self) -> bool {
|
||||
let hold = self.hold.borrow();
|
||||
hold.holding() || !hold.input.is_empty()
|
||||
}
|
||||
|
||||
/// The types of what the JS host sent while the tests hold it ([`Scripts::hold_answers`]), in order.
|
||||
#[doc(hidden)]
|
||||
pub fn held_types(&self) -> Vec<String> {
|
||||
@@ -2457,9 +2465,9 @@ impl Scripts {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the input waits for a plugin's dialog surface on top to focus its field: one that just opened, whose first
|
||||
/// tree and focus come with the JS host's next revisions. It goes on once a node of that dialog takes the focus, the
|
||||
/// dialog's mount ends, or [`crate::interact::OPENING`] passes. `incoming`: input comes now, which the wait takes.
|
||||
/// Whether the input waits for a plugin's dialog surface on top to show its first tree: one that just opened, whose
|
||||
/// tree and focus come with the JS host's next revisions. It goes on once that tree showed and the focus it asked
|
||||
/// for settled, the dialog's mount ends, or [`crate::interact::OPENING`] passes. `incoming`: input comes now, which the wait takes.
|
||||
fn await_opening(&self, host: &dyn Host, incoming: bool) -> bool {
|
||||
{
|
||||
let hold = self.hold.borrow();
|
||||
|
||||
@@ -154,6 +154,8 @@ pub(crate) struct Viewed {
|
||||
pub node: Option<Node>,
|
||||
/// The values the memo table of a library holds after the view; `None` for a table in the host.
|
||||
pub memo: Option<usize>,
|
||||
/// The node stands in for the plugin's first tree, which it has not given yet (the pipe's placeholder).
|
||||
pub stand_in: bool,
|
||||
}
|
||||
|
||||
/// A view in process: the plugin's own function, as one call of it, on the mount's memo table in the host; and the
|
||||
@@ -183,7 +185,7 @@ impl Viewer for Linked {
|
||||
node = Some((self.view)(&mut v2::View::new(read, input, mount, memo, &mut content, release)));
|
||||
});
|
||||
called.then_some(())?;
|
||||
Some(Viewed { node: Some(node?), memo: None })
|
||||
Some(Viewed { node: Some(node?), memo: None, stand_in: false })
|
||||
}
|
||||
|
||||
fn draws(&self) -> bool {
|
||||
@@ -576,6 +578,8 @@ struct Called {
|
||||
memo: v2::Memo,
|
||||
/// The values a library's own table holds after the call; `None` when the table is `memo`.
|
||||
remote: Option<usize>,
|
||||
/// [`Viewed::stand_in`].
|
||||
stand_in: bool,
|
||||
}
|
||||
|
||||
/// The content a view gave, which goes with its tree when the host refuses it.
|
||||
@@ -783,9 +787,10 @@ impl Surface {
|
||||
/// Settles a focus the mount's calls asked for before its tree held the node, with this view, the mount's next one
|
||||
/// that gives a tree ([`Owner::settle_pending`]). A view that gives none (the pipe's, while the JS host builds the
|
||||
/// revision) leaves it waiting; the last tree adopted again in a new theme ([`Surface::restyle`]) is no view.
|
||||
fn settle_pending(&self, host: &dyn Host) {
|
||||
/// `shows`: the view is the plugin's tree, not one standing in for it.
|
||||
fn settle_pending(&self, host: &dyn Host, shows: bool) {
|
||||
if let Some(owner) = self.holder() {
|
||||
owner.settle_pending(host);
|
||||
owner.settle_pending(host, shows);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -820,7 +825,7 @@ impl Surface {
|
||||
self.state.borrow_mut().memo = called.memo;
|
||||
return;
|
||||
};
|
||||
let (mut memo, remote) = (called.memo, called.remote);
|
||||
let (mut memo, remote, shows) = (called.memo, called.remote, !called.stand_in);
|
||||
// A keep of the last tree's root keeps the mount as it is: nothing to adopt and no frame (P6a), when the
|
||||
// pass would change nothing: no node shows what the host reads again (a tool call's output), and the tree
|
||||
// is in the theme of the moment. The memo table stays as it is: the next view discards what this one staged.
|
||||
@@ -830,7 +835,7 @@ impl Surface {
|
||||
book(&self.plugin, |ledger| ledger.memo_entries += size - state.booked);
|
||||
(state.memo, state.booked) = (memo, size);
|
||||
drop(state);
|
||||
self.settle_pending(host);
|
||||
self.settle_pending(host, shows);
|
||||
return;
|
||||
}
|
||||
let result = self.adopt(host, &theme, vec![node], &called.staged, full);
|
||||
@@ -850,7 +855,7 @@ impl Surface {
|
||||
handed.clear();
|
||||
}
|
||||
drop(handed);
|
||||
self.settle_pending(host);
|
||||
self.settle_pending(host, shows);
|
||||
return;
|
||||
}
|
||||
Err(refused) => {
|
||||
@@ -1059,7 +1064,7 @@ impl Surface {
|
||||
return None;
|
||||
};
|
||||
let staged = Staged { given: given.take() };
|
||||
Some(Called { node: viewed.node, staged, memo, remote: viewed.memo })
|
||||
Some(Called { node: viewed.node, staged, memo, remote: viewed.memo, stand_in: viewed.stand_in })
|
||||
}
|
||||
|
||||
/// Adopts `nodes`, with the content `staged` gives, or refuses them and changes nothing.
|
||||
|
||||
@@ -2161,7 +2161,8 @@ fn a_dialog_field_keeps_its_draft_across_a_reload() {
|
||||
}
|
||||
|
||||
// Expected (big-boi ds-3): keys typed right after the key that opens a JS plugin's dialog with a focused field go to that
|
||||
// field once it has the focus, in order, not to the composer under it and not lost.
|
||||
// field once it has the focus, in order, not to the composer under it and not lost. A dialog with nothing to focus
|
||||
// holds them only until its first view.
|
||||
#[test]
|
||||
fn keys_typed_as_a_dialog_opens_go_to_its_field() {
|
||||
let Some(config) = config() else { return };
|
||||
@@ -2179,4 +2180,12 @@ fn keys_typed_as_a_dialog_opens_go_to_its_field() {
|
||||
let screen = pipe.harness.app.text();
|
||||
let composed = screen.lines().any(|line| line.trim_start_matches([' ', '┃']).starts_with("ab"));
|
||||
assert!(!composed, "the keys reached the composer:\n{screen}");
|
||||
// A dialog with nothing to focus lets the keys go on as its first view shows, not after the wait's deadline (each
|
||||
// turn of a settle is a millisecond of the app's clock).
|
||||
pipe.harness.app.press("f3");
|
||||
pipe.harness.app.type_text("x");
|
||||
pipe.settle();
|
||||
let screen = pipe.harness.app.text();
|
||||
assert!(screen.contains("Plain dialog"), "{screen}");
|
||||
assert!(!pipe.scripts().holds_input(), "the keys wait for a dialog with nothing to focus");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user