Skip to content

Commit

Permalink
reduce theme lookups in slider
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 10, 2024
1 parent 8d62571 commit 1b3f8ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
36 changes: 19 additions & 17 deletions widget/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,16 @@ func (s *Slider) TypedKey(key *fyne.KeyEvent) {
func (s *Slider) TypedRune(_ rune) {
}

func (s *Slider) buttonDiameter() float32 {
return theme.IconInlineSize() - 4 // match radio icons
func (s *Slider) buttonDiameter(inlineIconSize float32) float32 {
return inlineIconSize - 4 // match radio icons
}

func (s *Slider) endOffset() float32 {
return s.buttonDiameter()/2 + theme.InnerPadding() - 1.5 // align with radio icons
func (s *Slider) endOffset(inlineIconSize, innerPadding float32) float32 {
return s.buttonDiameter(inlineIconSize)/2 + innerPadding - 1.5 // align with radio icons
}

func (s *Slider) getRatio(e *fyne.PointEvent) float64 {
pad := s.endOffset()
pad := s.endOffset(theme.IconInlineSize(), theme.InnerPadding())

x := e.Position.X
y := e.Position.Y
Expand Down Expand Up @@ -424,11 +424,10 @@ func (s *sliderRenderer) Refresh() {
s.track.FillColor = theme.InputBackgroundColor()
if s.slider.disabled {
s.thumb.FillColor = theme.DisabledColor()
s.active.FillColor = theme.DisabledColor()
} else {
s.thumb.FillColor = theme.ForegroundColor()
s.active.FillColor = theme.ForegroundColor()
}
s.active.FillColor = s.thumb.FillColor

if s.slider.focused && !s.slider.disabled {
s.focusIndicator.FillColor = theme.FocusColor()
Expand All @@ -447,27 +446,30 @@ func (s *sliderRenderer) Refresh() {

// Layout the components of the widget.
func (s *sliderRenderer) Layout(size fyne.Size) {
trackWidth := theme.InputBorderSize() * 2
diameter := s.slider.buttonDiameter()
endPad := s.slider.endOffset()
inputBorderSize := theme.InputBorderSize()
trackWidth := inputBorderSize * 2
inlineIconSize := theme.IconInlineSize()
innerPadding := theme.InnerPadding()
diameter := s.slider.buttonDiameter(inlineIconSize)
endPad := s.slider.endOffset(inlineIconSize, innerPadding)

var trackPos, activePos, thumbPos fyne.Position
var trackSize, activeSize fyne.Size

// some calculations are relative to trackSize, so we must update that first
switch s.slider.Orientation {
case Vertical:
trackPos = fyne.NewPos(size.Width/2-theme.InputBorderSize(), endPad)
trackPos = fyne.NewPos(size.Width/2-inputBorderSize, endPad)
trackSize = fyne.NewSize(trackWidth, size.Height-endPad*2)

case Horizontal:
trackPos = fyne.NewPos(endPad, size.Height/2-theme.InputBorderSize())
trackPos = fyne.NewPos(endPad, size.Height/2-inputBorderSize)
trackSize = fyne.NewSize(size.Width-endPad*2, trackWidth)
}
s.track.Move(trackPos)
s.track.Resize(trackSize)

activeOffset := s.getOffset() // TODO based on old size...0
activeOffset := s.getOffset(inlineIconSize, innerPadding) // TODO based on old size...0
switch s.slider.Orientation {
case Vertical:
activePos = fyne.NewPos(trackPos.X, activeOffset)
Expand All @@ -489,15 +491,15 @@ func (s *sliderRenderer) Layout(size fyne.Size) {
s.thumb.Move(thumbPos)
s.thumb.Resize(fyne.NewSize(diameter, diameter))

focusIndicatorSize := fyne.NewSquareSize(theme.IconInlineSize() + theme.InnerPadding())
focusIndicatorSize := fyne.NewSquareSize(inlineIconSize + innerPadding)
delta := (focusIndicatorSize.Width - diameter) / 2
s.focusIndicator.Resize(focusIndicatorSize)
s.focusIndicator.Move(thumbPos.SubtractXY(delta, delta))
}

// MinSize calculates the minimum size of a widget.
func (s *sliderRenderer) MinSize() fyne.Size {
dia := s.slider.buttonDiameter()
dia := s.slider.buttonDiameter(theme.IconInlineSize())
s1, s2 := minLongSide+dia, dia

switch s.slider.Orientation {
Expand All @@ -510,8 +512,8 @@ func (s *sliderRenderer) MinSize() fyne.Size {
return fyne.Size{Width: 0, Height: 0}
}

func (s *sliderRenderer) getOffset() float32 {
endPad := s.slider.endOffset()
func (s *sliderRenderer) getOffset(iconInlineSize, innerPadding float32) float32 {
endPad := s.slider.endOffset(iconInlineSize, innerPadding)
w := s.slider
size := s.track.Size()
if w.Value == w.Min || w.Min == w.Max {
Expand Down
6 changes: 4 additions & 2 deletions widget/slider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func TestSlider_HorizontalLayout(t *testing.T) {

assert.Greater(t, wSize.Width, wSize.Height)

assert.Equal(t, wSize.Width-slider.endOffset()*2, tSize.Width)
endOffset := slider.endOffset(theme.IconInlineSize(), theme.InnerPadding())
assert.Equal(t, wSize.Width-endOffset*2, tSize.Width)
assert.Equal(t, theme.InputBorderSize()*2, tSize.Height)

assert.Greater(t, wSize.Width, aSize.Width)
Expand Down Expand Up @@ -103,7 +104,8 @@ func TestSlider_VerticalLayout(t *testing.T) {

assert.Greater(t, wSize.Height, wSize.Width)

assert.Equal(t, wSize.Height-slider.endOffset()*2, tSize.Height)
endOffset := slider.endOffset(theme.IconInlineSize(), theme.InnerPadding())
assert.Equal(t, wSize.Height-endOffset*2, tSize.Height)
assert.Equal(t, theme.InputBorderSize()*2, tSize.Width)

assert.Greater(t, wSize.Height, aSize.Height)
Expand Down

0 comments on commit 1b3f8ed

Please sign in to comment.