Merge pull request #892 from tylerjenningsw/fix/text-click-away

Text: stop capturing keyboard input after clicking away (#831)
This commit is contained in:
Brandon Thomas
2026-10-12 04:20:45 -04:00
4 changed files with 163 additions and 6 deletions
+4 -1
View File
@@ -1336,10 +1336,13 @@ pub fn draw_group_chrome(ui: &mut egui::Ui, g: &Group, t: &Tokens, reg: &mut cra
}
}
}
// clicking anywhere in the panel focuses it
// clicking anywhere in the panel focuses it; a press on another of its tabs waits for that
// tab's click, which focuses the panel actually chosen (a text edit stays open in Character)
let at = ui.input(|i| i.pointer.interact_pos());
if let Some(p) = active_panel
&& ui.rect_contains_pointer(g.rect)
&& ui.input(|i| i.pointer.any_pressed())
&& !out.tabs.iter().any(|(q, r)| *q != p && at.is_some_and(|a| r.contains(a)))
{
out.actions.push(DockAction::Focus(p));
}
+1
View File
@@ -274,6 +274,7 @@ impl EffectcraftApp {
}
DockAction::Focus(p) => {
self.ui.focused = p;
panels::viewer_text::panel_clicked(self, &ctx, p);
// A click in (or on the tab of) another Composition viewer makes it active.
if let Some(id) = panels::viewers::id_of(p) {
panels::viewers::activate(self, &ctx, id);
+27 -5
View File
@@ -26,6 +26,7 @@ use serde_json::{Value, json};
use super::viewer::ViewerMap;
use crate::EffectcraftApp;
use crate::dock::PanelKind;
use crate::state::Tool;
/// Pointer interaction owned by text editing (kept across frames while the button is down).
@@ -135,6 +136,21 @@ fn exec(app: &mut EffectcraftApp, id: &str, p: Value) -> Option<Value> {
}
}
/// The panels a text edit stays open across: the viewers, and Character, Paragraph and
/// Properties, which format the selected characters.
fn keeps_text_edit(p: PanelKind) -> bool {
super::viewers::id_of(p).is_some() || matches!(p, PanelKind::Character | PanelKind::Paragraph | PanelKind::Properties)
}
/// A click in panel `p`. Any other panel (the Timeline, the Project panel, …) ends the edit, as a
/// click outside the text does, and gives the keyboard back to the app (#831).
pub fn panel_clicked(app: &mut EffectcraftApp, ctx: &egui::Context, p: PanelKind) {
if app.session.state.text_edit.is_some() && !keeps_text_edit(p) {
exec(app, "text.endEdit", json!({}));
ctx.memory_mut(|m| m.surrender_focus(focus_id()));
}
}
/// Typing merge key: consecutive keystrokes fold into one undo step until the caret moves.
fn typing_key(ui: &egui::Ui, bump: bool) -> String {
let id = egui::Id::new("viewer-text-typing");
@@ -332,16 +348,18 @@ pub fn hook(
ui.memory_mut(|m| {
m.set_focus_lock_filter(focus_id(), egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, escape: true });
});
// A session started elsewhere (menu, agent, Properties) takes the keyboard once.
// A session started elsewhere (menu, agent, Properties) takes the keyboard once…
let seen = egui::Id::new("viewer-text-seen");
let key = app.session.state.text_edit.as_ref().map(|e| e.layer.0);
// …and gets it back whenever nothing else holds it (after a panel click, say).
if ui.data(|d| d.get_temp::<Option<u64>>(seen)).flatten() != key || ui.memory(|m| m.focused().is_none()) {
// …and gets it back when a click in the viewer or a formatting panel left nothing holding
// it; not merely because nothing has the focus (#831).
let regain = ui.memory(|m| m.focused().is_none()) && keeps_text_edit(app.ui.focused);
if ui.data(|d| d.get_temp::<Option<u64>>(seen)).flatten() != key || regain {
ui.data_mut(|d| d.insert_temp(seen, key));
ui.memory_mut(|m| m.request_focus(focus_id()));
}
if ui.memory(|m| m.has_focus(focus_id())) {
keyboard(app, ui);
keyboard(app, ui, resp.rect);
}
} else {
ui.data_mut(|d| d.insert_temp::<Option<u64>>(egui::Id::new("viewer-text-seen"), None));
@@ -359,12 +377,16 @@ pub fn hook(
owns
}
fn keyboard(app: &mut EffectcraftApp, ui: &mut egui::Ui) {
/// This frame's keys, in order, into the edited text, up to a press outside the `viewer`: the
/// panel it lands in decides (after this frame's panels) whether the edit ends, so the keys after
/// it in this frame are neither typed nor run as shortcuts (#831).
fn keyboard(app: &mut EffectcraftApp, ui: &mut egui::Ui, viewer: Rect) {
let events = ui.input(|i| i.events.clone());
let ctx = ui.ctx().clone();
for ev in events {
let Some(sel) = app.session.state.text_edit.clone() else { return };
match ev {
egui::Event::PointerButton { pos, pressed: true, .. } if !viewer.contains(pos) => return,
egui::Event::Text(t) if !t.chars().any(char::is_control) => {
let k = typing_key(ui, false);
exec(app, "text.insert", json!({"text": t, "merge": k}));