File: gtk_mtk.rb

package info (click to toggle)
mikutter 5.0.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,700 kB
  • sloc: ruby: 21,307; sh: 181; makefile: 19
file content (268 lines) | stat: -rw-r--r-- 8,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding:utf-8 -*-

require 'mui/gtk_crud'
require 'mui/gtk_extension'
require 'mui/gtk_keyconfig'
require 'mui/gtk_message_picker'
require 'mui/gtk_selectbox'

require 'gtk3'

module Mtk
  extend self
  extend Gem::Deprecate

  def adjustment(name, config, min, max)
    container = Gtk::Box.new(:horizontal, 0)
    container.pack_start(Gtk::Label.new(name), expand: false, fill: true, padding: 0)
    adj = Gtk::Adjustment.new((UserConfig[config] or min), min*1.0, max*1.0, 1.0, 5.0, 0.0)
    spinner = Gtk::SpinButton.new(adj, 0, 0)
    spinner.wrap = true
    adj.ssc(:value_changed){ |widget, e|
      UserConfig[config] = widget.value.to_i
      false
    }
    container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(spinner), expand: true, fill: true, padding: 0)
  end
  deprecate :adjustment, :none, 2020, 8

  # [values] {値 => ラベル(String)} のようなHash
  def chooseone(key, label, values)
    values.freeze
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new === nil
          UserConfig[key]
        else
          UserConfig[key] = new end } end
    container = Gtk::Box.new(:horizontal, 0)
    input = Gtk::ComboBoxText.new
    sorted_keys = values.keys.freeze
    sorted_keys.each{ |x|
      input.append_text(values[x].respond_to?(:call) ? values[x].call(nil) : values[x])
    }
    input.active = (sorted_keys.index{ |i| i.to_s == proc.call(*[nil, input][0, proc.arity]).to_s } or 0)
    input.ssc(:changed){ |widget|
      proc.call(*[sorted_keys[widget.active], widget][0, proc.arity])
      nil
    }
    container.pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0) if label
    container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0)
  end

  def choosemany(key, label, values)
    values.freeze
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new === nil
          UserConfig[key] or []
        else
          UserConfig[key] = new end } end
    input = Gtk::SelectBox.new(values, proc.call(*[nil, input][0, proc.arity])){ |selected|
      proc.call(*[selected, input][0, proc.arity])
    }
    if label
      Gtk::Box.new(:horizontal, 0).
        pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0).
        pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0)
    else
      input end end
  deprecate :choosemany, :none, 2020, 8

  def boolean(key, label)
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new === nil
          UserConfig[key]
        else
          UserConfig[key] = new end } end
    input = Gtk::CheckButton.new(label)
    input.active = proc.call(*[nil, input][0, proc.arity])
    input.ssc(:toggled){ |widget|
      proc.call(*[widget.active?, widget][0, proc.arity]) }
    return input
  end
  deprecate :boolean, :none, 2020, 8

  def message_picker(key)
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new.nil?
          UserConfig[key]
        else
          UserConfig[key] = new.freeze end } end
    input = Gtk::MessagePicker.new(proc.call(*[nil, input][0, proc.arity])){
        proc.call(*[ input.to_a, input][0, proc.arity]) }
    input.ssc(:destroy){
      proc.call(*[ input.to_a, input][0, proc.arity]) }
    return input
  end
  deprecate :message_picker, :none, 2020, 8

  def default_or_custom(key, title, default_label, custom_label)
    group = default = Gtk::RadioButton.new(default_label)
    custom = Gtk::RadioButton.new(group, custom_label)
    input = Gtk::Entry.new
    input.text = UserConfig[:url_open_command] if UserConfig[:url_open_command].is_a?(String)
    default.active = !(input.sensitive = custom.active = UserConfig[key])
    default.ssc(:toggled){ |widget|
      UserConfig[key] = nil
      input.sensitive = !widget.active?
    }
    custom.ssc(:toggled){ |widget|
      UserConfig[key] = input.text
      input.sensitive = widget.active?
    }
    input.ssc(:changed){ |widget|
      UserConfig[key] = widget.text
    }
    self.group(title, default, Gtk::Box.new(:horizontal, 0).add(custom).add(input))
  end
  deprecate :default_or_custom, :none, 2020, 8

  def input(key, label, visibility=true, &callback)
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new
          UserConfig[key] = new
        else
          UserConfig[key].to_s end } end
    container = Gtk::Box.new(:horizontal, 0)
    input = Gtk::Entry.new
    input.text = proc.call(nil)
    input.visibility = visibility
    container.pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0) if label
    container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0)
    input.ssc(:changed){ |widget|
      proc.call(widget.text) }
    callback&.call(container, input)
    return container
  end
  deprecate :input, :none, 2020, 8

  def keyconfig(key, title)
    if key.respond_to?(:call)
      proc = key
    else
      proc = lambda{ |new|
        if new
          UserConfig[key] = new
        else
          UserConfig[key].to_s end } end
    keyconfig = Gtk::KeyConfig.new(title, proc.call(nil))
    container = Gtk::Box.new(:horizontal, 0)
    container.pack_start(Gtk::Label.new(title), expand: false, fill: true, padding: 0)
    container.pack_start(keyconfig.right, expand: false)
    keyconfig.change_hook = proc
    return container
  end
  deprecate :keyconfig, :none, 2020, 8

  def group(title, *children)
    group = Gtk::Frame.new.set_border_width(8)
    if(title.is_a?(Gtk::Widget))
      group.set_label_widget(title)
    else
      group.set_label(title) end
    box = Gtk::Box.new(:vertical, 0).set_border_width(4)
    group.add(box)
    children.each{ |w|
      box.pack_start(w, expand: false)
    }
    group
  end
  deprecate :group, :none, 2020, 8

  def expander(title, expanded, *children)
    group = Gtk::Expander.new(title).set_border_width(8)
    group.expanded = expanded
    box = Gtk::Box.new(:vertical, 0).set_border_width(4)
    group.add(box)
    children.each{ |w|
      box.pack_start(w, expand: false)
    }
    group
  end
  deprecate :expander, :none, 2020, 8

  def fileselect(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end
  deprecate :fileselect, :none, 2020, 8

  def _colorselect(key, label)
    color = UserConfig[key]
    button = Gtk::ColorButton.new((color and Gdk::Color.new(*color)))
    button.title = label
    button.ssc(:color_set){ |w|
      UserConfig[key] = w.color.to_a }
    button end

  def _fontselect(key, label)
    button = Gtk::FontButton.new(UserConfig[key])
    button.title = label
    button.ssc(:font_set){ |w|
      UserConfig[key] = w.font_name }
    button end

  def fontselect(key, label)
    Gtk::Box.new(:horizontal, 0).add(Gtk::Label.new(label).left).pack_start(_fontselect(key, label), expand: false)
  end
  deprecate :fontselect, :none, 2020, 8

  def colorselect(key, label)
    Gtk::Box.new(:horizontal, 0).add(Gtk::Label.new(label).left).pack_start(_colorselect(key, label), expand: false)
  end
  deprecate :colorselect, :none, 2020, 8

  def fontcolorselect(font, color, label)
    self.fontselect(font, label).pack_start(_colorselect(color, label), expand: false)
  end
  deprecate :fontcolorselect, :none, 2020, 8

  def accountdialog_button(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end
  deprecate :accountdialog_button, :none, 2020, 8

  def account_dialog_inner(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  def adi(symbol, label)
    input(lambda{ |new| UserConfig[symbol] }, label){ |c, i| yield(i) } end

  def account_dialog(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  def alert(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  def dialog_button(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  def scrolled_dialog(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  def dialog(*)
    raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994'
  end

  class Mtk::ValidateError < StandardError;  end
  class Mtk::Read994Error < StandardError;  end

end