fix(main_window): enhance tool bar management and cleanup#840
Conversation
- Refactor tool bar handling to ensure proper initialization and destruction, preventing potential memory leaks and dangling pointers. - Implement checks for tool bar existence before performing actions, improving stability during screen capture operations. - Add a new method to destroy the tool bar, allowing for better resource management when reselecting capture regions. This update addresses issues related to tool bar visibility and lifecycle, enhancing the overall user experience during screen recording. TASK: https://pms.uniontech.com/task-view-389563.html
deepin pr auto review这份 Git Diff 主要针对截屏工具在 Wayland(特别是 Treeland)环境下的生命周期管理进行了重构。核心改动是将 以下是对该 Diff 的详细代码审查及改进建议: 一、 语法与逻辑1.
2. toolBar->blockSignals(true);
disconnect(toolBar, nullptr, nullptr, nullptr);
disconnect(nullptr, nullptr, toolBar, nullptr);
3. ImageMenu *ImageBorderHelper::getBorderMenu(const BorderType type, const QString title, QWidget *parent) {
pruneBorderMenus(); // 每次获取都遍历清理
// ...
}
二、 代码质量1. 空指针检查的代码冗余
2.
3. 遗留的冗余代码 m_toolBar->showWidget();
const QPoint pos(region.x(), qMin(region.bottom(), height() - 2 * m_toolBar->height()));
m_toolBar->showAt(pos);
// m_toolBar->showWidget(); // 这行在 Diff 中被删除了,很好,之前是重复调用这部分清理得很好,但注意 三、 代码性能1. Wayland 下 if (windowHandle() && m_toolBar->windowHandle()
&& m_toolBar->windowHandle()->parent() != windowHandle()) {
m_toolBar->windowHandle()->setParent(windowHandle());
m_toolBar->hide();
m_toolBar->show();
}
2. 四、 代码安全1. 信号与槽的 connect(manager->context(), &TreelandCaptureContext::captureRegionChanged,
this, &MainWindow::updateCaptureRegion, Qt::UniqueConnection);
2.
3. if (toolBar->windowHandle()) {
toolBar->windowHandle()->setParent(nullptr);
}
总结与核心修改建议代码示例本次改动整体质量很高,思路清晰,对 Wayland 下 ToolBar 的生命周期管理更加合理。建议在以下方面进行微调: // 1. 优化 updateCaptureRegion 的职责划分
void MainWindow::updateCaptureRegion()
{
auto context = TreelandCaptureManager::instance()->context();
if (!context) return;
const QRect region = context->captureRegion().toRect();
if (region.width() <= 0 || region.height() <= 0) {
destroyTreelandToolBar();
return;
}
// 如果位置没变且工具栏存在,可以考虑不重建(视 Wayland 绑定逻辑而定)
// 如果必须重建:
destroyTreelandToolBar();
createAndShowTreelandToolBar(region); // 抽离创建逻辑
}
// 2. 精简 destroyTreelandToolBar 的断开连接逻辑
void MainWindow::destroyTreelandToolBar()
{
if (!m_toolBar) return;
ToolBar *toolBar = m_toolBar;
m_toolBar = nullptr;
m_toolBarInit = false;
if (toolBar->windowHandle()) {
toolBar->windowHandle()->setParent(nullptr);
}
// 精准断开与 MainWindow 的连接,不用全局断开
disconnect(toolBar, nullptr, this, nullptr);
toolBar->hide();
delete toolBar; // delete 会自动断开所有与 toolBar 相关的信号
// 清理悬空指针
ImageBorderHelper::instance()->pruneBorderMenus();
}
// 3. ImageBorderHelper::getBorderMenu 移除高频的 pruneBorderMenus 调用
ImageMenu *ImageBorderHelper::getBorderMenu(const BorderType type, const QString title, QWidget *parent)
{
// 不再每次都 pruneBorderMenus,利用 QPointer 的特性直接判断
QPointer<ImageMenu>& cachedRef = m_allBorderMenu[type];
if (!cachedRef.isNull()) {
if (cachedRef->parent() == parent) {
return cachedRef;
}
// 父对象变了,删除旧的
delete cachedRef;
cachedRef = nullptr;
}
ImageMenu *menu = new ImageMenu(type, title, parent);
cachedRef = menu; // QPointer 自动接管
return menu;
} |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
This update addresses issues related to tool bar visibility and lifecycle, enhancing the overall user experience during screen recording.
TASK: https://pms.uniontech.com/task-view-389563.html