Debian Codegraph @

plugin/guide/interactive.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 module Plugin::Guide
4 module InteractiveMixin
5 def say(message)
6 self.next {
7 Plugin[:guide].timeline(:guide) <<
8 Mikutter::System::Message.new(description: message,
9 source: "guide",
10 created: Time.now)
11 }.extend(InteractiveMixin)
12 end
13
14 def prompt(message, choose = {Plugin[:change_account]._('次へ') => nil})
15 self.next {
16 promise = Deferred.new(true).extend(InteractiveMixin)
17 Plugin[:guide].timeline(:guide) <<
18 Mikutter::System::Message.new(description: message,
19 source: "guide",
20 created: Time.now,
21 confirm: choose,
22 confirm_callback: promise)
23 promise
24 }
25 end
26
27 def next(&block)
28 super(&block).extend(InteractiveMixin) end
29
30 def trap(&block)
31 super(&block).extend(InteractiveMixin) end
32
33 end
34
35 class Interactive < Deferred
36 include InteractiveMixin
37
38 def self.generate
39 Deferred.new.extend(InteractiveMixin)
40 end
41 end
42
43 class SubPartsGuide < Gdk::SubParts
44 Button = Data.define(:layout, :value, :x, :y, :width, :height)
45 OutsideOffset = 48 # 最初のボタンの左端との隙間
46 ButtonLeft = 6 # ボタンの左端と文字の左側の距離
47 ButtonRight = 6 # ボタンの右端と文字の右側の距離
48 ButtonTop = 6 # わかるよね
49 ButtonBottom = 6 # わかるよね
50 ButtonMargin = 3 # ボタンとボタンの距離
51
52 register
53
54 def initialize(*args)
55 super
56 if message[:confirm]
57 sid = helper.ssc(:clicked){ |this, e|
58 x, y = e.x, e.y
59 ofsty = helper.mainpart_height
60 helper.subparts.each{ |part|
61 break if part == self
62 ofsty += part.height }
63 if ofsty <= y and (ofsty + height) >= y and 1 == e.button
64 button = generate_buttons.find{|b| b.x < x and x < (b.x+b.width) }
65 if button
66 helper.signal_handler_disconnect(sid)
67 message[:confirm] = nil
68 helper.queue_resize
69 message[:confirm_callback].call(button.value) end end
70 false } end
71 end
72
73 def render(context)
74 if helper.visible? and message and message[:confirm]
75 context.save{
76 buttons = generate_buttons(context)
77 return if not buttons
78 buttons.each{ |button|
79 render_outline(context, button.x, button.y, *button.layout.size.map{|_|_/Pango::SCALE})
80 context.save{
81 context.translate(button.x + ButtonLeft, button.y + ButtonTop)
82 context.set_source_rgb(*(UserConfig[:mumble_basic_color] || [0,0,0]).map{ |c| c.to_f / 65536 })
83 context.show_pango_layout(button.layout) } } } end end
84
85 def height
86 buttons = generate_buttons
87 return 0 if not buttons
88 @height ||= (buttons.map{|b|b.layout.size[1]}.max / Pango::SCALE) + ButtonMargin*2 + ButtonTop + ButtonBottom end
89
90 private
91
92 def generate_buttons(context = nil)
93 if not message[:confirm]
94 return nil end
95 ofst = OutsideOffset + ButtonMargin
96 message[:confirm].map{ |label, value|
97 layout = (context || helper).create_pango_layout
98 layout.font_description = helper.font_description(UserConfig[:mumble_basic_font])
99 layout.text = label
100 width = layout.size[0]/Pango::SCALE + ButtonLeft + ButtonRight
101 x = ofst
102 ofst += width + ButtonMargin
103 Button.new(layout, value,
104 x, 0,
105 width,
106 layout.size[1]/Pango::SCALE + ButtonTop + ButtonBottom) } end
107
108 def render_outline(context, x, y, width, height)
109 rect = [ButtonMargin + x,
110 ButtonMargin + y,
111 width - ButtonMargin*2 + ButtonLeft + ButtonRight,
112 height - ButtonMargin*2 + ButtonTop + ButtonBottom, 4]
113 context.save {
114 context.pseudo_blur(4) {
115 context.fill {
116 context.set_source_rgb(0.5, 0.5, 0.5)
117 context.rounded_rectangle(*rect)
118 }
119 }
120 context.fill {
121 context.set_source_rgb(1, 0.95, 0.95)
122 context.rounded_rectangle(*rect)
123 }
124 }
125 end
126
127 def message
128 helper.message end
129
130 end
131
132 end