| 1 |
|
| 2 |
|
| 3 |
require 'mui/gtk_crud' |
| 4 |
require 'mui/gtk_extension' |
| 5 |
require 'mui/gtk_keyconfig' |
| 6 |
require 'mui/gtk_message_picker' |
| 7 |
require 'mui/gtk_selectbox' |
| 8 |
|
| 9 |
require 'gtk3' |
| 10 |
|
| 11 |
module Mtk |
| 12 |
extend self |
| 13 |
extend Gem::Deprecate |
| 14 |
|
| 15 |
def adjustment(name, config, min, max) |
| 16 |
container = Gtk::Box.new(:horizontal, 0) |
| 17 |
container.pack_start(Gtk::Label.new(name), expand: false, fill: true, padding: 0) |
| 18 |
adj = Gtk::Adjustment.new((UserConfig[config] or min), min*1.0, max*1.0, 1.0, 5.0, 0.0) |
| 19 |
spinner = Gtk::SpinButton.new(adj, 0, 0) |
| 20 |
spinner.wrap = true |
| 21 |
adj.ssc(:value_changed){ |widget, e| |
| 22 |
UserConfig[config] = widget.value.to_i |
| 23 |
false |
| 24 |
} |
| 25 |
container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(spinner), expand: true, fill: true, padding: 0) |
| 26 |
end |
| 27 |
deprecate :adjustment, :none, 2020, 8 |
| 28 |
|
| 29 |
のようなHash |
| 30 |
def chooseone(key, label, values) |
| 31 |
values.freeze |
| 32 |
if key.respond_to?(:call) |
| 33 |
proc = key |
| 34 |
else |
| 35 |
proc = lambda{ |new| |
| 36 |
if new === nil |
| 37 |
UserConfig[key] |
| 38 |
else |
| 39 |
UserConfig[key] = new end } end |
| 40 |
container = Gtk::Box.new(:horizontal, 0) |
| 41 |
input = Gtk::ComboBoxText.new |
| 42 |
sorted_keys = values.keys.freeze |
| 43 |
sorted_keys.each{ |x| |
| 44 |
input.append_text(values[x].respond_to?(:call) ? values[x].call(nil) : values[x]) |
| 45 |
} |
| 46 |
input.active = (sorted_keys.index{ |i| i.to_s == proc.call(*[nil, input][0, proc.arity]).to_s } or 0) |
| 47 |
input.ssc(:changed){ |widget| |
| 48 |
proc.call(*[sorted_keys[widget.active], widget][0, proc.arity]) |
| 49 |
nil |
| 50 |
} |
| 51 |
container.pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0) if label |
| 52 |
container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0) |
| 53 |
end |
| 54 |
|
| 55 |
def choosemany(key, label, values) |
| 56 |
values.freeze |
| 57 |
if key.respond_to?(:call) |
| 58 |
proc = key |
| 59 |
else |
| 60 |
proc = lambda{ |new| |
| 61 |
if new === nil |
| 62 |
UserConfig[key] or [] |
| 63 |
else |
| 64 |
UserConfig[key] = new end } end |
| 65 |
input = Gtk::SelectBox.new(values, proc.call(*[nil, input][0, proc.arity])){ |selected| |
| 66 |
proc.call(*[selected, input][0, proc.arity]) |
| 67 |
} |
| 68 |
if label |
| 69 |
Gtk::Box.new(:horizontal, 0). |
| 70 |
pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0). |
| 71 |
pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0) |
| 72 |
else |
| 73 |
input end end |
| 74 |
deprecate :choosemany, :none, 2020, 8 |
| 75 |
|
| 76 |
def boolean(key, label) |
| 77 |
if key.respond_to?(:call) |
| 78 |
proc = key |
| 79 |
else |
| 80 |
proc = lambda{ |new| |
| 81 |
if new === nil |
| 82 |
UserConfig[key] |
| 83 |
else |
| 84 |
UserConfig[key] = new end } end |
| 85 |
input = Gtk::CheckButton.new(label) |
| 86 |
input.active = proc.call(*[nil, input][0, proc.arity]) |
| 87 |
input.ssc(:toggled){ |widget| |
| 88 |
proc.call(*[widget.active?, widget][0, proc.arity]) } |
| 89 |
return input |
| 90 |
end |
| 91 |
deprecate :boolean, :none, 2020, 8 |
| 92 |
|
| 93 |
def message_picker(key) |
| 94 |
if key.respond_to?(:call) |
| 95 |
proc = key |
| 96 |
else |
| 97 |
proc = lambda{ |new| |
| 98 |
if new.nil? |
| 99 |
UserConfig[key] |
| 100 |
else |
| 101 |
UserConfig[key] = new.freeze end } end |
| 102 |
input = Gtk::MessagePicker.new(proc.call(*[nil, input][0, proc.arity])){ |
| 103 |
proc.call(*[ input.to_a, input][0, proc.arity]) } |
| 104 |
input.ssc(:destroy){ |
| 105 |
proc.call(*[ input.to_a, input][0, proc.arity]) } |
| 106 |
return input |
| 107 |
end |
| 108 |
deprecate :message_picker, :none, 2020, 8 |
| 109 |
|
| 110 |
def default_or_custom(key, title, default_label, custom_label) |
| 111 |
group = default = Gtk::RadioButton.new(default_label) |
| 112 |
custom = Gtk::RadioButton.new(group, custom_label) |
| 113 |
input = Gtk::Entry.new |
| 114 |
input.text = UserConfig[:url_open_command] if UserConfig[:url_open_command].is_a?(String) |
| 115 |
default.active = !(input.sensitive = custom.active = UserConfig[key]) |
| 116 |
default.ssc(:toggled){ |widget| |
| 117 |
UserConfig[key] = nil |
| 118 |
input.sensitive = !widget.active? |
| 119 |
} |
| 120 |
custom.ssc(:toggled){ |widget| |
| 121 |
UserConfig[key] = input.text |
| 122 |
input.sensitive = widget.active? |
| 123 |
} |
| 124 |
input.ssc(:changed){ |widget| |
| 125 |
UserConfig[key] = widget.text |
| 126 |
} |
| 127 |
self.group(title, default, Gtk::Box.new(:horizontal, 0).add(custom).add(input)) |
| 128 |
end |
| 129 |
deprecate :default_or_custom, :none, 2020, 8 |
| 130 |
|
| 131 |
def input(key, label, visibility=true, &callback) |
| 132 |
if key.respond_to?(:call) |
| 133 |
proc = key |
| 134 |
else |
| 135 |
proc = lambda{ |new| |
| 136 |
if new |
| 137 |
UserConfig[key] = new |
| 138 |
else |
| 139 |
UserConfig[key].to_s end } end |
| 140 |
container = Gtk::Box.new(:horizontal, 0) |
| 141 |
input = Gtk::Entry.new |
| 142 |
input.text = proc.call(nil) |
| 143 |
input.visibility = visibility |
| 144 |
container.pack_start(Gtk::Label.new(label), expand: false, fill: true, padding: 0) if label |
| 145 |
container.pack_start(Gtk::Alignment.new(1.0, 0.5, 0, 0).add(input), expand: true, fill: true, padding: 0) |
| 146 |
input.ssc(:changed){ |widget| |
| 147 |
proc.call(widget.text) } |
| 148 |
callback&.call(container, input) |
| 149 |
return container |
| 150 |
end |
| 151 |
deprecate :input, :none, 2020, 8 |
| 152 |
|
| 153 |
def keyconfig(key, title) |
| 154 |
if key.respond_to?(:call) |
| 155 |
proc = key |
| 156 |
else |
| 157 |
proc = lambda{ |new| |
| 158 |
if new |
| 159 |
UserConfig[key] = new |
| 160 |
else |
| 161 |
UserConfig[key].to_s end } end |
| 162 |
keyconfig = Gtk::KeyConfig.new(title, proc.call(nil)) |
| 163 |
container = Gtk::Box.new(:horizontal, 0) |
| 164 |
container.pack_start(Gtk::Label.new(title), expand: false, fill: true, padding: 0) |
| 165 |
container.pack_start(keyconfig.right, expand: false) |
| 166 |
keyconfig.change_hook = proc |
| 167 |
return container |
| 168 |
end |
| 169 |
deprecate :keyconfig, :none, 2020, 8 |
| 170 |
|
| 171 |
def group(title, *children) |
| 172 |
group = Gtk::Frame.new.set_border_width(8) |
| 173 |
if(title.is_a?(Gtk::Widget)) |
| 174 |
group.set_label_widget(title) |
| 175 |
else |
| 176 |
group.set_label(title) end |
| 177 |
box = Gtk::Box.new(:vertical, 0).set_border_width(4) |
| 178 |
group.add(box) |
| 179 |
children.each{ |w| |
| 180 |
box.pack_start(w, expand: false) |
| 181 |
} |
| 182 |
group |
| 183 |
end |
| 184 |
deprecate :group, :none, 2020, 8 |
| 185 |
|
| 186 |
def expander(title, expanded, *children) |
| 187 |
group = Gtk::Expander.new(title).set_border_width(8) |
| 188 |
group.expanded = expanded |
| 189 |
box = Gtk::Box.new(:vertical, 0).set_border_width(4) |
| 190 |
group.add(box) |
| 191 |
children.each{ |w| |
| 192 |
box.pack_start(w, expand: false) |
| 193 |
} |
| 194 |
group |
| 195 |
end |
| 196 |
deprecate :expander, :none, 2020, 8 |
| 197 |
|
| 198 |
def fileselect(*) |
| 199 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 200 |
end |
| 201 |
deprecate :fileselect, :none, 2020, 8 |
| 202 |
|
| 203 |
def _colorselect(key, label) |
| 204 |
color = UserConfig[key] |
| 205 |
button = Gtk::ColorButton.new((color and Gdk::Color.new(*color))) |
| 206 |
button.title = label |
| 207 |
button.ssc(:color_set){ |w| |
| 208 |
UserConfig[key] = w.color.to_a } |
| 209 |
button end |
| 210 |
|
| 211 |
def _fontselect(key, label) |
| 212 |
button = Gtk::FontButton.new(UserConfig[key]) |
| 213 |
button.title = label |
| 214 |
button.ssc(:font_set){ |w| |
| 215 |
UserConfig[key] = w.font_name } |
| 216 |
button end |
| 217 |
|
| 218 |
def fontselect(key, label) |
| 219 |
Gtk::Box.new(:horizontal, 0).add(Gtk::Label.new(label).left).pack_start(_fontselect(key, label), expand: false) |
| 220 |
end |
| 221 |
deprecate :fontselect, :none, 2020, 8 |
| 222 |
|
| 223 |
def colorselect(key, label) |
| 224 |
Gtk::Box.new(:horizontal, 0).add(Gtk::Label.new(label).left).pack_start(_colorselect(key, label), expand: false) |
| 225 |
end |
| 226 |
deprecate :colorselect, :none, 2020, 8 |
| 227 |
|
| 228 |
def fontcolorselect(font, color, label) |
| 229 |
self.fontselect(font, label).pack_start(_colorselect(color, label), expand: false) |
| 230 |
end |
| 231 |
deprecate :fontcolorselect, :none, 2020, 8 |
| 232 |
|
| 233 |
def accountdialog_button(*) |
| 234 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 235 |
end |
| 236 |
deprecate :accountdialog_button, :none, 2020, 8 |
| 237 |
|
| 238 |
def account_dialog_inner(*) |
| 239 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 240 |
end |
| 241 |
|
| 242 |
def adi(symbol, label) |
| 243 |
input(lambda{ |new| UserConfig[symbol] }, label){ |c, i| yield(i) } end |
| 244 |
|
| 245 |
def account_dialog(*) |
| 246 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 247 |
end |
| 248 |
|
| 249 |
def alert(*) |
| 250 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 251 |
end |
| 252 |
|
| 253 |
def dialog_button(*) |
| 254 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 255 |
end |
| 256 |
|
| 257 |
def scrolled_dialog(*) |
| 258 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 259 |
end |
| 260 |
|
| 261 |
def dialog(*) |
| 262 |
raise Mtk::Read994Error, 'This method always crashes when it\'s called. see: https://dev.mikutter.hachune.net/issues/994' |
| 263 |
end |
| 264 |
|
| 265 |
class Mtk::ValidateError < StandardError; end |
| 266 |
class Mtk::Read994Error < StandardError; end |
| 267 |
|
| 268 |
end |