Skip to content

Commit

Permalink
Fix handling of changed KeyFocus via mouse
Browse files Browse the repository at this point in the history
Also improvements to SelFocus, NavFocus code.
  • Loading branch information
dhardy committed Dec 3, 2023
1 parent 03678d0 commit f132619
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
6 changes: 3 additions & 3 deletions crates/kas-core/src/event/cx/cx_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl EventState {
#[inline]
pub fn request_key_focus(&mut self, target: Id, source: FocusSource) {
self.pending_sel_focus = Some(PendingSelFocus {
target,
target: Some(target),
key_focus: true,
source,
});
Expand All @@ -421,13 +421,13 @@ impl EventState {
#[inline]
pub fn request_sel_focus(&mut self, target: Id, source: FocusSource) {
if let Some(ref pending) = self.pending_sel_focus {
if pending.target == target {
if pending.target.as_ref() == Some(&target) {
return;
}
}

self.pending_sel_focus = Some(PendingSelFocus {
target,
target: Some(target),
key_focus: false,
source,
});
Expand Down
9 changes: 5 additions & 4 deletions crates/kas-core/src/event/cx/cx_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ impl EventState {
}
}

if let Some(pending) = cx.pending_sel_focus.take() {
cx.set_sel_focus(win.as_node(data), pending);
}

match std::mem::take(&mut cx.pending_nav_focus) {
PendingNavFocus::None => (),
PendingNavFocus::Set { target, source } => {
Expand All @@ -216,6 +212,11 @@ impl EventState {
} => cx.next_nav_focus_impl(win.as_node(data), target, reverse, source),
}

// Update sel focus after nav focus:
if let Some(pending) = cx.pending_sel_focus.take() {
cx.set_sel_focus(win.as_node(data), pending);
}

while let Some((id, cmd)) = cx.pending_cmds.pop_front() {
log::trace!(target: "kas_core::event", "sending pending command {cmd:?} to {id}");
cx.send_event(win.as_node(data), id, Event::Command(cmd, None));
Expand Down
73 changes: 32 additions & 41 deletions crates/kas-core/src/event/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ struct PanGrab {
coords: [(Coord, Coord); MAX_PAN_GRABS],
}

#[derive(Debug)]
struct PendingSelFocus {
target: Id,
target: Option<Id>,
key_focus: bool,
source: FocusSource,
}
Expand Down Expand Up @@ -233,6 +234,22 @@ impl EventState {
}
}

fn clear_key_focus(&mut self) {
if self.key_focus {
if let Some(ref mut pending) = self.pending_sel_focus {
if pending.target == self.sel_focus {
pending.key_focus = false;
}
} else {
self.pending_sel_focus = Some(PendingSelFocus {
target: None,
key_focus: false,
source: FocusSource::Synthetic,
});
}
}
}

fn set_pan_on(
&mut self,
id: Id,
Expand Down Expand Up @@ -607,12 +624,10 @@ impl<'a> EventCx<'a> {
source,
} = pending;

log::trace!("set_sel_focus: target={target}, key_focus={key_focus}");
// The widget probably already has nav focus, but anyway:
self.set_nav_focus(target.clone(), FocusSource::Synthetic);
log::trace!("set_sel_focus: target={target:?}, key_focus={key_focus}");

if target == self.sel_focus {
self.key_focus = self.key_focus || key_focus;
self.key_focus = target.is_some() && (self.key_focus || key_focus);
return;
}

Expand All @@ -627,11 +642,16 @@ impl<'a> EventCx<'a> {
}

self.key_focus = key_focus;
self.sel_focus = Some(target.clone());
self.sel_focus = target.clone();

self.send_event(widget.re(), target.clone(), Event::SelFocus(source));
if key_focus {
self.send_event(widget, target, Event::KeyFocus);
if let Some(id) = target {
// The widget probably already has nav focus, but anyway:
self.set_nav_focus(id.clone(), FocusSource::Synthetic);

self.send_event(widget.re(), id.clone(), Event::SelFocus(source));
if key_focus {
self.send_event(widget, id, Event::KeyFocus);
}
}
}

Expand All @@ -641,16 +661,13 @@ impl<'a> EventCx<'a> {
return;
}

self.clear_key_focus();

if let Some(old) = self.nav_focus.take() {
self.action(&old, Action::REDRAW);
self.send_event(widget.re(), old, Event::LostNavFocus);
}

if let Some(id) = self.key_focus() {
self.key_focus = false;
self.send_event(widget.re(), id, Event::LostKeyFocus);
}

self.nav_focus = target.clone();
log::debug!(target: "kas_core::event", "nav_focus = {target:?}");
if let Some(id) = target {
Expand Down Expand Up @@ -702,32 +719,6 @@ impl<'a> EventCx<'a> {
opt_id = self.nav_next(widget.re(), None, advance);
}

log::trace!(
target: "kas_core::event",
"next_nav_focus: nav_focus={opt_id:?}",
);
if opt_id == self.nav_focus {
return;
}

if let Some(old) = self.nav_focus.take() {
self.redraw(&old);
self.send_event(widget.re(), old, Event::LostNavFocus);
}

if let Some(id) = self.key_focus() {
self.key_focus = false;
self.send_event(widget.re(), id, Event::LostKeyFocus);
}

self.nav_focus = opt_id.clone();
if let Some(id) = opt_id {
log::debug!(target: "kas_core::event", "nav_focus = Some({id})");
self.redraw(id.clone());
self.send_event(widget, id, Event::NavFocus(source));
} else {
log::debug!(target: "kas_core::event", "nav_focus = None");
// Most likely an error occurred
}
self.set_nav_focus_impl(widget, opt_id, source);
}
}
2 changes: 1 addition & 1 deletion crates/kas-widgets/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl_scope! {
if !self.has_key_focus {
cx.request_key_focus(self.id(), source);
}
if !self.class.multi_line() {
if source == FocusSource::Key && !self.class.multi_line() {
self.selection.clear();
self.selection.set_edit_pos(self.text.str_len());
cx.redraw(self);
Expand Down

0 comments on commit f132619

Please sign in to comment.