detached f2aaeaa

#1 fix(view): keep surface scale while widget is unmapped

state
merged · f2aaeaa · fast-forward
branch
fix/unmapped-scale-fallback → main
opened
bnema ·
updated

On fractional scales (e.g. 1.4), an unmapped widget has no native surface, so observeWidgetScale fell back to the integer widget scale (2). That replaced the cached scale and the effective scale became 2.8, so pages rendered heavily zoomed (seen in dumber when opening a new tab).

The view now keeps the last surface-derived scale while the surface is missing. The widget fallback is only used before the first surface observation.

Tests: regression tests in view_test.go; go vet, go test (incl. -race), make check pass.

2 files · +70 -3
2/2
M view.go +17 -3
@@ -123,6 +123,7 @@ type View struct {
123123 cachedWidth atomic.Int32
124124 cachedHeight atomic.Int32
125125 scaleBits atomic.Uint64
126+ hasSurfaceScale bool // Accessed only on the GTK thread.
126127 scaleMultiplierBits atomic.Uint64
127128 inputScaleOverride atomic.Uint64
128129 scaleTraceCount atomic.Uint64
@@ -451,9 +452,7 @@ func (v *View) updateCachedSizeOnGTKThread() {
451452 changed = true
452453 }
453454 prevScale := v.observedScale()
454- obs := observeWidgetScale(v.widget)
455- scaleChanged := prevScale != obs.scale
456- v.storeObservedScale(obs.scale)
455+ obs, scaleChanged := v.applyScaleObservation(observeWidgetScale(v.widget))
457456 if scaleChanged {
458457 if bridge := v.inputBridge(); bridge != nil {
459458 bridge.SetScale(v.inputScaleForObservedScale(v.effectiveScaleForSurface(obs.scale)))
@@ -612,6 +611,21 @@ func observeWidgetScale(widget *gtk.Widget) widgetScaleObservation {
612611 return obs
613612 }
614613
614+// applyScaleObservation keeps the last authoritative raw surface scale across
615+// native-surface gaps. The integer widget fallback is only useful at startup;
616+// it must not replace a fractional surface scale while the widget is unmapped.
617+func (v *View) applyScaleObservation(obs widgetScaleObservation) (widgetScaleObservation, bool) {
618+ previous := v.observedScale()
619+ if obs.hasSurface {
620+ v.hasSurfaceScale = true
621+ } else if v.hasSurfaceScale {
622+ obs.scale = previous
623+ }
624+ obs.scale = normalizeDeviceScale(obs.scale)
625+ v.storeObservedScale(obs.scale)
626+ return obs, previous != obs.scale
627+}
628+
615629 func widgetSurface(widget *gtk.Widget) *gdk.Surface {
616630 if widget == nil {
617631 return nil
@@ -123,6 +123,7 @@ type View struct {
123 cachedWidth atomic.Int32123 cachedWidth atomic.Int32
124 cachedHeight atomic.Int32124 cachedHeight atomic.Int32
125 scaleBits atomic.Uint64125 scaleBits atomic.Uint64
126+ hasSurfaceScale bool // Accessed only on the GTK thread.
126 scaleMultiplierBits atomic.Uint64127 scaleMultiplierBits atomic.Uint64
127 inputScaleOverride atomic.Uint64128 inputScaleOverride atomic.Uint64
128 scaleTraceCount atomic.Uint64129 scaleTraceCount atomic.Uint64
@@ -451,9 +452,7 @@ func (v *View) updateCachedSizeOnGTKThread() {
451 changed = true452 changed = true
452 }453 }
453 prevScale := v.observedScale()454 prevScale := v.observedScale()
454- obs := observeWidgetScale(v.widget)455+ obs, scaleChanged := v.applyScaleObservation(observeWidgetScale(v.widget))
455- scaleChanged := prevScale != obs.scale
456- v.storeObservedScale(obs.scale)
457 if scaleChanged {456 if scaleChanged {
458 if bridge := v.inputBridge(); bridge != nil {457 if bridge := v.inputBridge(); bridge != nil {
459 bridge.SetScale(v.inputScaleForObservedScale(v.effectiveScaleForSurface(obs.scale)))458 bridge.SetScale(v.inputScaleForObservedScale(v.effectiveScaleForSurface(obs.scale)))
@@ -612,6 +611,21 @@ func observeWidgetScale(widget *gtk.Widget) widgetScaleObservation {
612 return obs611 return obs
613 }612 }
614 613
614+// applyScaleObservation keeps the last authoritative raw surface scale across
615+// native-surface gaps. The integer widget fallback is only useful at startup;
616+// it must not replace a fractional surface scale while the widget is unmapped.
617+func (v *View) applyScaleObservation(obs widgetScaleObservation) (widgetScaleObservation, bool) {
618+ previous := v.observedScale()
619+ if obs.hasSurface {
620+ v.hasSurfaceScale = true
621+ } else if v.hasSurfaceScale {
622+ obs.scale = previous
623+ }
624+ obs.scale = normalizeDeviceScale(obs.scale)
625+ v.storeObservedScale(obs.scale)
626+ return obs, previous != obs.scale
627+}
628+
615 func widgetSurface(widget *gtk.Widget) *gdk.Surface {629 func widgetSurface(widget *gtk.Widget) *gdk.Surface {
616 if widget == nil {630 if widget == nil {
617 return nil631 return nil
M view_test.go +53 -0
@@ -408,3 +408,56 @@ func TestNewViewWidgetGLAreaBasics(t *testing.T) {
408408 t.Fatalf("unexpected initial diagnostics: %+v", d)
409409 }
410410 }
411+
412+func TestScaleObservationKeepsSurfaceScaleWhileUnmapped(t *testing.T) {
413+ setOSRBackingScaleEnv(t, "on")
414+ for _, tt := range []struct {
415+ name string
416+ surface, multiplier float64
417+ }{
418+ {"fractional surface", 1.4, 1},
419+ {"application multiplier", 1, 1.4},
420+ {"composed scales", 1.4, 1.4},
421+ } {
422+ t.Run(tt.name, func(t *testing.T) {
423+ v := &View{}
424+ v.setScaleMultiplier(tt.multiplier)
425+ v.applyScaleObservation(widgetScaleObservation{
426+ scale: tt.surface, surfaceScale: tt.surface, hasSurface: true,
427+ widgetScaleFactor: 2,
428+ })
429+ want := float32(tt.surface * tt.multiplier)
430+ for i := 0; i < 2; i++ {
431+ obs, changed := v.applyScaleObservation(widgetScaleObservation{scale: 2, widgetScaleFactor: 2})
432+ if changed || shouldEmitSizeHooks(false, changed) {
433+ t.Fatal("unmapped observation emitted a scale-only change")
434+ }
435+ if obs.scale != tt.surface || v.observedScale() != tt.surface {
436+ t.Fatalf("raw scale=%v, cached=%v, want %v", obs.scale, v.observedScale(), tt.surface)
437+ }
438+ if got := v.DeviceScaleFactor(); got != want {
439+ t.Fatalf("effective scale=%v, want %v", got, want)
440+ }
441+ if got := v.PageZoomCompensation(); got != float64(want) {
442+ t.Fatalf("compensation=%v, want %v", got, want)
443+ }
444+ }
445+ _, changed := v.applyScaleObservation(widgetScaleObservation{scale: 1.75, hasSurface: true})
446+ if !changed || v.observedScale() != 1.75 {
447+ t.Fatal("new surface scale was not accepted on remap")
448+ }
449+ })
450+ }
451+}
452+
453+func TestScaleObservationUsesWidgetFallbackBeforeSurface(t *testing.T) {
454+ v := &View{}
455+ _, changed := v.applyScaleObservation(widgetScaleObservation{scale: 2, widgetScaleFactor: 2})
456+ if !changed || v.observedScale() != 2 {
457+ t.Fatal("initial widget scale fallback was not accepted")
458+ }
459+ v.applyScaleObservation(widgetScaleObservation{scale: 1.4, hasSurface: true})
460+ if got := v.observedScale(); got != 1.4 {
461+ t.Fatalf("surface scale=%v, want 1.4", got)
462+ }
463+}
@@ -408,3 +408,56 @@ func TestNewViewWidgetGLAreaBasics(t *testing.T) {
408 t.Fatalf("unexpected initial diagnostics: %+v", d)408 t.Fatalf("unexpected initial diagnostics: %+v", d)
409 }409 }
410 }410 }
411+
412+func TestScaleObservationKeepsSurfaceScaleWhileUnmapped(t *testing.T) {
413+ setOSRBackingScaleEnv(t, "on")
414+ for _, tt := range []struct {
415+ name string
416+ surface, multiplier float64
417+ }{
418+ {"fractional surface", 1.4, 1},
419+ {"application multiplier", 1, 1.4},
420+ {"composed scales", 1.4, 1.4},
421+ } {
422+ t.Run(tt.name, func(t *testing.T) {
423+ v := &View{}
424+ v.setScaleMultiplier(tt.multiplier)
425+ v.applyScaleObservation(widgetScaleObservation{
426+ scale: tt.surface, surfaceScale: tt.surface, hasSurface: true,
427+ widgetScaleFactor: 2,
428+ })
429+ want := float32(tt.surface * tt.multiplier)
430+ for i := 0; i < 2; i++ {
431+ obs, changed := v.applyScaleObservation(widgetScaleObservation{scale: 2, widgetScaleFactor: 2})
432+ if changed || shouldEmitSizeHooks(false, changed) {
433+ t.Fatal("unmapped observation emitted a scale-only change")
434+ }
435+ if obs.scale != tt.surface || v.observedScale() != tt.surface {
436+ t.Fatalf("raw scale=%v, cached=%v, want %v", obs.scale, v.observedScale(), tt.surface)
437+ }
438+ if got := v.DeviceScaleFactor(); got != want {
439+ t.Fatalf("effective scale=%v, want %v", got, want)
440+ }
441+ if got := v.PageZoomCompensation(); got != float64(want) {
442+ t.Fatalf("compensation=%v, want %v", got, want)
443+ }
444+ }
445+ _, changed := v.applyScaleObservation(widgetScaleObservation{scale: 1.75, hasSurface: true})
446+ if !changed || v.observedScale() != 1.75 {
447+ t.Fatal("new surface scale was not accepted on remap")
448+ }
449+ })
450+ }
451+}
452+
453+func TestScaleObservationUsesWidgetFallbackBeforeSurface(t *testing.T) {
454+ v := &View{}
455+ _, changed := v.applyScaleObservation(widgetScaleObservation{scale: 2, widgetScaleFactor: 2})
456+ if !changed || v.observedScale() != 2 {
457+ t.Fatal("initial widget scale fallback was not accepted")
458+ }
459+ v.applyScaleObservation(widgetScaleObservation{scale: 1.4, hasSurface: true})
460+ if got := v.observedScale(); got != 1.4 {
461+ t.Fatalf("surface scale=%v, want 1.4", got)
462+ }
463+}

Comments00 unresolved

    no comments yet.

    Keyboard shortcuts

    Single-key shortcuts are on. ctrl k and esc work whatever this says.

    ctrl k :
    go to a section, branch, tag, file or repository
    ?
    this list
    j k
    move the row cursor down / up
    enter
    open the row under the cursor; on Files, a directory opens or closes in place
    ↑ ↓ ← → home end
    in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
    l h
    on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. On pages with a side panel: into the panel (on a merge request, its files) / back to the content; there j k move between its controls
    g g G
    first / last row
    /
    filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
    b
    switch branch or tag
    g s g f g c g b g m g i g r g a
    tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
    g h
    home
    w
    wide mode: use the whole width of the screen, or not (remembered)
    y
    copy a permalink pinned to the commit; in a file, click a line number to select it, shift-click for a range
    d u x
    diff: split / unified / collapse or expand all
    e E
    diff: the whole file in view / every file, with the lines the hunks hide
    n
    merge request: next note in the diff
    l
    merge request: the list of files (j k move, enter opens, esc back)
    M
    merge request: the merge menu; there 1–3 pick the mode, D deletes the branch, enter merges
    R
    merge request: mark the head read
    esc
    close a dialog, leave a field

    Branches 1

    Tags 35

    ↑↓ moveenter openesc close