Skip to content

fix: activate main window on D-Bus openImageFile - #418

Open
add-uos wants to merge 1 commit into
linuxdeepin:release/snipefrom
add-uos:fix-dbus-open-activate-window-snipe
Open

add-uos wants to merge 1 commit into
linuxdeepin:release/snipefrom
add-uos:fix-dbus-open-activate-window-snipe

Conversation

@add-uos

@add-uos add-uos commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

说明

master 分支修复 PR #417 的 backport,pick 到 release/snipe(基于 39b1669)。

仅包含代码修改(3 个 src 文件,+34 行),不含单元测试(release/snipe 分支无对应测试文件)。改动内容与 #417 一致:

问题 / Problem

应用已运行时(D-Bus 服务 com.deepin.imageViewer 已注册),外部通过 D-Bus 调用 openImageFile 打开图片:图片正常加载,但已有窗口不会还原/置顶/聚焦。

修复 / Fix

  • FileControl 新增 Q_INVOKABLE activateMainWindow():取第一个可见顶层窗口(排除 Popup);最小化时仅清除 WindowMinimized 还原(保留最大化/全屏形态),raise()+requestActivate() 推迟到下一轮事件循环避免与 WM 竞态;非最小化立即置顶+请求焦点。Wayland 下 xdg-activation 令牌限制已在代码注释说明。
  • ApplicationAdaptor::openImageFile 成功分支在 emit 前调用激活;校验不通过分支不激活。

验证

  • release/snipe 分支编译通过(Release,Qt6)。
  • X11 玲珑包环境实测(master 同款改动):最小化 → dbus-send openImageFile → 3/3 轮窗口还原且激活;窗口后台 → 置顶并聚焦;首次启动无回归。详见 fix: activate main window on D-Bus openImageFile #417。

Summary by Sourcery

Bug Fixes:

  • Activate the existing image viewer window when a valid image is opened through D-Bus, including restoring minimized windows and requesting focus.

When the viewer is already running, the D-Bus openImageFile call only
loaded the image without raising or focusing the existing window. Now
the running instance activates the main window (restores it from
minimized, raises, requests focus) before loading the image, so the
window pops up on top and focused.

已运行实例通过 D-Bus openImageFile 打开图片时仅加载图片,不会还原、
置顶或聚焦已有窗口。现在先激活主窗口(最小化则还原,再置顶并请求
焦点)再加载图片,保证窗口置顶弹出。

Log: D-Bus 调用 openImageFile 打开图片时激活主窗口
@sourcery-ai

sourcery-ai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 14 hours and 33 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR backports the D-Bus window-activation fix by adding centralized Qt window restoration/focus behavior and invoking it only for successfully validated image-open requests, preserving window state and avoiding minimize/WM focus races.

Sequence diagram for D-Bus image opening and window activation

sequenceDiagram
    participant Caller as D-Bus caller
    participant Adaptor as ApplicationAdaptor
    participant Control as FileControl
    participant Window as MainWindow
    participant EventLoop as Qt event loop

    Caller->>Adaptor: openImageFile(fileName)
    Adaptor->>Control: isCanReadable(urlPath)
    Adaptor->>Control: isImage(urlPath)
    alt image is valid
        Adaptor->>Control: activateMainWindow()
        alt window is minimized
            Control->>Window: setWindowStates(states without WindowMinimized)
            Control->>EventLoop: QTimer::singleShot(0)
            EventLoop->>Window: raise()
            EventLoop->>Window: requestActivate()
        else window is not minimized
            Control->>Window: raise()
            Control->>Window: requestActivate()
        end
        Adaptor-->>Control: openImageFile(urlPath)
        Adaptor-->>Caller: true
    else image is invalid
        Adaptor-->>Caller: false
    end
Loading

File-Level Changes

Change Details Files
Add a reusable main-window activation path that restores minimized windows without losing maximized/fullscreen state and coordinates activation with the window manager.
  • Expose activateMainWindow() as a Q_INVOKABLE method.
  • Select the first visible non-popup top-level window.
  • Clear only the minimized state, then defer raise/requestActivate until the next event-loop turn.
  • Raise and request focus immediately for non-minimized windows, with Wayland activation limitations documented.
src/src/filecontrol.h
src/src/filecontrol.cpp
Activate the existing application window only after a valid image request has passed validation.
  • Call activateMainWindow() before emitting openImageFile for readable image paths.
  • Leave invalid or unsupported requests without activation.
src/src/dbus/applicationadpator.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 99 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交在 D-Bus openImageFile 调用时激活主窗口,逻辑清晰,注释详尽,无安全漏洞。 QTimer::singleShot 传入 context 对象 win 作为第二参数,Qt 会在 win 被销毁时自动断开连接, 不存在悬垂指针风险。建议补充 ToolTip/SplashScreen 等窗口类型过滤以增强健壮性。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['建议将 raise()+requestActivate() 提取为私有辅助方法或在分支后统一调用, 减少代码重复。', '建议在窗口类型过滤中补充 Qt::ToolTip、Qt::SplashScreen 等排除条件, 使 activateMainWindow() 在多窗口场景下更健壮。']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 建议改进:补充窗口类型过滤并提取公共激活逻辑 void FileControl::activateMainWindow() {
    qCDebug(logImageViewer) << "FileControl::activateMainWindow() called.";
    const QList<QWindow *> windows = QGuiApplication::topLevelWindows();
    for (QWindow *win : windows) {
        if (!win || !win->isVisible() ||
            win->type() == Qt::Popup ||
            win->type() == Qt::ToolTip ||
            win->type() == Qt::SplashScreen) {
            continue;
        }

        auto doActivate = [win]() {
            win->raise();
            win->requestActivate();
        };

        const Qt::WindowStates states = win->windowStates();
        if (states & Qt::WindowMinimized) {
            win->setWindowStates(states & ~Qt::WindowMinimized);
            QTimer::singleShot(0, win, doActivate);
        } else {
            doActivate();
        }
        break;
    }
}

本报告由 AI 代码审查工具自动生成

@deepin-bot

deepin-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.61
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #435

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.

3 participants