Repository navigation
fix: preserve display settings across lid transitions - #1333
Conversation
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)
Reviewer's GuideData structuresAdd persisted per-display lid scaling state and a flag recording confirmed closed-lid settings.
ImplementationPreserve the new closed-lid state when screen configurations are updated or cloned.
Coordinate lid transitions, display configuration recovery, and scale saving or restoration across the manager.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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>| state = &LidScaleConfig{Factors: make(map[string]map[string]float64)} | ||
| m.userConfig.LidScale = state | ||
| } | ||
| state.Factors[builtin.uuid] = factors |
There was a problem hiding this comment.
🟠 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.| // 仅首次使用该外屏时计算默认值,之后优先恢复用户保存的值。 | ||
| target = map[string]float64{"ALL": scale.GetRecommendedScaleFactor(m.xConn)} | ||
| } | ||
| state.ExternalUUID = external.uuid |
There was a problem hiding this comment.
🟡 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`.| return | ||
| } | ||
| state.ExternalUUID = "" | ||
| if err := m.saveUserConfigNoLock(); err != nil { |
There was a problem hiding this comment.
🟡 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.| if changed { | ||
| m.lidScalePending = initializeExternal |
There was a problem hiding this comment.
🟡 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`.| logger.Warning("failed to save lid scale config:", err) | ||
| } | ||
| } | ||
| if !m.lidScalePending || !builtin.lidClosed || builtin.Enabled { |
There was a problem hiding this comment.
🟡 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`.| Factors map[string]map[string]float64 // 显示器 UUID -> 原有缩放映射 | ||
| ExternalUUID string `json:",omitempty"` // 尚未切回内屏的外屏 |
There was a problem hiding this comment.
🟡 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.| // 合盖初始化期间由最终显示配置统一设置缩放,避免中间分辨率事件覆盖结果。 | ||
| if m.lidDisplayResetPending() { |
There was a problem hiding this comment.
🟡 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.|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Log: Fix external display initialization and closed-lid settings recovery.
PMS: BUG-383753
Influence:
fix: 修复开合盖时外屏初始化及显示配置恢复
Influence:
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: