# ui/uiscrollbar (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uiscrollbar/

Scroll bar. (UIScrollBar, ScrollBarAxis of the original ui/uiscrollbar.js)
- Shows the target scroll view's scroll state as a track (background) + thumb (drag handle). Dragging the thumb or track changes the target's offset right away.
- Every tick, reads the target's offset / content size / visible area and fits the thumb position and size.
- Differences from the original:
  - The original UIScrollBar is a node that inherits WorldNode, and creates the track Paint and the thumb node (Paint) under itself.
    The Rust version is a component attached to a node, and draws the track and thumb directly. (holds only the thumb rectangle and its on state, without a thumb node)
    Like the original thumb node, the thumb state starts on with zero size, and the rectangle stays as is while dragging. (get_thumb_rect also starts as a zero-size rectangle like the original getThumbRect)
  - The target is the handle of a node with a UIScrollView attached instead of the scroll view component. (set_scroll_view / get_scroll_view)
  - The track press position and the inside-thumb test use local coordinates converted with the inverse of the node matrix. (the original subtracts the world position. the same without rotation / scale)
- The UI document (UIDocument) writes and reads it in the original node form when registered with register_node_type. (node type "UIScrollBar", track Paint, thumb node, UIDocumentNode in uidocument.rs)
- Usually created by UIScrollView::set_shows_vertical_scroll_bar. To create one directly, attach it with node_tree.add_component. (pivot / anchor at top left, interactable)

## UIScrollBar (struct) (Default, Component, UIDocumentNode)

Scroll bar.
- Reads the target scroll view node's offset, content size, and visible area every tick and fits the thumb. Dragging the thumb or track changes the target offset right away.

Example: Thumb of the vertical scroll bar created by a scroll view

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, Rect, UIScrollBar, UIScrollView, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let scroll_id = node_tree.create_child(root_id, "scroll");
node_tree[scroll_id].set_pivot(Pivot::TOP_LEFT);
node_tree[scroll_id].set_content_size(Vector2::create(200.0, 112.0));
let scroll_view = node_tree.add_component(scroll_id, UIScrollView::new()).expect("scroll view");
scroll_view.set_scroll_content_size(Vector2::create(200.0, 448.0));
let scroll_bar_id = UIScrollView::set_shows_vertical_scroll_bar_in_tree(&mut node_tree, scroll_id, true).expect("scroll bar");
node_tree.tick(&engine, 0.016);

let scroll_bar = node_tree[scroll_bar_id].get_component::<UIScrollBar>().expect("scroll bar");
assert_eq!(scroll_bar.get_thumb_rect(), Some(Rect::create(0.0, 0.0, 12.0, 25.0)));
```

- `pub fn new() -> UIScrollBar` (constructor, since 0.3.2): Constructor. (vertical, faint black track, translucent white thumb, minimum thumb length 24, auto hide)
  - To attach to a tree, use node_tree.add_component. (on attach, sets the pivot and anchor to the top left and turns interactable on)
  - returns UIScrollBar: New scroll bar with no target.

  Example: Attach a horizontal scroll bar directly under a scroll view

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, Rect, ScrollBarAxis, UIScrollBar, UIScrollView, Vector2};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let scroll_id = node_tree.create_child(root_id, "scroll");
  node_tree[scroll_id].set_pivot(Pivot::TOP_LEFT);
  node_tree[scroll_id].set_content_size(Vector2::create(200.0, 100.0));
  let scroll_view = node_tree.add_component(scroll_id, UIScrollView::new()).expect("scroll view");
  scroll_view.set_scroll_content_size(Vector2::create(800.0, 100.0));
  
  let scroll_bar_id = node_tree.create_child(scroll_id, "bar");
  let mut scroll_bar = UIScrollBar::new();
  scroll_bar.set_axis(ScrollBarAxis::Horizontal);
  scroll_bar.set_scroll_view(Some(scroll_id));
  node_tree.add_component(scroll_bar_id, scroll_bar);
  node_tree[scroll_bar_id].set_rect(0.0, 92.0, 200.0, 8.0);
  node_tree.tick(&engine, 0.016);
  
  let scroll_bar = node_tree[scroll_bar_id].get_component::<UIScrollBar>().expect("scroll bar");
  assert_eq!(scroll_bar.get_thumb_rect(), Some(Rect::create(0.0, 0.0, 50.0, 8.0)));
  ```

- `pub fn set_scroll_view(&mut self, scroll_view_node_id: Option<NodeId>)` (method, since 0.3.2): Sets the target scroll view. (a node with a UIScrollView attached)
  - `scroll_view_node_id` Option<NodeId>: Node with a UIScrollView attached. None means there is no target, so the thumb is turned off and touches are not taken.
- `pub fn get_scroll_view(&self) -> Option<NodeId>` (method, since 0.3.2): Returns the target scroll view. (read by TouchRecognizer so as not to hand drags over to its own scroll view)
  - returns Option<NodeId>: Target scroll view node. None if not set.
- `pub fn set_axis(&mut self, axis: ScrollBarAxis)` (method, since 0.3.2): Sets the axis.
  - `axis` ScrollBarAxis: Axis the thumb moves along. (default vertical)
- `pub fn get_axis(&self) -> ScrollBarAxis` (method, since 0.3.2): Returns the axis.
  - returns ScrollBarAxis: Current axis.
- `pub fn set_track_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the track color.
  - `color` impl Into<Color>: Track (background) color. (default (0, 0, 0, 0.15))
- `pub fn set_thumb_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the thumb color.
  - `color` impl Into<Color>: Thumb color. (default (1, 1, 1, 0.6))
- `pub fn get_track_color(&self) -> Color` (method, since 0.4.0): Returns the track color. (in the original, the color of the track Paint component. the Rust version draws it directly and holds the color)
  - returns Color: Track color.
- `pub fn get_thumb_color(&self) -> Color` (method, since 0.4.0): Returns the thumb color. (in the original, the color of the thumb node's Paint component)
  - returns Color: Thumb color.
- `pub fn set_min_thumb_size(&mut self, min_thumb_size: f64)` (method, since 0.3.2): Sets the minimum thumb length.
  - `min_thumb_size` f64: Shortest length the thumb can have along the axis. (pixels, default 24) If the length calculated from the visible ratio is shorter, this length is used.
- `pub fn set_auto_hide(&mut self, auto_hide: bool)` (method, since 0.3.2): Sets auto hide. (hides even the track when the content fits in the visible area)
  - The node stays active and is only made invisible, so it shows again on the next tick when the content grows or auto hide is turned off.
  - `auto_hide` bool: If true, turns off the scroll bar node when there is nothing to scroll. (default true) If false, turns off only the thumb and draws the track.
- `pub fn is_auto_hidden(&self) -> bool` (method, since 0.4.0): Whether it is hidden by auto hide. (the original isAutoHidden)
  - returns bool: True if the track is hidden by auto hide because there is nothing to scroll.
- `pub fn get_thumb_rect(&self) -> Option<Rect>` (method, since 0.3.2): Current thumb rectangle. (within the scroll bar node area, None if the thumb is off)
  - returns Option<Rect>: Thumb rectangle. (local coordinates of the scroll bar node, pixels) None if the thumb is off because there is no target or nothing to scroll. Before the first tick it is an on, zero-size rectangle.

  Example: Without a target, the thumb turns off after tick

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, Rect, UIScrollBar};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let scroll_bar_id = node_tree.create_child(root_id, "bar");
  node_tree.add_component(scroll_bar_id, UIScrollBar::new());
  let scroll_bar = node_tree[scroll_bar_id].get_component::<UIScrollBar>().expect("scroll bar");
  assert_eq!(scroll_bar.get_thumb_rect(), Some(Rect::create(0.0, 0.0, 0.0, 0.0)));
  
  node_tree.tick(&engine, 0.016);
  let scroll_bar = node_tree[scroll_bar_id].get_component::<UIScrollBar>().expect("scroll bar");
  assert_eq!(scroll_bar.get_thumb_rect(), None);
  ```

## ScrollBarAxis (enum) (Clone, Copy, Debug, PartialEq, Eq)

Scroll bar axis.

- `Horizontal`: Horizontal. (the thumb moves left and right and changes offset x)
- `Vertical`: Vertical. (the thumb moves up and down and changes offset y)
