Skip to content

Commit

Permalink
Move to float32 coordinates (#1661)
Browse files Browse the repository at this point in the history
* Move geometry from int to float32.

* remove deprecated Size.Union

* Move mouse events to Delta from (int, int)

* Add Vector2 for interoperability between Size, Position and Delta.

Fixes #835
  • Loading branch information
andydotxyz committed Dec 19, 2020
1 parent 1e82abf commit e30a503
Show file tree
Hide file tree
Showing 151 changed files with 848 additions and 785 deletions.
18 changes: 16 additions & 2 deletions CHANGELOG.md
Expand Up @@ -10,9 +10,19 @@ More detailed release notes can be found on the [releases page](https://github.c
These changes likely break some apps, please read the
[upgrading doc](https://developer.fyne.io/api/v2.0/upgrading) for more info

* Coordinate system to float32
* Size and Position units were changed from int to float32
* `Text.TextSize` moved to float32 and `fyne.MeasureText` now takes a float32 size parameter
* Removed `Size.Union`
* Added fyne.Delta for difference based X, Y representation
* DraggedEvent.DraggedX and DraggedX (int, int) to DraggedEvent.Dragged (Delta)
* ScrollEvent.DeltaX and DeltaY (int, int) moved to ScrollEvent.Scrolled (Delta)

* Theme API update
* `fyne.Theme` moved to `fyne.LegacyTheme` and can be load to a new theme using `theme.FromLegacy`
* A new, more flexible, Theme interface has been created that we encourage developers to use

* The second parameter of `theme.NewThemedResource` was removed, it was previously ignored
* `fyne.Theme` moved to `fyne.LegacyTheme` and can be load to a new theme using `theme.FromLegacy`
* A new, more flexible, Theme interface has been created that we encourage developers to use
* The desktop.Cursor definition was renamed desktop.StandardCursor to make way for custom cursors

* Dialogs no longer show when created, unless using the ShowXxx convenience methods
Expand All @@ -21,9 +31,13 @@ These changes likely break some apps, please read the
### Added

* Add MouseButtonTertiary for middle mouse button events on desktop
* Add fyne.Vector for simple x/y coordinates

### Changed

* Coordinate system is now float32 - see breaking changes above
* ScrollEvent and DragEvent moved to Delta from (int, int)

* Change bundled resources to use more efficient string storage
* Desktop left and right mouse buttons renamed to MouseButtonPrimary and MouseButtonSecondary
* Many optimisations and widget performance enhancements
Expand Down
8 changes: 4 additions & 4 deletions canvas/animation.go
Expand Up @@ -49,7 +49,7 @@ func NewPositionAnimation(start, stop fyne.Position, d time.Duration, fn func(fy
return &fyne.Animation{
Duration: d,
Tick: func(done float32) {
fn(fyne.NewPos(scaleInt(start.X, xDelta, done), scaleInt(start.Y, yDelta, done)))
fn(fyne.NewPos(scaleVal(start.X, xDelta, done), scaleVal(start.Y, yDelta, done)))
}}
}

Expand All @@ -63,14 +63,14 @@ func NewSizeAnimation(start, stop fyne.Size, d time.Duration, fn func(fyne.Size)
return &fyne.Animation{
Duration: d,
Tick: func(done float32) {
fn(fyne.NewSize(scaleInt(start.Width, widthDelta, done), scaleInt(start.Height, heightDelta, done)))
fn(fyne.NewSize(scaleVal(start.Width, widthDelta, done), scaleVal(start.Height, heightDelta, done)))
}}
}

func scaleChannel(start int, diff, done float32) uint8 {
return uint8(start + int(diff*done))
}

func scaleInt(start int, delta, done float32) int {
return start + int(delta*done)
func scaleVal(start float32, delta, done float32) float32 {
return start + delta*done
}
8 changes: 4 additions & 4 deletions canvas/base_internal_test.go
Expand Up @@ -21,17 +21,17 @@ func TestBase_Move(t *testing.T) {
base.Move(fyne.NewPos(10, 10))
pos := base.Position()

assert.Equal(t, 10, pos.X)
assert.Equal(t, 10, pos.Y)
assert.Equal(t, float32(10), pos.X)
assert.Equal(t, float32(10), pos.Y)
}

func TestBase_Resize(t *testing.T) {
base := &baseObject{}
base.Resize(fyne.NewSize(10, 10))
size := base.Size()

assert.Equal(t, 10, size.Width)
assert.Equal(t, 10, size.Height)
assert.Equal(t, float32(10), size.Width)
assert.Equal(t, float32(10), size.Height)
}

func TestBase_HideShow(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions canvas/circle_test.go
Expand Up @@ -26,8 +26,8 @@ func TestCircle_FillColor(t *testing.T) {
}

func TestCircle_Resize(t *testing.T) {
targetWidth := 50
targetHeight := 50
targetWidth := float32(50)
targetHeight := float32(50)
circle := canvas.NewCircle(color.White)
start := circle.Size()
assert.True(t, start.Height == 0)
Expand Down
4 changes: 2 additions & 2 deletions canvas/line.go
Expand Up @@ -24,8 +24,8 @@ type Line struct {

// Size returns the current size of bounding box for this line object
func (l *Line) Size() fyne.Size {
return fyne.NewSize(int(math.Abs(float64(l.Position2.X)-float64(l.Position1.X))),
int(math.Abs(float64(l.Position2.Y)-float64(l.Position1.Y))))
return fyne.NewSize(float32(math.Abs(float64(l.Position2.X)-float64(l.Position1.X))),
float32(math.Abs(float64(l.Position2.Y)-float64(l.Position1.Y))))
}

// Resize sets a new bottom-right position for the line object and it will then be refreshed.
Expand Down
4 changes: 2 additions & 2 deletions canvas/line_test.go
Expand Up @@ -24,8 +24,8 @@ func TestLine_Resize(t *testing.T) {
line.Resize(fyne.NewSize(10, 0))
size := line.Size()

assert.Equal(t, size.Width, 10)
assert.Equal(t, size.Height, 0)
assert.Equal(t, float32(10), size.Width)
assert.Equal(t, float32(0), size.Height)
}

func TestLine_StrokeColor(t *testing.T) {
Expand Down
Binary file modified canvas/testdata/text/layout_long_center_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified canvas/testdata/text/layout_long_leading_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified canvas/testdata/text/layout_long_trailing_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified canvas/testdata/text/layout_short_center_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified canvas/testdata/text/layout_short_leading_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified canvas/testdata/text/layout_short_trailing_large.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion canvas/text.go
Expand Up @@ -18,7 +18,7 @@ type Text struct {

Color color.Color // The main text draw color
Text string // The string content of this Text
TextSize int // Size of the text - if the Canvas scale is 1.0 this will be equivalent to point size
TextSize float32 // Size of the text - if the Canvas scale is 1.0 this will be equivalent to point size
TextStyle fyne.TextStyle // The style of the text content
}

Expand Down
12 changes: 6 additions & 6 deletions canvas/text_test.go
Expand Up @@ -51,7 +51,7 @@ func TestText_Layout(t *testing.T) {
"short_leading_large": {
text: "abc",
align: fyne.TextAlignLeading,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
"long_leading_small": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Expand All @@ -61,7 +61,7 @@ func TestText_Layout(t *testing.T) {
"long_leading_large": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
align: fyne.TextAlignLeading,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
"short_center_small": {
text: "abc",
Expand All @@ -71,7 +71,7 @@ func TestText_Layout(t *testing.T) {
"short_center_large": {
text: "abc",
align: fyne.TextAlignCenter,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
"long_center_small": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Expand All @@ -81,7 +81,7 @@ func TestText_Layout(t *testing.T) {
"long_center_large": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
align: fyne.TextAlignCenter,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
"short_trailing_small": {
text: "abc",
Expand All @@ -91,7 +91,7 @@ func TestText_Layout(t *testing.T) {
"short_trailing_large": {
text: "abc",
align: fyne.TextAlignTrailing,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
"long_trailing_small": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Expand All @@ -101,7 +101,7 @@ func TestText_Layout(t *testing.T) {
"long_trailing_large": {
text: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
align: fyne.TextAlignTrailing,
size: fyne.NewSize(500, 100),
size: fyne.NewSize(500, 101),
},
} {
t.Run(name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/fyne_demo/tutorials/animation.go
Expand Up @@ -34,7 +34,7 @@ func makeAnimationCanvas() fyne.CanvasObject {
a2 = canvas.NewPositionAnimation(fyne.NewPos(0, 0), fyne.NewPos(350, 80), time.Second*3, func(p fyne.Position) {
i.Move(p)

width := int(10 + (float64(p.X) / 7))
width := 10 + (p.X / 7)
i.Resize(fyne.NewSize(width, width))
})
a2.Repeat = true
Expand Down Expand Up @@ -78,7 +78,7 @@ func makeAnimationCurves() fyne.CanvasObject {
return fyne.NewContainerWithoutLayout(label1, label2, label3, label4, box1, box2, box3, box4, start)
}

func makeAnimationCurveItem(label string, curve fyne.AnimationCurve, yOff int) (
func makeAnimationCurveItem(label string, curve fyne.AnimationCurve, yOff float32) (
text *widget.Label, box fyne.CanvasObject, anim *fyne.Animation) {
text = widget.NewLabel(label)
text.Alignment = fyne.TextAlignCenter
Expand Down
2 changes: 1 addition & 1 deletion cmd/fyne_demo/tutorials/theme.go
Expand Up @@ -42,7 +42,7 @@ func (customTheme) Icon(n fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(n)
}

func (customTheme) Size(s fyne.ThemeSizeName) int {
func (customTheme) Size(s fyne.ThemeSizeName) float32 {
switch s {
case theme.SizeNamePadding:
return 10
Expand Down
4 changes: 2 additions & 2 deletions cmd/fyne_settings/settings/scale.go
Expand Up @@ -24,7 +24,7 @@ var scales = []*scaleItems{

func (s *Settings) appliedScale(value float32) {
for _, scale := range scales {
scale.preview.TextSize = int(float32(theme.TextSize()) * scale.scale / value)
scale.preview.TextSize = theme.TextSize() * scale.scale / value
}
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func (s *Settings) makeScalePreviews(value float32) []fyne.CanvasObject {
for i, scale := range scales {
text := canvas.NewText("A", theme.TextColor())
text.Alignment = fyne.TextAlignCenter
text.TextSize = int(float32(theme.TextSize()) * scale.scale / value)
text.TextSize = theme.TextSize() * scale.scale / value

scale.preview = text
previews[i] = text
Expand Down
2 changes: 1 addition & 1 deletion cmd/fyne_settings/settings/scale_test.go
Expand Up @@ -40,5 +40,5 @@ func TestMakeScalePreviews(t *testing.T) {
assert.Equal(t, theme.TextSize(), previews[2].(*canvas.Text).TextSize)

s.appliedScale(1.5)
assert.Equal(t, int(float32(theme.TextSize())/1.5), previews[2].(*canvas.Text).TextSize)
assert.Equal(t, theme.TextSize()/1.5, previews[2].(*canvas.Text).TextSize)
}
2 changes: 1 addition & 1 deletion container.go
Expand Up @@ -73,7 +73,7 @@ func (c *Container) MinSize() Size {

minSize := NewSize(1, 1)
for _, child := range c.Objects {
minSize = minSize.Union(child.MinSize())
minSize = minSize.Max(child.MinSize())
}

return minSize
Expand Down
4 changes: 2 additions & 2 deletions dialog/base.go
Expand Up @@ -105,7 +105,7 @@ func (d *dialog) Layout(obj []fyne.CanvasObject, size fyne.Size) {
d.bg.Move(fyne.NewPos(0, 0))
d.bg.Resize(size)

btnMin := obj[3].MinSize().Union(obj[3].Size())
btnMin := obj[3].MinSize().Max(obj[3].Size())

// icon
iconHeight := padHeight*2 + d.label.MinSize().Height*2 - theme.Padding()
Expand All @@ -125,7 +125,7 @@ func (d *dialog) Layout(obj []fyne.CanvasObject, size fyne.Size) {

func (d *dialog) MinSize(obj []fyne.CanvasObject) fyne.Size {
contentMin := obj[2].MinSize()
btnMin := obj[3].MinSize().Union(obj[3].Size())
btnMin := obj[3].MinSize().Max(obj[3].Size())

width := fyne.Max(fyne.Max(contentMin.Width, btnMin.Width), obj[4].MinSize().Width) + padWidth
height := contentMin.Height + btnMin.Height + d.label.MinSize().Height + theme.Padding() + padHeight*2
Expand Down
6 changes: 3 additions & 3 deletions dialog/color_wheel.go
Expand Up @@ -131,20 +131,20 @@ func (a *colorWheel) colorAt(x, y, w, h int) color.Color {

func (a *colorWheel) locationForPosition(pos fyne.Position) (x, y int) {
can := fyne.CurrentApp().Driver().CanvasForObject(a)
x, y = pos.X, pos.Y
x, y = int(pos.X), int(pos.Y)
if can != nil {
x, y = can.PixelCoordinateForPosition(pos)
}
return
}

func (a *colorWheel) selection(width, height int) (int, int) {
func (a *colorWheel) selection(width, height float32) (float32, float32) {
w, h := float64(width), float64(height)
radius := float64(a.Saturation) / 100.0 * math.Min(w, h) / 2.0
degrees := float64(a.Hue)
radians := degrees * math.Pi / 180.0
c := cmplx.Rect(radius, radians)
return int(real(c) + w/2.0), int(imag(c) + h/2.0)
return float32(real(c) + w/2.0), float32(imag(c) + h/2.0)
}

func (a *colorWheel) trigger(pos fyne.Position) {
Expand Down
4 changes: 2 additions & 2 deletions dialog/confirm_test.go
Expand Up @@ -72,9 +72,9 @@ func TestConfirmDialog_Resize(t *testing.T) {
//Test resize - normal size scenario
size := fyne.NewSize(300, 180) //normal size to fit (600,400)
theDialog.Resize(size)
expectedWidth := 300
expectedWidth := float32(300)
assert.Equal(t, expectedWidth, theDialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight := 180
expectedHeight := float32(180)
assert.Equal(t, expectedHeight, theDialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - normal size scenario again
size = fyne.NewSize(310, 280) //normal size to fit (600,400)
Expand Down
2 changes: 1 addition & 1 deletion dialog/file.go
Expand Up @@ -165,7 +165,7 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
fileIconSize+theme.Padding()+fileTextSize)),
)
f.fileScroll = widget.NewScrollContainer(f.files)
verticalExtra := int(float64(fileIconSize) * 0.25)
verticalExtra := float32(float64(fileIconSize) * 0.25)
f.fileScroll.SetMinSize(fyne.NewSize(fileIconCellWidth*2+theme.Padding(),
(fileIconSize+fileTextSize)+theme.Padding()*2+verticalExtra))

Expand Down
4 changes: 2 additions & 2 deletions dialog/file_test.go
Expand Up @@ -128,9 +128,9 @@ func TestFileDialogResize(t *testing.T) {
//Test resize - normal size scenario
size := fyne.NewSize(200, 180) //normal size to fit (600,400)
file.Resize(size)
expectedWidth := 200
expectedWidth := float32(200)
assert.Equal(t, expectedWidth, file.dialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight := 180
expectedHeight := float32(180)
assert.Equal(t, expectedHeight, file.dialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - normal size scenario again
size = fyne.NewSize(300, 280) //normal size to fit (600,400)
Expand Down
4 changes: 2 additions & 2 deletions dialog/information_test.go
Expand Up @@ -33,9 +33,9 @@ func TestDialog_Resize(t *testing.T) {
//Test resize - normal size scenario
size := fyne.NewSize(300, 180) //normal size to fit (600,400)
theDialog.Resize(size)
expectedWidth := 300
expectedWidth := float32(300)
assert.Equal(t, expectedWidth, theDialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight := 180
expectedHeight := float32(180)
assert.Equal(t, expectedHeight, theDialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - normal size scenario again
size = fyne.NewSize(310, 280) //normal size to fit (600,400)
Expand Down
4 changes: 2 additions & 2 deletions dialog/progressinfinite_test.go
Expand Up @@ -32,9 +32,9 @@ func TestProgressInfiniteDialog_Resize(t *testing.T) {
//Test resize - normal size scenario
size := fyne.NewSize(300, 180) //normal size to fit (600,400)
theDialog.Resize(size)
expectedWidth := 300
expectedWidth := float32(300)
assert.Equal(t, expectedWidth, theDialog.win.Content.Size().Width+theme.Padding()*2)
expectedHeight := 180
expectedHeight := float32(180)
assert.Equal(t, expectedHeight, theDialog.win.Content.Size().Height+theme.Padding()*2)
//Test resize - normal size scenario again
size = fyne.NewSize(310, 280) //normal size to fit (600,400)
Expand Down
Binary file modified dialog/testdata/color/channel_layout_foobar_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/channel_layout_foobar_100.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/channel_layout_foobar_50.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_expanded_theme_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_expanded_theme_ugly.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_recents_theme_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_recents_theme_ugly.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_simple_recents_theme_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_simple_recents_theme_ugly.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_theme_default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/dialog_theme_ugly.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/color/picker_layout_advanced.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/dialog-custom-default.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dialog/testdata/dialog-custom-ugly.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion driver.go
Expand Up @@ -10,7 +10,7 @@ type Driver interface {

// RenderedTextSize returns the size required to render the given string of specified
// font size and style.
RenderedTextSize(string, int, TextStyle) Size
RenderedTextSize(string, float32, TextStyle) Size

// FileReaderForURI opens a file reader for the given resource indicator.
// This may refer to a filesystem (typical on desktop) or data from another application.
Expand Down
4 changes: 2 additions & 2 deletions event.go
Expand Up @@ -16,12 +16,12 @@ type PointEvent struct {
// The DeltaX and DeltaY represent how large the scroll was in two dimensions.
type ScrollEvent struct {
PointEvent
DeltaX, DeltaY int
Scrolled Delta
}

// DragEvent defines the parameters of a pointer or other drag event.
// The DraggedX and DraggedY fields show how far the item was dragged since the last event.
type DragEvent struct {
PointEvent
DraggedX, DraggedY int
Dragged Delta
}

0 comments on commit e30a503

Please sign in to comment.