Debian Codegraph @

core/mui/gtk_message_picker.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require 'gtk3'
3
4 require 'miku/miku'
5 require 'mui/gtk_extension'
6 require 'mui/gtk_mtk'
7 require 'mui/gtk_webicon'
8 require 'skin'
9
10 class Gtk::MessagePicker < Gtk::Frame
11 DEFAULT_CONDITION = [:==, :user, ''.freeze].freeze
12
13 def initialize(conditions, &block)
14 conditions = [] unless conditions.is_a? MIKU::List
15 super()
16 @not = (conditions.respond_to?(:car) and (conditions.car == :not))
17 if(@not)
18 conditions = (conditions[1] or []).freeze end
19 @changed_hook = block
20 @function, *exprs = *conditions
21 @function ||= :and
22
23 self.border_width = 8
24 self.label_widget = option_widgets
25
26 shell = Gtk::Grid.new
27 shell.orientation = :vertical
28 @container = Gtk::Grid.new
29 @container.orientation = :vertical
30 @container.expand = true
31 buttons = add_button
32 buttons.halign = :center
33 add(shell.add(@container).add(buttons))
34
35 exprs.each{|x| add_condition(x) }
36 end
37
38 def function(new = @function)
39 (new ? :or : :and) end
40
41 def option_widgets
42 @option_widgets ||= Gtk::Grid.new.
43 add(Mtk::boolean(lambda{ |new|
44 unless new.nil?
45 @function = function(new)
46 call end
47 @function == :or },
48 'いずれかにマッチする')).
49 add(Mtk::boolean(lambda{ |new|
50 unless new.nil?
51 @not = new
52 call end
53 @not },
54 '否定')) end
55
56 def add_button
57 @add_button ||= gen_add_button end
58
59 def add_condition(expr = DEFAULT_CONDITION)
60 pack = Gtk::Grid.new
61 close = Gtk::Button.new.add(Gtk::WebIcon.new(Skin[:close], 16, 16)).set_relief(Gtk::RELIEF_NONE)
62 close.valign = :start
63 close.signal_connect(:clicked){
64 @container.remove(pack)
65 pack.destroy
66 call
67 false }
68 pack.add(close)
69 case expr.first
70 when :and, :or, :not
71 pack.add(Gtk::MessagePicker.new(expr, &method(:call)))
72 else
73 pack.add(Gtk::MessagePicker::PickCondition.new(expr, &method(:call))) end
74 @container.add(pack) end
75
76 def to_a
77 result = [
78 @function,
79 *@container.children.map do |c| # c: Gtk::Grid
80 c.children.select do |w| # w: Gtk::Widget
81 (w.is_a?(Gtk::MessagePicker) ||
82 w.is_a?(Gtk::MessagePicker::PickCondition))
83 end.first.to_a
84 end.reject(&:empty?)
85 ].freeze
86 if result.size == 1
87 [].freeze
88 else
89 if @not
90 result = [:not, result].freeze end
91 result end end
92
93 private
94
95 def call
96 if @changed_hook
97 @changed_hook.call end end
98
99 def gen_add_button
100 container = Gtk::Grid.new
101 btn = Gtk::Button.new(label: '条件を追加')
102 btn.signal_connect(:clicked){
103 add_condition.show_all }
104 btn2 = Gtk::Button.new(label: 'サブフィルタを追加')
105 btn2.signal_connect(:clicked){
106 add_condition([:and, DEFAULT_CONDITION]).show_all }
107 container.add(btn).add(btn2) end
108
109 class PickCondition < Gtk::Grid
110 def initialize(conditions = DEFAULT_CONDITION, &block)
111 super()
112 @changed_hook = block
113 @condition, @subject, @expr = *conditions
114 build
115 end
116
117 def to_a
118 [@condition, @subject, @expr].freeze end
119
120 private
121
122 def call
123 if @changed_hook
124 @changed_hook.call end end
125
126 def build
127 extract_condition = Plugin.filtering(:extract_condition, []).first.to_h { |ec| [ec.slug, ec] }
128 w_argument = Mtk::input(lambda{ |new|
129 unless new === nil
130 @expr = new.freeze
131 call end
132 @expr },
133 nil)
134 w_operator = Mtk::chooseone(lambda{ |new|
135 unless new === nil
136 @condition = new.to_sym
137 call end
138 @condition.to_s },
139 nil,
140 Plugin.filtering(:extract_operator, []).first.to_h { |eo| [eo.slug.to_s, eo.name] })
141 w_condition = Mtk::chooseone(lambda{ |new|
142 unless new === nil
143 @subject = new.to_sym
144 call end
145 sensitivity = extract_condition.dig(@subject, :operator) && 0 != extract_condition.dig(@subject, :args)
146 w_argument.set_sensitive(sensitivity)
147 w_operator.set_sensitive(sensitivity)
148 @subject.to_s },
149 nil,
150 extract_condition.to_h { |slug, ec| [slug.to_s, ec.name] })
151 add(w_condition)
152 add(w_operator)
153 add(w_argument)
154 end
155 end
156
157 end