Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs/default canvas scale #3673

Merged
merged 6 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions canvas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,10 @@ func (img *Image) minSizeFromReader(source io.Reader) (fyne.Size, error) {
img.aspect = float32(width) / float32(height)
}

app := fyne.CurrentApp()
if app == nil {
return fyne.NewSize(0, 0), errors.New("no current app")
}
driver := app.Driver()
if driver == nil {
return fyne.NewSize(0, 0), errors.New("no current driver")
}
c := driver.CanvasForObject(img)
if c == nil {
return fyne.NewSize(0, 0), errors.New("object is not attached to a canvas yet")
dpSize, err := scale.AdaptToFyneCoordinate(img, width, height)
if err != nil {
dpSize = fyne.NewSize(float32(width), float32(height))
}
dpSize := fyne.NewSize(scale.ToFyneCoordinate(c, width), scale.ToFyneCoordinate(c, height))

return dpSize, nil
}
Expand Down
14 changes: 14 additions & 0 deletions driver/software/render_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package software

import (
"image/color"
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -49,3 +51,15 @@ func TestRenderCanvas(t *testing.T) {
test.AssertImageMatches(t, "canvas.png", RenderCanvas(c, theme.LightTheme()))
}
}

func TestRender_ImageSize(t *testing.T) {
image := canvas.NewImageFromFile("../../theme/icons/fyne.png")
image.FillMode = canvas.ImageFillOriginal
bg := canvas.NewCircle(color.NRGBA{255, 0, 0, 128})
bg.StrokeColor = color.White
bg.StrokeWidth = 5

c := container.NewMax(image, bg)

test.AssertImageMatches(t, "image_size.png", Render(c, theme.LightTheme()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using this deprecated API why not use the test theme? Or default theme

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did just follow the pattern of the other tests in this file. I don't really mind switching to a different API, but should we do the same for the rest of the file to keep things consistent?

}
Binary file added driver/software/testdata/image_size.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions internal/scale/scale.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scale

import (
"errors"
"math"

"fyne.io/fyne/v2"
Expand All @@ -22,3 +23,21 @@ func ToFyneCoordinate(c fyne.Canvas, v int) float32 {
return float32(v) / c.Scale()
}
}

func AdaptToFyneCoordinate(obj fyne.CanvasObject, width, height int) (fyne.Size, error) {
app := fyne.CurrentApp()
if app == nil {
return fyne.NewSize(0, 0), errors.New("no current app")
}
driver := app.Driver()
if driver == nil {
return fyne.NewSize(0, 0), errors.New("no current driver")
}
c := driver.CanvasForObject(obj)
if c == nil {
return fyne.NewSize(0, 0), errors.New("object is not attached to a canvas yet")
}
dpSize := fyne.NewSize(ToFyneCoordinate(c, width), ToFyneCoordinate(c, height))

return dpSize, nil
}