Skip to content

WM-448: fix: place new windows on cursor's output instead of primary output - #1389

Open
deepin-wm wants to merge 1 commit into
linuxdeepin:masterfrom
deepin-wm:agent/developer/7cb1aea4cb13
Open

WM-448: fix: place new windows on cursor's output instead of primary output#1389
deepin-wm wants to merge 1 commit into
linuxdeepin:masterfrom
deepin-wm:agent/developer/7cb1aea4cb13

Conversation

@deepin-wm

@deepin-wm deepin-wm commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

修复多屏时新开窗口位置不正确

多屏环境下,新开的顶层窗口始终出现在主屏或左屏,而非光标所在的屏幕。

根因

存在两条代码路径导致新窗口位置错误:

路径一:addBySubContainer 使用 primaryOutput() 而非 cursorOutput()

RootSurfaceContainer::addBySubContainer() 在为无父窗口的顶层窗口分配 ownsOutput 时使用 primaryOutput(),导致所有新窗口被固定到主屏。

Workspace::updateSurfaceOwnsOutput() 的早退守卫 if (surface->ownsOutput() && outputs.contains(surface->ownsOutput()->output())) return; 在新窗口初始 getIntersectedOutputs 恰好返回含主屏的列表时会命中,旧代码因 ownsOutput=primary 早退后保留主屏(即 bug)。

路径二:updateSurfaceOwnsOutput 中 geometry-based 覆盖 cursor-based ownsOutput

仅修复路径一不够充分。当新窗口获得初始尺寸时:

  1. addBySubContainer 设置 ownsOutput = cursorOutput()(如右屏)✅
  2. 窗口获得初始尺寸 → geometryChanged 触发 → updateSurfaceOutputsgetIntersectedOutputs(QRect(0,0,w,h))
  3. 窗口此时 position 仍为 (0,0),在多屏布局中 (0,0) 落在左屏 → 返回左屏的 output
  4. updateSurfaceOwnsOutput 发现 cursor output(右屏)不在 geometry-based outputs 列表中 → 不触发早退守卫 → outputs.first()覆盖为左屏

这就是窗口总是出现在左屏的原因。

修复

Commit 1:addBySubContainerprimaryOutput()cursorOutput()

将无父窗口分支的 primaryOutput() 替换为 cursorOutput(),并在光标不在任何输出上时回退到 primaryOutput()

auto output = parentSurface ? parentSurface->ownsOutput() : cursorOutput();
if (!output)
    output = primaryOutput();

Commit 2:updateSurfaceOwnsOutput 中防止 geometry-based 覆盖 cursor-based ownsOutput

当窗口 positionAutomatic()true(即尚未被用户手动定位)且 ownsOutput 已设置时,优先保留现有 ownsOutput,而非使用 geometry-based outputs.first()

if (surface->positionAutomatic() && surface->ownsOutput())
    output = surface->ownsOutput();
else if (!outputs.isEmpty())
    output = Helper::instance()->getOutput(outputs.first());

安全性分析

  • 有父窗口(如 popup)分支不受影响,仍走 parentSurface->ownsOutput()
  • getIntersectedOutputs 返回空(常见情形)时,早退守卫不触发,fallback 链本身已优先 cursorOutput(),行为不变
  • 单屏环境下 cursorOutput()primaryOutput() 等价,行为不变
  • 光标处于输出布局空白区时回退主屏,与旧代码等价
  • beginMoveResize 会将 positionAutomatic 设为 false,因此用户拖拽窗口到其他屏幕时,仍会使用 geometry-based 交集来更新 ownsOutput
  • 输出断开时通过 moveSurfacesToOutput 显式设置 ownsOutput,不经过 updateSurfaceOwnsOutput

变更文件

  • src/core/rootsurfacecontainer.cppaddBySubContainerprimaryOutput()cursorOutput() + 空值回退
  • src/workspace/workspace.cppupdateSurfaceOwnsOutputpositionAutomatic 守卫防止 geometry 覆盖

关联

Multica Issue: WM-448
PMS: https://pms.uniontech.com/bug-view-376719.html

Summary by Sourcery

Place new windows on the cursor's display while retaining existing behavior for parented and manually positioned windows.

Bug Fixes:

  • Place newly created top-level windows on the display containing the cursor instead of always assigning them to the primary or left display.
  • Preserve the cursor-selected output for automatically positioned windows so initial geometry updates do not overwrite it with a geometry-derived output.

@deepin-ci-robot

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: deepin-wm

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

@sourcery-ai

sourcery-ai Bot commented Sep 10, 2026

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

Reviewer's Guide

Fix multi-monitor placement by initially assigning new top-level windows to the cursor’s output, while preserving parent-output behavior and switching to geometry-based tracking after the window is moved.

Sequence diagram for cursor-based new window output assignment

sequenceDiagram
    participant WindowManager
    participant RootSurfaceContainer
    participant Workspace
    participant Cursor
    participant Outputs

    WindowManager->>RootSurfaceContainer: addBySubContainer(sub, surface)
    RootSurfaceContainer->>RootSurfaceContainer: parentSurface()
    alt top-level window
        RootSurfaceContainer->>Cursor: cursorOutput()
        Cursor-->>RootSurfaceContainer: cursor output or null
        opt cursor output unavailable
            RootSurfaceContainer->>Outputs: primaryOutput()
            Outputs-->>RootSurfaceContainer: primary output
        end
    else child window
        RootSurfaceContainer->>RootSurfaceContainer: ownsOutput()
    end
    RootSurfaceContainer->>Workspace: updateSurfaceOwnsOutput(surface)
    alt positionAutomatic and ownsOutput exists
        Workspace->>Workspace: ownsOutput()
    else geometry-based tracking
        Workspace->>Workspace: getIntersectedOutputs(outputs)
    end
Loading

State diagram for window output tracking after placement

stateDiagram-v2
    [*] --> CursorOutputAssigned: new top-level window
    CursorOutputAssigned --> CursorOutputAssigned: positionAutomatic()
    CursorOutputAssigned --> GeometryOutputTracking: beginMoveResize()
    GeometryOutputTracking --> GeometryOutputTracking: getIntersectedOutputs(outputs)
    CursorOutputAssigned --> PrimaryOutputFallback: cursorOutput unavailable
    PrimaryOutputFallback --> CursorOutputAssigned: window created
Loading

File-Level Changes

Change Details Files
Assign new top-level windows to the output containing the cursor, with a primary-output fallback.
  • Use cursorOutput() for parentless surfaces and retain parent-owned output assignment for child surfaces.
  • Fall back to primaryOutput() when the cursor is not over an output.
src/core/rootsurfacecontainer.cpp
Preserve cursor-based ownership for automatically positioned windows until the user moves them, then resume geometry-based output tracking.
  • Prefer an existing ownsOutput for automatically positioned surfaces to avoid initial geometry overriding cursor placement.
  • Use intersected outputs after user movement, with cursor output as the final fallback.
src/core/workspace/workspace.cpp

Possibly linked issues

  • #Multiple screen issues: PR directly fixes the unchecked issue by assigning new top-level windows to the cursor’s output with primary fallback.

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-wm
deepin-wm marked this pull request as ready for review September 10, 2026 09:28

@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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@deepin-wm
deepin-wm force-pushed the agent/developer/7cb1aea4cb13 branch 2 times, most recently from 9ae733f to 699d797 Compare September 10, 2026 11:40
@deepin-bot

deepin-bot Bot commented Sep 10, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 0.10.0
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1392

Use cursorOutput() instead of primaryOutput() in addBySubContainer for
new top-level windows, with null fallback to primaryOutput(). Also
preserve existing ownsOutput for positionAutomatic windows in
updateSurfaceOwnsOutput to prevent geometry-based override when the
window receives its initial size at position (0,0).

多屏环境下新窗口的 ownsOutput 原先使用 primaryOutput() 而非
cursorOutput(),且窗口获得初始尺寸时 updateSurfaceOwnsOutput 会用
基于几何位置的输出覆盖光标所在输出。修改两处:addBySubContainer
改用 cursorOutput() 并在空值时回退 primaryOutput();
updateSurfaceOwnsOutput 对 positionAutomatic 窗口优先保留已有
ownsOutput,用户拖拽后 positionAutomatic 变 false 仍按几何跟随。

Log: 修复多屏时新窗口位置不跟随光标所在屏幕的问题
PMS: BUG-376719
Issue: Fixes WM-448
Influence: 多屏环境下新窗口将出现在光标所在屏幕而非固定主屏,
用户拖拽窗口跨屏后 ownsOutput 正常跟随,单屏行为不变。
@deepin-wm
deepin-wm force-pushed the agent/developer/7cb1aea4cb13 branch from 699d797 to 6d610f1 Compare September 10, 2026 12:03

Output *output = nullptr;
if (!outputs.isEmpty())
if (surface->positionAutomatic() && surface->ownsOutput())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

为什么改这里

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