windows: Make IME input on Windows consistent with that on macOS + some small fixs (#13386)

### Before

![Screenshot 2024-06-22
180000](https://github.com/zed-industries/zed/assets/14981363/9efc3ccd-553c-4fe8-ada5-1b54f80bfbd5)

### After

![Screenshot 2024-06-22
175850](https://github.com/zed-industries/zed/assets/14981363/172675ca-9fda-45ae-bd97-59c33573a766)


Release Notes:

- N/A
This commit is contained in:
张小白 2024-06-25 10:55:15 +08:00 committed by GitHub
parent 76ab9e4d66
commit 328d98dddc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -267,14 +267,8 @@ fn handle_syskeydown_msg(
) -> Option<isize> { ) -> Option<isize> {
// we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}` // we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}`
// shortcuts. // shortcuts.
let Some(keystroke) = parse_syskeydown_msg_keystroke(wparam) else { let keystroke = parse_syskeydown_msg_keystroke(wparam)?;
return None; let mut func = state_ptr.state.borrow_mut().callbacks.input.take()?;
};
let mut lock = state_ptr.state.borrow_mut();
let Some(mut func) = lock.callbacks.input.take() else {
return None;
};
drop(lock);
let event = KeyDownEvent { let event = KeyDownEvent {
keystroke, keystroke,
is_held: lparam.0 & (0x1 << 30) > 0, is_held: lparam.0 & (0x1 << 30) > 0,
@ -292,14 +286,8 @@ fn handle_syskeydown_msg(
fn handle_syskeyup_msg(wparam: WPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> { fn handle_syskeyup_msg(wparam: WPARAM, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
// we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}` // we need to call `DefWindowProcW`, or we will lose the system-wide `Alt+F4`, `Alt+{other keys}`
// shortcuts. // shortcuts.
let Some(keystroke) = parse_syskeydown_msg_keystroke(wparam) else { let keystroke = parse_syskeydown_msg_keystroke(wparam)?;
return None; let mut func = state_ptr.state.borrow_mut().callbacks.input.take()?;
};
let mut lock = state_ptr.state.borrow_mut();
let Some(mut func) = lock.callbacks.input.take() else {
return None;
};
drop(lock);
let event = KeyUpEvent { keystroke }; let event = KeyUpEvent { keystroke };
let result = if func(PlatformInput::KeyUp(event)).default_prevented { let result = if func(PlatformInput::KeyUp(event)).default_prevented {
Some(0) Some(0)
@ -614,35 +602,25 @@ fn handle_ime_composition(
) -> Option<isize> { ) -> Option<isize> {
let mut ime_input = None; let mut ime_input = None;
if lparam.0 as u32 & GCS_COMPSTR.0 > 0 { if lparam.0 as u32 & GCS_COMPSTR.0 > 0 {
let Some((string, string_len)) = parse_ime_compostion_string(handle) else { let (comp_string, string_len) = parse_ime_compostion_string(handle)?;
return None; let mut input_handler = state_ptr.state.borrow_mut().input_handler.take()?;
}; input_handler.replace_and_mark_text_in_range(
let mut lock = state_ptr.state.borrow_mut(); None,
let Some(mut input_handler) = lock.input_handler.take() else { &comp_string,
return None; Some(string_len..string_len),
}; );
drop(lock);
input_handler.replace_and_mark_text_in_range(None, string.as_str(), Some(0..string_len));
state_ptr.state.borrow_mut().input_handler = Some(input_handler); state_ptr.state.borrow_mut().input_handler = Some(input_handler);
ime_input = Some(string); ime_input = Some(comp_string);
} }
if lparam.0 as u32 & GCS_CURSORPOS.0 > 0 { if lparam.0 as u32 & GCS_CURSORPOS.0 > 0 {
let Some(ref comp_string) = ime_input else { let comp_string = &ime_input?;
return None;
};
let caret_pos = retrieve_composition_cursor_position(handle); let caret_pos = retrieve_composition_cursor_position(handle);
let mut lock = state_ptr.state.borrow_mut(); let mut input_handler = state_ptr.state.borrow_mut().input_handler.take()?;
let Some(mut input_handler) = lock.input_handler.take() else { input_handler.replace_and_mark_text_in_range(None, comp_string, Some(caret_pos..caret_pos));
return None;
};
drop(lock);
input_handler.replace_and_mark_text_in_range(None, comp_string, Some(0..caret_pos));
state_ptr.state.borrow_mut().input_handler = Some(input_handler); state_ptr.state.borrow_mut().input_handler = Some(input_handler);
} }
if lparam.0 as u32 & GCS_RESULTSTR.0 > 0 { if lparam.0 as u32 & GCS_RESULTSTR.0 > 0 {
let Some(comp_result) = parse_ime_compostion_result(handle) else { let comp_result = parse_ime_compostion_result(handle)?;
return None;
};
let mut lock = state_ptr.state.borrow_mut(); let mut lock = state_ptr.state.borrow_mut();
let Some(mut input_handler) = lock.input_handler.take() else { let Some(mut input_handler) = lock.input_handler.take() else {
return Some(1); return Some(1);
@ -663,11 +641,7 @@ fn handle_calc_client_size(
lparam: LPARAM, lparam: LPARAM,
state_ptr: Rc<WindowsWindowStatePtr>, state_ptr: Rc<WindowsWindowStatePtr>,
) -> Option<isize> { ) -> Option<isize> {
if !state_ptr.hide_title_bar || state_ptr.state.borrow().is_fullscreen() { if !state_ptr.hide_title_bar || state_ptr.state.borrow().is_fullscreen() || wparam.0 == 0 {
return None;
}
if wparam.0 == 0 {
return None; return None;
} }
@ -1097,13 +1071,14 @@ fn parse_syskeydown_msg_keystroke(wparam: WPARAM) -> Option<Keystroke> {
VK_NEXT => "pagedown", VK_NEXT => "pagedown",
VK_ESCAPE => "escape", VK_ESCAPE => "escape",
VK_INSERT => "insert", VK_INSERT => "insert",
VK_DELETE => "delete",
_ => return basic_vkcode_to_string(vk_code, modifiers), _ => return basic_vkcode_to_string(vk_code, modifiers),
} }
.to_owned(); .to_owned();
Some(Keystroke { Some(Keystroke {
modifiers, modifiers,
key: key, key,
ime_key: None, ime_key: None,
}) })
} }
@ -1160,7 +1135,7 @@ fn parse_keydown_msg_keystroke(wparam: WPARAM) -> Option<KeystrokeOrModifier> {
Some(KeystrokeOrModifier::Keystroke(Keystroke { Some(KeystrokeOrModifier::Keystroke(Keystroke {
modifiers, modifiers,
key: key, key,
ime_key: None, ime_key: None,
})) }))
} }