Skip to content

fix: Refine background rendering logic for toolbar in recording modes#794

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:commit20260122
Jan 23, 2026
Merged

fix: Refine background rendering logic for toolbar in recording modes#794
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:commit20260122

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

Updated the background rendering conditions to ensure the toolbar blur effect is applied correctly in recording and scrolling screenshot modes, enhancing visual consistency across different function types. This change improves the user experience by maintaining the intended appearance of the toolbar during various capture scenarios.

Updated the background rendering conditions to ensure the toolbar blur effect is applied correctly in recording and scrolling screenshot modes, enhancing visual consistency across different function types. This change improves the user experience by maintaining the intended appearance of the toolbar during various capture scenarios.
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

代码审查意见

整体评价

这段代码主要是针对 Qt6+XCB 环境下工具栏模糊效果的修复,解决了不同模式下的背景绘制问题。代码结构清晰,注释充分,但存在一些可以改进的地方。

具体问题与建议

1. 代码逻辑问题

问题1:backgroundRect 的计算位置不当

  • backgroundRect 的计算被移到了条件判断之前,但在 else if 分支中仍然使用这个变量
  • 建议:将 backgroundRect 的计算封装为一个私有方法,避免重复代码

问题2:工具栏区域调整的硬编码

toolBarRect.adjust(-20, -20, 20, 20);
  • 调整值 20 是硬编码的,缺乏灵活性
  • 建议:定义为类成员变量或配置项,便于维护和调整

2. 代码质量问题

问题1:重复代码

  • painter.setRenderHint(QPainter::Antialiasing, true)m_backgroundPixmap.setDevicePixelRatio(m_pixelRatio) 在两个分支中重复
  • 建议:将这些公共设置提取到条件判断之前

问题2:条件判断可读性

  • 多个 status:: 枚举值的判断可以优化
  • 建议:考虑使用辅助函数如 isScreenCaptureMode() 来提高可读性

3. 性能问题

问题1:不必要的渲染提示设置

  • setRenderHint 在同一个方法中被多次设置相同值
  • 建议:在方法开始处统一设置一次即可

问题2:频繁的几何计算

  • m_toolBar->geometry() 在每次重绘时都会调用
  • 建议:如果工具栏位置不经常变化,可以考虑缓存其几何信息

4. 安全问题

问题1:空指针检查不完整

  • 只检查了 m_toolBar 是否存在,但未检查 m_toolBar->isVisible() 是否会崩溃
  • 建议:确保 m_toolBar 的生命周期管理正确

改进后的代码示例

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    
    // 获取背景绘制区域
    QRect backgroundRect = getBackgroundRect();
    
    // 设置设备像素比
    m_backgroundPixmap.setDevicePixelRatio(m_pixelRatio);
    
    if (isScreenCaptureMode() || !m_hasComposite) {
        // 截图模式或2D模式:全屏绘制背景
        painter.drawPixmap(backgroundRect, m_backgroundPixmap);
    } else if (isRecordingMode()) {
        // 录屏/滚动截图模式:只在工具栏区域绘制背景
        if (m_toolBar && m_toolBar->isVisible()) {
            QRect toolBarRect = getAdjustedToolBarRect();
            
            painter.setClipRect(toolBarRect);
            painter.drawPixmap(backgroundRect, m_backgroundPixmap);
            painter.setClipping(false);
        }
    }
}

QRect MainWindow::getBackgroundRect() const
{
    if (Utils::isQt6XcbEnv) {
        return QRect(0, 0, m_backgroundRect.width(), m_backgroundRect.height());
    }
    return QRect(0, 0, rootWindowRect.width(), rootWindowRect.height());
}

bool MainWindow::isScreenCaptureMode() const
{
    return status::shot == m_functionType;
}

bool MainWindow::isRecordingMode() const
{
    return status::record == m_functionType || status::scrollshot == m_functionType;
}

QRect MainWindow::getAdjustedToolBarRect() const
{
    QRect toolBarRect = m_toolBar->geometry();
    const int margin = m_toolBarBackgroundMargin; // 假设这是类成员变量
    toolBarRect.adjust(-margin, -margin, margin, margin);
    return toolBarRect;
}

总结

这段代码主要解决了 Qt6+XCB 环境下的工具栏模糊问题,但可以通过减少重复代码、提高可读性和优化性能来进一步改进。建议将硬编码值提取为配置项,并确保空指针检查完整。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit a91090a into linuxdeepin:develop/snipe Jan 23, 2026
7 of 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.

3 participants