Skip to content

fix: adjust view scale ratio when adding new page - #294

Closed
lzwind wants to merge 1 commit into
linuxdeepin:masterfrom
lzwind:agent/bugfix/287301
Closed

lzwind wants to merge 1 commit into
linuxdeepin:masterfrom
lzwind:agent/bugfix/287301

Conversation

@lzwind

@lzwind lzwind commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

When deepin-draw creates a new blank page, DrawBoard::addPage(Page*) (src/frame/ccentralwidget.cpp:871) calls setCurrentPage(page) but does not call adjustViewScaleRatio() to fit the canvas to the viewport. The canvas scene rect is set to screen resolution × DPR in PageContext constructor, while PageView's initial scale is 1.0 (100%). Since the viewport is always smaller than the full screen resolution (due to window chrome — title bar, toolbar, side panels), the canvas overflows the viewport at 100% scale, causing the canvas to touch screen edges with uneven margins. The image-loading path (ccentralwidget.cpp:1208) already calls adjustViewScaleRatio() after page creation, but the blank-page creation path does not, creating an inconsistency.

Fix

Add page->adjustViewScaleRatio() after setCurrentPage(page) in DrawBoard::addPage(Page*) (src/frame/ccentralwidget.cpp:881), making the blank-page creation path consistent with the image-loading path. The method checks if the canvas is larger than the viewport and adjusts the scale ratio accordingly.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • The target code (DrawBoard::addPage) was introduced in commit bdcb567b (2021-07-21, architecture refactoring) and is not a product of any historical bug fix — this change does not revert any previous fix
  • adjustViewScaleRatio() is an existing method already called at line 1208 (image-loading path); it has internal null checks and is idempotent (second call is a no-op when canvas already fits viewport)
  • No new interface added; no function signature changed; no public member modified

Business Impact Scope

The fix affects canvas display behavior when creating new blank pages in deepin-draw. Users will see the canvas properly fitted within the viewport at 100% scale, instead of overflowing with uneven margins. The image-loading path is unaffected due to the idempotent nature of the additional call. No other modules or features are impacted.

Verification Suggestion

Test creating new blank canvas at different screen resolutions and DPR settings — verify the canvas fits within the viewport with even margins. Also verify image loading canvas display has no regression.


根因分析

deepin-draw 创建新空白页时,DrawBoard::addPage(Page*)(src/frame/ccentralwidget.cpp:871)调用 setCurrentPage(page) 后未调用 adjustViewScaleRatio() 进行缩放适配。画布 scene rect 在 PageContext 构造函数中被设置为屏幕分辨率 × DPR,而 PageView 初始缩放比例为 1.0(100%)。由于窗口标题栏、工具栏等 chrome 占用导致 viewport 始终小于全屏分辨率,画布在 100% 比例下溢出 viewport,表现为画板上下贴边、左右留白不均。加载图片路径(ccentralwidget.cpp:1208)已调用 adjustViewScaleRatio(),但空白页创建路径未调用,导致两条路径行为不一致。

修复方案

在 DrawBoard::addPage(Page*) 的 setCurrentPage(page) 之后添加 page->adjustViewScaleRatio() 调用(src/frame/ccentralwidget.cpp:881),使空白页创建路径与加载图片路径行为一致。该方法在画布大于 viewport 时自动计算并调整缩放比例。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 目标代码(DrawBoard::addPage)由 commit bdcb567b(2021-07-21,架构重构)引入,非历史 bug 修复产物——本次改动不撤销任何已有修复
  • adjustViewScaleRatio() 是已有方法,在加载图片路径(line 1208)已被稳定调用;内部有 null check,且操作幂等(画布已适配时第二次调用为空操作)
  • 未新增接口、未修改函数签名、未修改公开成员

业务影响范围

本次修复影响 deepin-draw 新建空白页时的画布显示行为。用户将看到画布在 100% 比例下正确适配窗口可视区域,不再溢出且留白均匀。加载图片路径因调用幂等性不受影响。不影响其他模块或功能。

验证建议

在不同屏幕分辨率和 DPR 设置下测试新建空白画板——验证画布在窗口内正确显示、留白均匀。同时验证加载图片时画布显示无回归。

Summary by Sourcery

Bug Fixes:

  • Fit newly created blank pages to the available viewport so their canvas displays with consistent margins.

1. Root cause: DrawBoard::addPage(Page*) does not call
   adjustViewScaleRatio() after setCurrentPage(), leaving canvas
   at 100% scale which overflows the viewport area
2. Fix: add page->adjustViewScaleRatio() after setCurrentPage(page)
   to auto-fit canvas to viewport, matching image-loading path
3. Impact: new blank page canvas now fits viewport; image loading
   path unaffected due to idempotent nature of the call

Log: fix canvas displayed too large when creating new blank page

Influence:
1. Test creating new blank canvas at different resolutions and DPR
2. Verify image loading canvas display has no regression
3. Verify canvas margins are even and not touching screen edges

fix: 修复画板新建空白页时画布显示偏大

1. 根因:DrawBoard::addPage(Page*) 在 setCurrentPage() 后未调用
   adjustViewScaleRatio(),画布以 100% 比例显示时溢出 viewport
2. 方案:在 setCurrentPage(page) 后补调 adjustViewScaleRatio(),
   使空白页自动适配 viewport 缩放,与加载图片路径行为一致
3. 影响:新建空白页画布将自动适配窗口可视区域;加载图片路径
   不受影响(调用幂等,无副作用)

Log: 修复画板新建空白页时画布显示尺寸偏大的问题

Influence:
1. 测试不同分辨率和 DPR 下新建空白画板的显示效果
2. 验证加载图片时画布显示无回归
3. 验证画布留白均匀、上下不贴边

PMS: BUG-287301
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 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

@sourcery-ai

sourcery-ai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Reviewer's Guide

Uses of the API

Fit a newly selected blank page to the viewport by calling its existing scale-adjustment method.

  • src/frame/ccentralwidget.cpp L880-881

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

@lzwind
lzwind marked this pull request as draft November 15, 2026 05:09

@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 found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/frame/ccentralwidget.cpp" line_range="881" />
<code_context>

     setCurrentPage(page);
+
+    page->adjustViewScaleRatio();
 }

</code_context>
<issue_to_address>
**Page construction crashes**

When a `Page` is constructed with a non-null `DrawBoard*` before its context is set, `Page` registers with the board before its context exists, so `adjustViewScaleRatio()` reaches `scene()` with no scene and dereferences the null result, crashing construction.

Ensure the page has a context and scene before invoking `adjustViewScaleRatio()`, or defer the adjustment until they exist.
</issue_to_address>

### Comment 2
<location path="src/frame/ccentralwidget.cpp" line_range="881" />
<code_context>

     setCurrentPage(page);
+
+    page->adjustViewScaleRatio();
 }

</code_context>
<issue_to_address>
**Imported images appear too small**

When an image is loaded through a path that creates a blank page before replacing its scene rect with the image bounds, `addPage()` scales the default screen-sized scene before the image dimensions are installed, and the later `adjustViewScaleRatio()` does not reset that scale; it either compounds the zoom-out or skips fitting when the image scene fits the viewport, leaving the imported image smaller than intended.

Fit the view after installing the image scene rect, setting the scale from the desired fit instead of retaining or compounding the blank page's scale.
</issue_to_address>

### Comment 3
<location path="src/frame/ccentralwidget.cpp" line_range="881" />
<code_context>

     setCurrentPage(page);
+
+    page->adjustViewScaleRatio();
 }

</code_context>
<issue_to_address>
**Hidden pages stay undersized**

When a page is added before its window has been shown or its layout has established nonzero viewport dimensions, `adjustViewScaleRatio()` reads the not-yet-laid-out viewport dimensions, computes a zero ratio, and passes it to `PageView::scale()`, which clamps the scale to 0.1. The later resize and show handlers do not fit the page again, so the canvas stays at 10% instead of fitting the viewport.

Defer fitting until the viewport has valid dimensions, and retry the fit after the page is shown or resized.
</issue_to_address>

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


setCurrentPage(page);

page->adjustViewScaleRatio();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Page construction crashes

When a Page is constructed with a non-null DrawBoard* before its context is set, Page registers with the board before its context exists, so adjustViewScaleRatio() reaches scene() with no scene and dereferences the null result, crashing construction.

Ensure the page has a context and scene before invoking adjustViewScaleRatio(), or defer the adjustment until they exist.

Prompt for AI agents
In `src/frame/ccentralwidget.cpp` at line 881:

**Page construction crashes**

When a `Page` is constructed with a non-null `DrawBoard*` before its context is set, `Page` registers with the board before its context exists, so `adjustViewScaleRatio()` reaches `scene()` with no scene and dereferences the null result, crashing construction.

Ensure the page has a context and scene before invoking `adjustViewScaleRatio()`, or defer the adjustment until they exist.


setCurrentPage(page);

page->adjustViewScaleRatio();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Imported images appear too small

When an image is loaded through a path that creates a blank page before replacing its scene rect with the image bounds, addPage() scales the default screen-sized scene before the image dimensions are installed, and the later adjustViewScaleRatio() does not reset that scale; it either compounds the zoom-out or skips fitting when the image scene fits the viewport, leaving the imported image smaller than intended.

Fit the view after installing the image scene rect, setting the scale from the desired fit instead of retaining or compounding the blank page's scale.

Prompt for AI agents
In `src/frame/ccentralwidget.cpp` at line 881:

**Imported images appear too small**

When an image is loaded through a path that creates a blank page before replacing its scene rect with the image bounds, `addPage()` scales the default screen-sized scene before the image dimensions are installed, and the later `adjustViewScaleRatio()` does not reset that scale; it either compounds the zoom-out or skips fitting when the image scene fits the viewport, leaving the imported image smaller than intended.

Fit the view after installing the image scene rect, setting the scale from the desired fit instead of retaining or compounding the blank page's scale.


setCurrentPage(page);

page->adjustViewScaleRatio();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Hidden pages stay undersized

When a page is added before its window has been shown or its layout has established nonzero viewport dimensions, adjustViewScaleRatio() reads the not-yet-laid-out viewport dimensions, computes a zero ratio, and passes it to PageView::scale(), which clamps the scale to 0.1. The later resize and show handlers do not fit the page again, so the canvas stays at 10% instead of fitting the viewport.

Defer fitting until the viewport has valid dimensions, and retry the fit after the page is shown or resized.

Prompt for AI agents
In `src/frame/ccentralwidget.cpp` at line 881:

**Hidden pages stay undersized**

When a page is added before its window has been shown or its layout has established nonzero viewport dimensions, `adjustViewScaleRatio()` reads the not-yet-laid-out viewport dimensions, computes a zero ratio, and passes it to `PageView::scale()`, which clamps the scale to 0.1. The later resize and show handlers do not fit the page again, so the canvas stays at 10% instead of fitting the viewport.

Defer fitting until the viewport has valid dimensions, and retry the fit after the page is shown or resized.

@lzwind lzwind closed this Nov 17, 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