Debian Codegraph @

plugin/mastodon_gtk/subparts_status_info.rb

raw symbols SCIP
1 class Gdk::SubPartsMastodonStatusInfo < Gdk::SubParts
2 DEFAULT_ICON_SIZE = 20
3 ICON_NAME = {
4 'unlisted' => Skin[:unlisted],
5 'private' => Skin[:protected],
6 'direct' => Skin[:directmessage]
7 }.freeze
8
9 register
10
11 def icon_pixbuf
12 ICON_NAME[helper.message.visibility]&.pixbuf(width: icon_size.width, height: icon_size.height)
13 end
14
15 def show_icon?
16 (UserConfig[:mastodon_show_subparts_bot] && bot?) ||
17 (UserConfig[:mastodon_show_subparts_pin] && pinned?) ||
18 (UserConfig[:mastodon_show_subparts_visibility] && odd_visibility?)
19 end
20
21 def bot?
22 helper.message.user.respond_to?(:bot) && helper.message.user.bot
23 end
24
25 def pinned?
26 helper.message.respond_to?(:pinned) && helper.message.pinned
27 end
28
29 def odd_visibility?
30 helper.message.respond_to?(:visibility) && ICON_NAME.has_key?(helper.message.visibility)
31 end
32
33 def visibility_text(visibility)
34 case visibility
35 when 'unlisted'
36 Plugin[:mastodon]._('未収載')
37 when 'private'
38 Plugin[:mastodon]._('非公開')
39 when 'direct'
40 Plugin[:mastodon]._('ダイレクト')
41 else
42 ''
43 end
44 end
45
46 def initialize(*args)
47 super
48
49 @margin = 2
50 end
51
52 def render(context)
53 if helper.visible? && show_icon?
54 context.save do
55 context.translate(0, margin)
56 render_bot(context)
57 render_pin(context)
58 render_visibility(context)
59 end
60 end
61 end
62
63 def render_bot(context)
64 return unless UserConfig[:mastodon_show_subparts_bot] && bot?
65 context.translate(margin, 0)
66 context.set_source_pixbuf(
67 Skin[:underconstruction].pixbuf(width: icon_size.width, height: icon_size.height)
68 )
69 context.paint
70 context.translate(icon_size.width + margin, 0)
71 render_text(
72 context,
73 Plugin[:mastodon]._('bot'),
74 font_description: helper.font_description(UserConfig[:mumble_basic_font]),
75 color: UserConfig[:mumble_basic_color]
76 )
77 end
78
79 def render_pin(context)
80 return unless UserConfig[:mastodon_show_subparts_pin] && pinned?
81 context.translate(margin, 0)
82 context.set_source_pixbuf(
83 Skin[:underconstruction].pixbuf(width: icon_size.width, height: icon_size.height)
84 )
85 context.paint
86 context.translate(icon_size.width + margin, 0)
87 render_text(
88 context,
89 Plugin[:mastodon]._('ピン留め'),
90 font_description: helper.font_description(UserConfig[:mumble_basic_font]),
91 color: UserConfig[:mumble_basic_color]
92 )
93 end
94
95 def render_visibility(context)
96 return unless UserConfig[:mastodon_show_subparts_visibility] && odd_visibility?
97 context.translate(margin, 0)
98 context.set_source_pixbuf(icon_pixbuf)
99 context.paint
100 context.translate(icon_size.width + margin, 0)
101 render_text(
102 context,
103 visibility_text(helper.message.visibility),
104 font_description: helper.font_description(UserConfig[:mumble_basic_font]),
105 color: UserConfig[:mumble_basic_color]
106 )
107 end
108
109 def render_text(context, text, font_description: nil, color: nil)
110 layout = context.create_pango_layout
111 layout.font_description = font_description
112 layout.text = text
113 bot_text_width = layout.extents[1].width / Pango::SCALE
114 context.set_source_rgb(*(color || [0, 0, 0]).map { |c| c.to_f / 65536 })
115 context.show_pango_layout(layout)
116 context.translate(bot_text_width, 0)
117 end
118
119 def margin
120 Gdk.scale(@margin)
121 end
122
123 def icon_size
124 @icon_size ||= Gdk.scale(DEFAULT_ICON_SIZE).then { Gdk::Rectangle.new(0, 0, _1, _1) }
125 end
126
127 def height
128 @height ||= show_icon? ? icon_size.height + 2 * margin : 0
129 end
130 end