Skip to content

应用运行 - 添加应用时按名称排序以便查找#2219

Open
JoshCai233 wants to merge 2 commits into
OneDragon-Anything:mainfrom
JoshCai233:APP-sort
Open

应用运行 - 添加应用时按名称排序以便查找#2219
JoshCai233 wants to merge 2 commits into
OneDragon-Anything:mainfrom
JoshCai233:APP-sort

Conversation

@JoshCai233
Copy link
Copy Markdown
Contributor

@JoshCai233 JoshCai233 commented May 3, 2026

image

Summary by CodeRabbit

  • 改进
    • 应用添加对话框的候选应用现在以“标识-名称”配对的列表形式缓存并展示,按本地化规则(优先尝试 zh_CN.UTF-8)排序以改善中文显示与检索;添加与刷新流程已基于该列表映射选择项与有效应用集合,提升检索准确性与界面稳定性。

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 3, 2026

📝 Walkthrough

Walkthrough

将默认组应用缓存从 dict 改为按 [id, name] 对的列表(self.all_apps),按 locale(尝试 zh_CN.UTF-8)对 name 排序,更新从缓存推导有效 id、刷新列表、AddAppDialog 构造与所选 id 的解析逻辑。

Changes

应用列表 -> 对偶列表与区域感知排序

Layer / File(s) Summary
依赖导入
src/one_dragon_qt/view/standalone_app_run_interface.py
新增 import locale 用于区域排序。
数据形状
src/one_dragon_qt/view/standalone_app_run_interface.py
新增缓存字段 self.all_apps,由原先基于 dict 的 app 缓存改为 list[[str,str]][app_id, app_name] 对)。
核心实现(获取与排序)
src/one_dragon_qt/view/standalone_app_run_interface.py
新增/重写 _get_all_apps():从 run_context 取名,尝试设置 zh_CN.UTF-8locale.LC_COLLATE(失败则回退),使用 locale.strxfrm 对 name 排序并返回按 name 排序的 [id,name] 列表。
界面刷新 / 有效 id 计算
src/one_dragon_qt/view/standalone_app_run_interface.py
_refresh_app_list() 基于 self.all_apps 构建 id->name 映射,计算 valid_ids(过滤 config.app_list 中的 id),并向 app_list_widget.set_app_list(...) 传入 [id,name] 列表或对应 name。
添加交互逻辑
src/one_dragon_qt/view/standalone_app_run_interface.py
_on_add_app_clicked() 将可用应用收集为 [id,name] 对列表传入 AddAppDialog,收到选择的 id 后通过缓存对偶列表查找对应 name 再调用 app_list_widget.add_app(...)
对话框构造与选择映射
src/one_dragon_qt/view/standalone_app_run_interface.py
AddAppDialog.__init__ 签名从 available_apps: dict[str,str] 改为 available_apps: list[list[str]],保存该列表并基于对偶列表创建 QListWidgetItemget_selected_ids() 使用选中行索引映射回 available_apps[i][0] 返回 id 列表。

Sequence Diagram(s)

sequenceDiagram
    participant UI as StandaloneRunInterface
    participant Dialog as AddAppDialog
    participant Widget as AppListWidget
    participant Context as RunContext

    UI->>Context: 请求所有应用 id/name
    Context-->>UI: 返回未排序 id/name 列表
    UI->>UI: 使用 locale.strxfrm 对 name 排序并缓存 self.all_apps
    UI->>Dialog: 打开 AddAppDialog(available_apps=[id,name] 列表)
    Dialog-->>UI: 返回 selected_ids
    UI->>UI: 通过 self.all_apps 找到对应 name
    UI->>Widget: 调用 add_app(id, name)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 我在代码地里跳,
名单成对排队好,
拼音风吹把序调,
小窗点选乐逍遥,
胡萝卜笑在怀抱 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确总结了主要变更内容——实现了在添加应用时按名称排序的功能,与代码变更内容一致。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get your free trial and get 200 agent minutes per Slack user (a $50 value).


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/one_dragon_qt/view/standalone_app_run_interface.py (1)

194-211: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

self._app_ids 与排序后的列表行索引不匹配,导致返回错误的 app_id

self._app_ids(第 194 行)按 available_apps.keys() 的原始迭代顺序构建,但列表控件中的条目是按名称排序后的顺序添加的。get_selected_ids() 通过行号 i 访问 self._app_ids[i],一旦排序顺序与原始顺序不同,就会返回错误app_id

示例:

  • available_apps = {"app_b": "Beta", "app_a": "Alpha"}
  • self._app_ids = ["app_b", "app_a"]
  • 排序后列表控件:第 0 行 = "Alpha",第 1 行 = "Beta"
  • 用户选中 "Alpha"(第 0 行)→ get_selected_ids() 返回 self._app_ids[0] = "app_b"

修复方式:在循环中同时追踪 app_id,排序后再重建 self._app_ids;或使用 QListWidgetItem.setData(Qt.ItemDataRole.UserRole, app_id)app_id 嵌入条目中。

🐛 建议修复(追踪 app_id 并在排序后重建 self._app_ids)
-        self._app_ids = list(available_apps.keys())
-
         self._list_widget = MultiSelectListWidget()
         self._list_widget.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
         self._list_widget.setViewportMargins(0, 0, 16, 0)
         self._list_widget.setMinimumHeight(400)
         self._list_widget.setMinimumWidth(480)
         item_font = getFont(16)
-        items = []
-        for _, app_name in available_apps.items():
+        items: list[tuple[str, QListWidgetItem]] = []
+        for app_id, app_name in available_apps.items():
             item = QListWidgetItem(gt(app_name))
             item.setSizeHint(QSize(0, 36))
             item.setFont(item_font)
-            items.append(item)
+            items.append((app_id, item))

         items.sort(key=lambda x: x[1].text())  # 按名称排序以便查找
+        self._app_ids = [app_id for app_id, _ in items]
-        for item in items:
+        for _, item in items:
             self._list_widget.addItem(item)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/one_dragon_qt/view/standalone_app_run_interface.py` around lines 194 -
211, The list widget items are sorted by display name but self._app_ids is built
from available_apps.keys() so indexes no longer match in get_selected_ids(); fix
by storing the app_id with each QListWidgetItem (use
QListWidgetItem.setData(Qt.ItemDataRole.UserRole, app_id)) when creating items
or rebuild self._app_ids after sorting so the index order matches the visual
order, then update get_selected_ids() to retrieve app_id from
item.data(UserRole) (or from the new self._app_ids) rather than assuming
original index alignment.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Around line 194-211: The list widget items are sorted by display name but
self._app_ids is built from available_apps.keys() so indexes no longer match in
get_selected_ids(); fix by storing the app_id with each QListWidgetItem (use
QListWidgetItem.setData(Qt.ItemDataRole.UserRole, app_id)) when creating items
or rebuild self._app_ids after sorting so the index order matches the visual
order, then update get_selected_ids() to retrieve app_id from
item.data(UserRole) (or from the new self._app_ids) rather than assuming
original index alignment.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 78959b52-f093-46c0-b09a-0c8ae6cf767a

📥 Commits

Reviewing files that changed from the base of the PR and between eda5df2 and 7e76adb.

📒 Files selected for processing (1)
  • src/one_dragon_qt/view/standalone_app_run_interface.py

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/one_dragon_qt/view/standalone_app_run_interface.py (1)

195-218: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

排序后仍沿用原始 app_id 顺序会选错应用。

self._app_ids 在排序前就按 available_apps 的原始顺序保存了,但 Line 215 又把显示项重排了一次。这样 get_selected_ids() 再按选中行号回查时,取到的就是排序前的 app_id,用户选中的名称和最终添加的应用会错位。

🐛 建议修复
-        self._app_ids = list(available_apps.keys())
+        self._app_ids: list[str] = []

         self._list_widget = MultiSelectListWidget()
         self._list_widget.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
         self._list_widget.setViewportMargins(0, 0, 16, 0)
         self._list_widget.setMinimumHeight(400)
         self._list_widget.setMinimumWidth(480)
         item_font = getFont(16)
-        items = []
-        for _, app_name in available_apps.items():
+        items: list[tuple[str, QListWidgetItem]] = []
+        for app_id, app_name in available_apps.items():
             item = QListWidgetItem(gt(app_name))
             item.setSizeHint(QSize(0, 36))
             item.setFont(item_font)
-            items.append(item)
+            items.append((app_id, item))

         # 按拼音排序以便查找
         try:
             locale.setlocale(locale.LC_COLLATE, "zh_CN.UTF-8")
         except:
             locale.setlocale(locale.LC_COLLATE, "")
-        items.sort(key=lambda x: locale.strxfrm(x.text()))
+        items.sort(key=lambda x: locale.strxfrm(x[1].text()))

-        for item in items:
+        self._app_ids = [app_id for app_id, _ in items]
+        for _, item in items:
             self._list_widget.addItem(item)

Also applies to: 225-227

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/one_dragon_qt/view/standalone_app_run_interface.py` around lines 195 -
218, The displayed QListWidget items are sorted independently of self._app_ids,
so get_selected_ids() which maps selected row indices back to self._app_ids
returns wrong app_ids; after you build and sort the items list (the variable
items and its sort call using locale.strxfrm), rebuild self._app_ids to match
that new visual order by mapping each sorted QListWidgetItem back to its app_id
from available_apps (or create a list of tuples (app_id, app_name) before
creating items and sort that), and use that reordered self._app_ids wherever
get_selected_ids() or the later code (the block around the later use referenced)
relies on row indices to look up ids.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Around line 210-215: The code calls locale.setlocale(locale.LC_COLLATE, ...)
during dialog initialization (using items.sort with key=lambda x:
locale.strxfrm(x.text())), which mutates process-global locale and uses a bare
except; change to a local, Qt-based comparison using QCollator or QLocale (e.g.,
create a QCollator/QLocale instance and use its key/compare for items.sort) so
sorting is localized without touching process state; if you must keep the
locale.setlocale approach temporarily, narrow the except to catch only
locale.Error and avoid swallowing other exceptions, and keep the items.sort key
using the localized transform consistently.

---

Outside diff comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Around line 195-218: The displayed QListWidget items are sorted independently
of self._app_ids, so get_selected_ids() which maps selected row indices back to
self._app_ids returns wrong app_ids; after you build and sort the items list
(the variable items and its sort call using locale.strxfrm), rebuild
self._app_ids to match that new visual order by mapping each sorted
QListWidgetItem back to its app_id from available_apps (or create a list of
tuples (app_id, app_name) before creating items and sort that), and use that
reordered self._app_ids wherever get_selected_ids() or the later code (the block
around the later use referenced) relies on row indices to look up ids.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d8cf0759-649c-44cb-af44-77de288ed710

📥 Commits

Reviewing files that changed from the base of the PR and between 7e76adb and f003e51.

📒 Files selected for processing (1)
  • src/one_dragon_qt/view/standalone_app_run_interface.py

Comment thread src/one_dragon_qt/view/standalone_app_run_interface.py Outdated
@JoshCai233 JoshCai233 changed the title 添加应用时按名称排序以便查找 应用运行 - 添加应用时按名称排序以便查找 May 3, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/one_dragon_qt/view/standalone_app_run_interface.py (1)

168-168: ⚡ Quick win

next() 替代单元素切片(Ruff RUF015)。

♻️ 建议修复
-                self.app_list_widget.add_app(app_id, [item[1] for item in all_apps if item[0] == app_id][0])
+                self.app_list_widget.add_app(app_id, next(item[1] for item in all_apps if item[0] == app_id))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/one_dragon_qt/view/standalone_app_run_interface.py` at line 168, Replace
the single-element list slice [item[1] for item in all_apps if item[0] ==
app_id][0] with next(item[1] for item in all_apps if item[0] == app_id) to avoid
creating an intermediate list; update the call site
self.app_list_widget.add_app(app_id, ...) accordingly (use next(...) as the
second argument), and if missing entries are possible consider supplying a
default to next(..., default_value) to avoid StopIteration.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Line 54: The type annotation list[[str, str]] is invalid; replace it with a
proper nested collection type (either list[list[str]] or list[tuple[str, str]]
for a more precise tuple). Update the four occurrences of list[[str, str]] in
this module (for example the attribute self.all_apps) to use list[list[str]] or
list[tuple[str, str]] consistently, and if you need to support older Python
versions, import and use typing.List/typing.Tuple instead.
- Around line 122-128: _refresh_app_list currently filters the pinyin-sorted
_get_all_apps() (all_apps) which resets the user order saved in config.app_list
and uses an O(n²) nested comprehension; change the logic to preserve
config.app_list order by mapping IDs to names from all_apps (use a dict like
{id: name}) and then build app_list by iterating config.app_list and only
including ids present in that map, and for better performance when you need the
inverse (filtering all_apps) use a set(config.app_list) to avoid O(n²); update
the code paths in _refresh_app_list that produce app_list and then call
app_list_widget.set_app_list with the reordered list so the UI reflects the
saved user order.

---

Nitpick comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Line 168: Replace the single-element list slice [item[1] for item in all_apps
if item[0] == app_id][0] with next(item[1] for item in all_apps if item[0] ==
app_id) to avoid creating an intermediate list; update the call site
self.app_list_widget.add_app(app_id, ...) accordingly (use next(...) as the
second argument), and if missing entries are possible consider supplying a
default to next(..., default_value) to avoid StopIteration.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d4931015-6f6e-4462-a25c-8f6164e4fd7b

📥 Commits

Reviewing files that changed from the base of the PR and between f003e51 and 4a5d1d8.

📒 Files selected for processing (1)
  • src/one_dragon_qt/view/standalone_app_run_interface.py

Comment thread src/one_dragon_qt/view/standalone_app_run_interface.py Outdated
Comment thread src/one_dragon_qt/view/standalone_app_run_interface.py
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/one_dragon_qt/view/standalone_app_run_interface.py (1)

97-118: ⚡ Quick win

请确认 self.all_apps 不会变成陈旧缓存。

_get_all_apps() 首次构建后就一直复用 self.all_apps;如果默认组应用、应用名称或语言环境会在界面生命周期内变化,这里会继续显示旧列表/旧排序,导致“添加应用”弹窗和实际注册信息不一致。若这些数据是动态的,建议在变更时失效缓存,或者改成每次打开弹窗时重建。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/one_dragon_qt/view/standalone_app_run_interface.py` around lines 97 -
118, The method _get_all_apps currently caches results in self.all_apps and
never refreshes, causing stale data if default_group_apps, application names, or
locale change; update the code to avoid or invalidate this cache: either remove
persistent caching and always rebuild the list inside _get_all_apps, or add
cache invalidation hooks that clear self.all_apps when
ctx.run_context.default_group_apps, get_application_name results, or locale
changes (e.g. on language change or group update events); reference symbols:
_get_all_apps, self.all_apps, self.ctx.run_context.default_group_apps,
self.ctx.run_context.get_application_name, and the locale handling block so the
cache is refreshed before sorting.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/one_dragon_qt/view/standalone_app_run_interface.py`:
- Around line 97-118: The method _get_all_apps currently caches results in
self.all_apps and never refreshes, causing stale data if default_group_apps,
application names, or locale change; update the code to avoid or invalidate this
cache: either remove persistent caching and always rebuild the list inside
_get_all_apps, or add cache invalidation hooks that clear self.all_apps when
ctx.run_context.default_group_apps, get_application_name results, or locale
changes (e.g. on language change or group update events); reference symbols:
_get_all_apps, self.all_apps, self.ctx.run_context.default_group_apps,
self.ctx.run_context.get_application_name, and the locale handling block so the
cache is refreshed before sorting.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8c723666-092c-4bee-9f0e-685004e348d4

📥 Commits

Reviewing files that changed from the base of the PR and between 4a5d1d8 and 583bea0.

📒 Files selected for processing (1)
  • src/one_dragon_qt/view/standalone_app_run_interface.py

@Usagi-wusaqi
Copy link
Copy Markdown
Contributor

同步一条龙运行列表的排序会不会更好

@JoshCai233
Copy link
Copy Markdown
Contributor Author

JoshCai233 commented May 3, 2026

同步一条龙运行列表的排序会不会更好

还有插件呢,其实可以搞个分组,但是目前插件并没有那么多

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants