Skip to content

Commit

Permalink
[internal/driver/mobile] fix absolute position computation for padded…
Browse files Browse the repository at this point in the history
… child windows

This led to misplaced pop-ups.
  • Loading branch information
toaster committed May 21, 2024
1 parent ea87f77 commit eea39e2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
7 changes: 2 additions & 5 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ func (d *mobileDriver) AbsolutePositionForObject(co fyne.CanvasObject) fyne.Posi
pos := driver.AbsolutePositionForObject(co, mc.ObjectTrees())
inset, _ := c.InteractiveArea()

if mc.windowHead != nil {
if len(mc.windowHead.(*fyne.Container).Objects) > 1 {
topHeight := mc.windowHead.MinSize().Height
pos = pos.Subtract(fyne.NewSize(0, topHeight))
}
if chromeBoxOffset := mc.chromeBoxVerticalOffset(); chromeBoxOffset > 0 {
pos = pos.SubtractXY(0, chromeBoxOffset)
}
return pos.Subtract(inset)
}
Expand Down
82 changes: 82 additions & 0 deletions internal/driver/mobile/driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mobile

import (
"image/color"
"testing"

"github.com/stretchr/testify/assert"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)

func Test_mobileDriver_AbsolutePositionForObject(t *testing.T) {
for name, tt := range map[string]struct {
want fyne.Position
windowIsChild bool
windowPadded bool
}{
"for an unpadded primary (non-child) window it is (0,0)": {
want: fyne.NewPos(0, 0),
windowIsChild: false,
windowPadded: false,
},
"for a padded primary (non-child) window it is (padding,padding)": {
want: fyne.NewPos(4, 4),
windowIsChild: false,
windowPadded: true,
},
"for an unpadded child window it is (0,0)": {
want: fyne.NewPos(0, 0),
windowIsChild: true,
windowPadded: false,
},
"for a padded child window it is (padding,padding)": {
want: fyne.NewPos(4, 4),
windowIsChild: true,
windowPadded: true,
},
} {
t.Run(name, func(t *testing.T) {
var o fyne.CanvasObject
size := fyne.NewSize(100, 100)
d := &mobileDriver{}
w := d.CreateWindow("main")
w.SetPadded(tt.windowPadded)
l := widget.NewLabel("main window")
if !tt.windowIsChild {
o = l
}
w.SetContent(l)
w.Show()
w.Resize(size)
w = d.CreateWindow("child1")
w.SetContent(widget.NewLabel("first child"))
if tt.windowIsChild {
w.Show()
}
w.Resize(size)
w = d.CreateWindow("child2 - hidden")
w.SetContent(widget.NewLabel("second child"))
w.Resize(size)
w = d.CreateWindow("child3")
r := canvas.NewRectangle(color.White)
r.SetMinSize(fyne.NewSize(42, 17))
w.SetPadded(tt.windowPadded)
w.SetContent(container.NewVBox(r))
if tt.windowIsChild {
w.Show()
o = r
}
w.Resize(size)
w = d.CreateWindow("child4 - hidden")
w.SetContent(widget.NewLabel("fourth child"))
w.Resize(size)

got := d.AbsolutePositionForObject(o)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit eea39e2

Please sign in to comment.