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

ResponsiveCanvasObject interface + object hiding #59

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 109 additions & 34 deletions layout/responsive.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ const (
// Breakpoint is a uint16 that should be set from const SMALL, MEDIUM, LARGE and XLARGE.
type responsiveConfig map[responsiveBreakpoint]float32

// hidableConfig = true if the object should be hidden at that breakpoint
type hidableConfig map[responsiveBreakpoint]bool

// newResponsiveConf return a new responsive configuration.
// The optional ratios must
// be 0 < ratio <= 1 and passed in this order:
// Responsive(object, smallRatio, mediumRatio, largeRatio, xlargeRatio)
//
// Responsive(object, smallRatio, mediumRatio, largeRatio, xlargeRatio)
//
// They are set to previous value if a value is not passed, or 1.0 if there is no previous value.
func newResponsiveConf(ratios ...float32) responsiveConfig {
responsive := responsiveConfig{}
Expand Down Expand Up @@ -124,40 +129,23 @@ func (resp *ResponsiveLayout) Layout(objects []fyne.CanvasObject, containerSize
// objects in a line
line := []fyne.CanvasObject{}

// cast windowSize.Width to responsiveBreakpoint (uint16)
ww := responsiveBreakpoint(window.Size().Width)
windowSize := window.Size()

// For each object, place it at the right position (pos) and resize it.
for _, o := range objects {
if o == nil || !o.Visible() {
continue
}

// get tht configuration
ro, ok := o.(*responsiveWidget)
ro, ok := o.(ResponsiveWidget)
if !ok {
log.Fatal("A non responsive object has been packed inside a ResponsibleLayout. This is impossible.")
}
conf := ro.responsiveConfig

line = append(line, o) // add the container to the line
size := o.MinSize() // get some informations

// adapt object witdh from the configuration
if ww <= SMALL {
size.Width = conf[SMALL] * containerSize.Width
} else if ww <= MEDIUM {
size.Width = conf[MEDIUM] * containerSize.Width
} else if ww <= LARGE {
size.Width = conf[LARGE] * containerSize.Width
} else {
size.Width = conf[XLARGE] * containerSize.Width
ro.HandleResize(pos, windowSize, containerSize)
if !ro.Visible() {
continue
}

// place and resize the element
o.Resize(size)
o.Move(pos)

size := ro.Size()
line = append(line, o) // add the container to the line
// next element X position
pos = pos.Add(fyne.NewPos(size.Width+theme.Padding(), 0))

Expand Down Expand Up @@ -232,12 +220,13 @@ func (resp *ResponsiveLayout) maxFloat32(a, b float32) float32 {
// configure the rule, each object could be encapsulated by a "Responsive" object.
//
// Example:
// container := NewResponsiveLayout(
// Responsive(label, 1, .5, .25), // 100% for small, 50% for medium, 25% for large
// Responsive(button, 1, .5, .25), // ...
// label2, // this will be placed and resized with default behaviors
// // => 1, 1, 1
// )
//
// container := NewResponsiveLayout(
// Responsive(label, 1, .5, .25), // 100% for small, 50% for medium, 25% for large
// Responsive(button, 1, .5, .25), // ...
// label2, // this will be placed and resized with default behaviors
// // => 1, 1, 1
// )
func NewResponsiveLayout(o ...fyne.CanvasObject) *fyne.Container {
r := &ResponsiveLayout{}

Expand All @@ -252,27 +241,113 @@ func NewResponsiveLayout(o ...fyne.CanvasObject) *fyne.Container {
return container.New(r, objects...)
}

type ResponsiveWidget interface {
fyne.Widget
// ComputeWindowResize calculates, resizes and returns the object size
// based on the window resizing to a new width. Returns 0 if not visible
HandleResize(newPos fyne.Position, windowSize, containerSize fyne.Size)
}

type HidableResponsiveWidget interface {
ResponsiveWidget
// Configure whether the element should be hidden at each breakpoint
Hidable(hiddenAtBreakpoint ...bool)
}

type responsiveWidget struct {
widget.BaseWidget

render fyne.CanvasObject
responsiveConfig responsiveConfig
hidableConfig *hidableConfig
}

var _ fyne.Widget = (*responsiveWidget)(nil)

// Responsive register the object with a responsive configuration.
// The optional ratios must
// be 0 < ratio <= 1 and passed in this order:
// Responsive(object, smallRatio, mediumRatio, largeRatio, xlargeRatio)
//
// Responsive(object, smallRatio, mediumRatio, largeRatio, xlargeRatio)
//
// They are set to previous value if a value is not passed, or 1.0 if there is no previous value.
// The returned object is not modified.
func Responsive(object fyne.CanvasObject, breakpointRatio ...float32) fyne.CanvasObject {
ro := &responsiveWidget{render: object, responsiveConfig: newResponsiveConf(breakpointRatio...)}
func Responsive(object fyne.CanvasObject, breakpointRatio ...float32) ResponsiveWidget {
ro := &responsiveWidget{
render: object,
responsiveConfig: newResponsiveConf(breakpointRatio...),
hidableConfig: nil,
}
ro.ExtendBaseWidget(ro)
return ro
}

func HidableResponsive(object fyne.CanvasObject, breakpointRatio ...float32) HidableResponsiveWidget {
return Responsive(object, breakpointRatio...).(HidableResponsiveWidget)
}

func (ro *responsiveWidget) Hidable(hiddenAtBreakpoint ...bool) {
hc := hidableConfig{
SMALL: false,
MEDIUM: false,
LARGE: false,
XLARGE: false,
}

for i, bp := range []responsiveBreakpoint{SMALL, MEDIUM, LARGE, XLARGE} {
if len(hiddenAtBreakpoint) > i {
hc[bp] = hiddenAtBreakpoint[i]
}
}

ro.hidableConfig = &hc
}

func (ro *responsiveWidget) hideOnResize(windowSize fyne.Size) {
// cast windowSize.Width to responsiveBreakpoint (uint16)
windowWidth := responsiveBreakpoint(windowSize.Width)

if ro.hidableConfig != nil {
hideConf := *ro.hidableConfig
for _, bp := range []responsiveBreakpoint{SMALL, MEDIUM, LARGE, XLARGE} {
if windowWidth <= bp {
if hideConf[bp] {
ro.Hide()
} else {
ro.Show()
}
break
}
}
}
}

func (ro *responsiveWidget) HandleResize(
newPos fyne.Position,
windowSize, containerSize fyne.Size) {

ro.Move(newPos)

ro.hideOnResize(windowSize)
if !ro.Visible() {
return
}

conf := ro.responsiveConfig
size := ro.MinSize() // get some informations

// cast windowSize.Width to responsiveBreakpoint (uint16)
windowWidth := responsiveBreakpoint(windowSize.Width)
// adapt object witdh from the configuration
for _, bp := range []responsiveBreakpoint{SMALL, MEDIUM, LARGE, XLARGE} {
if windowWidth <= bp {
size.Width = conf[bp] * containerSize.Width
break
}
}
ro.Resize(size)
}

func (ro *responsiveWidget) CreateRenderer() fyne.WidgetRenderer {
if ro.render == nil {
return nil
Expand Down
34 changes: 34 additions & 0 deletions layout/responsive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ func TestResponsive_Responsive(t *testing.T) {
assert.Equal(t, w/2-padding*2, size2.Width)
}

// Test is a basic responsive layout is correctly configured. This test 2 widgets
// with 100% for small size and 50% for medium size or taller.
func TestResponsive_HidableResponsive(t *testing.T) {
padding := theme.Padding()

// build
label1 := HidableResponsive(widget.NewLabel("Hello World"), 1, .5)
label2 := HidableResponsive(widget.NewLabel("Hello World"), 1, .5)
label2.Hidable(true, false)

win := test.NewWindow(
NewResponsiveLayout(label1, label2),
)
win.SetPadded(true)
defer win.Close()

// First, we are at w < SMALL so the labels should be sized to 100% of the layout
w, h := float32(SMALL), float32(300)
win.Resize(fyne.NewSize(w, h))
size1 := label1.Size()
size2 := label2.Size()
assert.Equal(t, w-padding*2, size1.Width)
assert.Equal(t, float32(0), size2.Width)

// Then resize to w > SMALL so the labels should be sized to 50% of the layout
w = float32(MEDIUM)
win.Resize(fyne.NewSize(w, h))
size1 = label1.Size()
size2 = label2.Size()
// remove 2 * padding as there is 2 objects in a line
assert.Equal(t, w/2-padding*2, size1.Width)
assert.Equal(t, w/2-padding*2, size2.Width)
}

// Check if a widget that overflows the container goes to the next line.
func TestResponsive_GoToNextLine(t *testing.T) {
w, h := float32(200), float32(300)
Expand Down