Skip to content

Commit 6a338ac

Browse files
committed
feat(coordinator): 临拼/混输接入音节跳转,Shift+←/→ 单字符精调
overlay 的 ←/→ 与 Ctrl+←/→ 同主输入路径的 rime 语义;Shift+←/→ 总是单字符, 用来走进音节中间。其余 overlay 没有音节,仍恒单字符。
1 parent 2b411fb commit 6a338ac

9 files changed

Lines changed: 142 additions & 32 deletions

File tree

‎docs/design/macos-style-arrow-keys.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,7 @@ macOS 的 → 在末尾是「切到下一个候选词」,本仓出厂语义是
9090
- 两端回绕(开头再 ← 到最后一个音节起点,末尾再 → 到第一个边界)。
9191
- 切分取引擎对整串缓冲的拆分(`preedit_split_body` / `preedit_codetable_body`),不随高亮候选变;
9292
不足两个音节、双拼等对不回缓冲时退回单字符。
93-
- 只在主输入路径;临拼等 overlay 仍是单字符。macOS 的 `caret_left` / `caret_right` 动词不变。
93+
- **Shift+←/→**:总是单字符,是走进音节中间做精调的通路(rime 的 `left_by_char` 对应物)。
94+
- 主输入路径与临拼 / 混输 overlay 同一套;其余 overlay(临英、URL、Email、Unicode、特殊)没有音节,恒单字符。
95+
macOS 的 `caret_left` / `caret_right` 动词不变。
9496
- `syllable_prev` / `syllable_next` 动词可绑到普通键(会话键表只认键 + Shift,不带 Ctrl)。

‎wind_input/crates/wind-coordinator/src/coordinator.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6006,6 +6006,23 @@ impl Coordinator {
60066006

60076007
/// 编码区光标移动后的返回:刷新候选窗,并带上组合区新插入符位置(与默认 ←/→ 臂同形)。
60086008
/// 只回 `Consumed` 的话宿主里的插入符不会跟着动。
6009+
/// Ctrl+←/→ 整音节跳是否启用:`session_actions` 里写了 `ctrl+left` / `ctrl+right = "none"` 即关
6010+
/// (同时热键编译器也不再向宿主转发该键)。
6011+
pub(crate) fn ctrl_syllable_enabled(&self, vk: u32) -> bool {
6012+
let name = if vk == keymap::VK_LEFT {
6013+
"ctrl+left"
6014+
} else {
6015+
"ctrl+right"
6016+
};
6017+
!self
6018+
.rt()
6019+
.config
6020+
.keys
6021+
.session_actions
6022+
.get(name)
6023+
.is_some_and(|v| v.trim().eq_ignore_ascii_case("none"))
6024+
}
6025+
60096026
/// 编码区的音节停靠点(缓冲内字节位置,见 [`preedit_cursor::syllable_stops`])。
60106027
///
60116028
/// 拆分串取引擎对**整串缓冲**的切分(`preedit_split_body` / 码表整句的 `preedit_codetable_body`),

‎wind_input/crates/wind-coordinator/src/coordinator/message_handler.rs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,17 +1641,7 @@ impl MessageHandler for Coordinator {
16411641
if data.modifiers & (MOD_SHORTCUT | MOD_SHIFT) == wind_ipc::protocol::MOD_CTRL
16421642
&& matches!(data.key_code, keymap::VK_LEFT | keymap::VK_RIGHT)
16431643
&& !state.input_buffer.is_empty()
1644-
&& !self
1645-
.rt()
1646-
.config
1647-
.keys
1648-
.session_actions
1649-
.get(if data.key_code == keymap::VK_LEFT {
1650-
"ctrl+left"
1651-
} else {
1652-
"ctrl+right"
1653-
})
1654-
.is_some_and(|v| v.trim().eq_ignore_ascii_case("none"))
1644+
&& self.ctrl_syllable_enabled(data.key_code)
16551645
{
16561646
let moved =
16571647
Self::move_caret_syllable(&mut state, data.key_code == keymap::VK_RIGHT, false);
@@ -2236,6 +2226,7 @@ impl MessageHandler for Coordinator {
22362226
// (见 `move_caret_syllable`);无切分(双拼 / 单音节 / 码表单码)自然退回单字符。
22372227
// 手机端的按音节左移(上面的 `syllable_jump`)优先,保持原样。
22382228
let smart = (syllable_jump.is_none()
2229+
&& data.modifiers & MOD_SHIFT == 0
22392230
&& matches!(data.key_code, keymap::VK_LEFT | keymap::VK_RIGHT))
22402231
.then(|| {
22412232
Self::move_caret_syllable(st, data.key_code == keymap::VK_RIGHT, true)

‎wind_input/crates/wind-coordinator/src/handle_candidate.rs‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tracing::{debug, warn};
1313
use wind_bridge::handler::{KeyAction, KeyEventData};
1414
use wind_candidate::{Candidate, CandidateMeta, CandidateSource};
1515
use wind_engine::manager::ENGLISH_SCHEMA;
16-
use wind_ipc::protocol::MOD_SHIFT;
16+
use wind_ipc::protocol::{MOD_SHIFT, MOD_SHORTCUT};
1717
use wind_keys::keymap;
1818
use wind_store::freq::FreqRecord;
1919
use wind_ui_types::CandidateOp;
@@ -3720,6 +3720,29 @@ impl Coordinator {
37203720
}
37213721
}
37223722

3723+
/// overlay 的音节停靠点:只有临拼 / 混输(显示串 `overlay_body` 带引擎拆的 `'`)有;
3724+
/// 对不上缓冲时 `None`,调用方退回单字符。
3725+
pub(crate) fn overlay_syllable_stops(state: &State) -> Option<Vec<usize>> {
3726+
let buffer = match state.active? {
3727+
ModeKind::TempPinyin => &state.temp_pinyin_buffer,
3728+
ModeKind::Mix(_) => &state.mix_buffer,
3729+
// 其余 overlay 没有音节切分,恒单字符。
3730+
ModeKind::TempEnglish
3731+
| ModeKind::Url
3732+
| ModeKind::Email
3733+
| ModeKind::Unicode
3734+
| ModeKind::Special(_)
3735+
| ModeKind::RareChar
3736+
| ModeKind::Reverse
3737+
| ModeKind::AuxCode => return None,
3738+
};
3739+
state
3740+
.overlay_body
3741+
.contains('\'')
3742+
.then(|| preedit_cursor::syllable_stops(buffer, &state.overlay_body))
3743+
.flatten()
3744+
}
3745+
37233746
/// overlay 模式的编码区光标移动(左右 / Home / End)。`None` = 该键不是光标移动键,
37243747
/// 调用方继续分派。
37253748
///
@@ -3736,13 +3759,30 @@ impl Coordinator {
37363759
) {
37373760
return None;
37383761
}
3762+
let arrow = matches!(data.key_code, keymap::VK_LEFT | keymap::VK_RIGHT);
3763+
// ←/→ 与主输入路径同一套 rime 语义(见 `move_caret_syllable`):默认「边界整音节 / 中间单字符」,
3764+
// Ctrl 总是整音节,Shift 总是单字符。只有临拼 / 混输有音节切分,其余 overlay 恒单字符。
3765+
let stops = if arrow && data.modifiers & MOD_SHIFT == 0 {
3766+
Self::overlay_syllable_stops(state)
3767+
} else {
3768+
None
3769+
};
3770+
let smart = data.modifiers & MOD_SHORTCUT == 0;
37393771
let moved = {
37403772
let mut ed = Self::overlay_buf_edit(state)?;
3741-
match data.key_code {
3742-
keymap::VK_LEFT => ed.move_left(),
3743-
keymap::VK_RIGHT => ed.move_right(),
3744-
keymap::VK_HOME => ed.home(),
3745-
_ => ed.end(),
3773+
let right = data.key_code == keymap::VK_RIGHT;
3774+
let jump = stops
3775+
.and_then(|stops| preedit_cursor::syllable_jump(&stops, ed.pos(), right, smart));
3776+
if let Some(pos) = jump {
3777+
ed.set_pos(pos);
3778+
true
3779+
} else {
3780+
match data.key_code {
3781+
keymap::VK_LEFT => ed.move_left(),
3782+
keymap::VK_RIGHT => ed.move_right(),
3783+
keymap::VK_HOME => ed.home(),
3784+
_ => ed.end(),
3785+
}
37463786
}
37473787
};
37483788
// 已在边界(含缓冲空、只剩只读前缀):吃掉不透传,否则宿主光标会跳出组合区。

‎wind_input/crates/wind-coordinator/src/handle_mode.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,16 @@ impl Coordinator {
31003100
if data.modifiers & MOD_SHORTCUT == 0 {
31013101
return None;
31023102
}
3103+
// Ctrl+←/→:临拼 / 混输里同主输入路径整音节跳(rime 同款)。其余 overlay 无音节,
3104+
// 仍按宿主快捷键处理。用户写了 `ctrl+left = "none"` 则不拦。
3105+
if has_pending
3106+
&& data.modifiers & (MOD_SHORTCUT | MOD_SHIFT) == wind_ipc::protocol::MOD_CTRL
3107+
&& matches!(data.key_code, keymap::VK_LEFT | keymap::VK_RIGHT)
3108+
&& matches!(state.active, Some(ModeKind::TempPinyin | ModeKind::Mix(_)))
3109+
&& self.ctrl_syllable_enabled(data.key_code)
3110+
{
3111+
return self.overlay_cursor_key(state, data);
3112+
}
31033113
if has_pending {
31043114
exit(self, state);
31053115
self.notify_ui_hide();

‎wind_input/crates/wind-coordinator/src/preedit_cursor.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,15 @@ impl<'a> BufEdit<'a> {
256256
}
257257
}
258258

259-
#[cfg(test)]
260259
pub(crate) fn pos(&self) -> usize {
261260
*self.cursor
262261
}
263262

263+
/// 直接落到 `pos`(调用方保证是字符边界,如 [`syllable_jump`] 的停靠点)。
264+
pub(crate) fn set_pos(&mut self, pos: usize) {
265+
*self.cursor = floor_boundary(self.buf, pos);
266+
}
267+
264268
/// 在光标处插入字符,光标随之后移(光标在末尾时等价于原来的 `push`)。
265269
pub(crate) fn insert(&mut self, ch: char) {
266270
self.insert_cased(ch, ch);

‎wind_input/crates/wind-coordinator/tests/caret_syllable_jump.rs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use std::path::PathBuf;
1212
use wind_bridge::handler::{KeyEventData, MessageHandler};
1313
use wind_config::Config;
1414
use wind_coordinator::Coordinator;
15+
use wind_ipc::protocol::MOD_SHIFT;
1516
use wind_ipc::protocol::{EVENT_KEY_DOWN, MOD_CTRL};
16-
use wind_keys::keymap::{VK_LEFT, VK_RIGHT};
17+
use wind_keys::keymap::{VK_BACKTICK, VK_LEFT, VK_RIGHT};
1718

1819
fn data_dir() -> PathBuf {
1920
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../../build_dev/data")
@@ -158,3 +159,56 @@ fn ctrl_left_without_composition_passes_through() {
158159
"没有编码时 Ctrl+← 归宿主(按词移动),实际: {act:?}"
159160
);
160161
}
162+
163+
/// Shift+←/→:总是单字符,是走到音节中间的精调通路。
164+
#[test]
165+
fn shift_arrows_step_one_char() {
166+
if !data_ready() {
167+
return;
168+
}
169+
let c = coord();
170+
type_code(&c, "nihao");
171+
c.handle_key_event(&key(VK_LEFT, MOD_SHIFT));
172+
assert_eq!(c.debug_ui_caret_bytes(), 5, "ni'ha|o:单字符");
173+
c.handle_key_event(&key(VK_LEFT, MOD_SHIFT));
174+
assert_eq!(c.debug_ui_caret_bytes(), 4, "ni'h|ao:单字符");
175+
left(&c); // 缓冲 nih| 在音节中间 → 单字符,落到 ni|hao(显示串里在 ' 之前)
176+
assert_eq!(c.debug_ui_caret_bytes(), 2, "音节中间 ← 仍是单字符");
177+
}
178+
179+
fn overlay_coord() -> std::sync::Arc<Coordinator> {
180+
let mut cfg = Config::default();
181+
cfg.schema.available = vec!["wubi86".into(), "pinyin".into()];
182+
cfg.schema.active = "wubi86".into();
183+
cfg.input.default.chinese_mode = true;
184+
Coordinator::new_headless(cfg, Some(&data_dir()))
185+
}
186+
187+
/// 临拼 overlay(`` ` `` 引导)与主路同一套:边界整音节跳、Ctrl 总是整音节、Shift 单字符。
188+
#[test]
189+
fn temp_pinyin_overlay_follows_same_rules() {
190+
if !data_ready() || !data_dir().join("schemas/wubi86.schema.toml").exists() {
191+
return;
192+
}
193+
let c = overlay_coord();
194+
c.handle_key_event(&key(VK_BACKTICK, 0));
195+
type_code(&c, "nihao");
196+
let end = c.debug_ui_caret_bytes();
197+
left(&c);
198+
assert_eq!(
199+
c.debug_ui_caret_bytes(),
200+
3,
201+
"`ni|hao:边界整音节(前缀 ` 占 1)"
202+
);
203+
assert!(end > 3);
204+
c.handle_key_event(&key(VK_RIGHT, MOD_SHIFT));
205+
assert_eq!(c.debug_ui_caret_bytes(), 5, "Shift+→ 单字符(`ni'h|)");
206+
c.handle_key_event(&key(VK_LEFT, MOD_CTRL));
207+
assert_eq!(c.debug_ui_caret_bytes(), 3, "Ctrl+← 音节中间也跳到边界");
208+
c.handle_key_event(&key(VK_LEFT, MOD_CTRL));
209+
assert_eq!(c.debug_ui_caret_bytes(), 1, "再 Ctrl+← 到开头");
210+
assert!(
211+
c.debug_preedit().starts_with('`'),
212+
"仍在临拼里,组合没被清掉"
213+
);
214+
}

‎wind_input/crates/wind-coordinator/tests/input_flow.rs‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8962,17 +8962,13 @@ fn test_temp_pinyin_cursor_maps_through_separator() {
89628962
assert_eq!(action_text(&act).as_deref(), Some("`ni'hao"));
89638963
assert_eq!(action_caret(&act), Some(7), "引导符 1 + 显示串 6");
89648964

8965-
// 左移三次:`ni'ha|o → `ni'h|ao → `ni|'hao(跨过分隔符,6 → 5 → 3)
8966-
assert_eq!(action_caret(&tap(&coord, VK_LEFT)), Some(6));
8967-
assert_eq!(action_caret(&tap(&coord, VK_LEFT)), Some(5));
8965+
// ← 在音节边界上整音节跳(rime 语义):`ni'hao| → `ni|'hao(跨过分隔符,7 → 3)。
89688966
assert_eq!(action_caret(&tap(&coord, VK_LEFT)), Some(3));
89698967

89708968
// Home 只到剩余拼音开头(引导符之后),不进只读前缀
89718969
assert_eq!(action_caret(&tap(&coord, VK_HOME)), Some(1));
8972-
assert!(
8973-
matches!(tap(&coord, VK_LEFT), KeyAction::Consumed),
8974-
"已在最左:吃掉,不得退进引导符"
8975-
);
8970+
// 已在最左再 ←:rime 回绕到最后一个音节起点(`ni|'hao 的 3),不得退进引导符。
8971+
assert_eq!(action_caret(&tap(&coord, VK_LEFT)), Some(3));
89768972
}
89778973

89788974
// ── 全角(英文模式 / 中文模式数字)─────────────────────────────────────────────

‎wind_input/crates/wind-coordinator/tests/preedit_follow_highlight.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,8 @@ fn caret_tracks_form_switch() {
305305
for (enter, prefix) in cases {
306306
let active = if enter.is_some() { "wubi86" } else { "pinyin" };
307307
let coord = typed(in_ing(cfg(active)), enter, "pinan");
308-
// 主路:末尾是音节边界,← 整音节跳一次就到 `pin|an`(rime 导航语义)。
309-
// overlay(临拼等)有自己的光标臂,仍按单字符,要两次。
308+
// 末尾是音节边界,← 整音节跳一次就到 `pin|an`(rime 导航语义;临拼 overlay 同)。
310309
press(&coord, VK_LEFT);
311-
if enter.is_some() {
312-
press(&coord, VK_LEFT);
313-
}
314310
let p = prefix.len();
315311
assert_eq!(coord.debug_preedit(), format!("{prefix}pin'an"));
316312
assert_eq!(coord.debug_ui_caret_bytes(), p + 3, "{prefix:?}:pin| 'an");

0 commit comments

Comments
 (0)