Class: PopupFormGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/gui_form_generator.rb

Overview

PopupFormGenerator.new().run

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ PopupFormGenerator

Returns a new instance of PopupFormGenerator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vimamsa/gui_form_generator.rb', line 13

def initialize(params = nil)
  @ret = {}
  @window = Gtk::Window.new()
  # @window.screen = main_window.screen
  # @window.title = title
  # params = {}
  # params["inputs"] = {}
  # params["inputs"]["search"] = { :label => "Search", :type => :entry }
  # params["inputs"]["replace"] = { :label => "Replace", :type => :entry }
  # params["inputs"]["btn1"] = { :label => "Replace all", :type => :button }
  # params[:callback] = proc { |x| puts "====="; puts x.inspect; puts "=====" }


  @callback = params[:callback]
  @window.title = ""

  frame = Gtk::Frame.new()
  frame.margin_bottom = 8
  frame.margin_top = 8
  frame.margin_end = 8
  frame.margin_start = 8

  @window.set_child(frame)

  # @window.title = params["title"]

  # @callback = params["callback"]

  vbox = Gtk::Box.new(:vertical, 8)
  vbox.margin_bottom = 8
  vbox.margin_top = 8
  vbox.margin_end = 8
  vbox.margin_start = 8

  frame.set_child(vbox)

  if params.has_key?("title")
    infolabel = Gtk::Label.new
    infolabel.markup = params["title"]
    #TODO:gtk4
    # vbox.pack_start(infolabel, :expand => false, :fill => false, :padding => 0)
    vbox.pack_end(infolabel, :expand => false, :fill => false, :padding => 0)
  end

  hbox = Gtk::Box.new(:horizontal, 8)
  @vals = {}
  @default_button = nil

  for id, elem in params["inputs"]
    if elem[:type] == :button
      button = Gtk::Button.new(:label => elem[:label])
      hbox.pack_end(button, :expand => false, :fill => false, :padding => 0)
      if elem[:default_focus] == true
        @default_button = button
      end
      button.signal_connect "clicked" do
        @ret[id] = "submit"
        submit
      end
    elsif elem[:type] == :entry
      label = Gtk::Label.new(elem[:label])
      entry = Gtk::Entry.new
      if elem.has_key?(:initial_text)
        entry.text = elem[:initial_text]
      end
      hbox.pack_end(label, :expand => false, :fill => false, :padding => 0)
      hbox.pack_end(entry, :expand => false, :fill => false, :padding => 0)
      @vals[id] = entry

      press = Gtk::EventControllerKey.new
      press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
      entry.add_controller(press)
      press.signal_connect "key-pressed" do |gesture, keyval, keycode, y|
        if keyval == Gdk::Keyval::KEY_Return
          submit
          true
        elsif keyval == Gdk::Keyval::KEY_Escape
          @window.destroy
          true
        else
          false
        end
      end
    end
  end

  vbox.pack_end(hbox, :expand => false, :fill => false, :padding => 0)

  cancel_button = Gtk::Button.new(:label => "Cancel")
  cancel_button.signal_connect "clicked" do
    @window.destroy
  end
  hbox.pack_end(cancel_button, :expand => false, :fill => false, :padding => 0)
  @cancel_button = cancel_button
  return
end

Instance Method Details

#runObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vimamsa/gui_form_generator.rb', line 110

def run
  if !@window.visible?
    @window.show
  else
    @window.destroy
  end
  if !@default_button.nil?
    @default_button.grab_focus
  end
  @window.set_focus_visible(true)
  @window
end

#submitObject



3
4
5
6
7
8
9
10
11
# File 'lib/vimamsa/gui_form_generator.rb', line 3

def submit()
  for id, entry in @vals
    @ret[id] = entry.text
  end
  if !@callback.nil?
    @callback.call(@ret)
  end
  @window.destroy
end