Class: MittensUi::Button

Inherits:
Core
  • Object
show all
Includes:
Helpers
Defined in:
lib/mittens_ui/button.rb

Overview

A clickable button widget with optional icon and loading state support.

Wraps Gtk::Button. When in loading state, the button is disabled and a spinner replaces or appears alongside the label to indicate background work is running.

Examples:

Basic button

btn = MittensUi::Button.new(title: "Click Me")
btn.click { puts "clicked!" }

Button with icon

btn = MittensUi::Button.new(title: "Add", icon: :add_green)

Button with loading state

btn = MittensUi::Button.new(title: "Save")
btn.click do
  btn.loading do
    sleep 2  # do some work
    puts "done!"
  end
end

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Methods inherited from Core

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

Constructor Details

#initialize(options = {}) ⇒ Button

Creates a new Button widget.

Parameters:

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

    configuration options

Options Hash (options):

  • :title (String) — default: "Button"

    the button label text

  • :icon (Symbol) — default: nil

    an icon key from the icon map. When set, an icon is displayed instead of a text label.

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mittens_ui/button.rb', line 40

def initialize(options = {})
  button_title = options.fetch(:title, 'Button')
  icon_type    = options.fetch(:icon, nil)

  @loading  = false
  @button   = Gtk::Button.new
  @box      = Gtk::Box.new(:horizontal, 4)
  @spinner  = Gtk::Spinner.new

  if icon_type
    image = Gtk::Image.new(icon_name: icon_map[icon_type], size: :button)
    @box.append(image)
  end

  @label = Gtk::Label.new(button_title)
  @box.append(@label)
  @box.set_halign(:center)
  @box.append(@spinner)

  @button.set_child(@box)
  @spinner.hide

  super(@button, options)
end

Instance Method Details

#click {|button_widget| ... } ⇒ void

This method returns an undefined value.

Connects a block to the button’s click event.

Examples:

btn.click do |b|
  puts "Button was clicked!"
end

Yields:

  • (button_widget)

    the GTK button widget that was clicked

Yield Parameters:

  • button_widget (Gtk::Button)

    the underlying GTK button



92
93
94
95
96
# File 'lib/mittens_ui/button.rb', line 92

def click
  @button.signal_connect('clicked') do
    yield(self)
  end
end

#enable(answer) ⇒ void

This method returns an undefined value.

Enables or disables the button.

Examples:

btn.enable(false)  # disable
btn.enable(true)   # enable

Parameters:

  • answer (Boolean)

    true to enable, false to disable



72
73
74
# File 'lib/mittens_ui/button.rb', line 72

def enable(answer)
  @button.set_sensitive(answer)
end

#loading { ... } ⇒ void

This method returns an undefined value.

Runs a block in a background thread while showing a loading spinner and disabling the button. Re-enables the button when the block finishes. Safe to call from a click handler.

Examples:

btn.click do
  btn.loading do
    sleep 3
    puts "Work done!"
  end
end

Yields:

  • the block of work to run in the background



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mittens_ui/button.rb', line 111

def loading
  return if @loading

  @loading = true
  @button.set_sensitive(false)
  @spinner.show
  @spinner.start

  Thread.new do
    begin
      yield
    ensure
      GLib::Idle.add do
        @spinner.stop
        @spinner.hide
        @button.set_sensitive(true)
        @loading = false
        false
      end
    end
  end
end

#loading?Boolean

Returns whether the button is currently in a loading state.

Returns:

  • (Boolean)

    true if loading, false otherwise



79
80
81
# File 'lib/mittens_ui/button.rb', line 79

def loading?
  @loading
end