mittens_ui 0.0.4 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ module MittensUi
2
+ module Dialogs
3
+ class File
4
+ attr_reader :path
5
+
6
+ def initialize(options={})
7
+ @path = ""
8
+
9
+ dialog_options = {
10
+ title: "Select File",
11
+ parent: $app_window,
12
+ action: options[:action] || :open,
13
+ buttons: [
14
+ [Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT],
15
+ [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]
16
+ ]
17
+ }.freeze
18
+
19
+ dialog = Gtk::FileChooserDialog.new(dialog_options)
20
+
21
+ if dialog.run == :accept
22
+ @path = dialog.filename
23
+ end
24
+
25
+ dialog.destroy
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ module MittensUi
2
+ module Helpers
3
+ def set_margin_from_opts_for(widget, options={})
4
+ margin_top = options[:top].nil? ? nil : options[:top]
5
+ margin_bottom = options[:bottom].nil? ? nil : options[:bottom]
6
+ margin_right = options[:right].nil? ? nil : options[:right]
7
+ margin_left = options[:left].nil? ? nil : options[:left]
8
+
9
+ unless margin_top.nil?
10
+ widget.set_margin_top(margin_top)
11
+ end
12
+
13
+ unless margin_bottom.nil?
14
+ widget.set_margin_bottom(margin_bottom)
15
+ end
16
+
17
+ unless margin_left.nil?
18
+ widget.set_margin_left(margin_left)
19
+ end
20
+
21
+ unless margin_right.nil?
22
+ widget.set_margin_right(margin_right)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -1,3 +1,3 @@
1
1
  module MittensUi
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,16 +1,16 @@
1
1
  module MittensUi
2
2
  module Widgets
3
3
  class Alert
4
- def initialize(window, message, options)
4
+ def initialize(message, options)
5
5
  dialog_options = {
6
- title: "Alert",
7
- parent: window,
6
+ title: options[:title] || "Alert",
7
+ parent: $app_window,
8
8
  flags: [:modal, :destroy_with_parent],
9
- :buttons => [["_OK", :none]]
9
+ :buttons => [[Gtk::Stock::OK, :none]]
10
10
  }.freeze
11
11
 
12
12
  alert_dialog = Gtk::Dialog.new(dialog_options)
13
- alert_dialog.set_transient_for(window)
13
+ alert_dialog.set_transient_for($app_window)
14
14
  alert_dialog.set_default_width(420)
15
15
  alert_dialog.set_default_height(200)
16
16
  alert_dialog.set_modal(true)
@@ -1,33 +1,24 @@
1
+ require_relative "./core"
2
+
1
3
  module MittensUi
2
4
  module Widgets
3
- class Button
4
- def initialize(layout, options, &block)
5
+ class Button < Core
6
+ def initialize(options={})
5
7
  button_title = options[:title] || "Button"
6
8
 
7
- margin_top = options[:top].nil? ? nil : options[:top]
8
- margin_bottom = options[:bottom].nil? ? nil : options[:bottom]
9
- margin_right = options[:right].nil? ? nil : options[:right]
10
- margin_left = options[:left].nil? ? nil : options[:left]
11
-
12
9
  @button = Gtk::Button.new(label: button_title)
13
-
14
- @button.set_margin_top(margin_top) unless margin_top.nil?
15
- @button.set_margin_left(margin_left) unless margin_left.nil?
16
- @button.set_margin_right(margin_right) unless margin_right.nil?
17
- @button.set_margin_bottom(margin_bottom) unless margin_bottom.nil?
18
10
 
19
- if layout
20
- layout.pack_start(@button)
21
- end
22
-
23
- @button.signal_connect "clicked" do |button_widget|
24
- block.call(button_widget)
25
- end
11
+ set_margin_from_opts_for(@button, options)
12
+
13
+ $vertical_box.pack_start(@button)
14
+
15
+ super(@button)
26
16
  end
27
-
28
- def remove
29
- return if @button.nil?
30
- @button.destroy
17
+
18
+ def click
19
+ @button.signal_connect("clicked") do |button_widget|
20
+ yield(button_widget)
21
+ end
31
22
  end
32
23
  end
33
24
  end
@@ -0,0 +1,28 @@
1
+ require_relative "./core"
2
+
3
+ module MittensUi
4
+ module Widgets
5
+ class Checkbox < Core
6
+ attr_accessor :value
7
+
8
+ def initialize(options={})
9
+ label = options[:label] || "Checkbox"
10
+
11
+ @value = nil
12
+ @checkbox = Gtk::CheckButton.new(label)
13
+
14
+ set_margin_from_opts_for(@checkbox, options)
15
+
16
+ $vertical_box.pack_start(@checkbox)
17
+
18
+ super(@checkbox)
19
+ end
20
+
21
+ def toggle
22
+ @checkbox.signal_connect "toggled" do
23
+ yield
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ require "mittens_ui/helpers"
2
+
3
+ module MittensUi
4
+ module Widgets
5
+ class Core
6
+ include Helpers
7
+
8
+ # All MittenUi::Widgets::* classes should inherit from this base class.
9
+
10
+ def initialize(widget)
11
+ @core_widget = widget
12
+ end
13
+
14
+ def show
15
+ @core_widget.show_all
16
+ end
17
+
18
+ def hidden?
19
+ @core_widget.visible?
20
+ end
21
+
22
+ def hide
23
+ return if @core_widget.nil?
24
+ @core_widget.hide
25
+ end
26
+
27
+ def remove
28
+ $vertical_box.remove(@core_widget)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "./core"
2
+
3
+ module MittensUi
4
+ module Widgets
5
+ class Image < Core
6
+ attr_reader :path
7
+
8
+ def initialize(path, options = {})
9
+ @path = File.join(path.strip)
10
+
11
+ tooltip_text = options[:tooltip_text].nil? ? "" : options[:tooltip_text]
12
+ width = options[:width].nil? ? 80 : options[:width]
13
+ height = options[:height].nil? ? 80 : options[:height]
14
+
15
+ pixbuf = GdkPixbuf::Pixbuf.new(file: @path, width: width, height: height)
16
+
17
+ @image = Gtk::Image.new(pixbuf: pixbuf)
18
+ @image.tooltip_text = tooltip_text
19
+
20
+ set_margin_from_opts_for(@image, options)
21
+
22
+ @event_box = Gtk::EventBox.new.add_child(@image)
23
+
24
+ $vertical_box.pack_start(@event_box)
25
+
26
+ super(@image)
27
+ end
28
+
29
+ def click
30
+ @event_box.signal_connect("button_press_event") do
31
+ yield
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,38 +1,20 @@
1
+ require_relative "./core"
2
+
1
3
  module MittensUi
2
4
  module Widgets
3
- class Label
4
- def initialize(text, layout, options)
5
- margin_top = options[:top].nil? ? nil : options[:top]
6
- margin_bottom = options[:bottom].nil? ? nil : options[:bottom]
7
- margin_right = options[:right].nil? ? nil : options[:right]
8
- margin_left = options[:left].nil? ? nil : options[:left]
9
-
10
- @label = Gtk::Label.new(text)
11
-
12
- unless margin_top.nil?
13
- @label.set_margin_top(margin_top)
14
- end
15
-
16
- unless margin_bottom.nil?
17
- @label.set_margin_bottom(margin_top)
5
+ class Label < Core
6
+ def initialize(text, options)
7
+ if text.nil? || text == "" || text == " "
8
+ text = "Label"
18
9
  end
19
10
 
20
- unless margin_left.nil?
21
- @label.set_margin_left(margin_left)
22
- end
11
+ @label = Gtk::Label.new(text)
23
12
 
24
- unless margin_right.nil?
25
- @label.set_margin_right(margin_right)
26
- end
13
+ set_margin_from_opts_for(@label, options)
27
14
 
28
- if layout
29
- layout.pack_start(@label)
30
- end
31
- end
15
+ $vertical_box.pack_start(@label)
32
16
 
33
- def remove
34
- return if @label.nil?
35
- @label.destroy
17
+ super(@label)
36
18
  end
37
19
  end
38
20
  end
@@ -1,10 +1,12 @@
1
+ require_relative "./core"
2
+
1
3
  module MittensUi
2
4
  module Widgets
3
- class ListBox
5
+ class ListBox < Core
4
6
  attr_reader :items
5
7
 
6
- def initialize(layout, options={})
7
- @items = options[:items]
8
+ def initialize(options={})
9
+ @items = options[:items]
8
10
 
9
11
  list_store = Gtk::ListStore.new(String)
10
12
 
@@ -24,25 +26,20 @@ module MittensUi
24
26
 
25
27
  @gtk_combobox.set_active(0)
26
28
 
27
- if layout
28
- layout.pack_start(@gtk_combobox)
29
- end
29
+ $vertical_box.pack_start(@gtk_combobox)
30
30
 
31
- return self
31
+ super(@gtk_combobox)
32
32
  end
33
33
 
34
34
  def set_selected_value(value)
35
35
  @selected_value = value
36
36
  end
37
+ alias :set_value :set_selected_value
37
38
 
38
39
  def get_selected_value
39
40
  @selected_value
40
41
  end
41
-
42
- def remove
43
- return if @gtk_combobox.nil?
44
- @gtk_combobox.destroy
45
- end
42
+ alias :selected_value :get_selected_value
46
43
  end
47
44
  end
48
45
  end
@@ -0,0 +1,34 @@
1
+ require_relative "./core"
2
+
3
+ module MittensUi
4
+ module Widgets
5
+ class Loader < Core
6
+ def initialize(options={})
7
+ @spinner = Gtk::Spinner.new
8
+
9
+ @processing = false
10
+
11
+ set_margin_from_opts_for(@spinner, options)
12
+
13
+ $vertical_box.pack_end(@spinner)
14
+
15
+ super(@spinner)
16
+
17
+ self.hide
18
+ end
19
+
20
+ def start(&block)
21
+ return if @processing
22
+
23
+ return if @worker_thread && @worker_thread.alive?
24
+
25
+ self.show
26
+
27
+ @spinner.start
28
+
29
+ @worker_thread = Thread.new { yield; self.remove; @processing = true }
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -1,31 +1,31 @@
1
+ require_relative "./core"
2
+
1
3
  module MittensUi
2
4
  module Widgets
3
- class Slider
4
- def initialize(layout, options={}, &block)
5
- start_value = options[:start_value].nil? ? 1.0 : options[:start_value]
6
- stop_value = options[:stop_value].nil? ? 10.0 : options[:stop_value]
7
- step_value = options[:step_value].nil? ? 1.0 : options[:step_value]
5
+ class Slider < Core
6
+ def initialize(options={})
7
+ start_value = options[:start_value].nil? ? 1.0 : options[:start_value]
8
+ stop_value = options[:stop_value].nil? ? 10.0 : options[:stop_value]
9
+ step_value = options[:step_value].nil? ? 1.0 : options[:step_value]
8
10
  init_value = options[:initial_value].nil? ? 1.0 : options[:initial_value]
9
11
 
10
12
  @scale = Gtk::Scale.new(:horizontal, start_value, stop_value, step_value)
11
- puts @scale.methods
12
13
  @scale.digits = 0
13
14
  @scale.draw_value = true
14
15
  @scale.value = init_value
15
16
 
16
- @scale.signal_connect "value_changed" do |scale_widget|
17
- block.call(scale_widget)
18
- end
17
+ $vertical_box.pack_start(@scale)
19
18
 
20
- if layout
21
- layout.pack_start(@scale)
22
- end
19
+ super(@scale)
23
20
  end
24
21
 
25
- def remove
26
- return if @scale.nil?
27
- @scale.destroy
22
+ def move
23
+ @scale.signal_connect "value_changed" do |scale_widget|
24
+ yield(scale_widget)
25
+ end
28
26
  end
27
+
28
+ alias :slide :move
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,37 @@
1
+ require_relative "./core"
2
+
3
+ module MittensUi
4
+ module Widgets
5
+ class Switch < Core
6
+ def initialize(options = {})
7
+ @switch = Gtk::Switch.new
8
+ @switch.set_active(false)
9
+
10
+ set_margin_from_opts_for(@switch, options)
11
+
12
+ # We need a Grid within our global $vertical_box layout
13
+ # in order to make the Widget look good (meaning not overly streched).
14
+ grid = Gtk::Grid.new
15
+ grid.set_column_spacing(1)
16
+ grid.set_row_spacing(1)
17
+ grid.attach(@switch, 0, 0, 1, 1)
18
+
19
+ $vertical_box.pack_start(grid)
20
+
21
+ super(@switch)
22
+ end
23
+
24
+ def activate
25
+ @switch.signal_connect('notify::active') do |switch_widget|
26
+ switch_widget.active? ? switch_widget.set_active(true) : switch_widget.set_active(false)
27
+ yield
28
+ end
29
+ end
30
+ alias :on :activate
31
+
32
+ def status
33
+ @switch.active? ? 'on' : 'off'
34
+ end
35
+ end
36
+ end
37
+ end