-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it a real container and optimize computation
- Loading branch information
Showing
5 changed files
with
154 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package container | ||
|
||
import ( | ||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/container" | ||
"fyne.io/x/fyne/layout" | ||
) | ||
|
||
// NewResponsive returns a container with a responsive layout. The objects | ||
// can be copmmon containers or responsive objects using the Responsive() | ||
// function. | ||
// | ||
// Example: | ||
// | ||
// NewResponsive( | ||
// widget.NewLabel("Hello World"), // 100% by default | ||
// Responsive(widget.NewLabel("Hello World"), 1, .5), // 100% for small, 50% for others | ||
// Responsive(widget.NewLabel("Hello World"), 1, .5), // 100% for small, 50% for others | ||
// ) | ||
func NewResponsive(objects ...fyne.CanvasObject) *fyne.Container { | ||
container := container.New(layout.NewResponsiveLayout()) | ||
container.Objects = objects | ||
return container | ||
} | ||
|
||
// Responsive returns a responsive object configured with breakpoint sizes. | ||
// If no size is provided, the object will be 100% of the layout. | ||
// The number of sizes can be up to 4, for small, medium, large and extra large. | ||
// If more than 4 sizes are provided, the extra sizes are ignored. | ||
// | ||
// Example: | ||
// | ||
// Responsive(widget.NewLabel("Hello World"), 1, .5) | ||
// | ||
// It's commonly use in a responsive container, like this: | ||
// | ||
// NewResponsive( | ||
// Responsive(widget.NewLabel("Hello World"), 1, .5), | ||
// Responsive(widget.NewLabel("Hello World"), 1, .5), | ||
// ) | ||
// | ||
// Or with the Add() method: | ||
// | ||
// ctn := NewResponsive() | ||
// ctn.Add(Responsive(widget.NewLabel("Hello World"), 1, .5)) | ||
// ctn.Add(Responsive(widget.NewLabel("Hello World"), 1, .5)) | ||
// | ||
// This function is a shortcut for layout.Responsive() | ||
func Responsive(object fyne.CanvasObject, sizes ...float32) fyne.CanvasObject { | ||
return layout.Responsive(object, sizes...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package container | ||
|
||
import ( | ||
"testing" | ||
|
||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/test" | ||
"fyne.io/fyne/v2/widget" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// This is a simple test to check if the responsive layout is correctly configured. | ||
// The others tests are in the layout package. | ||
|
||
// Check if a simple widget is responsive to fill 100% of the layout. | ||
func TestResponsive_Responsive(t *testing.T) { | ||
responsive := NewResponsive( | ||
widget.NewLabel("Hello World"), // 100% by default | ||
Responsive(widget.NewLabel("Hello World"), 1, .5), // 100% for small, 50% for others | ||
Responsive(widget.NewLabel("Hello World"), 1, .5), // 100% for small, 50% for others | ||
) | ||
win := test.NewWindow(responsive) | ||
win.Resize(fyne.NewSize(320, 480)) | ||
size1 := responsive.Objects[1].Size() | ||
size2 := responsive.Objects[2].Size() | ||
assert.Equal(t, size1.Width, size2.Width) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters