Skip to content

Commit 0989d37

Browse files
authored
[0707] Collab 文档禁用 Preedit (#4282)
1 parent c716ad8 commit 0989d37

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

devel/0707.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [0707] Collab 文档禁用 IM 预编辑(preedit)
2+
3+
## What & Why
4+
协作文档下,输入法的实时预编辑(preedit,即正在拼写的候选拼音/字符装饰)会干扰 CRDT
5+
协同,需要禁用。参考 `(set-preference "IM:preedit" "off")` 的效果,但**不应改用户配置**
6+
——不能持久写 `IM:preedit` 偏好,只在运行期、按当前文档是否处于协作会话判定。
7+
8+
## How
9+
复刻既有 `in-math?` 的运行期门控模式:数学模式下同样会在不改偏好的前提下强制关 preedit。
10+
Scheme 谓词 `(collab-enabled?)` **已存在**`src/Scheme/Glue/glue_editor.lua`,glue 规则
11+
生成 `get_current_editor()->collab_enabled()`),其实现为 `edit_modify_rep::collab_enabled()`
12+
→ 返回 `loro_collab_on``src/Edit/Modify/edit_collab.cpp`)。故无需新增 glue,只在 3 处
13+
预编辑入口各加一行判断:
14+
15+
- Qt:`QTMWidget::inputMethodEvent``if (as_bool (call ("collab-enabled?"))) im_preedit_switch= false;`
16+
- ImGui/web:`mogan_ime_preedit``if (as_bool (call ("collab-enabled?"))) return;`
17+
- ImGui/macOS:`im_macos_enqueue_preedit` — 同上
18+
19+
提交文本(commit string)路径不经过这些门控,协作文档中正常输入提交文本不受影响;只压制
20+
实时 preedit 装饰。亦未动 `inputMethodQuery``Qt::ImEnabled`(那会整体关掉 IME,连提交
21+
文本一起禁,过重)。
22+
23+
行为:普通文档 preedit 跟随用户 `IM:preedit` 偏好(默认开);协作文档强制关(即使偏好为 on)。
24+
25+
## 涉及文件
26+
- `src/Plugins/Qt/QTMWidget.cpp``inputMethodEvent` 加协作门控
27+
- `src/Plugins/ImGui/im_tm_widget.cpp``mogan_ime_preedit`(web)、
28+
`im_macos_enqueue_preedit`(macOS)各加协作门控
29+
30+
## 如何测试
31+
`--loro=y` 构建后 GUI 验证(preedit 是真实 IME 行为,headless 跑不出):
32+
```
33+
xmake f --loro=y && xmake b stem && xmake r stem
34+
```
35+
1. 普通文档:中文输入法拼写时 preedit 候选正常显示(偏好未变)。
36+
2. 开协作会话(CREATE/JOIN,触发 `collab_enable`)后:同一输入法拼写时不再显示 preedit
37+
装饰,但提交文本(选词后上屏)仍正常。

src/Plugins/ImGui/im_tm_widget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ extern "C" EMSCRIPTEN_KEEPALIVE void
219219
mogan_ime_preedit (const char* utf8) {
220220
if (utf8 == nullptr) return;
221221
if (as_bool (call ("in-math?"))) return;
222+
// 协作文档禁用预编辑
223+
editor ed= get_current_editor ();
224+
if (!is_nil (ed) && ed->collab_enabled ()) return;
222225
g_ime_pending << ("pre-edit:" * string (utf8));
223226
}
224227
#endif // __EMSCRIPTEN__
@@ -231,6 +234,9 @@ void
231234
im_macos_enqueue_preedit (const char* utf8) {
232235
if (utf8 == nullptr) return;
233236
if (as_bool (call ("in-math?"))) return;
237+
// 协作文档禁用预编辑
238+
editor ed= get_current_editor ();
239+
if (!is_nil (ed) && ed->collab_enabled ()) return;
234240
g_ime_pending << ("pre-edit:" * string (utf8));
235241
}
236242

src/Plugins/Qt/QTMWidget.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ QTMWidget::inputMethodEvent (QInputMethodEvent* event) {
369369
}
370370
// Disable preedit in math mode to prevent crash in QQPinyin
371371
if (as_bool (call ("in-math?"))) im_preedit_switch= false;
372+
// 协作文档禁用预编辑(与数学模式同理)
373+
editor ed= get_current_editor ();
374+
if (!is_nil (ed) && ed->collab_enabled ()) im_preedit_switch= false;
372375

373376
string r= "pre-edit:";
374377
if (im_preedit_switch && !preedit_string.isEmpty ()) {

0 commit comments

Comments
 (0)