Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Fix behaviour on window focus loss
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Jul 29, 2023
1 parent 1d20b21 commit 6c36415
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/backend/shared/xkb/xkb_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,7 @@ impl KeyEventsState {
CompositionResult::Finished(&self.compose_string)
}
xkb_compose_status::XKB_COMPOSE_CANCELLED => {
// Clearing the compose string and other state isn't needed,
// as it is cleared at the start of the next composition
self.is_composing = false;
if self.previous_was_compose {
self.compose_string.pop();
}
CompositionResult::Cancelled(&self.compose_string)
CompositionResult::Cancelled(self.cancelled_string())
}
xkb_compose_status::XKB_COMPOSE_NOTHING => {
assert!(!self.is_composing);
Expand All @@ -491,6 +485,16 @@ impl KeyEventsState {
}
}

pub fn cancelled_string(&mut self) -> &str {
// Clearing the compose string and other state isn't needed,
// as it is cleared at the start of the next composition
self.is_composing = false;
if self.previous_was_compose {
self.compose_string.pop();
}
&self.compose_string
}

fn compose_handle_backspace(
&mut self,
compose_state: NonNull<xkb_compose_state>,
Expand Down
11 changes: 8 additions & 3 deletions src/backend/x11/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,15 @@ impl AppInner {
w.handle_got_focus();
}
Event::FocusOut(ev) => {
let w = self
.window(ev.event)
let mut state = borrow_mut!(self.state)?;
let w = state
.windows
.get(&ev.event)
.cloned()
.ok_or_else(|| anyhow!("No window with id {}", ev.event))
.context("FOCUS_OUT - failed to get window")?;
w.handle_lost_focus();

w.handle_lost_focus(&mut state.xkb_state);
}
Event::Error(e) => {
// TODO: if an error is caused by the present extension, disable it and fall back
Expand Down
23 changes: 21 additions & 2 deletions src/backend/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,27 @@ impl Window {
self.with_handler(|h| h.got_focus());
}

pub fn handle_lost_focus(&self) {
self.with_handler(|h| h.lost_focus());
pub fn handle_lost_focus(&self, xkb_state: &mut KeyEventsState) {
self.with_handler(|h| {
h.lost_focus();
let active = self.active_text_field.get();
if let Some(field) = active {
if xkb_state.cancel_composing() {
let mut ime = h.acquire_input_lock(field, true);
let range = ime.composition_range();
// If we were composing, a composition range must have been set.
// To be safe, avoid unwrapping it anyway
if let Some(range) = range {
// replace_range resets the composition string
ime.replace_range(range, xkb_state.cancelled_string());
} else {
ime.set_composition_range(None);
}

h.release_input_lock(field);
}
}
});
}

pub fn handle_client_message(&self, client_message: &xproto::ClientMessageEvent) {
Expand Down

0 comments on commit 6c36415

Please sign in to comment.