Debian Codegraph @

core/mui/cairo_sub_parts_message_base.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require 'gtk3'
4 require 'cairo'
5
6 =begin rdoc
7 ナウい引用っぽく _Cairo::MiraclePainter_ のSubPartsとして別の _Message_ を表示するSubPartsを作るときの基底クラス。
8
9 = 使い方
10 このクラスを継承しましょう。
11 そして、以下のドキュメントを参考に、必要なメソッドをオーバライドします。
12 =end
13 class Gdk::SubPartsMessageBase < Gdk::SubParts
14 extend Memoist
15
16 DEFAULT_ICON_SIZE = 32
17 WHITE = [1.0, 1.0, 1.0].freeze
18
19 # SubPartsに表示する _Message_ 。
20 # 複数表示可能なので、それらを上に表示されるものから順番に返す。
21 # サブクラスで処理を実装すること。
22 # このメソッドはサブパーツの描画中に何回も呼ばれるので、キャッシュなどで高速化に努めること。
23 # ==== Return
24 # _Array_ :: このSubParts上に表示する _Message_
25 def messages
26 [] end
27
28 # :nodoc:
29 memoize def score(message)
30 Plugin[:gtk3].score_of(message)
31 end
32
33 # ヘッダの左の、Screen name、名前が表示されている場所に表示するテキスト。
34 # オーバライドしなければ、 _message_ の投稿者のscreen nameと名前が表示される。
35 # nilを返した場合、ヘッダは表示されない。この場合、ヘッダ右も表示されない。
36 # ==== Args
37 # [message] Message 表示するMessage
38 # ==== Return
39 # 次の3つの値またはnil(ヘッダ左を使用しない場合)
40 # [String] 表示する文字列
41 # [Pango::FontDescription] フォント情報
42 # [Pango::Attribute] マークアップ情報
43 def header_left_content(message)
44 user = message.user
45 if user.respond_to?(:idname)
46 attr_list, text = Pango.parse_markup("<b>#{Pango.escape(user.idname)}</b> #{Pango.escape(user.name || '')}")
47 else
48 attr_list, text = Pango.parse_markup(Pango.escape(user.name || ''))
49 end
50 return text, header_left_font(message), attr_list
51 end
52
53 # ヘッダ左に使用するフォントを返す
54 # ==== Args
55 # [message] Message 表示するMessage
56 # ==== Return
57 # [Pango::FontDescription] フォント情報
58 def header_left_font(message)
59 default_font end
60
61 # ヘッダの右の、タイムスタンプが表示されているところに表示するテキスト。
62 # オーバーライドしなければ、 _message_ のタイムスタンプが表示される。
63 # 表示される時に、 Pango.escape を通るので、この戻り値がエスケープを考慮する必要はないが、装飾を指定することはできない。
64 # ==== Args
65 # [message] Message 対象のMessage
66 # ==== Return
67 # [String] 表示する文字列。
68 def header_right_text(message)
69 now = Time.now
70 if message[:created].year == now.year && message[:created].month == now.month && message[:created].day == now.day
71 message[:created].strftime('%H:%M:%S'.freeze)
72 else
73 message[:created].strftime('%Y/%m/%d %H:%M:%S'.freeze) end end
74
75 # Gdk::SubPartsMessageBase#header_right_text にマークアップを足した文字列を返す。
76 # 通常は header_right_text をオーバライドするようにし、
77 # テキストがエスケープされるのが問題になる場合は、こちらをオーバライドする。
78 # nilを返した場合、ヘッダ右は表示されない。この場合、ヘッダ左のみが表示される。
79 # ヘッダ自体を消す方法については、 Gdk::SubPartsMessageBase#header_left_text を参照
80 # ==== Args
81 # [message] Message 対象のMessage
82 # ==== Return
83 # 次の3つの値またはnil(ヘッダ右を使用しない場合)
84 # [String] 表示する文字列
85 # [Pango::FontDescription] フォント情報
86 # [Pango::Attribute] マークアップ情報
87 def header_right_content(message)
88 attr_list, text = Pango.parse_markup("<span foreground=\"#999999\">#{Pango.escape(header_right_text(message))}</span>")
89 return text, header_right_font(message), attr_list end
90
91 # ヘッダ右に使用するフォントを返す
92 # ==== Args
93 # [message] Message 表示するMessage
94 # ==== Return
95 # [Pango::FontDescription] フォント情報
96 def header_right_font(message)
97 default_font end
98
99 # SubParts内の _Message_ の左上に表示するバッジ。
100 # サブクラスで処理を実装すること。
101 # ==== Args
102 # [message] Message
103 # ==== Return
104 # 以下の値のいずれか一つ
105 # GdkPixbuf::Pixbuf :: _message_ の左上に表示するバッジ画像
106 # nil :: バッジを表示しない
107 def badge(message)
108 nil end
109
110 # 表示している _Message_ がクリックされた時、その _Message_ を引数に呼ばれる。
111 # サブクラスで処理を実装すること。
112 # ==== Args
113 # [e] Gdk::EventButton クリックイベント
114 # [message] Message クリックされた _Message_
115 def on_click(e, message)
116 end
117
118 # SubParts内の _Diva::Model_ の背景色を返す
119 # ==== Args
120 # [model] Diva::Model
121 # ==== Return
122 # Array :: red, green, blueの配列。各要素は0.0..1.0の範囲。
123 def background_color(model)
124 color = Plugin.filtering(:message_bg_color, model, nil).last
125 rgb(color || WHITE)
126 end
127
128 # SubParts内の _Message_ の枠の色を返す
129 # ==== Args
130 # [message] Message
131 # ==== Return
132 # Array :: red, green, blueの配列。各要素は0.0..1.0の範囲。
133 def edge_color(message)
134 [0.5]*3 end
135
136 # GTK2のGtk::ColorとGTK3のGdk::RGBAをRGBの_Float_値に変換する
137 def rgb(color)
138 r, g, b = color
139 return [r, g, b] if r.is_a? Float
140 [r.fdiv(65536), g.fdiv(65536), b.fdiv(65536)].freeze
141 end
142
143 # アイコンのサイズを返す。
144 # ==== Return
145 # [Gdk::Rectangle] サイズ(px)。xとyは無視され、widthとheightのみが利用される
146 # [nil] アイコンを表示しない
147 def icon_size
148 Gdk::Rectangle.new(0, 0, Gdk.scale(DEFAULT_ICON_SIZE), Gdk.scale(DEFAULT_ICON_SIZE))
149 end
150
151 # _message_ の本文のテキスト色を返す
152 # ==== Args
153 # [message] Message
154 # ==== Return
155 # Array :: red, green, blueの配列。各要素は0.0..1.0の範囲。
156 def main_text_color(message)
157 ([0,0,0]).map{ |c| c.to_f / 65536 } end
158
159 # 本文使用するフォントを返す
160 # ==== Args
161 # [message] Message 表示するMessage
162 # ==== Return
163 # [Pango::FontDescription] フォント情報
164 def main_text_font(message)
165 default_font end
166
167 # 本文の最大表示行数を返す。
168 # この行数を超えた行は表示されない
169 # ==== Args
170 # [message] Message 表示するMessage
171 # ==== Return
172 # Fixnum 行数
173 def text_max_line_count(message)
174 3 end
175
176 # :nodoc:
177 memoize def default_font
178 helper.font_description(UserConfig[:reply_text_font])
179 end
180
181 def margin
182 Gdk.scale(@margin)
183 end
184
185 def edge
186 Gdk.scale(@edge)
187 end
188
189 # Fixnum 枠線の太さ(px)
190 def border_weight
191 Gdk.scale(@border_weight)
192 end
193
194 # Fixnum バッジの半径(px)
195 def badge_radius
196 Gdk.scale(@badge_radius)
197 end
198
199 # :nodoc:
200 def initialize(*args)
201 super
202 @margin, @edge, @border_weight, @badge_radius = 2, 8, 1, 6 end
203
204 # :nodoc:
205 def render_messages
206 if not helper.destroyed?
207 helper.queue_allocate
208 helper.ssc(:clicked) { |_, ev|
209 x, y = ev.x, ev.y
210 ofsty = helper.mainpart_height
211 helper.subparts.each { |part|
212 break if part == self
213 ofsty += part.height }
214 if ofsty <= y and (ofsty + height) >= y
215 my = 0
216 messages.each { |m|
217 my += message_height(m)
218 if y <= ofsty + my
219 on_click(ev, m)
220 break end } end } end end
221
222 # :nodoc:
223 def render(context)
224 if messages and not messages.empty?
225 messages.inject(0) { |base_y, message|
226 render_single_message(message, context, base_y) } end end
227
228 # :nodoc:
229 def height
230 if helper.destroyed?
231 0
232 else
233 messages&.sum(&method(:message_height)) || 0
234 end
235 end
236
237 private
238
239 def icon_width
240 size = icon_size
241 if size
242 size.width
243 else
244 0 end end
245
246 def icon_height
247 size = icon_size
248 if size
249 size.height
250 else
251 0 end end
252
253 def render_single_message(message, context, base_y)
254 render_outline(message, context, base_y)
255 _header_width, header_height = render_header(message, context, base_y)
256 context.save do
257 context.translate(margin + edge, margin + edge + base_y)
258 render_icon(message, context)
259 context.save do
260 context.translate(icon_width + margin*2, header_height || 0)
261 context.set_source_rgb(*main_text_color(message))
262 pango_layout = main_message(message)
263 if pango_layout.line_count <= text_max_line_count(message)
264 context.show_pango_layout(pango_layout)
265 else
266 line_height = pango_layout.pixel_size[1] / pango_layout.line_count + pango_layout.spacing / Pango::SCALE
267 context.translate(0, line_height*0.75)
268 (0...text_max_line_count(message)).map(&pango_layout.method(:get_line)).each do |line|
269 context.show_pango_layout_line(line)
270 context.translate(0, line_height) end end end
271 render_badge(message, context) end
272
273 base_y + message_height(message) end
274
275 def message_height(message)
276 header_height = [0, *[header_left(message), header_right(message)].compact.map{|h|
277 h.pixel_size[1]}].max
278 [icon_height, (header_height + main_message_height(message))].max + (margin + edge) * 2 end
279
280 def main_message_height(message)
281 pango_layout = main_message(message)
282 result = pango_layout.pixel_size[1]
283 if pango_layout.line_count <= text_max_line_count(message)
284 result
285 else
286 (result / pango_layout.line_count) * text_max_line_count(message) + pango_layout.spacing/Pango::SCALE * 2 end end
287
288 def header_left_context(message)
289 PangoCairo::FontMap.default.create_context.tap do |context|
290 context.set_font_description(header_left_font(message))
291 end
292 end
293
294 # ヘッダ(左)のための Pango::Layout のインスタンスを返す
295 def header_left(message, context=nil)
296 text, _font, attr_list = header_left_content(message)
297 if text
298 layout = Pango::Layout.new(header_left_context(message))
299 layout.attributes = attr_list if attr_list
300 layout.text = text
301 layout
302 end
303 end
304
305 def header_right_context(message)
306 PangoCairo::FontMap.default.create_context.tap do |context|
307 context.set_font_description(header_right_font(message))
308 end
309 end
310
311 # ヘッダ(右)のための Pango::Layout のインスタンスを返す
312 def header_right(message, context=nil)
313 text, _font, attr_list = header_right_content(message)
314 if text
315 layout = Pango::Layout.new(header_right_context(message))
316 layout.attributes = attr_list if attr_list
317 layout.text = text
318 layout.alignment = Pango::Alignment::RIGHT
319 layout
320 end
321 end
322
323 def render_header(message, context, base_y)
324 context.save do
325 context.translate(icon_width + margin*2 + edge, margin + edge + base_y)
326 context.set_source_rgb(0,0,0)
327 hl_layout = header_left(message, context)
328 if hl_layout
329 context.show_pango_layout(hl_layout)
330 hl_w, hl_h = hl_layout.pixel_size
331 hr_layout = render_header_right(message, context, base_y, hl_w)
332 if hr_layout
333 [[hl_w, hr_layout.pixel_size[0]].max,
334 [hl_h, hr_layout.pixel_size[1]].max]
335 else
336 [hl_w, hl_h] end end end end
337
338 def render_header_right(message, context, base_y, header_left_width)
339 header_w = width - icon_width - margin*3 - edge*2
340 hr_layout = header_right(message, context)
341 if hr_layout
342 context.save do
343 context.translate(header_w - hr_layout.pixel_size[0], 0)
344 if header_left_width > header_w - hr_layout.pixel_size[0] - 20
345 r, g, b = background_color(message)
346 grad = Cairo::LinearPattern.new(-20, base_y, hr_layout.pixel_size[0] + 20, base_y)
347 grad.add_color_stop_rgba(0.0, r, g, b, 0.0)
348 grad.add_color_stop_rgba(20.0 / (hr_layout.pixel_size[0] + 20), r, g, b, 1.0)
349 grad.add_color_stop_rgba(1.0, r, g, b, 1.0)
350 context.rectangle(-20, 0, hr_layout.pixel_size[0] + 20, hr_layout.pixel_size[1])
351 context.set_source(grad)
352 context.fill() end
353 context.show_pango_layout(hr_layout)
354 hr_layout end end end
355
356 def main_message_context
357 PangoCairo::FontMap.default.create_context.tap do |context|
358 context.set_font_description(default_font)
359 end
360 end
361
362 # _message_ に対する _Pango::Layout_ を得る。
363 # @params message [Diva::Model] 対象Message Model
364 # @params _context [nil] 互換性のため
365 def main_message(message, _context=nil)
366 Pango::Layout.new(main_message_context).tap do |layout|
367 layout.width = (width - icon_width - margin*3 - edge*2) * Pango::SCALE
368 layout.attributes = description_attr_list(
369 message,
370 emoji_height: layout.context.font_description.forecast_font_size,
371 pango_context: layout.context
372 )
373 layout.wrap = Pango::WrapMode::CHAR
374 layout.text = plain_description(message)
375 layout.context.set_shape_renderer do |c, shape, _|
376 photo = shape.data
377 if photo
378 draw_area = layout.index_to_pos(shape.start_index)
379 width = draw_area.width / Pango::SCALE
380 height = draw_area.height / Pango::SCALE
381 pixbuf = photo.load_pixbuf(width: width, height: height) do
382 helper.queue_resize
383 end
384 x = draw_area.x / Pango::SCALE
385 y = draw_area.y / Pango::SCALE
386 c.translate(x, y)
387 c.set_source_pixbuf(pixbuf)
388 c.rectangle(0, 0, width, height)
389 c.fill
390 end
391 end
392 end
393 end
394
395 def render_outline(message, context, base_y)
396 render_outline_floating(message, context, base_y) end
397
398 # エッジの描画。
399 # 影にblurを入れて、浮いているような感じに
400 def render_outline_floating(message, context, base_y, radius: 4, blur: 4)
401 x,y,w,h = edge, edge + base_y, width - edge*2, message_height(message) - edge*2
402 context.save {
403 context.pseudo_blur(blur) {
404 context.fill {
405 context.set_source_rgb(*edge_color(message))
406 context.rounded_rectangle(x,y,w,h, radius) } }
407 context.fill {
408 context.set_source_rgb(*background_color(message))
409 context.rounded_rectangle(x,y,w,h, radius) } } end
410
411 # エッジの描画。
412 # 細い線を入れる
413 def render_outline_solid(message, context, base_y, radius: 4)
414 context.save {
415 x,y,w,h = edge, edge + base_y, width - edge*2, message_height(message) - edge*2
416 #context.fill {
417 context.rounded_rectangle(x,y,w,h, radius)
418 context.set_source_rgb(*background_color(message))
419 context.fill_preserve
420 context.set_line_width(border_weight)
421 context.set_source_rgb(*edge_color(message))
422 context.stroke } end
423
424 # エッジの描画。
425 # 枠線なし
426 def render_outline_flat(message, context, base_y, radius: 4)
427 context.save {
428 x,y,w,h = edge, edge + base_y, width - edge*2, message_height(message) - edge*2
429 context.fill {
430 context.set_source_rgb(*background_color(message))
431 context.rounded_rectangle(x,y,w,h, radius) } } end
432
433 def render_badge(message, context)
434 render_badge_floating(message, context) end
435
436 # バッジの描画。
437 # 影にblurを入れて、浮いているような感じに
438 def render_badge_floating(message, context)
439 badge_pixbuf = badge(message)
440 if badge_pixbuf
441 context.save {
442 context.pseudo_blur(4) {
443 context.fill {
444 context.set_source_rgb(*edge_color(message))
445 context.circle(0, 0, badge_radius) } }
446 context.fill {
447 context.set_source_rgb(*background_color(message))
448 context.circle(0, 0, badge_radius) } }
449 context.translate(-badge_radius, -badge_radius)
450 context.set_source_pixbuf(badge_pixbuf)
451 context.paint end end
452
453 # バッジの描画。
454 # 細い線を入れる
455 def render_badge_solid(message, context)
456 badge_pixbuf = badge(message)
457 if badge_pixbuf
458 context.save {
459 context.circle(0, 0, badge_radius)
460 context.set_source_rgb(*background_color(message))
461 context.fill_preserve
462 context.set_source_rgb(*edge_color(message))
463 context.set_line_width(border_weight)
464 context.stroke }
465 context.translate(-badge_radius, -badge_radius)
466 context.set_source_pixbuf(badge_pixbuf)
467 context.paint end end
468
469 # バッジの描画。
470 # 枠線を入れない
471 def render_badge_flat(message, context)
472 badge_pixbuf = badge(message)
473 if badge_pixbuf
474 context.fill {
475 context.set_source_rgb(*background_color(message))
476 context.circle(0, 0, badge_radius) }
477 context.translate(-badge_radius, -badge_radius)
478 context.set_source_pixbuf(badge_pixbuf)
479 context.paint end end
480
481 def render_icon(message, context)
482 if icon_size
483 context.set_source_pixbuf(main_icon(message))
484 context.paint end end
485
486 def main_icon(message)
487 message.user.icon.load_pixbuf(width: icon_size.width, height: icon_size.width){ helper.queue_draw }
488 end
489
490 # 表示する際に本文に適用すべき装飾オブジェクトを作成する
491 # ==== Return
492 # Pango::AttrList 本文に適用する装飾
493 def description_attr_list(message, attr_list: Pango::AttrList.new, emoji_height: 24, pango_context: nil)
494 descent = 0
495 if pango_context
496 metrics = pango_context.get_metrics(pango_context.font_description, pango_context.language)
497 descent = metrics.descent
498 end
499 height = emoji_height * Pango::SCALE
500 y = -height + descent
501 score(message).inject(0){|start_index, note|
502 end_index = start_index + note.description.bytesize
503 if UserConfig[:miraclepainter_expand_custom_emoji] && note.respond_to?(:inline_photo)
504 end_index += -note.description.bytesize + 1
505 rect = Pango::Rectangle.new(0, y, height, height)
506 shape = Pango::AttrShape.new(rect, rect, note.inline_photo)
507 shape.start_index = start_index
508 shape.end_index = end_index
509 attr_list.insert(shape)
510 end
511 end_index
512 }
513 attr_list
514 end
515
516 # Entityを適用したあとのプレーンテキストを返す。
517 # Pangoの都合上、絵文字は1文字で表現する
518 def plain_description(message)
519 _plain_description[[message, UserConfig[:miraclepainter_expand_custom_emoji]]]
520 end
521
522 private memoize def _plain_description
523 Hash.new do |h, k|
524 message, expand_emoji = k
525 h[k] = score(message).map{|note|
526 if expand_emoji && note.respond_to?(:inline_photo)
527 '.'
528 else
529 note.description
530 end
531 }.to_a.join
532 end
533 end
534 end