Class: SettingsDialog

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

Instance Method Summary collapse

Constructor Details

#initializeSettingsDialog

Returns a new instance of SettingsDialog.



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
# File 'lib/vimamsa/gui_settings.rb', line 35

def initialize
  @widgets = {}
  @window = Gtk::Window.new
  @window.set_transient_for($vmag.window) if $vmag&.window
  @window.modal = true
  @window.title = "Preferences"
  @window.default_width = 500

  outer = Gtk::Box.new(:vertical, 12)
  # outer.margin = 16
  @window.set_child(outer)

  notebook = Gtk::Notebook.new
  outer.append(notebook)

  SETTINGS_DEFS.each do |section|
    grid = Gtk::Grid.new
    grid.row_spacing = 10
    grid.column_spacing = 16
    grid.margin_top = 12
    grid.margin_bottom = 12
    grid.margin_start = 12
    grid.margin_end = 12

    section[:settings].each_with_index do |s, row|
      label = Gtk::Label.new(s[:label])
      label.halign = :start
      label.hexpand = true

      widget = make_widget(s)
      @widgets[s[:key]] = { :widget => widget, :type => s[:type] }

      grid.attach(label, 0, row, 1, 1)
      grid.attach(widget, 1, row, 1, 1)
    end

    notebook.append_page(grid, Gtk::Label.new(section[:label]))
  end

  hbox = Gtk::Box.new(:horizontal, 8)
  hbox.halign = :end

  cancel_btn = Gtk::Button.new(:label => "Cancel")
  save_btn = Gtk::Button.new(:label => "Save")
  cancel_btn.signal_connect("clicked") { @window.destroy }
  save_btn.signal_connect("clicked") { save_and_close }

  hbox.append(cancel_btn)
  hbox.append(save_btn)
  outer.append(hbox)

  press = Gtk::EventControllerKey.new
  press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
  @window.add_controller(press)
  press.signal_connect("key-pressed") do |_g, keyval, _kc, _y|
    if keyval == Gdk::Keyval::KEY_Escape
      @window.destroy
      true
    else
      false
    end
  end
end

Instance Method Details

#make_widget(s) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vimamsa/gui_settings.rb', line 99

def make_widget(s)
  cur = get(s[:key])
  case s[:type]
  when :bool
    w = Gtk::Switch.new
    w.active = cur == true
    w.valign = :center
    w
  when :int
    adj = Gtk::Adjustment.new(cur.to_f, s[:min].to_f, s[:max].to_f, s[:step].to_f, s[:step].to_f * 5, 0.0)
    Gtk::SpinButton.new(adj, s[:step].to_f, 0)
  when :float
    adj = Gtk::Adjustment.new(cur.to_f, s[:min].to_f, s[:max].to_f, s[:step].to_f, s[:step].to_f * 5, 0.0)
    digits = s[:step].to_s.split(".").last.to_s.length
    digits = 2 if digits < 2
    Gtk::SpinButton.new(adj, s[:step].to_f, digits)
  when :string
    w = Gtk::Entry.new
    w.text = cur.to_s
    w
  end
end

#runObject



137
138
139
# File 'lib/vimamsa/gui_settings.rb', line 137

def run
  @window.show
end

#save_and_closeObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vimamsa/gui_settings.rb', line 122

def save_and_close
  @widgets.each do |key, info|
    val = case info[:type]
          when :bool   then info[:widget].active?
          when :int    then info[:widget].value.to_i
          when :float  then info[:widget].value.to_f
          when :string then info[:widget].text
          end
    set(key, val)
  end
  save_settings_to_file
  gui_refresh_font
  @window.destroy
end