class Cura::Component::Scrollbar

A component for scrolling. TODO: Option to have buttons or not

Attributes

buttons[R]

Get the buttons for this scrollbar.

max[R]

Get the maximum value of this scrollbar.

min[R]

Get the minimum value of this scrollbar.

percent[R]

Get the percentage of this scrollbar.

value[R]

Get the value of this scrollbar.

Public Class Methods

new(attributes={}) click to toggle source
Calls superclass method Cura::Attributes::HasOrientation::new
# File lib/cura/component/scrollbar.rb, line 14
def initialize(attributes={})
  @value = 0
  @min = 0
  @max = 100
  @orientation = :vertical
  @buttons = {}

  @buttons[:decrement] = Button.new(width: 1, height: 1) { parent.decrement }
  @buttons[:increment] = Button.new(width: 1, height: 1) { parent.increment }
  @handle = Component.new(width: 1, height: 1)

  super

  setup_value
  setup_buttons
  setup_handle

  set_button_labels_based_on_orientation
  set_button_coordinates_based_on_orientation
  set_percentage
  set_handle_position
end

Public Instance Methods

decrement(value=1) click to toggle source

Decrement the value of this scrollbar by the given number.

# File lib/cura/component/scrollbar.rb, line 112
def decrement(value=1)
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)

  self.value -= value.to_i
end
height=(value) click to toggle source

Set the height of this scrollbar.

Calls superclass method
# File lib/cura/component/scrollbar.rb, line 69
def height=(value)
  super

  # @height = 2 if @height == 0 # TODO: Depends on if buttons are shown or not AND orientation

  set_button_coordinates_based_on_orientation
end
increment(value=1) click to toggle source

Increment the value of this scrollbar by the given number.

# File lib/cura/component/scrollbar.rb, line 105
def increment(value=1)
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)

  self.value += value.to_i
end
max=(value) click to toggle source

Set the maximum value of this scrollbar.

# File lib/cura/component/scrollbar.rb, line 91
def max=(value)
  raise ArgumentError, "max must respond to :to_i" unless value.respond_to?(:to_i)

  @max = value.to_i
end
min=(value) click to toggle source

Set the minimum value of this scrollbar.

# File lib/cura/component/scrollbar.rb, line 81
def min=(value)
  raise ArgumentError, "min must respond to :to_i" unless value.respond_to?(:to_i)

  @min = value.to_i
end
orientation=(value) click to toggle source

Set the orientation of this scrollbar.

# File lib/cura/component/scrollbar.rb, line 98
def orientation=(value)
  super

  set_button_labels_based_on_orientation
end
value=(value) click to toggle source

Set the value of this scrollbar.

# File lib/cura/component/scrollbar.rb, line 44
def value=(value)
  raise ArgumentError, "value must respond to :to_i" unless value.respond_to?(:to_i)

  value = max if value > max
  value = min if value < min

  @value = value.to_i

  set_percentage
  set_handle_position
end
width=(value) click to toggle source

Set the width of this scrollbar.

Calls superclass method
# File lib/cura/component/scrollbar.rb, line 60
def width=(value)
  super

  # @width = 2 if @width == 0 # TODO: Depends on if buttons are shown or not AND orientation

  set_button_coordinates_based_on_orientation
end

Protected Instance Methods

set_button_coordinates_based_on_orientation() click to toggle source
# File lib/cura/component/scrollbar.rb, line 151
def set_button_coordinates_based_on_orientation
  if vertical?
    @buttons[:increment].x = 0
    @buttons[:increment].y = height - 1
  elsif horizontal?
    @buttons[:increment].x = width - 1
    @buttons[:increment].y = 0
  end
end
set_button_labels_based_on_orientation() click to toggle source
# File lib/cura/component/scrollbar.rb, line 141
def set_button_labels_based_on_orientation
  if vertical?
    @buttons[:decrement].text = "˄"
    @buttons[:increment].text = "˅"
  elsif horizontal?
    @buttons[:decrement].text = "˂"
    @buttons[:increment].text = "˃"
  end
end
set_handle_position() click to toggle source
# File lib/cura/component/scrollbar.rb, line 165
def set_handle_position
  # TODO: Only +/- padding when buttons are shown
  dimension = (vertical? ? height : width) - 3
  position = (dimension.to_f * percent.to_f / 100.0).to_i + 1

  if vertical?
    @handle.x = 0
    @handle.y = position
  elsif horizontal?
    @handle.x = position
    @handle.y = 0
  end
end
set_percentage() click to toggle source
# File lib/cura/component/scrollbar.rb, line 161
def set_percentage
  @percent = ((value.to_f - min.to_f) / (max.to_f - min.to_f) * 100.0).to_i
end
setup_buttons() click to toggle source
# File lib/cura/component/scrollbar.rb, line 125
def setup_buttons
  @buttons.each do |_, button|
    button.foreground = foreground
    button.background = background

    add(button)
  end
end
setup_handle() click to toggle source
# File lib/cura/component/scrollbar.rb, line 134
def setup_handle
  @handle.foreground = background
  @handle.background = foreground

  add(@handle)
end
setup_value() click to toggle source
# File lib/cura/component/scrollbar.rb, line 120
def setup_value
  @value = min if value < min
  @value = max if value > max
end