Skip to content

Commit

Permalink
internal: Last bit of code cleanup and improvements
Browse files Browse the repository at this point in the history
Refactors some code to be more readable, makes sure that we run MinSize() less often for the scroller and caches some sizes. I think all of the internal/ packages should be up to our usual standards now.
  • Loading branch information
Jacalz committed Dec 5, 2023
1 parent 30adb16 commit 2a5d472
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
8 changes: 4 additions & 4 deletions internal/overlay_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func (s *OverlayStack) TopFocusManager() *app.FocusManager {
}

func (s *OverlayStack) topFocusManager() *app.FocusManager {
var fm *app.FocusManager
if len(s.focusManagers) > 0 {
fm = s.focusManagers[len(s.focusManagers)-1]
if len(s.focusManagers) == 0 {
return nil
}
return fm

return s.focusManagers[len(s.focusManagers)-1]
}
4 changes: 2 additions & 2 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func (d *Decoder) Draw(width, height int) (*image.NRGBA, error) {
}

func IsFileSVG(path string) bool {
return strings.ToLower(filepath.Ext(path)) == ".svg"
return strings.EqualFold(filepath.Ext(path), ".svg")
}

// IsResourceSVG checks if the resource is an SVG or not.
func IsResourceSVG(res fyne.Resource) bool {
if strings.ToLower(filepath.Ext(res.Name())) == ".svg" {
if IsFileSVG(res.Name()) {
return true
}

Expand Down
33 changes: 18 additions & 15 deletions internal/widget/scroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,22 @@ func (r *scrollBarAreaRenderer) Refresh() {
}

func (r *scrollBarAreaRenderer) barSizeAndOffset(contentOffset, contentLength, scrollLength float32) (length, width, lengthOffset, widthOffset float32) {
scrollBarSize := theme.ScrollBarSize()
if scrollLength < contentLength {
portion := scrollLength / contentLength
length = float32(int(scrollLength)) * portion
if length < theme.ScrollBarSize() {
length = theme.ScrollBarSize()
}
length = fyne.Max(length, scrollBarSize)
} else {
length = scrollLength
}
if contentOffset != 0 {
lengthOffset = (scrollLength - length) * (contentOffset / (contentLength - scrollLength))
}
if r.area.isLarge {
width = theme.ScrollBarSize()
width = scrollBarSize
} else {
widthOffset = theme.ScrollBarSmallSize()
width = theme.ScrollBarSmallSize()
width = widthOffset
}
return
}
Expand Down Expand Up @@ -287,13 +286,14 @@ func (r *scrollContainerRenderer) Refresh() {
// push updated content object to baseRenderer
r.BaseRenderer.Objects()[0] = r.scroll.Content
}
if r.oldMinSize == r.scroll.Content.MinSize() && r.oldMinSize == r.scroll.Content.Size() &&
newMin := r.scroll.Content.MinSize()
if r.oldMinSize == newMin && r.oldMinSize == r.scroll.Content.Size() &&
(r.scroll.Size().Width <= r.oldMinSize.Width && r.scroll.Size().Height <= r.oldMinSize.Height) {
r.layoutBars(r.scroll.Size())
return
}

r.oldMinSize = r.scroll.Content.MinSize()
r.oldMinSize = newMin
r.Layout(r.scroll.Size())
}

Expand Down Expand Up @@ -463,7 +463,9 @@ func (s *Scroll) Scrolled(ev *fyne.ScrollEvent) {
}

func (s *Scroll) scrollBy(dx, dy float32) {
if s.Size().Width < s.Content.MinSize().Width && s.Size().Height >= s.Content.MinSize().Height && dx == 0 {
min := s.Content.MinSize()
size := s.Size()
if size.Width < min.Width && size.Height >= min.Height && dx == 0 {
dx, dy = dy, dx
}
if s.updateOffset(dx, dy) {
Expand All @@ -472,7 +474,10 @@ func (s *Scroll) scrollBy(dx, dy float32) {
}

func (s *Scroll) updateOffset(deltaX, deltaY float32) bool {
if s.Content.Size().Width <= s.Size().Width && s.Content.Size().Height <= s.Size().Height {
min := s.Content.MinSize()
contentSize := s.Content.Size()
size := s.Size()
if contentSize.Width <= size.Width && contentSize.Height <= size.Height {
if s.Offset.X != 0 || s.Offset.Y != 0 {
s.Offset.X = 0
s.Offset.Y = 0
Expand All @@ -482,8 +487,8 @@ func (s *Scroll) updateOffset(deltaX, deltaY float32) bool {
}
oldX := s.Offset.X
oldY := s.Offset.Y
s.Offset.X = computeOffset(s.Offset.X, -deltaX, s.Size().Width, s.Content.MinSize().Width)
s.Offset.Y = computeOffset(s.Offset.Y, -deltaY, s.Size().Height, s.Content.MinSize().Height)
s.Offset.X = computeOffset(s.Offset.X, -deltaX, size.Width, min.Width)
s.Offset.Y = computeOffset(s.Offset.Y, -deltaY, size.Height, min.Height)
if f := s.OnScrolled; f != nil && (s.Offset.X != oldX || s.Offset.Y != oldY) {
f(s.Offset)
}
Expand All @@ -495,10 +500,8 @@ func computeOffset(start, delta, outerWidth, innerWidth float32) float32 {
if offset+outerWidth >= innerWidth {
offset = innerWidth - outerWidth
}
if offset < 0 {
offset = 0
}
return offset

return fyne.Max(offset, 0)
}

// NewScroll creates a scrollable parent wrapping the specified content.
Expand Down

0 comments on commit 2a5d472

Please sign in to comment.