Skip to content

Commit

Permalink
fixed trackpad horizontal scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
danaugrs committed May 29, 2018
1 parent c2e7b11 commit cb0b7b7
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions gui/scroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gui

import (
"github.com/g3n/engine/window"
"github.com/g3n/engine/math32"
)

// Scroller is the GUI element that allows scrolling of a target IPanel.
Expand Down Expand Up @@ -79,10 +80,7 @@ type ScrollerScrollbarStyle struct {
// TODO these configuration variables could be made part of a global engine configuration object in the future
// They should not be added to style since they are not style changes and not to the struct since they are global

// ScrollPreference specifies the default scroll direction if both scrollbars are present
const ScrollPreference = ScrollVertical

// ScrollModifierKey is the Key that changes the scrolling direction to the non-preferred direction
// ScrollModifierKey is the Key that changes the scrolling direction from vertical to horizontal
const ScrollModifierKey = window.KeyLeftShift

// NewScroller creates and returns a pointer to a new Scroller with the specified
Expand Down Expand Up @@ -301,33 +299,32 @@ func (s *Scroller) onScroll(evname string, ev interface{}) {
vScrollVisible := (s.vscroll != nil) && s.vscroll.Visible()
hScrollVisible := (s.hscroll != nil) && s.hscroll.Visible()

valOffset := sev.Yoffset / 10
mult := float32(1)/float32(10)
offsetX := sev.Xoffset * mult
offsetY := sev.Yoffset * mult

// If modifier key is pressed (left shift by default) - then scroll in the horizontal direction
if s.modKeyPressed {
if math32.Abs(offsetY) > math32.Abs(offsetX) {
offsetX = offsetY
}
offsetY = 0
}

log.Error("X: %v, Y: %v", offsetX, offsetY)

if vScrollVisible {
if hScrollVisible {
// Both scrollbars are present. Which to scroll depends on the system-set preference
pref := ScrollPreference
// If modifier key is pressed (left shift by default) - then scroll in the non-preferential direction
if s.modKeyPressed {
if pref == ScrollVertical {
pref = ScrollHorizontal
} else if pref == ScrollHorizontal {
pref = ScrollVertical
}
}
// Scroll the appropriate scrollbar
if pref == ScrollVertical {
s.vscroll.SetValue(float32(s.vscroll.Value()) - valOffset)
} else if pref == ScrollHorizontal {
s.hscroll.SetValue(float32(s.hscroll.Value()) - valOffset)
}
// Both scrollbars are present - scroll both
s.vscroll.SetValue(float32(s.vscroll.Value()) - offsetY)
s.hscroll.SetValue(float32(s.hscroll.Value()) - offsetX)
} else {
// Only vertical scrollbar present - scroll it
s.vscroll.SetValue(float32(s.vscroll.Value()) - valOffset)
s.vscroll.SetValue(float32(s.vscroll.Value()) - offsetY)
}
} else if hScrollVisible {
// Only horizontal scrollbar present - scroll it
s.hscroll.SetValue(float32(s.hscroll.Value()) - valOffset)
s.hscroll.SetValue(float32(s.hscroll.Value()) - offsetX)
}

s.recalc()
Expand Down

0 comments on commit cb0b7b7

Please sign in to comment.