fix: forward InputMethodQuery to searchEdit for correct IME candidate…#731
Conversation
|
TAG Bot New tag: 2.0.31 |
e03a9e0 to
e4107f7
Compare
inputeventitem.cpp
Outdated
| QVariant InputEventItem::inputMethodQuery(Qt::InputMethodQuery query) const | ||
| { | ||
| if (m_inputMethodSource) { | ||
| QVariant result = m_inputMethodSource->inputMethodQuery(query); |
There was a problem hiding this comment.
这个变量的定义放在query == Qt::ImCursorRect里面吧,只有它使用了,
There was a problem hiding this comment.
分别删除测试了一下:
- Qt::ImCursorRectangle需要完成从searchEdit的相对坐标转换成InputEventItem的相对坐标。这个删不动。
- Qt::ImEnabled看起来可以去掉。这个是防御性的,防止某些情况下无法输入。
- 其他情况正常返回result即可。
e4107f7 to
be3b392
Compare
… window positioning Override inputMethodQuery() virtual function in InputEventItem to forward input method queries to searchEdit. When IME queries cursor position for candidate window placement, the function forwards the query to searchEdit and maps the returned cursor rectangle coordinates from searchEdit's local coordinate system to InputEventItem's coordinate system. This ensures the candidate window appears at the correct position near the search box while keeping focus on InputEventItem. Changes: - Add inputMethodSource property to InputEventItem for specifying the text control to forward queries to - Override inputMethodQuery() to forward queries and map coordinates for ImCursorRectangle - Set inputMethodSource to searchEdit and add focus property in FullscreenFrame and WindowedFrame - Remove redundant ImEnabled override as SearchEdit already returns correct state 重写 InputEventItem 的 inputMethodQuery() 虚函数,将输入法查询转发给 searchEdit。当输入法查询光标位置以定位候选框时,该函数将查询转发给 searchEdit,并将返回的光标矩形坐标从 searchEdit 的局部坐标系映射到 InputEventItem 的坐标系。这确保候选框出现在搜索框附近的正确位置,同时焦点保持在 InputEventItem 上。 修改内容: - 为 InputEventItem 添加 inputMethodSource 属性,用于指定转发查询的文本控件 - 重写 inputMethodQuery() 以转发查询并映射 ImCursorRectangle 的坐标 - 在 FullscreenFrame 和 WindowedFrame 中设置 inputMethodSource 为 searchEdit 并添加 focus 属性 - 移除冗余的 ImEnabled 覆盖,因为 SearchEdit 已经返回正确状态 PMS: BUG-301743
be3b392 to
eeba2de
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, Ivy233 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
deepin pr auto review这段代码主要是为 1. 语法逻辑审查
2. 代码质量
3. 代码性能
4. 代码安全
综合改进代码示例针对上述安全和质量建议,以下是修改后的代码片段建议: inputeventitem.h #include <QPointer> // 引入 QPointer
// ...
private:
QPointer<QQuickItem> m_inputMethodSource; // 使用 QPointer 防止悬空指针
// ...inputeventitem.cpp QVariant InputEventItem::inputMethodQuery(Qt::InputMethodQuery query) const
{
// QPointer 会在对象销毁时自动置空,这里检查既检查了是否为空,也检查了对象是否存活
if (m_inputMethodSource) {
if (query == Qt::ImCursorRectangle) {
QVariant result = m_inputMethodSource->inputMethodQuery(query);
// 增加有效性检查
if (result.isValid()) {
QRectF rect = result.toRectF();
// 坐标映射
QPointF mapped = m_inputMethodSource->mapToItem(this, rect.topLeft());
rect.moveTopLeft(mapped);
return rect;
}
}
}
return QQuickItem::inputMethodQuery(query);
}总结这段代码实现了预期的功能,逻辑基本正确。主要改进点在于对象生命周期的安全性(使用 |
|
/merge |
… window positioning
When launcher opens, focus is on InputEventItem (root node) while searchEdit has no focus or cursor, as per design requirements. However, IMEs like Sogou send InputMethodQuery events to the focused control during pre-edit stage to query cursor position for candidate window placement. Since InputEventItem is not a text input control, it cannot return meaningful cursor position, causing the candidate window to appear at incorrect positions (e.g., top-left corner or other wrong locations).
This commit implements a forwarding mechanism: when InputEventItem receives InputMethodQuery, it forwards the query to searchEdit and maps the returned cursor rectangle coordinates from searchEdit's local coordinate system to the query target's coordinate system, then returns the mapped result to the IME framework. This allows the candidate window to be positioned correctly near the search box while keeping focus on InputEventItem (no visible cursor in search box).
Changes:
启动器打开时,焦点在 InputEventItem(根节点)上,searchEdit 无焦点、无光标,这是设计要求。但搜狗等输入法在预编辑阶段会向焦点控件发送 InputMethodQuery 事件查询光标位置以定位候选框。由于 InputEventItem 不是文本输入控件,无法返回有意义的光标位置,导致候选框出现在错误位置(如屏幕左上角或其他位置)。
本次提交实现了转发机制:InputEventItem 收到 InputMethodQuery 时,将查询转发给 searchEdit,并将 searchEdit 返回的光标矩形坐标从其局部坐标系映射到查询目标的坐标系,再将映射后的结果返回给输入法框架。这样候选框能正确定位到搜索框附近,同时焦点保持在 InputEventItem 上(搜索框不显示光标)。
修改内容:
PMS: BUG-301743