documents: an open block stays open while an overlay borrows focus
A menu or dialog took focus, the open block wrote back and closed, and the overlay handed focus back to a closed input: its command reached nothing and the selection was gone. The block now waits for the overlay to let focus go, and writes back only when focus lands elsewhere. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
6b033f3eb0
commit
6d90351423
7 files changed
+150
-15
No files matched your search
@@ -10,6 +10,7 @@ use crate::{
|
||||
modes::{at_edge, joint},
|
||||
},
|
||||
forms::{InputEvent, TextInput},
|
||||
primitives::lent,
|
||||
};
|
||||
|
||||
/// Where an opened block's caret lands.
|
||||
@@ -114,29 +115,56 @@ impl MarkdownDocument {
|
||||
}
|
||||
});
|
||||
window.focus(&input.focus_handle(cx), cx);
|
||||
let subscriptions =
|
||||
vec![
|
||||
cx.subscribe(&input, |document, _, event: &InputEvent, cx| match event {
|
||||
InputEvent::Blur => {
|
||||
document.commit(cx);
|
||||
}
|
||||
InputEvent::Changed => {
|
||||
document.typed += 1;
|
||||
cx.emit(DocumentEvent::Changed)
|
||||
}
|
||||
_ => {}
|
||||
}),
|
||||
];
|
||||
let subscriptions = vec![cx.subscribe_in(
|
||||
&input,
|
||||
window,
|
||||
|document, _, event: &InputEvent, window, cx| match event {
|
||||
InputEvent::Blur if lent(window, cx) => document.lend(window, cx),
|
||||
InputEvent::Blur => {
|
||||
document.commit(cx);
|
||||
}
|
||||
InputEvent::Changed => {
|
||||
document.typed += 1;
|
||||
cx.emit(DocumentEvent::Changed)
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
)];
|
||||
log::info!("markdown document: block {range:?} opens");
|
||||
self.open = Some(Open {
|
||||
range,
|
||||
input,
|
||||
append,
|
||||
lent: None,
|
||||
_subscriptions: subscriptions,
|
||||
});
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Keeps the block open while an overlay holds focus.
|
||||
fn lend(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let (Some(open), Some(holder)) = (self.open.as_mut(), window.focused(cx)) else {
|
||||
return;
|
||||
};
|
||||
log::info!("markdown document: an overlay borrows the open block's focus");
|
||||
let watch = cx.on_focus_out(&holder, window, |document, _, window, cx| {
|
||||
let back = (document.open.as_ref())
|
||||
.is_some_and(|open| open.input.focus_handle(cx).is_focused(window));
|
||||
match (back, lent(window, cx)) {
|
||||
(true, _) => {
|
||||
if let Some(open) = document.open.as_mut() {
|
||||
open.lent = None;
|
||||
}
|
||||
}
|
||||
(false, true) => document.lend(window, cx),
|
||||
(false, false) => {
|
||||
document.commit(cx);
|
||||
}
|
||||
}
|
||||
});
|
||||
open.lent = Some(watch);
|
||||
}
|
||||
|
||||
/// Writes the open block as one step; the edit made.
|
||||
pub(super) fn commit(&mut self, cx: &mut Context<Self>) -> Option<(Range<usize>, usize)> {
|
||||
let open = self.open.take()?;
|
||||
|
||||
@@ -80,6 +80,8 @@ struct Open {
|
||||
input: Entity<TextInput>,
|
||||
/// Opened past the end, to add a block.
|
||||
append: bool,
|
||||
/// Watches an overlay that borrowed focus.
|
||||
lent: Option<Subscription>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
use gpui::{
|
||||
AppContext as _, Context, Entity, FocusHandle, InteractiveElement as _, IntoElement,
|
||||
KeyUpEvent, Keystroke, Menu, MenuItem, Modifiers, ParentElement as _, Render, Styled as _,
|
||||
TestAppContext, VisualTestContext, Window, actions, div, point, px,
|
||||
};
|
||||
|
||||
use super::{MarkdownMode, opened, press, text};
|
||||
use crate::{
|
||||
documents::{InlineFormat, MarkdownDocument},
|
||||
menus::MenuBar,
|
||||
};
|
||||
|
||||
actions!(lend_tests, [Bold, Away]);
|
||||
|
||||
/// A menu bar over a document, as an app lays them out.
|
||||
struct Host {
|
||||
document: Entity<MarkdownDocument>,
|
||||
away: FocusHandle,
|
||||
}
|
||||
|
||||
impl Render for Host {
|
||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
.on_action(cx.listener(|host, _: &Bold, _, cx| {
|
||||
(host
|
||||
.document
|
||||
.update(cx, |document, cx| document.format(InlineFormat::Strong, cx)))
|
||||
.expect("formatted");
|
||||
}))
|
||||
.on_action(cx.listener(|host, _: &Away, window, cx| window.focus(&host.away, cx)))
|
||||
.child(MenuBar::new("bar", cx.get_menus().expect("menus")))
|
||||
.child(div().track_focus(&self.away).h(px(20.0)))
|
||||
.child(self.document.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn host(cx: &mut TestAppContext) -> (Entity<MarkdownDocument>, &mut VisualTestContext) {
|
||||
cx.update(crate::init_for_tests);
|
||||
cx.update(|cx| {
|
||||
cx.set_menus(vec![Menu {
|
||||
name: "Format".into(),
|
||||
items: vec![
|
||||
MenuItem::action("Strong", Bold),
|
||||
MenuItem::action("Away", Away),
|
||||
],
|
||||
disabled: false,
|
||||
}])
|
||||
});
|
||||
let (host, cx) = cx.add_window_view(|window, cx| Host {
|
||||
document: cx
|
||||
.new(|cx| MarkdownDocument::new("alpha beta\n", MarkdownMode::Visual, window, cx)),
|
||||
away: cx.focus_handle(),
|
||||
});
|
||||
cx.update(|window, _| window.activate_window());
|
||||
cx.run_until_parked();
|
||||
let document = host.read_with(cx, |host, _| host.document.clone());
|
||||
press(0, Modifiers::none(), cx);
|
||||
document.update(cx, |document, cx| {
|
||||
let input = document.open.as_ref().expect("opened").input.clone();
|
||||
input.update(cx, |input, cx| input.select(0..5, cx));
|
||||
});
|
||||
(document, cx)
|
||||
}
|
||||
|
||||
/// Opens the bar's only menu and runs row `ix`.
|
||||
fn choose(ix: usize, cx: &mut VisualTestContext) {
|
||||
cx.simulate_click(point(px(12.0), px(10.0)), Modifiers::none());
|
||||
cx.run_until_parked();
|
||||
for _ in 0..=ix {
|
||||
cx.simulate_keystrokes("down");
|
||||
cx.run_until_parked();
|
||||
}
|
||||
cx.simulate_keystrokes("enter");
|
||||
cx.simulate_event(KeyUpEvent {
|
||||
keystroke: Keystroke::parse("enter").expect("a key"),
|
||||
});
|
||||
cx.run_until_parked();
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_menu_acts_on_the_open_block_selection(cx: &mut TestAppContext) {
|
||||
let (document, cx) = host(cx);
|
||||
choose(0, cx);
|
||||
assert_eq!(opened(&document, cx), "**alpha** beta");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_menu_that_moves_focus_away_closes_the_block(cx: &mut TestAppContext) {
|
||||
let (document, cx) = host(cx);
|
||||
choose(1, cx);
|
||||
let open = document.read_with(cx, |document, _| document.open.is_some());
|
||||
assert!(!open, "the block wrote back");
|
||||
assert_eq!(text(&document, cx), "alpha beta\n");
|
||||
}
|
||||
@@ -370,6 +370,7 @@ mod block;
|
||||
mod block_edges;
|
||||
mod format;
|
||||
mod history;
|
||||
mod lend;
|
||||
mod links;
|
||||
mod look;
|
||||
mod place;
|
||||
|
||||
@@ -210,6 +210,14 @@ pub(crate) fn take_focus(
|
||||
state
|
||||
}
|
||||
|
||||
/// Whether an open overlay holds the window's focus.
|
||||
pub(crate) fn lent(window: &Window, cx: &App) -> bool {
|
||||
cx.try_global::<Holders>().is_some_and(|holders| {
|
||||
(holders.0.iter())
|
||||
.any(|holder| holder.takeover.upgrade().is_some() && holder.focus.is_focused(window))
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns focus to whatever held it before the overlay opened.
|
||||
pub(crate) fn give_back(state: &Entity<Takeover>, window: &mut Window, cx: &mut App) {
|
||||
let closed = state.downgrade();
|
||||
|
||||
@@ -25,7 +25,8 @@ pub use divider::Divider;
|
||||
pub use files::IconTheme;
|
||||
pub(crate) use files::{file_icon, icon_theme};
|
||||
pub(crate) use focus::{
|
||||
CONTEXT as FOCUS_CONTEXT, Takeover, give_back, hand_back, hold_focus, tab_stop, take_focus,
|
||||
CONTEXT as FOCUS_CONTEXT, Takeover, give_back, hand_back, hold_focus, lent, tab_stop,
|
||||
take_focus,
|
||||
};
|
||||
pub use focus::{FocusNext, FocusPrev, FocusRing, FocusScope};
|
||||
pub use ghost::DragGhost;
|
||||
|
||||
Reference in new issue
Block a user