| 1 |
|
| 2 |
|
| 3 |
require 'gtk3' |
| 4 |
require 'cairo' |
| 5 |
|
| 6 |
|
| 7 |
e_ を表示するSubPartsを作るときの基底クラス。 |
| 8 |
|
| 9 |
い方 |
| 10 |
スを継承しましょう。 |
| 11 |
トを参考に、必要なメソッドをオーバライドします。 |
| 12 |
|
| 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 |
Message_ 。 |
| 20 |
それらを上に表示されるものから順番に返す。 |
| 21 |
で処理を実装すること。 |
| 22 |
中に何回も呼ばれるので、キャッシュなどで高速化に努めること。 |
| 23 |
|
| 24 |
する _Message_ |
| 25 |
def messages |
| 26 |
[] end |
| 27 |
|
| 28 |
|
| 29 |
memoize def score(message) |
| 30 |
Plugin[:gtk3].score_of(message) |
| 31 |
end |
| 32 |
|
| 33 |
が表示されている場所に表示するテキスト。 |
| 34 |
の投稿者のscreen nameと名前が表示される。 |
| 35 |
表示されない。この場合、ヘッダ右も表示されない。 |
| 36 |
|
| 37 |
るMessage |
| 38 |
|
| 39 |
il(ヘッダ左を使用しない場合) |
| 40 |
する文字列 |
| 41 |
ント情報 |
| 42 |
クアップ情報 |
| 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 |
|
| 55 |
るMessage |
| 56 |
|
| 57 |
ント情報 |
| 58 |
def header_left_font(message) |
| 59 |
default_font end |
| 60 |
|
| 61 |
タンプが表示されているところに表示するテキスト。 |
| 62 |
_message_ のタイムスタンプが表示される。 |
| 63 |
り値がエスケープを考慮する必要はないが、装飾を指定することはできない。 |
| 64 |
|
| 65 |
essage |
| 66 |
|
| 67 |
する文字列。 |
| 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 |
プを足した文字列を返す。 |
| 76 |
ーバライドするようにし、 |
| 77 |
るのが問題になる場合は、こちらをオーバライドする。 |
| 78 |
表示されない。この場合、ヘッダ左のみが表示される。 |
| 79 |
MessageBase#header_left_text を参照 |
| 80 |
|
| 81 |
essage |
| 82 |
|
| 83 |
il(ヘッダ右を使用しない場合) |
| 84 |
する文字列 |
| 85 |
ント情報 |
| 86 |
クアップ情報 |
| 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 |
|
| 93 |
るMessage |
| 94 |
|
| 95 |
ント情報 |
| 96 |
def header_right_font(message) |
| 97 |
default_font end |
| 98 |
|
| 99 |
上に表示するバッジ。 |
| 100 |
で処理を実装すること。 |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
値のいずれか一つ |
| 105 |
表示するバッジ画像 |
| 106 |
を表示しない |
| 107 |
def badge(message) |
| 108 |
nil end |
| 109 |
|
| 110 |
れた時、その _Message_ を引数に呼ばれる。 |
| 111 |
で処理を実装すること。 |
| 112 |
|
| 113 |
ックイベント |
| 114 |
れた _Message_ |
| 115 |
def on_click(e, message) |
| 116 |
end |
| 117 |
|
| 118 |
背景色を返す |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
素は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 |
枠の色を返す |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
素は0.0..1.0の範囲。 |
| 133 |
def edge_color(message) |
| 134 |
[0.5]*3 end |
| 135 |
|
| 136 |
oat_値に変換する |
| 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 |
|
| 145 |
、widthとheightのみが利用される |
| 146 |
ンを表示しない |
| 147 |
def icon_size |
| 148 |
Gdk::Rectangle.new(0, 0, Gdk.scale(DEFAULT_ICON_SIZE), Gdk.scale(DEFAULT_ICON_SIZE)) |
| 149 |
end |
| 150 |
|
| 151 |
テキスト色を返す |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
素は0.0..1.0の範囲。 |
| 156 |
def main_text_color(message) |
| 157 |
([0,0,0]).map{ |c| c.to_f / 65536 } end |
| 158 |
|
| 159 |
するフォントを返す |
| 160 |
|
| 161 |
るMessage |
| 162 |
|
| 163 |
ント情報 |
| 164 |
def main_text_font(message) |
| 165 |
default_font end |
| 166 |
|
| 167 |
大表示行数を返す。 |
| 168 |
超えた行は表示されない |
| 169 |
|
| 170 |
るMessage |
| 171 |
|
| 172 |
行数 |
| 173 |
def text_max_line_count(message) |
| 174 |
3 end |
| 175 |
|
| 176 |
|
| 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 |
太さ(px) |
| 190 |
def border_weight |
| 191 |
Gdk.scale(@border_weight) |
| 192 |
end |
| 193 |
|
| 194 |
の半径(px) |
| 195 |
def badge_radius |
| 196 |
Gdk.scale(@badge_radius) |
| 197 |
end |
| 198 |
|
| 199 |
|
| 200 |
def initialize(*args) |
| 201 |
super |
| 202 |
@margin, @edge, @border_weight, @badge_radius = 2, 8, 1, 6 end |
| 203 |
|
| 204 |
|
| 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 |
|
| 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 |
|
| 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 |
o::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 |
o::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 |
ut_ を得る。 |
| 363 |
odel |
| 364 |
性のため |
| 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 |
、浮いているような感じに |
| 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 |
|
| 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 |
、浮いているような感じに |
| 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 |
|
| 492 |
適用する装飾 |
| 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 |
とのプレーンテキストを返す。 |
| 517 |
絵文字は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 |