| 1 |
|
| 2 |
|
| 3 |
module Gtk::FormDSL |
| 4 |
class SelectBuilder |
| 5 |
|
| 6 |
ルテキスト |
| 7 |
定のキー |
| 8 |
def initialize(formdsl, label_text, config_key, values={}, mode: :auto) |
| 9 |
@formdsl = formdsl |
| 10 |
@label_text = label_text |
| 11 |
@config_key = config_key |
| 12 |
@options = values.to_a |
| 13 |
@mode = mode |
| 14 |
end |
| 15 |
|
| 16 |
ックスに要素を追加する |
| 17 |
|
| 18 |
たらセットされる値 |
| 19 |
lock_ がなければ使われる。 |
| 20 |
価され、そのインスタンスが内容として使われる |
| 21 |
def option(value, text=nil, &block) |
| 22 |
@options ||= [] |
| 23 |
@options << if block |
| 24 |
grid = @formdsl.create_inner_setting |
| 25 |
grid.instance_eval(&block) |
| 26 |
[value, text, grid].freeze |
| 27 |
else |
| 28 |
[value, text].freeze |
| 29 |
end |
| 30 |
self |
| 31 |
end |
| 32 |
|
| 33 |
れた項目をウィジェットに組み立てる |
| 34 |
|
| 35 |
|
| 36 |
def build |
| 37 |
if @mode == :auto && !widget? |
| 38 |
build_combo |
| 39 |
else |
| 40 |
build_list |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
def method_missing(...) |
| 45 |
@formdsl.method_missing(...) |
| 46 |
end |
| 47 |
|
| 48 |
def respond_to_missing?(...) |
| 49 |
@formdsl.respond_to?(...) |
| 50 |
end |
| 51 |
|
| 52 |
private |
| 53 |
|
| 54 |
def widget? |
| 55 |
@options.any? { |_, _, w| w } |
| 56 |
end |
| 57 |
|
| 58 |
def build_combo |
| 59 |
label = Gtk::Label.new @label_text |
| 60 |
combo = Gtk::ComboBoxText.new |
| 61 |
@options.each { |_, text| combo.append(text, text) } |
| 62 |
_, combo.active_id = @options.find { |value,| value == @formdsl[@config_key] } |
| 63 |
combo.ssc :changed do |
| 64 |
@formdsl[@config_key], = @options[combo.active] |
| 65 |
end |
| 66 |
|
| 67 |
[label, combo] |
| 68 |
end |
| 69 |
|
| 70 |
def build_list |
| 71 |
list = Gtk::ListBox.new |
| 72 |
list.hexpand = true |
| 73 |
list.selection_mode = :none |
| 74 |
list.set_header_func do |row, before| |
| 75 |
next if before |
| 76 |
row.header = Gtk::Label.new.tap do |w| |
| 77 |
w.markup = "<b>#{@label_text}</b>" |
| 78 |
w.margin = 6 |
| 79 |
w.margin_start = 12 |
| 80 |
w.halign = :start |
| 81 |
end |
| 82 |
end |
| 83 |
list.ssc :row_activated do |_, row| |
| 84 |
row.child.each do |w| |
| 85 |
if w.is_a? Gtk::CheckButton |
| 86 |
w.clicked |
| 87 |
else |
| 88 |
w.can_focus? and w.has_focus = true |
| 89 |
end |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
@group = Gtk::RadioButton.new |
| 94 |
@options.each do |value, text, widget| |
| 95 |
box = Gtk::Box.new(:vertical) |
| 96 |
box.margin = box.spacing = 12 |
| 97 |
|
| 98 |
if widget |
| 99 |
け存在する場合は内容をRadioButtonに移動させる |
| 100 |
unless text |
| 101 |
labels = widget.children.filter { |w| w.is_a?(Gtk::Label) } |
| 102 |
if labels.size == 1 |
| 103 |
text = labels.first.label |
| 104 |
widget.remove(labels.first) |
| 105 |
end |
| 106 |
end |
| 107 |
box << build_check(value, text) |
| 108 |
|
| 109 |
widget.margin = 0 |
| 110 |
widget.margin_start = 24 |
| 111 |
box << widget |
| 112 |
else |
| 113 |
box << build_check(value, text) |
| 114 |
end |
| 115 |
|
| 116 |
list << box |
| 117 |
end |
| 118 |
|
| 119 |
[Gtk::Frame.new << list] |
| 120 |
end |
| 121 |
|
| 122 |
def build_check(value, text) |
| 123 |
Gtk::RadioButton.new(label: text, member: @group).tap do |w| |
| 124 |
@formdsl[@config_key] == value and w.active = true |
| 125 |
w.ssc(:toggled) { w.active? and @formdsl[@config_key] = value } |
| 126 |
end |
| 127 |
end |
| 128 |
end |
| 129 |
end |