scroller.go 22 KB

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