Class: MittensUi::Button
- 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.
Instance Attribute Summary
Attributes inherited from Core
Instance Method Summary collapse
-
#click {|button_widget| ... } ⇒ void
Connects a block to the button’s click event.
-
#enable(answer) ⇒ void
Enables or disables the button.
-
#initialize(options = {}) ⇒ Button
constructor
Creates a new Button widget.
-
#loading { ... } ⇒ void
Runs a block in a background thread while showing a loading spinner and disabling the button.
-
#loading? ⇒ Boolean
Returns whether the button is currently in a loading state.
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.
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( = {}) = .fetch(:title, 'Button') icon_type = .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() @box.append(@label) @box.set_halign(:center) @box.append(@spinner) @button.set_child(@box) @spinner.hide super(@button, ) end |
Instance Method Details
#click {|button_widget| ... } ⇒ void
This method returns an undefined value.
Connects a block to the button’s click event.
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.
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.
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.
79 80 81 |
# File 'lib/mittens_ui/button.rb', line 79 def loading? @loading end |