Repository navigation
fix: preserve display settings across lid transitions - #1332
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 lid-scale state to the user configuration and a flag marking closed-lid single-display settings as confirmed, preserving both through UUID updates and clones.
ImplementationCoordinate lid transitions with display configuration application so saved closed-lid settings are reused, defaults do not overwrite confirmed settings, and per-display scale is saved or restored at the appropriate transition.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 5 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="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 display keeps wrong scale**
When the daemon stops after saving `ExternalUUID` but before applying the external scale, or `SetScreenScaleFactors` fails, `applyLidScale` saves `ExternalUUID` before the scale change succeeds, so a later call takes the matching-UUID branch, clears `lidScalePending`, and skips the retry. The external display keeps the wrong scale.
Only treat `ExternalUUID` as proof that the external scale is active after the scale change succeeds.
Also at `display1/manager_lid.go:212`, `display1/manager_lid.go:237-240`, `display1/manager_lid.go:251`, `display1/manager_lid.go:263`.
</issue_to_address>
### Comment 2
<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 transition crashes the daemon**
When the user config contains a `LidScale` object with `Factors` omitted or null, `applyLidScale` assigns into `state.Factors` while the map is nil, causing a panic during the lid transition and terminating the daemon.
Initialize `state.Factors` when loading or creating a `LidScaleConfig` if it is nil.
Also at `display1/manager_lid.go:212`.
</issue_to_address>
### Comment 3
<location path="display1/manager_lid.go" line_range="217-218" />
<code_context>
+ logger.Warning("failed to restore builtin scale after lid open:", err)
+ 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 gets overwritten**
When saving user configuration fails after restoring builtin scaling on lid open, `applyLidScale` clears `ExternalUUID` in memory, but the saved config retains the old external UUID. A later lid-open apply treats the builtin factors as the external scale and overwrites the saved external factors, so the next lid close restores the wrong scale.
Handle a failed config save so later applies cannot treat the restored builtin scale as the external scale.
</issue_to_address>
### Comment 4
<location path="display1/manager_lid.go" line_range="212" />
<code_context>
+ logger.Warning("failed to read external scale before lid open:", err)
+ return
+ }
+ state.Factors[state.ExternalUUID] = factors
+ if err := m.xsManager.SetScreenScaleFactors(0, state.Factors[builtin.uuid]); err != nil {
+ logger.Warning("failed to restore builtin scale after lid open:", err)
</code_context>
<issue_to_address>
**Wrong display scale overwrites saved settings**
When the external display named by `state.ExternalUUID` is disconnected while the lid is closed before the lid is reopened, `applyLidScale` stores the current screen factors under `state.ExternalUUID` without checking that this display is still connected and active. If the external display is unplugged while the lid is closed, reopening captures the builtin display’s factors and overwrites the unplugged display’s saved scale; reconnecting it later restores the wrong scale.
Before saving current factors under `ExternalUUID`, verify that the corresponding external display is active; otherwise preserve its saved factors and only restore the builtin scale.
</issue_to_address>
### Comment 5
<location path="display1/manager_lid.go" line_range="228" />
<code_context>
+ var external *Monitor
+ for _, monitor := range monitorMap {
+ if monitor.ID != builtin.ID && monitor.realConnected && monitor.Enabled {
+ if external != nil {
+ return // 多外屏组合沿用原有逻辑,不擅自套用单外屏的缩放。
+ }
</code_context>
<issue_to_address>
**Automatic scaling stops for multiple displays**
When the lid closes with multiple enabled external displays and a resolution-change event occurs before it opens, `applyLidScale` returns without clearing `lidScalePending`, so `tryToChangeScaleFactor` skips automatic scale adjustments while the lid remains closed. External displays keep an unsuitable scale.
Clear `lidScalePending` before returning for multiple enabled external displays.
Also at `display1/manager_lid.go:229-230`, `display1/manager.go:3891-3893`.
</issue_to_address>| // 仅首次使用该外屏时计算默认值,之后优先恢复用户保存的值。 | ||
| target = map[string]float64{"ALL": scale.GetRecommendedScaleFactor(m.xConn)} | ||
| } | ||
| state.ExternalUUID = external.uuid |
There was a problem hiding this comment.
🟡 Medium · External display keeps wrong scale
When the daemon stops after saving ExternalUUID but before applying the external scale, or SetScreenScaleFactors fails, applyLidScale saves ExternalUUID before the scale change succeeds, so a later call takes the matching-UUID branch, clears lidScalePending, and skips the retry. The external display keeps the wrong scale.
Only treat ExternalUUID as proof that the external scale is active after the scale change succeeds.
Also at display1/manager_lid.go:212, display1/manager_lid.go:237-240, display1/manager_lid.go:251, display1/manager_lid.go:263.
Prompt for AI agents
In `display1/manager_lid.go` at line 257:
**External display keeps wrong scale**
When the daemon stops after saving `ExternalUUID` but before applying the external scale, or `SetScreenScaleFactors` fails, `applyLidScale` saves `ExternalUUID` before the scale change succeeds, so a later call takes the matching-UUID branch, clears `lidScalePending`, and skips the retry. The external display keeps the wrong scale.
Only treat `ExternalUUID` as proof that the external scale is active after the scale change succeeds.
Also at `display1/manager_lid.go:212`, `display1/manager_lid.go:237-240`, `display1/manager_lid.go:251`, `display1/manager_lid.go:263`.| 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 transition crashes the daemon
When the user config contains a LidScale object with Factors omitted or null, applyLidScale assigns into state.Factors while the map is nil, causing a panic during the lid transition and terminating the daemon.
Initialize state.Factors when loading or creating a LidScaleConfig if it is nil.
Also at display1/manager_lid.go:212.
Prompt for AI agents
In `display1/manager_lid.go` at line 251:
**Lid transition crashes the daemon**
When the user config contains a `LidScale` object with `Factors` omitted or null, `applyLidScale` assigns into `state.Factors` while the map is nil, causing a panic during the lid transition and terminating the daemon.
Initialize `state.Factors` when loading or creating a `LidScaleConfig` if it is nil.
Also at `display1/manager_lid.go:212`.| state.ExternalUUID = "" | ||
| if err := m.saveUserConfigNoLock(); err != nil { |
There was a problem hiding this comment.
🟡 Medium · Saved external scale gets overwritten
When saving user configuration fails after restoring builtin scaling on lid open, applyLidScale clears ExternalUUID in memory, but the saved config retains the old external UUID. A later lid-open apply treats the builtin factors as the external scale and overwrites the saved external factors, so the next lid close restores the wrong scale.
Handle a failed config save so later applies cannot treat the restored builtin scale as the external scale.
Prompt for AI agents
In `display1/manager_lid.go` at lines 217-218:
**Saved external scale gets overwritten**
When saving user configuration fails after restoring builtin scaling on lid open, `applyLidScale` clears `ExternalUUID` in memory, but the saved config retains the old external UUID. A later lid-open apply treats the builtin factors as the external scale and overwrites the saved external factors, so the next lid close restores the wrong scale.
Handle a failed config save so later applies cannot treat the restored builtin scale as the external scale.| logger.Warning("failed to read external scale before lid open:", err) | ||
| return | ||
| } | ||
| state.Factors[state.ExternalUUID] = factors |
There was a problem hiding this comment.
🟡 Medium · Wrong display scale overwrites saved settings
When the external display named by state.ExternalUUID is disconnected while the lid is closed before the lid is reopened, applyLidScale stores the current screen factors under state.ExternalUUID without checking that this display is still connected and active. If the external display is unplugged while the lid is closed, reopening captures the builtin display’s factors and overwrites the unplugged display’s saved scale; reconnecting it later restores the wrong scale.
Before saving current factors under ExternalUUID, verify that the corresponding external display is active; otherwise preserve its saved factors and only restore the builtin scale.
Prompt for AI agents
In `display1/manager_lid.go` at line 212:
**Wrong display scale overwrites saved settings**
When the external display named by `state.ExternalUUID` is disconnected while the lid is closed before the lid is reopened, `applyLidScale` stores the current screen factors under `state.ExternalUUID` without checking that this display is still connected and active. If the external display is unplugged while the lid is closed, reopening captures the builtin display’s factors and overwrites the unplugged display’s saved scale; reconnecting it later restores the wrong scale.
Before saving current factors under `ExternalUUID`, verify that the corresponding external display is active; otherwise preserve its saved factors and only restore the builtin scale.| var external *Monitor | ||
| for _, monitor := range monitorMap { | ||
| if monitor.ID != builtin.ID && monitor.realConnected && monitor.Enabled { | ||
| if external != nil { |
There was a problem hiding this comment.
🟡 Medium · Automatic scaling stops for multiple displays
When the lid closes with multiple enabled external displays and a resolution-change event occurs before it opens, applyLidScale returns without clearing lidScalePending, so tryToChangeScaleFactor skips automatic scale adjustments while the lid remains closed. External displays keep an unsuitable scale.
Clear lidScalePending before returning for multiple enabled external displays.
Also at display1/manager_lid.go:229-230, display1/manager.go:3891-3893.
Prompt for AI agents
In `display1/manager_lid.go` at line 228:
**Automatic scaling stops for multiple displays**
When the lid closes with multiple enabled external displays and a resolution-change event occurs before it opens, `applyLidScale` returns without clearing `lidScalePending`, so `tryToChangeScaleFactor` skips automatic scale adjustments while the lid remains closed. External displays keep an unsuitable scale.
Clear `lidScalePending` before returning for multiple enabled external displays.
Also at `display1/manager_lid.go:229-230`, `display1/manager.go:3891-3893`.|
[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 external and builtin display settings when the lid closes or opens.
Bug Fixes: