Skip to content

fix: restore focus after showing desktop - #1271

Merged
Groveer merged 2 commits into
linuxdeepin:masterfrom
Groveer:master
Aug 17, 2026
Merged

fix: restore focus after showing desktop#1271
Groveer merged 2 commits into
linuxdeepin:masterfrom
Groveer:master

Conversation

@Groveer

@Groveer Groveer commented Aug 12, 2026

Copy link
Copy Markdown
Contributor
  1. Move keyboard focus to the desktop layer when showing desktop.
  2. Preserve the previously active normal surface with QPointer.
  3. Restore activation and keyboard focus when leaving show-desktop mode.

Log: Layer-shell windows that close on focus loss now exit when showing desktop.

Influence:

  1. Trigger Super+D while a focus-loss layer-shell window is visible.
  2. Verify the layer-shell window exits and the desktop receives keyboard focus.
  3. Trigger Super+D again and verify focus returns to the previous normal window.

fix: 显示桌面后恢复窗口焦点

  1. 显示桌面时将键盘焦点转移到 desktop 图层窗口。
  2. 使用 QPointer 保存此前激活的普通窗口。
  3. 退出显示桌面时恢复窗口激活状态及键盘焦点。

Log: 显示桌面时,依赖失焦退出的图层窗口会正常退出。

Influence:

  1. 在依赖失焦退出的图层窗口显示时触发 Super+D。
  2. 确认该图层窗口退出,且 desktop 获得键盘焦点。
  3. 再次触发 Super+D,确认焦点恢复到此前的普通窗口。

PMS: BUG-372393

Summary by Sourcery

Improve show-desktop behavior by moving focus to the desktop layer and restoring the previous window focus when exiting show-desktop mode.

Bug Fixes:

  • Ensure layer-shell windows that close on focus loss correctly exit when entering show-desktop mode.
  • Restore activation and keyboard focus to the previously focused normal window after leaving show-desktop mode.

Enhancements:

  • Track the previously active surface during show-desktop mode using a safe pointer to support reliable focus restoration.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @Groveer, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR updates the show-desktop handling so that keyboard focus is explicitly moved to the desktop layer when entering show-desktop mode and restored to the previously active normal surface when exiting, using a QPointer to safely track the previously focused surface.

Sequence diagram for updated show-desktop focus handling

sequenceDiagram
    actor User
    participant Helper
    participant BackgroundContainer as BackgroundContainer
    participant SurfaceWrapper_desktop as SurfaceWrapper_desktop

    User->>Helper: onShowDesktop(Show)
    Helper->>Helper: activatedSurface()
    Helper->>BackgroundContainer: surfaces()
    loop find desktop surface
        Helper->>SurfaceWrapper_desktop: requestKeyboardFocus(SurfaceWrapper_desktop)
    end
    alt [layer-shell windows depend on focus loss]
        note over Helper,SurfaceWrapper_desktop: Layer-shell window exits on focus loss
    end

    User->>Helper: onShowDesktop(Normal)
    Helper->>Helper: activateSurface(m_showDesktopFocusSurface)
    Helper->>Helper: m_showDesktopFocusSurface = nullptr
Loading

File-Level Changes

Change Details Files
Adjust show-desktop behavior to transfer focus to the desktop layer and restore it to the previously active normal window on exit.
  • Store the currently activated surface in a QPointer member when entering show-desktop mode.
  • Iterate background container surfaces to find the desktop layer surface (by scope) and request keyboard focus for it when showing desktop.
  • On returning to normal desktop state, reactivate the previously stored surface if it still exists and clear the stored pointer.
src/seat/helper.cpp
src/seat/helper.h

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

Comment thread src/seat/helper.cpp Outdated
Comment thread src/seat/helper.h Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

EN: This PR improves Treeland’s show-desktop focus behavior (seat/input layer) so that entering show-desktop moves keyboard focus to the desktop layer, and exiting show-desktop restores activation/keyboard focus to the previously active normal surface per seat.

ZH: 该 PR 改进 Treeland 的“显示桌面”焦点逻辑(seat/input 层),进入显示桌面时将键盘焦点移动到 desktop 图层,退出显示桌面时按 seat 恢复到此前激活的普通窗口,并保证依赖失焦退出的 layer-shell 窗口能正确退出。

Changes / 变更:

  • EN: Save per-seat previously activated surface via QPointer and restore it when leaving show-desktop.
    ZH:使用 QPointer 按 seat 保存此前激活窗口,并在退出显示桌面时恢复。
  • EN: On entering show-desktop, attempt to focus the desktop background layer-surface (dde-shell/desktop).
    ZH:进入显示桌面时尝试将键盘焦点切到 desktop 背景图层窗口(dde-shell/desktop)。
  • EN: Ensure clicking/activating a window while in show-desktop restores other seats’ previous focus state.
    ZH:在显示桌面状态下激活窗口时,恢复其它 seat 的此前焦点状态。

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/seat/helper.h Adds QPointer include, declares focus-restore helper, stores per-seat saved focus surfaces.
src/seat/helper.cpp Implements show-desktop focus handoff to desktop layer and per-seat focus restoration logic.
Suppressed comments (1)

src/seat/helper.cpp:1504

  • EN: restoreShowDesktopFocusSurfaces() can call activateSurface(saved, ..., seat) even if this seat has no SeatSurfaceManager. activateSurface() may end up calling requestKeyboardFocus(), which asserts on a missing seat container; add a guard to avoid crashes when seats are in transition.
    ZH: restoreShowDesktopFocusSurfaces() 在 seat 没有对应 SeatSurfaceManager 时仍可能调用 activateSurface(saved, ..., seat);而 activateSurface() 进一步可能调用 requestKeyboardFocus(),该函数会对缺失的 seat container 断言,seat 处于增删/切换过程中可能导致崩溃。建议加保护判断。
    for (auto *seat : seats) {
        if (seat == exceptSeat)
            continue;
        auto it = m_showDesktopFocusSurfaces.find(seat);

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/seat/helper.cpp Outdated
@deepin-bot

deepin-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.8.18
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1286

@Groveer
Groveer requested a review from wineee August 14, 2026 08:15
Comment thread src/seat/helper.cpp Outdated
@Groveer
Groveer force-pushed the master branch 2 times, most recently from f711787 to 51fbba2 Compare August 17, 2026 07:49
Why: showing the desktop (Super+D) did not move keyboard focus, so
layer-shell windows that close on focus loss (e.g. dde-shell popups,
tooltips) never exited. Also, focus was never restored to the previously
active window when leaving show-desktop mode.

1. On entering show-desktop, dismiss any popup keyboard grab and move
   keyboard focus to the desktop layer surface for every seat; drop focus
   when no desktop surface exists, so focus-loss layer-shell windows exit.
2. On exit, restore keyboard focus to each seat's activated surface via
   the shared restoreShowDesktopFocus(). It is reached from three
   triggers: the protocol desktop-state change (onShowDesktop),
   click-activation of a window (setActivatedSurface), and
   shortcut/forced-activation exits (restoreFromShowDesktop).
3. Skip seats that are still in transition (hotplug) instead of asserting
   on a missing seat container.
4. While showing the desktop, do not yank keyboard focus back to the
   (hidden) activated surface when its focus capability changes or when a
   popup keyboard grab ends.

Log: 显示桌面时键盘焦点正确转移到桌面层,退出时恢复之前的激活窗口。
Influence:
1. 在依赖失焦退出的图层窗口显示时触发 Super+D。
2. 确认该图层窗口退出,且 desktop 获得键盘焦点。
3. 再次触发 Super+D,确认焦点恢复到此前的普通窗口。

fix: 显示桌面后恢复窗口焦点

1. 显示桌面时关闭弹窗键盘抓取,将键盘焦点转移到 desktop 图层窗口。
2. 退出显示桌面时,将键盘焦点恢复到各 seat 的激活窗口。
3. 跳过热插拔切换中的 seat,避免断言。

PMS: BUG-372393
Signed-off-by: groveer <guoyao@uniontech.com>
Why: two popup focus problems.

1. Clicking outside a popup did not always dismiss it. wlroots' xdg popup
   grab only auto-dismisses when the clicked surface cannot receive the
   event (pointer focus cleared, serial == 0), i.e. clicks on other
   clients. Clicks on the popup client's own toplevel were delivered
   normally and the popup stayed open. End the grab explicitly on a
   mouse press that is not on an xdg popup surface, per xdg-shell
   semantics.

2. onKeyboardGrabBegin cast grab->data (a wlr_xdg_popup_grab*) to
   wlr_drag* and read grab_type from it. The first member of
   wlr_xdg_popup_grab is a wl_client* pointer, not an enum, so this is
   undefined behavior and could misclassify a popup grab as a drag,
   leaving m_hasPopupGrab false and breaking outside-click dismissal.
   seat->drag is set only AFTER keyboard_grab_begin fires
   (wlr_seat_start_drag), so drags cannot be detected there; exclude
   them at dismissal time instead.

1. Dismiss the popup keyboard grab in beforeDisposeEvent on a
   MouseButtonPress outside any xdg popup surface.
2. Never dismiss during an active DnD drag (seat->drag non-null): a drag
   also installs a keyboard grab, and ending it would break the drag.
3. Remove the undefined-behavior grab_type cast in onKeyboardGrabBegin.

Log: 点击弹出窗口外部可以关闭弹出窗口;移除 DnD 抓取检测的未定义行为。
Influence:
1. 打开一个弹出窗口(如右键菜单)。
2. 点击该弹出窗口所属应用自身的窗口,确认弹出窗口关闭。
3. 点击其他应用的窗口,确认弹出窗口同样关闭且点击正常传递。
4. 拖拽文件时确认拖拽流程不受影响。
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Groveer, wineee

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

@Groveer
Groveer merged commit 7df69e6 into linuxdeepin:master Aug 17, 2026
9 checks passed
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.

4 participants