Debian Codegraph @

core/mui/cairo_sub_parts_voter.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require 'mui/cairo_sub_parts_helper'
4
5 require 'gtk3'
6 require 'cairo'
7
8 class ::Gdk::SubPartsVoter < Gdk::SubParts
9
10 attr_reader :votes
11
12 def initialize(*args)
13 super
14 @icon_width, @icon_height, @margin, @votes, @user_icon = 24, 24, 2, get_default_votes.to_a, Hash.new
15 @avatar_rect = []
16 @icon_ofst = 0
17 helper.ssc(:clicked){ |_, ev|
18 x, y = ev.x, ev.y
19 ofsty = helper.mainpart_height
20 helper.subparts.each{ |part|
21 break if part == self
22 ofsty += part.height }
23 if ofsty <= y and (ofsty + height) >= y
24 case ev.button
25 when 1
26 if(x >= @icon_ofst)
27 user = get_user_by_point(x)
28 if user
29 Plugin.call(:open, user)
30 end
31 end
32 end
33 end
34 false
35 }
36 last_motion_user = nil
37 helper.ssc(:motion_notify_event){ |_, ev|
38 x, y = ev.x, ev.y
39
40 if 0 != height
41 tipset = ''
42 ofsty = helper.mainpart_height
43 helper.subparts.each{ |part|
44 break if part == self
45 ofsty += part.height }
46 if ofsty <= y and (ofsty + height) >= y
47 if(x >= @icon_ofst)
48 user = get_user_by_point(x)
49 last_motion_user = user
50 if user
51 tipset = user.title end end end
52 helper.tooltip_text = tipset
53 end
54 false }
55 end
56
57 def icon_width
58 Gdk.scale @icon_height
59 end
60
61 def icon_height
62 Gdk.scale @icon_height
63 end
64
65 def margin
66 Gdk.scale @margin
67 end
68
69 def get_user_by_point(x)
70 if x >= @icon_ofst
71 _, index = @avatar_rect.each_with_index.to_a.bsearch{ |range, i| range.last > x }
72 @votes[index] if index
73 end
74 end
75
76 def render(context)
77 if get_vote_count != 0
78 context.save do
79 context.translate(@margin, 0)
80 put_title_icon(context)
81 put_counter(context)
82 put_voter(context)
83 end
84 end
85 @last_height = height
86 end
87
88 def height
89 if get_vote_count == 0
90 0
91 else
92 icon_height end end
93
94 def add(new)
95 if !@votes.include?(new)
96 @votes << new
97 helper.queue_draw
98 end
99 self
100 end
101 alias << add
102
103 def delete(user)
104 if @votes.delete(user)
105 helper.queue_draw
106 end
107 self
108 end
109
110 def name
111 raise end
112
113 # このSubPartsのアイコンのPixbufを返す。
114 # title_icon_model メソッドをオーバライドしない場合、こちらを必ずオーバライドしなければならない
115 def title_icon
116 title_icon_model.pixbuf(width: icon_width, height: icon_height)
117 end
118
119 # このSubPartsのアイコンをあらわすModelを返す。
120 # title_icon の内部でしか使われないので、このメソッドを使わないように title_icon を再定義した場合は
121 # このメソッドをオーバライドする必要はない。
122 def title_icon_model
123 raise
124 end
125
126 private
127
128 def put_title_icon(context)
129 context.save{
130 context.set_source_pixbuf(title_icon)
131 context.paint }
132 end
133
134 def put_counter(context)
135 plc = pl_count(context)
136 context.set_source_rgb(*(UserConfig[:mumble_basic_color] || [0,0,0]).map{ |c| c.to_f / 65536 })
137 context.save{
138 context.translate(icon_width + margin, (icon_width/2) - (plc.size[1] / Pango::SCALE / 2))
139 context.show_pango_layout(plc) }
140 @icon_ofst = ((plc.size[0] / Pango::SCALE + icon_width + margin*2).to_f / icon_width).ceil * icon_width
141 end
142
143 def put_voter(context)
144 context.translate(@icon_ofst, 0)
145 xpos = @icon_ofst
146 @avatar_rect = []
147 votes.each{ |user|
148 left = xpos
149 xpos += render_user(context, user)
150 @avatar_rect << (left...xpos)
151 break if width <= xpos } end
152
153 def render_user(context, user)
154 render_icon(context, user)
155 icon_width
156 end
157
158 def render_icon(context, user)
159 context.set_source_pixbuf(user_icon(user))
160 context.paint
161 context.translate(icon_width, 0)
162 end
163
164 def user_icon(user)
165 @user_icon[user[:id]] ||= user.icon.load_pixbuf(width: icon_width, height: icon_height) do |pb|
166 @user_icon[user[:id]] = pb
167 helper.queue_draw
168 end
169 end
170
171 def pl_count(context = Cairo::Context.dummy)
172 layout = context.create_pango_layout
173 layout.wrap = Pango::WrapMode::CHAR
174 layout.font_description = helper.font_description(UserConfig[:mumble_basic_font])
175 layout.text = "#{get_vote_count}"
176 layout
177 end
178
179 def get_vote_count
180 votes.size || votes.count
181 end
182
183 end