Skip to content

Commit 85c64e1

Browse files
committed
Try keybindings if no input handler is accepting text input
1 parent f0065a3 commit 85c64e1

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

crates/editor/src/editor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23964,6 +23964,10 @@ impl EntityInputHandler for Editor {
2396423964
let utf16_offset = anchor.to_offset_utf16(&position_map.snapshot.buffer_snapshot());
2396523965
Some(utf16_offset.0)
2396623966
}
23967+
23968+
fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
23969+
self.input_enabled
23970+
}
2396723971
}
2396823972

2396923973
trait SelectionExt {

crates/gpui/src/input.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ pub trait EntityInputHandler: 'static + Sized {
7070
window: &mut Window,
7171
cx: &mut Context<Self>,
7272
) -> Option<usize>;
73+
74+
/// See [`InputHandler::accepts_text_input`] for details
75+
fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
76+
true
77+
}
7378
}
7479

7580
/// The canonical implementation of [`crate::PlatformInputHandler`]. Call [`Window::handle_input`]
@@ -177,4 +182,9 @@ impl<V: EntityInputHandler> InputHandler for ElementInputHandler<V> {
177182
view.character_index_for_point(point, window, cx)
178183
})
179184
}
185+
186+
fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
187+
self.view
188+
.update(cx, |view, cx| view.accepts_text_input(window, cx))
189+
}
180190
}

crates/gpui/src/platform.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,14 @@ impl PlatformInputHandler {
974974
.ok()
975975
.flatten()
976976
}
977+
978+
#[allow(dead_code)]
979+
pub fn accepts_text_input(&mut self) -> bool {
980+
self.cx
981+
.update(|window, cx| self.handler.accepts_text_input(window, cx))
982+
.ok()
983+
.unwrap_or(true)
984+
}
977985
}
978986

979987
/// A struct representing a selection in a text buffer, in UTF16 characters.
@@ -1082,6 +1090,11 @@ pub trait InputHandler: 'static {
10821090
fn apple_press_and_hold_enabled(&mut self) -> bool {
10831091
true
10841092
}
1093+
1094+
/// Returns whether this handler is accepting text input to be inserted.
1095+
fn accepts_text_input(&mut self, _window: &mut Window, _cx: &mut App) -> bool {
1096+
true
1097+
}
10851098
}
10861099

10871100
/// The variables that can be configured when creating a new window

crates/gpui/src/platform/windows/events.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(crate) const WM_GPUI_FORCE_UPDATE_WINDOW: u32 = WM_USER + 5;
2727
pub(crate) const WM_GPUI_KEYBOARD_LAYOUT_CHANGED: u32 = WM_USER + 6;
2828
pub(crate) const WM_GPUI_GPU_DEVICE_LOST: u32 = WM_USER + 7;
2929
pub(crate) const WM_GPUI_KEYDOWN: u32 = WM_USER + 8;
30+
pub(crate) const WM_GPUI_ACCEPTS_TEXT_INPUT: u32 = WM_USER + 9;
3031

3132
const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
3233
const AUTO_HIDE_TASKBAR_THICKNESS_PX: i32 = 1;
@@ -106,6 +107,7 @@ impl WindowsWindowInner {
106107
WM_GPUI_CURSOR_STYLE_CHANGED => self.handle_cursor_changed(lparam),
107108
WM_GPUI_FORCE_UPDATE_WINDOW => self.draw_window(handle, true),
108109
WM_GPUI_GPU_DEVICE_LOST => self.handle_device_lost(lparam),
110+
WM_GPUI_ACCEPTS_TEXT_INPUT => self.has_input_handler(),
109111
_ => None,
110112
};
111113
if let Some(n) = handled {
@@ -1131,6 +1133,15 @@ impl WindowsWindowInner {
11311133
Some(0)
11321134
}
11331135

1136+
fn has_input_handler(&self) -> Option<isize> {
1137+
let mut state = self.state.borrow_mut();
1138+
let has_enabled_handler = state
1139+
.input_handler
1140+
.as_mut()
1141+
.map_or(false, |handler| handler.accepts_text_input());
1142+
Some(if has_enabled_handler { 1 } else { 0 })
1143+
}
1144+
11341145
#[inline]
11351146
fn draw_window(&self, handle: HWND, force_render: bool) -> Option<isize> {
11361147
let mut request_frame = self.state.borrow_mut().callbacks.request_frame.take()?;

crates/gpui/src/platform/windows/platform.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn translate_accelerator(msg: &MSG) -> Option<()> {
328328
state_no_modifiers[VK_RWIN.0 as usize] = 0;
329329

330330
let mut buffer_c_no_modifiers = [0u16; 8];
331-
let result_c_no_modifers = unsafe {
331+
let result_c_no_modifiers = unsafe {
332332
ToUnicode(
333333
vkey.0 as u32,
334334
scan_code,
@@ -338,15 +338,18 @@ fn translate_accelerator(msg: &MSG) -> Option<()> {
338338
)
339339
};
340340

341-
if result_c_no_modifers <= 0 {
341+
if result_c_no_modifiers <= 0 {
342342
return dispatch_accelerator(msg);
343343
}
344344

345345
let c_no_modifiers =
346-
String::from_utf16_lossy(&buffer_c_no_modifiers[..result_c_no_modifers as usize]);
346+
String::from_utf16_lossy(&buffer_c_no_modifiers[..result_c_no_modifiers as usize]);
347347

348348
if c != c_no_modifiers {
349-
return None;
349+
let accepts_text_input = unsafe { SendMessageW(msg.hwnd, WM_GPUI_ACCEPTS_TEXT_INPUT, None, None) };
350+
if accepts_text_input.0 != 0 {
351+
return None;
352+
}
350353
}
351354

352355
dispatch_accelerator(msg)
@@ -361,7 +364,7 @@ fn dispatch_accelerator(msg: &MSG) -> Option<()> {
361364
Some(msg.lParam),
362365
)
363366
};
364-
(result == LRESULT(0)).then_some(())
367+
(result.0 == 0).then_some(())
365368
}
366369

367370
impl Platform for WindowsPlatform {

0 commit comments

Comments
 (0)