Class: MittensUi::Slider

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/slider.rb

Overview

A horizontal slider widget for selecting values from a range.

The ‘Slider` class allows users to select a value within a specified range by dragging a handle along a horizontal track. It emits a signal when the value changes, enabling callbacks to be executed.

Examples:

Create a slider with custom range and initial value

slider = MittensUi::Slider.new(
  start_value: 0,
  stop_value: 100,
  initial_value: 50
)

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(options = {}) ⇒ Slider

Initializes a new Slider widget.

Parameters:

  • options (Hash) (defaults to: {})

    The options for configuring the slider.

Options Hash (options):

  • :start_value (Float)

    The minimum value of the slider (default: 1.0).

  • :stop_value (Float)

    The maximum value of the slider (default: 10.0).

  • :step_value (Float)

    The step increment for the slider (default: 1.0).

  • :initial_value (Float)

    The initial value of the slider (default: 1.0).



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mittens_ui/slider.rb', line 26

def initialize(options = {})
  start_value = options.fetch(:start_value, 1.0)
  stop_value  = options.fetch(:stop_value, 10.0)
  step_value  = options.fetch(:step_value, 1.0)
  init_value  = options.fetch(:initial_value, 1.0)

  adjustment = Gtk::Adjustment.new(
    init_value,
    start_value,
    stop_value,
    step_value,
    step_value,
    0
  )

  @scale = Gtk::Scale.new(:horizontal, adjustment)
  @scale.set_digits(0)
  @scale.set_draw_value(true)

  super(@scale, options)
end

Instance Method Details

#move {|value| ... } ⇒ void Also known as: slide

This method returns an undefined value.

Registers a callback to be executed when the slider value changes.

The block will be called with the new slider value as its argument.

Examples:

Register a callback

slider.move do |value|
  puts "Slider value changed to #{value}"
end

Yields:

  • (value)

    The block to execute when the slider value changes.

Yield Parameters:

  • value (Float)

    The new value of the slider.



60
61
62
63
64
# File 'lib/mittens_ui/slider.rb', line 60

def move
  @scale.signal_connect('value_changed') do |scale_widget|
    yield(scale_widget, scale_widget.value)
  end
end