scroller.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/math32"
  7. "github.com/g3n/engine/window"
  8. )
  9. // Scroller is the GUI element that allows scrolling of a target IPanel.
  10. // A scroller can have up to two scrollbars, one vertical and one horizontal.
  11. // The vertical scrollbar, if any, can be located either on the left or on the right.
  12. // The horizontal scrollbar, if any, can be located either on the top or on the bottom.
  13. // The interlocking of the scrollbars (which happens when both scrollbars are visible) can be configured.
  14. // Whether each scrollbar overlaps the content can also be configured (useful for transparent UIs).
  15. type Scroller struct {
  16. Panel // Embedded panel
  17. mode ScrollMode // ScrollMode specifies which scroll directions are allowed
  18. target IPanel // The IPanel that will be scrolled through
  19. hscroll *ScrollBar // Horizontal scrollbar (may be nil)
  20. vscroll *ScrollBar // Vertical scrollbar (may be nil)
  21. style *ScrollerStyle // The current style
  22. corner *Panel // The optional corner panel (can be visible when scrollMode==Both, interlocking==None, corner=true)
  23. cursorOver bool // Cursor is over the scroller
  24. modKeyPressed bool // Modifier key is pressed
  25. }
  26. // ScrollMode specifies which scroll directions are allowed.
  27. type ScrollMode int
  28. // The various scroll modes.
  29. const (
  30. ScrollNone = ScrollMode(0x00) // No scrolling allowed
  31. ScrollVertical = ScrollMode(0x01) // Vertical scrolling allowed
  32. ScrollHorizontal = ScrollMode(0x02) // Horizontal scrolling allowed
  33. ScrollBoth = ScrollMode(ScrollVertical | ScrollHorizontal) // Both vertical and horizontal scrolling allowed
  34. )
  35. // ScrollbarInterlocking specifies what happens where the vertical and horizontal scrollbars meet.
  36. type ScrollbarInterlocking int
  37. // The three scrollbar interlocking types.
  38. const (
  39. ScrollbarInterlockingNone = ScrollbarInterlocking(iota) // No scrollbar interlocking
  40. ScrollbarInterlockingVertical // Vertical scrollbar takes precedence
  41. ScrollbarInterlockingHorizontal // Horizontal scrollbar takes precedence
  42. )
  43. // ScrollbarPosition specifies where the scrollbar is located.
  44. // For the vertical scrollbar it specifies whether it's added to the left or to the right.
  45. // For the horizontal scrollbar it specifies whether it's added to the top or to the bottom.
  46. type ScrollbarPosition int
  47. // The four possible scrollbar positions.
  48. const (
  49. ScrollbarLeft = ScrollbarPosition(iota) // Scrollbar is positioned on the left of the scroller
  50. ScrollbarRight // Scrollbar is positioned on the right of the scroller
  51. ScrollbarTop // Scrollbar is positioned on the top of the scroller
  52. ScrollbarBottom // Scrollbar is positioned on the bottom of the scroller
  53. )
  54. // ScrollerStyle contains the styling of a Scroller
  55. type ScrollerStyle struct {
  56. PanelStyle // Embedded PanelStyle
  57. VerticalScrollbar ScrollerScrollbarStyle // The style of the vertical scrollbar
  58. HorizontalScrollbar ScrollerScrollbarStyle // The style of the horizontal scrollbar
  59. CornerPanel PanelStyle // The style of the corner panel
  60. ScrollbarInterlocking ScrollbarInterlocking // Specifies what happens where the vertical and horizontal scrollbars meet
  61. CornerCovered bool // True indicates that the corner panel should be visible when appropriate
  62. }
  63. // ScrollerScrollbarStyle is the set of style options for a scrollbar that is part of a scroller.
  64. type ScrollerScrollbarStyle struct {
  65. ScrollBarStyle // Embedded ScrollBarStyle (TODO, should be ScrollBarStyle*S*, implement style logic)
  66. Position ScrollbarPosition // Specifies the positioning of the scrollbar
  67. Broadness float32 // Broadness of the scrollbar
  68. OverlapContent bool // Specifies whether the scrollbar is shown above the content area
  69. AutoSizeButton bool // Specifies whether the scrollbar button size is adjusted based on content/view proportion
  70. }
  71. // TODO these configuration variables could be made part of a global engine configuration object in the future
  72. // They should not be added to style since they are not style changes and not to the struct since they are global
  73. // ScrollModifierKey is the Key that changes the scrolling direction from vertical to horizontal
  74. const ScrollModifierKey = window.KeyLeftShift
  75. // NewScroller creates and returns a pointer to a new Scroller with the specified
  76. // target IPanel and ScrollMode.
  77. func NewScroller(width, height float32, mode ScrollMode, target IPanel) *Scroller {
  78. s := new(Scroller)
  79. s.initialize(width, height, mode, target)
  80. return s
  81. }
  82. // initialize initializes this scroller and can be called by other types which embed a scroller
  83. func (s *Scroller) initialize(width, height float32, mode ScrollMode, target IPanel) {
  84. s.Panel.Initialize(s, width, height)
  85. s.style = &StyleDefault().Scroller
  86. s.target = target
  87. s.Panel.Add(s.target)
  88. s.mode = mode
  89. s.Subscribe(OnCursorEnter, s.onCursor)
  90. s.Subscribe(OnCursorLeave, s.onCursor)
  91. s.Subscribe(OnScroll, s.onScroll)
  92. s.Subscribe(OnKeyDown, s.onKey)
  93. s.Subscribe(OnKeyUp, s.onKey)
  94. s.Subscribe(OnResize, s.onResize)
  95. s.Update()
  96. }
  97. // SetScrollMode sets the scroll mode
  98. func (s *Scroller) SetScrollMode(mode ScrollMode) {
  99. s.mode = mode
  100. s.Update()
  101. }
  102. // ScrollMode returns the current scroll mode
  103. func (s *Scroller) ScrollMode() ScrollMode {
  104. return s.mode
  105. }
  106. // SetScrollbarInterlocking sets the scrollbar interlocking mode
  107. func (s *Scroller) SetScrollbarInterlocking(interlocking ScrollbarInterlocking) {
  108. s.style.ScrollbarInterlocking = interlocking
  109. s.Update()
  110. }
  111. // ScrollbarInterlocking returns the current scrollbar interlocking mode
  112. func (s *Scroller) ScrollbarInterlocking() ScrollbarInterlocking {
  113. return s.style.ScrollbarInterlocking
  114. }
  115. // SetCornerCovered specifies whether the corner covering panel is shown when appropriate
  116. func (s *Scroller) SetCornerCovered(state bool) {
  117. s.style.CornerCovered = state
  118. s.Update()
  119. }
  120. // CornerCovered returns whether the corner covering panel is being shown when appropriate
  121. func (s *Scroller) CornerCovered() bool {
  122. return s.style.CornerCovered
  123. }
  124. // SetVerticalScrollbarPosition sets the position of the vertical scrollbar (i.e. left or right)
  125. func (s *Scroller) SetVerticalScrollbarPosition(pos ScrollbarPosition) {
  126. s.style.VerticalScrollbar.Position = pos
  127. s.recalc()
  128. }
  129. // VerticalScrollbarPosition returns the current position of the vertical scrollbar (i.e. left or right)
  130. func (s *Scroller) VerticalScrollbarPosition() ScrollbarPosition {
  131. return s.style.VerticalScrollbar.Position
  132. }
  133. // SetHorizontalScrollbarPosition sets the position of the horizontal scrollbar (i.e. top or bottom)
  134. func (s *Scroller) SetHorizontalScrollbarPosition(pos ScrollbarPosition) {
  135. s.style.HorizontalScrollbar.Position = pos
  136. s.recalc()
  137. }
  138. // HorizontalScrollbarPosition returns the current position of the horizontal scrollbar (i.e. top or bottom)
  139. func (s *Scroller) HorizontalScrollbarPosition() ScrollbarPosition {
  140. return s.style.HorizontalScrollbar.Position
  141. }
  142. // SetVerticalScrollbarOverlapping specifies whether the vertical scrollbar overlaps the content area
  143. func (s *Scroller) SetVerticalScrollbarOverlapping(state bool) {
  144. s.style.VerticalScrollbar.OverlapContent = state
  145. s.Update()
  146. }
  147. // VerticalScrollbarOverlapping returns whether the vertical scrollbar overlaps the content area
  148. func (s *Scroller) VerticalScrollbarOverlapping() bool {
  149. return s.style.VerticalScrollbar.OverlapContent
  150. }
  151. // SetHorizontalScrollbarOverlapping specifies whether the horizontal scrollbar overlaps the content area
  152. func (s *Scroller) SetHorizontalScrollbarOverlapping(state bool) {
  153. s.style.HorizontalScrollbar.OverlapContent = state
  154. s.Update()
  155. }
  156. // HorizontalScrollbarOverlapping returns whether the horizontal scrollbar overlaps the content area
  157. func (s *Scroller) HorizontalScrollbarOverlapping() bool {
  158. return s.style.HorizontalScrollbar.OverlapContent
  159. }
  160. // SetVerticalScrollbarAutoSizeButton specifies whether the vertical scrollbar button is sized automatically
  161. func (s *Scroller) SetVerticalScrollbarAutoSizeButton(state bool) {
  162. s.style.VerticalScrollbar.AutoSizeButton = state
  163. if s.vscroll != nil {
  164. if state == false {
  165. s.vscroll.SetButtonSize(s.style.VerticalScrollbar.ScrollBarStyle.ButtonLength)
  166. }
  167. s.recalc()
  168. }
  169. }
  170. // VerticalScrollbarAutoSizeButton returns whether the vertical scrollbar button is sized automatically
  171. func (s *Scroller) VerticalScrollbarAutoSizeButton() bool {
  172. return s.style.VerticalScrollbar.AutoSizeButton
  173. }
  174. // SetHorizontalScrollbarAutoSizeButton specifies whether the horizontal scrollbar button is sized automatically
  175. func (s *Scroller) SetHorizontalScrollbarAutoSizeButton(state bool) {
  176. s.style.HorizontalScrollbar.AutoSizeButton = state
  177. if s.hscroll != nil {
  178. if state == false {
  179. s.hscroll.SetButtonSize(s.style.HorizontalScrollbar.ScrollBarStyle.ButtonLength)
  180. }
  181. s.recalc()
  182. }
  183. }
  184. // HorizontalScrollbarAutoSizeButton returns whether the horizontal scrollbar button is sized automatically
  185. func (s *Scroller) HorizontalScrollbarAutoSizeButton() bool {
  186. return s.style.HorizontalScrollbar.AutoSizeButton
  187. }
  188. // SetVerticalScrollbarBroadness sets the broadness of the vertical scrollbar
  189. func (s *Scroller) SetVerticalScrollbarBroadness(broadness float32) {
  190. s.style.VerticalScrollbar.Broadness = broadness
  191. if s.vscroll != nil {
  192. s.vscroll.SetWidth(broadness)
  193. s.Update()
  194. }
  195. }
  196. // VerticalScrollbarBroadness returns the broadness of the vertical scrollbar
  197. func (s *Scroller) VerticalScrollbarBroadness() float32 {
  198. return s.style.VerticalScrollbar.Broadness
  199. }
  200. // SetHorizontalScrollbarBroadness sets the broadness of the horizontal scrollbar
  201. func (s *Scroller) SetHorizontalScrollbarBroadness(broadness float32) {
  202. s.style.HorizontalScrollbar.Broadness = broadness
  203. if s.hscroll != nil {
  204. s.hscroll.SetHeight(broadness)
  205. s.Update()
  206. }
  207. }
  208. // HorizontalScrollbarBroadness returns the broadness of the horizontal scrollbar
  209. func (s *Scroller) HorizontalScrollbarBroadness() float32 {
  210. return s.style.HorizontalScrollbar.Broadness
  211. }
  212. // ScrollTo scrolls the target panel such that the specified target point is centered on the scroller's view area
  213. func (s *Scroller) ScrollTo(x, y float32) {
  214. // TODO
  215. }
  216. // onCursor receives subscribed cursor events over the panel
  217. func (s *Scroller) onCursor(evname string, ev interface{}) {
  218. switch evname {
  219. case OnCursorEnter:
  220. s.root.SetScrollFocus(s)
  221. s.root.SetKeyFocus(s)
  222. s.cursorOver = true
  223. case OnCursorLeave:
  224. s.root.SetScrollFocus(nil)
  225. s.root.SetKeyFocus(nil)
  226. s.cursorOver = false
  227. }
  228. s.root.StopPropagation(Stop3D)
  229. }
  230. // onScroll receives mouse scroll events when this scroller has the scroll focus (set by OnMouseEnter)
  231. func (s *Scroller) onScroll(evname string, ev interface{}) {
  232. sev := ev.(*window.ScrollEvent)
  233. vScrollVisible := (s.vscroll != nil) && s.vscroll.Visible()
  234. hScrollVisible := (s.hscroll != nil) && s.hscroll.Visible()
  235. mult := float32(1) / float32(10)
  236. offsetX := sev.Xoffset * mult
  237. offsetY := sev.Yoffset * mult
  238. // If modifier key is pressed (left shift by default) - then scroll in the horizontal direction
  239. if s.modKeyPressed {
  240. if math32.Abs(offsetY) > math32.Abs(offsetX) {
  241. offsetX = offsetY
  242. }
  243. offsetY = 0
  244. }
  245. log.Error("X: %v, Y: %v", offsetX, offsetY)
  246. if vScrollVisible {
  247. if hScrollVisible {
  248. // Both scrollbars are present - scroll both
  249. s.vscroll.SetValue(float32(s.vscroll.Value()) - offsetY)
  250. s.hscroll.SetValue(float32(s.hscroll.Value()) - offsetX)
  251. } else {
  252. // Only vertical scrollbar present - scroll it
  253. s.vscroll.SetValue(float32(s.vscroll.Value()) - offsetY)
  254. }
  255. } else if hScrollVisible {
  256. // Only horizontal scrollbar present - scroll it
  257. s.hscroll.SetValue(float32(s.hscroll.Value()) - offsetX)
  258. }
  259. s.recalc()
  260. s.root.StopPropagation(Stop3D)
  261. }
  262. // onKey receives key events
  263. func (s *Scroller) onKey(evname string, ev interface{}) {
  264. key := ev.(*window.KeyEvent)
  265. log.Error("Key %v", key)
  266. if key.Keycode == ScrollModifierKey {
  267. if evname == OnKeyDown {
  268. s.modKeyPressed = true
  269. log.Error("true")
  270. } else if evname == OnKeyUp {
  271. log.Error("false")
  272. s.modKeyPressed = false
  273. }
  274. }
  275. s.root.StopPropagation(Stop3D)
  276. }
  277. // onResize receives resize events
  278. func (s *Scroller) onResize(evname string, ev interface{}) {
  279. s.Update()
  280. }
  281. // setVerticalScrollbarVisible sets the vertical scrollbar visible, creating and initializing it if it's the first time
  282. func (s *Scroller) setVerticalScrollbarVisible() {
  283. if s.vscroll == nil {
  284. s.vscroll = NewVScrollBar(s.style.VerticalScrollbar.Broadness, 0)
  285. s.vscroll.applyStyle(&s.style.VerticalScrollbar.ScrollBarStyle)
  286. s.vscroll.Subscribe(OnChange, s.onScrollBarEvent)
  287. s.Add(s.vscroll)
  288. }
  289. s.vscroll.SetVisible(true)
  290. }
  291. // setVerticalScrollbarVisible sets the horizontal scrollbar visible, creating and initializing it if it's the first time
  292. func (s *Scroller) setHorizontalScrollbarVisible() {
  293. if s.hscroll == nil {
  294. s.hscroll = NewHScrollBar(0, s.style.HorizontalScrollbar.Broadness)
  295. s.hscroll.applyStyle(&s.style.HorizontalScrollbar.ScrollBarStyle)
  296. s.hscroll.Subscribe(OnChange, s.onScrollBarEvent)
  297. s.Add(s.hscroll)
  298. }
  299. s.hscroll.SetVisible(true)
  300. }
  301. // updateScrollbarsVisibility updates the visibility of the scrollbars and corner panel, creating them if necessary.
  302. // This method should be called when either the target panel changes size or when either the scroll mode or
  303. // style of the Scroller changes.
  304. func (s *Scroller) updateScrollbarsVisibility() {
  305. // Obtain the size of the target panel
  306. targetWidth := s.target.TotalWidth()
  307. targetHeight := s.target.TotalHeight()
  308. // If vertical scrolling is enabled and the vertical scrollbar should be visible
  309. if (s.mode&ScrollVertical > 0) && (targetHeight > s.content.Height) {
  310. s.setVerticalScrollbarVisible()
  311. } else if s.vscroll != nil {
  312. s.vscroll.SetVisible(false)
  313. s.vscroll.SetValue(0)
  314. }
  315. // If horizontal scrolling is enabled and the horizontal scrollbar should be visible
  316. if (s.mode&ScrollHorizontal > 0) && (targetWidth > s.content.Width) {
  317. s.setHorizontalScrollbarVisible()
  318. } else if s.hscroll != nil {
  319. s.hscroll.SetVisible(false)
  320. s.hscroll.SetValue(0)
  321. }
  322. // If both scrollbars can be visible we need to check whether we should show the corner panel and also whether
  323. // any scrollbar's presence caused the other to be required. The latter is a literal and figurative edge case
  324. // that happens when the target panel is larger than the content in one dimension but smaller than the content
  325. // in the other, and in the dimension that it is smaller than the content, the difference is less than the width
  326. // of the scrollbar. In that case we need to show both scrollbars to allow viewing of the complete target panel.
  327. if s.mode == ScrollBoth {
  328. vScrollVisible := (s.vscroll != nil) && s.vscroll.Visible()
  329. hScrollVisible := (s.hscroll != nil) && s.hscroll.Visible()
  330. // Check if adding any of the scrollbars ended up covering an edge of the target. If that's the case,
  331. // then show the other scrollbar as well (if the covering scrollbar's style is set to non-overlapping).
  332. // If the vertical scrollbar is visible and covering the target (and its style is not set to overlap)
  333. if vScrollVisible && (targetWidth > (s.content.Width - s.vscroll.width)) && !s.style.VerticalScrollbar.OverlapContent {
  334. s.setHorizontalScrollbarVisible() // Show the other scrollbar too
  335. }
  336. // If the horizontal scrollbar is visible and covering the target (and its style is not set to overlap)
  337. if hScrollVisible && (targetHeight > (s.content.Height - s.hscroll.height)) && !s.style.HorizontalScrollbar.OverlapContent {
  338. s.setVerticalScrollbarVisible() // Show the other scrollbar too
  339. }
  340. // Update visibility variables since they may have changed
  341. vScrollVisible = (s.vscroll != nil) && s.vscroll.Visible()
  342. hScrollVisible = (s.hscroll != nil) && s.hscroll.Visible()
  343. // If both vertical and horizontal scrolling is enabled, and the style specifies no interlocking
  344. // and a corner panel, and both scrollbars are visible - then the corner panel should be visible
  345. if (s.style.ScrollbarInterlocking == ScrollbarInterlockingNone) && s.style.CornerCovered && vScrollVisible && hScrollVisible {
  346. if s.corner == nil {
  347. s.corner = NewPanel(s.vscroll.width, s.hscroll.height)
  348. s.corner.ApplyStyle(&s.style.CornerPanel)
  349. s.Add(s.corner)
  350. }
  351. s.corner.SetVisible(true)
  352. } else if s.corner != nil {
  353. s.corner.SetVisible(false)
  354. }
  355. }
  356. }
  357. // onScrollEvent is called when the scrollbar value changes
  358. func (s *Scroller) onScrollBarEvent(evname string, ev interface{}) {
  359. s.recalc()
  360. }
  361. // recalc recalculates the positions and sizes of the scrollbars and corner panel,
  362. // updates the size of the scrollbar buttons, and repositions the target panel
  363. func (s *Scroller) recalc() {
  364. // The multipliers of the scrollbars' [0,1] values.
  365. // After applied, they will give the correct target panel position.
  366. // They can be thought of as the range of motion of the target panel in each axis
  367. multHeight := s.target.TotalHeight() - s.content.Height
  368. multWidth := s.target.TotalWidth() - s.content.Width
  369. var targetX, targetY float32
  370. var offsetX, offsetY float32
  371. vScrollVisible := (s.mode&ScrollVertical > 0) && (s.vscroll != nil) && s.vscroll.Visible()
  372. hScrollVisible := (s.mode&ScrollHorizontal > 0) && (s.hscroll != nil) && s.hscroll.Visible()
  373. // If the vertical scrollbar is visible
  374. if vScrollVisible {
  375. s.recalcV() // Recalculate scrollbar size/position (checks for the other scrollbar's presence)
  376. targetY = -float32(s.vscroll.Value())
  377. // If we don't want it to overlap the content area
  378. if s.style.VerticalScrollbar.OverlapContent == false {
  379. // Increase the target's range of X motion by the width of the vertical scrollbar
  380. multWidth += s.vscroll.width
  381. // If the vertical scrollbar is on the left we also want to add an offset to the target panel
  382. if s.style.VerticalScrollbar.Position == ScrollbarLeft {
  383. offsetX += s.vscroll.width
  384. }
  385. }
  386. }
  387. // If the horizontal scrollbar is visible
  388. if hScrollVisible {
  389. s.recalcH() // Recalculate scrollbar size/position (checks for the other scrollbar's presence)
  390. targetX = -float32(s.hscroll.Value())
  391. // If we don't want it to overlap the content area
  392. if s.style.HorizontalScrollbar.OverlapContent == false {
  393. // Increase the target's range of Y motion by the height of the horizontal scrollbar
  394. multHeight += s.hscroll.height
  395. // If the horizontal scrollbar is on the top we also want to add an offset to the target panel
  396. if s.style.HorizontalScrollbar.Position == ScrollbarTop {
  397. offsetY += s.hscroll.height
  398. }
  399. }
  400. }
  401. // Reposition the target panel
  402. s.target.SetPosition(targetX*multWidth+offsetX, targetY*multHeight+offsetY)
  403. // If the corner panel should be visible, update its position and size
  404. if (s.mode == ScrollBoth) && (s.style.ScrollbarInterlocking == ScrollbarInterlockingNone) &&
  405. (s.style.CornerCovered == true) && vScrollVisible && hScrollVisible {
  406. s.corner.SetPosition(s.vscroll.Position().X, s.hscroll.Position().Y)
  407. s.corner.SetSize(s.vscroll.width, s.hscroll.height)
  408. }
  409. }
  410. // recalcV recalculates the size and position of the vertical scrollbar
  411. func (s *Scroller) recalcV() {
  412. // Position the vertical scrollbar horizontally according to the style
  413. var vscrollPosX float32 // = 0 (ScrollbarLeft)
  414. if s.style.VerticalScrollbar.Position == ScrollbarRight {
  415. vscrollPosX = s.ContentWidth() - s.vscroll.width
  416. }
  417. // Start with the default Y position and height of the vertical scrollbar
  418. var vscrollPosY float32
  419. vscrollHeight := s.ContentHeight()
  420. viewHeight := s.ContentHeight()
  421. // If the horizontal scrollbar is present - reduce the viewHeight ...
  422. if (s.hscroll != nil) && s.hscroll.Visible() {
  423. if s.style.HorizontalScrollbar.OverlapContent == false {
  424. viewHeight -= s.hscroll.height
  425. }
  426. // If the interlocking style doesn't give precedence to the vertical scrollbar - reduce the scrollbar height ...
  427. if s.style.ScrollbarInterlocking != ScrollbarInterlockingVertical {
  428. vscrollHeight -= s.hscroll.height
  429. // If the horizontal scrollbar is on top - offset the vertical scrollbar vertically
  430. if s.style.HorizontalScrollbar.Position == ScrollbarTop {
  431. vscrollPosY = s.hscroll.height
  432. }
  433. }
  434. }
  435. // Adjust the scrollbar button size to the correct proportion proportion according to the style
  436. if s.style.VerticalScrollbar.AutoSizeButton {
  437. s.vscroll.SetButtonSize(vscrollHeight * viewHeight / s.target.TotalHeight())
  438. }
  439. // Update the position and height of the vertical scrollbar
  440. s.vscroll.SetPosition(vscrollPosX, vscrollPosY)
  441. s.vscroll.SetHeight(vscrollHeight)
  442. }
  443. // recalcH recalculates the size and position of the horizontal scrollbar
  444. func (s *Scroller) recalcH() {
  445. // Position the horizontal scrollbar vertically according to the style
  446. var hscrollPosY float32 // = 0 (ScrollbarTop)
  447. if s.style.HorizontalScrollbar.Position == ScrollbarBottom {
  448. hscrollPosY = s.ContentHeight() - s.hscroll.height
  449. }
  450. // Start with default X position and width of the horizontal scrollbar
  451. var hscrollPosX float32
  452. hscrollWidth := s.ContentWidth()
  453. viewWidth := s.ContentWidth()
  454. // If the vertical scrollbar is present - reduce the viewWidth ...
  455. if (s.vscroll != nil) && s.vscroll.Visible() {
  456. if s.style.VerticalScrollbar.OverlapContent == false {
  457. viewWidth -= s.vscroll.width
  458. }
  459. // If the interlocking style doesn't give precedence to the horizontal scrollbar - reduce the scrollbar width ...
  460. if s.style.ScrollbarInterlocking != ScrollbarInterlockingHorizontal {
  461. hscrollWidth -= s.vscroll.width
  462. // If the vertical scrollbar is on the left - offset the horizontal scrollbar horizontally
  463. if s.style.VerticalScrollbar.Position == ScrollbarLeft {
  464. hscrollPosX = s.vscroll.width
  465. }
  466. }
  467. }
  468. // Adjust the scrollbar button size to the correct proportion proportion according to the style
  469. if s.style.HorizontalScrollbar.AutoSizeButton {
  470. s.hscroll.SetButtonSize(hscrollWidth * viewWidth / s.target.TotalWidth())
  471. }
  472. // Update the position and width of the horizontal scrollbar
  473. s.hscroll.SetPosition(hscrollPosX, hscrollPosY)
  474. s.hscroll.SetWidth(hscrollWidth)
  475. }
  476. // Update updates the visibility of the scrollbars, corner panel, and then recalculates
  477. func (s *Scroller) Update() {
  478. s.updateScrollbarsVisibility()
  479. s.recalc()
  480. }
  481. // TODO - if the style is changed this needs to be called to update the scrollbars and corner panel
  482. func (s *Scroller) applyStyle(ss *ScrollerStyle) {
  483. s.style = ss
  484. s.vscroll.applyStyle(&s.style.VerticalScrollbar.ScrollBarStyle)
  485. s.hscroll.applyStyle(&s.style.HorizontalScrollbar.ScrollBarStyle)
  486. s.corner.ApplyStyle(&s.style.CornerPanel)
  487. s.Update()
  488. }