Skip to content

fix: preserve display settings across lid transitions - #1333

Merged
xujin177 merged 1 commit into
linuxdeepin:develop/2500u3from
xujin177:fix/lid
Dec 2, 2026
Merged

xujin177 merged 1 commit into
linuxdeepin:develop/2500u3from
xujin177:fix/lid

Conversation

@xujin177

@xujin177 xujin177 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor
  1. Initialize external display defaults when closing a builtin-only setup.
  2. Remember per-display scaling and restore builtin scaling on lid open.
  3. Preserve confirmed closed-lid settings across cycles and service restarts.

Log: Fix external display initialization and closed-lid settings recovery.
PMS: BUG-383753

Influence:

  1. Close a builtin-only setup and check external resolution, rotation and scale.
  2. Save an external resolution, reopen and close the lid, then check recovery.
  3. Restart the daemon with the lid closed and verify saved display settings.

fix: 修复开合盖时外屏初始化及显示配置恢复

  1. 仅内屏模式下合盖时初始化外屏默认显示参数。
  2. 按显示器记忆缩放,开盖时恢复内屏原有缩放。
  3. 保留合盖时确认保存的配置,避免反复开合盖或重启服务后重置。

Influence:

  1. 仅内屏模式下合盖,检查外屏分辨率、方向和缩放初始化。
  2. 保存外屏分辨率后反复开合盖,检查配置恢复和内屏状态。
  3. 合盖时重启 daemon,检查已保存显示参数是否保持。

Log: 修复合盖外屏初始化异常及已保存显示配置无法恢复的问题。
PMS: BUG-383753
(cherry picked from commit 5bf9eb0)

Summary by Sourcery

Preserve the intended display configuration and per-display scaling when the lid is closed or reopened.

Bug Fixes:

  • Correct external display initialization when closing the lid in extended or builtin-only setups, including resolution, orientation, and scaling defaults.
  • Preserve confirmed closed-lid display settings across lid cycles and daemon restarts, and restore each display's saved scale when switching back to the builtin display.

1. Initialize external display defaults when closing a builtin-only setup.
2. Remember per-display scaling and restore builtin scaling on lid open.
3. Preserve confirmed closed-lid settings across cycles and service restarts.

Log: Fix external display initialization and closed-lid settings recovery.
PMS: BUG-383753

Influence:
1. Close a builtin-only setup and check external resolution, rotation and scale.
2. Save an external resolution, reopen and close the lid, then check recovery.
3. Restart the daemon with the lid closed and verify saved display settings.

fix: 修复开合盖时外屏初始化及显示配置恢复

1. 仅内屏模式下合盖时初始化外屏默认显示参数。
2. 按显示器记忆缩放,开盖时恢复内屏原有缩放。
3. 保留合盖时确认保存的配置,避免反复开合盖或重启服务后重置。

Influence:
1. 仅内屏模式下合盖,检查外屏分辨率、方向和缩放初始化。
2. 保存外屏分辨率后反复开合盖,检查配置恢复和内屏状态。
3. 合盖时重启 daemon,检查已保存显示参数是否保持。

Log: 修复合盖外屏初始化异常及已保存显示配置无法恢复的问题。
PMS: BUG-383753
(cherry picked from commit 5bf9eb0)
@sourcery-ai

sourcery-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add persisted per-display lid scaling state and a flag recording confirmed closed-lid settings.

  • display1/displayconf.go L53-61, L392-393

Implementation

Preserve the new closed-lid state when screen configurations are updated or cloned.

  • display1/displayconf.go L405, L490-494

Coordinate lid transitions, display configuration recovery, and scale saving or restoration across the manager.

  • display1/manager.go L1080-1102, L2398-2405, L3890-3904 (+4 small edits)
  • display1/manager_lid.go L10, L82-95, L159-269

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

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

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

## Individual Comments

### Comment 1
<location path="display1/manager_lid.go" line_range="251" />
<code_context>
+		state = &LidScaleConfig{Factors: make(map[string]map[string]float64)}
+		m.userConfig.LidScale = state
+	}
+	state.Factors[builtin.uuid] = factors
+	target := state.Factors[external.uuid]
+	if len(target) == 0 {
</code_context>
<issue_to_address>
**Lid-close handling crashes**

When the user config contains a `LidScale` object whose `Factors` map is missing or null, `applyLidScale` assigns to `state.Factors` without initializing the map, causing a nil-map panic that terminates the daemon.

Initialize `state.Factors` before assigning the builtin factors.
</issue_to_address>

### Comment 2
<location path="display1/manager_lid.go" line_range="257" />
<code_context>
+		// 仅首次使用该外屏时计算默认值,之后优先恢复用户保存的值。
+		target = map[string]float64{"ALL": scale.GetRecommendedScaleFactor(m.xConn)}
+	}
+	state.ExternalUUID = external.uuid
+	if err := m.saveUserConfigNoLock(); err != nil {
+		state.ExternalUUID = ""
</code_context>
<issue_to_address>
**External scale initialization is skipped**

When `ExternalUUID` is saved but setting the external scale fails or the daemon stops before the scale is applied, `applyLidScale` treats the matching `ExternalUUID` as proof that the scale is active, clears `lidScalePending`, and skips the retry, so the external display keeps the wrong scale.

Save `ExternalUUID` only after `SetScreenScaleFactors` succeeds, and ensure a failed scale-setting call does not leave the marker in place.

Also at `display1/manager_lid.go:237-240`.
</issue_to_address>

### Comment 3
<location path="display1/manager_lid.go" line_range="218" />
<code_context>
+			return
+		}
+		state.ExternalUUID = ""
+		if err := m.saveUserConfigNoLock(); err != nil {
+			logger.Warning("failed to save lid scale config:", err)
+		}
</code_context>
<issue_to_address>
**Saved external scale is overwritten**

When saving the cleared `ExternalUUID` fails while restoring the builtin scale and the daemon restarts before a successful save, the disk retains the stale external marker, so after restart `applyLidScale` records the builtin scale under the external UUID and overwrites the remembered external scale.

Handle the save failure so a stale `ExternalUUID` cannot remain on disk after restoring the builtin scale.
</issue_to_address>

### Comment 4
<location path="display1/manager_lid.go" line_range="92-93" />
<code_context>
 	logger.Infof("sync lid closed: %v", builtin.lidClosed)
-	return previous != builtin.lidClosed
+	builtin.PropsMu.Unlock()
+	if changed {
+		m.lidScalePending = initializeExternal
+	}
+	return changed
</code_context>
<issue_to_address>
**Replacement display inherits stale scale**

When an external display is added or replaced while the lid stays closed and the lid-closed boolean does not change, `updateLidState` does not re-arm `lidScalePending`, so `applyLidScale` skips the new display; on lid open, the stale `ExternalUUID` can cause its scale to overwrite the previous display’s saved scale.

Re-arm `lidScalePending` when the external display configuration changes while the lid remains closed.

Also at `display1/manager_lid.go:222`.
</issue_to_address>

### Comment 5
<location path="display1/manager_lid.go" line_range="222" />
<code_context>
+			logger.Warning("failed to save lid scale config:", err)
+		}
+	}
+	if !m.lidScalePending || !builtin.lidClosed || builtin.Enabled {
+		return
+	}
</code_context>
<issue_to_address>
**Scale adjustment stays disabled**

When lid-close initialization is requested while the builtin monitor is enabled or multiple external monitors are active, `applyLidScale` returns without clearing `lidScalePending`, so `tryToChangeScaleFactor` keeps returning during the closed-lid session and the intended scale adjustment does not run.

Clear or otherwise resolve `lidScalePending` on these early-return paths.

Also at `display1/manager_lid.go:229-230`.
</issue_to_address>

### Comment 6
<location path="display1/displayconf.go" line_range="60-61" />
<code_context>
+
+// 缩放仍由 XSettings 生效,这里只保存开合盖切换时各显示器的配置。
+type LidScaleConfig struct {
+	Factors      map[string]map[string]float64 // 显示器 UUID -> 原有缩放映射
+	ExternalUUID string                        `json:",omitempty"` // 尚未切回内屏的外屏
 }

</code_context>
<issue_to_address>
**Saved display scale is not restored**

When a monitor UUID changes during UUID migration, `updateUserConfigUuid` migrates `Screens` but not the UUIDs in `LidScale.Factors` or `ExternalUUID`, so `applyLidScale` misses the saved factors for the new UUID and fails to restore the monitor’s previous scale.

Migrate the UUIDs in `LidScale.Factors` and `ExternalUUID` along with the screen configuration UUIDs.
</issue_to_address>

### Comment 7
<location path="display1/manager.go" line_range="3890-3891" />
<code_context>
 	if monitorWidth == 0 || monitorHeight == 0 || !m.dsAutoChangeScaleEnabled {
 		return
 	}
+	// 合盖初始化期间由最终显示配置统一设置缩放,避免中间分辨率事件覆盖结果。
+	if m.lidDisplayResetPending() {
+		return
+	}
</code_context>
<issue_to_address>
**Late scale event overrides lid scale**

When a monitor-change handler passes the pending check while a lid-close transition is concurrently applying the external scale, a monitor-change handler can pass the pending check, then the lid handler sets pending and applies the external target, after which the first handler calls `SetScaleFactor` using its stale dimensions. The external display ends with the automatic factor instead of its remembered or recommended target.

Serialize automatic scale changes with lid-scale transitions, or recheck the transition state immediately before writing the scale.
</issue_to_address>

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

Comment thread display1/manager_lid.go
state = &LidScaleConfig{Factors: make(map[string]map[string]float64)}
m.userConfig.LidScale = state
}
state.Factors[builtin.uuid] = factors

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 · Lid-close handling crashes

When the user config contains a LidScale object whose Factors map is missing or null, applyLidScale assigns to state.Factors without initializing the map, causing a nil-map panic that terminates the daemon.

Initialize state.Factors before assigning the builtin factors.

Prompt for AI agents
In `display1/manager_lid.go` at line 251:

**Lid-close handling crashes**

When the user config contains a `LidScale` object whose `Factors` map is missing or null, `applyLidScale` assigns to `state.Factors` without initializing the map, causing a nil-map panic that terminates the daemon.

Initialize `state.Factors` before assigning the builtin factors.

Comment thread display1/manager_lid.go
// 仅首次使用该外屏时计算默认值,之后优先恢复用户保存的值。
target = map[string]float64{"ALL": scale.GetRecommendedScaleFactor(m.xConn)}
}
state.ExternalUUID = external.uuid

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 · External scale initialization is skipped

When ExternalUUID is saved but setting the external scale fails or the daemon stops before the scale is applied, applyLidScale treats the matching ExternalUUID as proof that the scale is active, clears lidScalePending, and skips the retry, so the external display keeps the wrong scale.

Save ExternalUUID only after SetScreenScaleFactors succeeds, and ensure a failed scale-setting call does not leave the marker in place.

Also at display1/manager_lid.go:237-240.

Prompt for AI agents
In `display1/manager_lid.go` at line 257:

**External scale initialization is skipped**

When `ExternalUUID` is saved but setting the external scale fails or the daemon stops before the scale is applied, `applyLidScale` treats the matching `ExternalUUID` as proof that the scale is active, clears `lidScalePending`, and skips the retry, so the external display keeps the wrong scale.

Save `ExternalUUID` only after `SetScreenScaleFactors` succeeds, and ensure a failed scale-setting call does not leave the marker in place.

Also at `display1/manager_lid.go:237-240`.

Comment thread display1/manager_lid.go
return
}
state.ExternalUUID = ""
if err := m.saveUserConfigNoLock(); err != nil {

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 · Saved external scale is overwritten

When saving the cleared ExternalUUID fails while restoring the builtin scale and the daemon restarts before a successful save, the disk retains the stale external marker, so after restart applyLidScale records the builtin scale under the external UUID and overwrites the remembered external scale.

Handle the save failure so a stale ExternalUUID cannot remain on disk after restoring the builtin scale.

Prompt for AI agents
In `display1/manager_lid.go` at line 218:

**Saved external scale is overwritten**

When saving the cleared `ExternalUUID` fails while restoring the builtin scale and the daemon restarts before a successful save, the disk retains the stale external marker, so after restart `applyLidScale` records the builtin scale under the external UUID and overwrites the remembered external scale.

Handle the save failure so a stale `ExternalUUID` cannot remain on disk after restoring the builtin scale.

Comment thread display1/manager_lid.go
Comment on lines +92 to +93
if changed {
m.lidScalePending = initializeExternal

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 · Replacement display inherits stale scale

When an external display is added or replaced while the lid stays closed and the lid-closed boolean does not change, updateLidState does not re-arm lidScalePending, so applyLidScale skips the new display; on lid open, the stale ExternalUUID can cause its scale to overwrite the previous display’s saved scale.

Re-arm lidScalePending when the external display configuration changes while the lid remains closed.

Also at display1/manager_lid.go:222.

Prompt for AI agents
In `display1/manager_lid.go` at lines 92-93:

**Replacement display inherits stale scale**

When an external display is added or replaced while the lid stays closed and the lid-closed boolean does not change, `updateLidState` does not re-arm `lidScalePending`, so `applyLidScale` skips the new display; on lid open, the stale `ExternalUUID` can cause its scale to overwrite the previous display’s saved scale.

Re-arm `lidScalePending` when the external display configuration changes while the lid remains closed.

Also at `display1/manager_lid.go:222`.

Comment thread display1/manager_lid.go
logger.Warning("failed to save lid scale config:", err)
}
}
if !m.lidScalePending || !builtin.lidClosed || builtin.Enabled {

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 · Scale adjustment stays disabled

When lid-close initialization is requested while the builtin monitor is enabled or multiple external monitors are active, applyLidScale returns without clearing lidScalePending, so tryToChangeScaleFactor keeps returning during the closed-lid session and the intended scale adjustment does not run.

Clear or otherwise resolve lidScalePending on these early-return paths.

Also at display1/manager_lid.go:229-230.

Prompt for AI agents
In `display1/manager_lid.go` at line 222:

**Scale adjustment stays disabled**

When lid-close initialization is requested while the builtin monitor is enabled or multiple external monitors are active, `applyLidScale` returns without clearing `lidScalePending`, so `tryToChangeScaleFactor` keeps returning during the closed-lid session and the intended scale adjustment does not run.

Clear or otherwise resolve `lidScalePending` on these early-return paths.

Also at `display1/manager_lid.go:229-230`.

Comment thread display1/displayconf.go
Comment on lines +60 to +61
Factors map[string]map[string]float64 // 显示器 UUID -> 原有缩放映射
ExternalUUID string `json:",omitempty"` // 尚未切回内屏的外屏

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 · Saved display scale is not restored

When a monitor UUID changes during UUID migration, updateUserConfigUuid migrates Screens but not the UUIDs in LidScale.Factors or ExternalUUID, so applyLidScale misses the saved factors for the new UUID and fails to restore the monitor’s previous scale.

Migrate the UUIDs in LidScale.Factors and ExternalUUID along with the screen configuration UUIDs.

Prompt for AI agents
In `display1/displayconf.go` at lines 60-61:

**Saved display scale is not restored**

When a monitor UUID changes during UUID migration, `updateUserConfigUuid` migrates `Screens` but not the UUIDs in `LidScale.Factors` or `ExternalUUID`, so `applyLidScale` misses the saved factors for the new UUID and fails to restore the monitor’s previous scale.

Migrate the UUIDs in `LidScale.Factors` and `ExternalUUID` along with the screen configuration UUIDs.

Comment thread display1/manager.go
Comment on lines +3890 to +3891
// 合盖初始化期间由最终显示配置统一设置缩放,避免中间分辨率事件覆盖结果。
if m.lidDisplayResetPending() {

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 · Late scale event overrides lid scale

When a monitor-change handler passes the pending check while a lid-close transition is concurrently applying the external scale, a monitor-change handler can pass the pending check, then the lid handler sets pending and applies the external target, after which the first handler calls SetScaleFactor using its stale dimensions. The external display ends with the automatic factor instead of its remembered or recommended target.

Serialize automatic scale changes with lid-scale transitions, or recheck the transition state immediately before writing the scale.

Prompt for AI agents
In `display1/manager.go` at lines 3890-3891:

**Late scale event overrides lid scale**

When a monitor-change handler passes the pending check while a lid-close transition is concurrently applying the external scale, a monitor-change handler can pass the pending check, then the lid handler sets pending and applies the external target, after which the first handler calls `SetScaleFactor` using its stale dimensions. The external display ends with the automatic factor instead of its remembered or recommended target.

Serialize automatic scale changes with lid-scale transitions, or recheck the transition state immediately before writing the scale.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy, xujin177

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

@xujin177
xujin177 merged commit 2e02516 into linuxdeepin:develop/2500u3 Dec 2, 2026
17 of 19 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