Skip to content

fix(window): persist maximized state and center window on startup#627

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/persist-window-maximized-state
Jun 10, 2026
Merged

fix(window): persist maximized state and center window on startup#627
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/persist-window-maximized-state

Conversation

@pengfeixx

Copy link
Copy Markdown
Contributor

Save window maximized state to config and restore on next launch. Use normalGeometry() when maximized and sync() before _Exit(). Add screen centering and auto-maximize when size exceeds screen.

持久化窗口最大化状态,重启后恢复最大化或居中显示窗口。
最大化时使用 normalGeometry() 保存正常尺寸,添加 sync() 确
保配置在 _Exit() 前写入磁盘。

Log: 修复重启后窗口未恢复最大化状态的问题
PMS: BUG-340035
Influence: 窗口关闭时保存最大化状态,重新打开时恢复最大化;非最大化窗口居中显示;窗口尺寸超过屏幕时自动最大化。

@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 @pengfeixx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

Save window maximized state to config and restore on next launch.
Use normalGeometry() when maximized and sync() before _Exit().
Add screen centering and auto-maximize when size exceeds screen.

持久化窗口最大化状态,重启后恢复最大化或居中显示窗口。
最大化时使用 normalGeometry() 保存正常尺寸,添加 sync() 确
保配置在 _Exit() 前写入磁盘。

Log: 修复重启后窗口未恢复最大化状态的问题
PMS: BUG-340035
Influence: 窗口关闭时保存最大化状态,重新打开时恢复最大化;非最大化窗口居中显示;窗口尺寸超过屏幕时自动最大化。
@pengfeixx pengfeixx force-pushed the fix/persist-window-maximized-state branch from 810f136 to 5ad4fb3 Compare June 10, 2026 09:17
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这段代码主要实现了窗口状态的保存和恢复功能,包括窗口尺寸和最大化状态。以下是我的审查意见:

  1. 代码逻辑:
  • 代码逻辑基本清晰,但有一些可以优化的地方
  • 在window_manager.cpp中,判断窗口是否应该最大化的条件可以更精确
  • 在web_window.cpp中,保存窗口状态时的逻辑可以更健壮
  1. 代码质量:
  • 代码注释充分,但可以添加更多关于窗口状态管理的说明
  • 变量命名清晰,遵循了项目的命名规范
  • 错误处理可以更完善
  1. 代码性能:
  • 性能方面没有明显问题
  • QSettings的使用方式合理,没有频繁的IO操作
  1. 代码安全:
  • 没有明显的安全问题
  • 配置值的读取和写入都使用了类型安全的函数

具体改进建议:

  1. window_manager.cpp中的窗口尺寸判断可以优化:
if (saveMaximized || saveWidth > screenRect.width() - 100 || saveHeight > screenRect.height() - 100) {
    // 上次关闭时最大化 或 保存的窗口尺寸接近屏幕可用区域,直接最大化
    window->showMaximized();
}
  1. web_window.cpp中的窗口状态保存可以增加错误处理:
void WebWindow::saveWindowSize()
{
    qCDebug(app) << "Saving window size";
    QSettings *setting = ConfigManager::getInstance()->getSettings();
    if (!setting) {
        qCWarning(app) << "Failed to get settings instance";
        return;
    }
    
    bool isMaximized = windowState() & Qt::WindowMaximized;
    setting->beginGroup(QString(kConfigWindowInfo));
    try {
        if (isMaximized) {
            setting->setValue(QString(kConfigWindowWidth), normalGeometry().width());
            setting->setValue(QString(kConfigWindowHeight), normalGeometry().height());
        } else {
            setting->setValue(QString(kConfigWindowWidth), width());
            setting->setValue(QString(kConfigWindowHeight), height());
        }
        setting->setValue(QString(kConfigWindowMaximized), isMaximized);
        setting->sync();
    } catch (...) {
        qCWarning(app) << "Error occurred while saving window size";
    }
    setting->endGroup();
    qCDebug(app) << "Window size saved, maximized:" << isMaximized;
}
  1. 建议在consts.h中添加相关注释:
// 窗口配置相关常量
extern const char kConfigWindowWidth[];     // 窗口宽度配置键
extern const char kConfigWindowHeight[];    // 窗口高度配置键
extern const char kConfigWindowMaximized[]; // 窗口最大化状态配置键
extern const char kConfigWindowInfo[];      // 窗口信息配置组名
extern const char kConfigAppList[];         // 应用列表配置键
  1. 建议在window_manager.cpp中添加屏幕可用性检查:
// 获取当前屏幕可用区域(已排除Dock栏占用区域)
QScreen *primaryScreen = QGuiApplication::primaryScreen();
if (!primaryScreen) {
    qCWarning(app) << "No primary screen available";
    return;
}
QRect screenRect = primaryScreen->availableGeometry();

这些改进可以提高代码的健壮性和可维护性,同时保持原有的功能完整性。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pengfeixx

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

@pengfeixx pengfeixx merged commit ba7db19 into linuxdeepin:master Jun 10, 2026
16 of 17 checks passed
@pengfeixx pengfeixx deleted the fix/persist-window-maximized-state branch June 10, 2026 09:22
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