Created
July 18, 2024 22:17
-
-
Save hkparker/873fffedc5bb94506e5bdbaf6d088408 to your computer and use it in GitHub Desktop.
Fyne layout to auto-scroll a container.Scroll when resized
This file contains hidden or 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
package ui | |
import ( | |
"fyne.io/fyne/v2" | |
"fyne.io/fyne/v2/container" | |
log "github.com/sirupsen/logrus" | |
) | |
type autoscollLayout struct { | |
lastHeight float32 | |
} | |
func (mal *autoscollLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) { | |
if len(objects) != 1 { | |
log.WithFields(log.Fields{ | |
"oject_count": len(objects), | |
}).Fatal("cannot create autoscollLayout with multiple objects") | |
} | |
embedded := objects[0] | |
embedded.Move(fyne.Position{0, 0}) | |
embedded.Resize(size) | |
s, ok := embedded.(*container.Scroll) | |
if !ok { | |
log.Fatal("cannot create autoscollLayout with anything other than container.Scroll") | |
} | |
if mal.lastHeight != 0 { | |
if size.Height < mal.lastHeight { | |
diff := mal.lastHeight - size.Height | |
s.Offset.Y += diff | |
s.Refresh() | |
} else { | |
diff := size.Height - mal.lastHeight | |
if s.Content.Size().Height-mal.lastHeight == s.Offset.Y+diff { | |
s.ScrollToBottom() | |
} else { | |
s.Offset.Y -= diff | |
s.Refresh() | |
} | |
} | |
} | |
mal.lastHeight = size.Height | |
} | |
func (mal *autoscollLayout) MinSize(objects []fyne.CanvasObject) fyne.Size { | |
if len(objects) != 1 { | |
log.WithFields(log.Fields{ | |
"oject_count": len(objects), | |
}).Fatal("cannot create autoscollLayout with multiple objects") | |
} | |
return objects[0].MinSize() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment