| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
き入力ボックス。 |
| 5 |
|
| 6 |
|
| 7 |
require 'gtk3' |
| 8 |
require 'thread' |
| 9 |
require 'mui/gtk_intelligent_textview' |
| 10 |
|
| 11 |
module Gtk |
| 12 |
class PostBox < Gtk::EventBox |
| 13 |
extend Gem::Deprecate |
| 14 |
attr_accessor :return_to_top |
| 15 |
|
| 16 |
@@ringlock = Mutex.new |
| 17 |
@@postboxes = [] |
| 18 |
|
| 19 |
インスタンスを返す |
| 20 |
def self.list |
| 21 |
return @@postboxes |
| 22 |
end |
| 23 |
|
| 24 |
|
| 25 |
るアカウント(3.3 obsolete) |
| 26 |
るMessage |
| 27 |
ると、その時のカレントワールドになる |
| 28 |
カーソルの前に最初から入力されている文字列 |
| 29 |
カーソルの後ろに最初から入力されている文字列 |
| 30 |
るなら偽。真ならUI上にtoが表示されるだけ |
| 31 |
加するか否か |
| 32 |
オプションの値 |
| 33 |
時々の :world_current フィルタの値を使う |
| 34 |
から成る連想配列 |
| 35 |
oxに移譲したPostBox |
| 36 |
xを持つことができるコンテナ |
| 37 |
_self_ が設定される。Procを指定した場合、新しいPostBoxを作る処理として、その無名関数を使う |
| 38 |
を引数に呼び出される |
| 39 |
def initialize(postable = nil, |
| 40 |
to: [], |
| 41 |
from: nil, |
| 42 |
header: ''.freeze, |
| 43 |
footer: ''.freeze, |
| 44 |
to_display_only: false, |
| 45 |
use_blind_footer: true, |
| 46 |
visibility: nil, |
| 47 |
target_world: nil, |
| 48 |
**kwrest) |
| 49 |
mainthread_only |
| 50 |
@posting = nil |
| 51 |
@return_to_top = nil |
| 52 |
@options = kwrest |
| 53 |
|
| 54 |
@from = from |
| 55 |
@to = (Array(to) + Array(@options[:subreplies])).uniq.freeze |
| 56 |
if postable |
| 57 |
warn "Gtk::Postbox.new(postable) is deprecated. see https://mikutter.hachune.net/rdoc/Gtk/PostBox.html" |
| 58 |
case postable |
| 59 |
when Message |
| 60 |
@to = [postable, *@to].freeze unless @to.include? postable |
| 61 |
when Diva::Model |
| 62 |
@from = postable |
| 63 |
end |
| 64 |
end |
| 65 |
@header = (header || '').freeze |
| 66 |
@footer = (footer || '').freeze |
| 67 |
@to_display_only = !!to_display_only |
| 68 |
@use_blind_footer = !!use_blind_footer |
| 69 |
@visibility = visibility |
| 70 |
@target_world = target_world |
| 71 |
super() |
| 72 |
ssc(:parent_set) do |
| 73 |
if parent |
| 74 |
sw = get_ancestor(Gtk::ScrolledWindow) |
| 75 |
if sw |
| 76 |
@return_to_top = sw.vadjustment.value == 0 |
| 77 |
else |
| 78 |
@return_to_top = false |
| 79 |
end |
| 80 |
post_it if @options[:delegated_by] |
| 81 |
end |
| 82 |
end |
| 83 |
add(generate_box) |
| 84 |
set_border_width(2) |
| 85 |
register end |
| 86 |
|
| 87 |
def widget_post |
| 88 |
return @post if defined?(@post) |
| 89 |
@post = gen_widget_post |
| 90 |
@post.hexpand = true |
| 91 |
post_set_default_text(@post) |
| 92 |
@post.wrap_mode = :char |
| 93 |
@post.border_width = 2 |
| 94 |
@post.buffer.ssc('changed') { |textview| |
| 95 |
refresh_buttons(false) |
| 96 |
false } |
| 97 |
@post.signal_connect_after('focus_out_event', &method(:focus_out_event)) |
| 98 |
@post end |
| 99 |
alias post widget_post |
| 100 |
|
| 101 |
def widget_remain |
| 102 |
return @remain if defined?(@remain) |
| 103 |
@remain = Gtk::Label.new('---') |
| 104 |
tag = Plugin[:gtk3].handler_tag |
| 105 |
@remain.ssc_atonce(:draw) { |
| 106 |
Plugin[:gtk3].on_world_change_current(tags: tag) { |world| |
| 107 |
update_remain_charcount |
| 108 |
} |
| 109 |
false |
| 110 |
} |
| 111 |
@remain.ssc(:destroy) { |
| 112 |
Plugin[:gtk3].detach(tag) |
| 113 |
} |
| 114 |
Delayer.new{ |
| 115 |
update_remain_charcount |
| 116 |
} |
| 117 |
widget_post.buffer.ssc(:changed){ |textview, event| |
| 118 |
update_remain_charcount |
| 119 |
} |
| 120 |
@remain end |
| 121 |
|
| 122 |
def widget_send |
| 123 |
return @send if defined?(@send) |
| 124 |
@send = Gtk::Button.new |
| 125 |
@send.add(Gtk::WebIcon.new(Skin.get_path('post.png'), 16, 16)) |
| 126 |
@send.sensitive = postable? |
| 127 |
@send.ssc(:clicked) do |button| |
| 128 |
post_it |
| 129 |
false |
| 130 |
end |
| 131 |
@send |
| 132 |
end |
| 133 |
|
| 134 |
def widget_tool |
| 135 |
return @tool if defined?(@tool) |
| 136 |
@tool = Gtk::Button.new |
| 137 |
@tool.add(Gtk::WebIcon.new(Skin.get_path('close.png'), 16, 16)) |
| 138 |
@tool.sensitive = destructible? || posting? |
| 139 |
@tool.signal_connect_after('focus_out_event', &method(:focus_out_event)) |
| 140 |
@tool.ssc(:event) do |
| 141 |
@tool.sensitive = destructible? || posting? |
| 142 |
false |
| 143 |
end |
| 144 |
@tool.ssc(:clicked) do |
| 145 |
if posting? |
| 146 |
@posting.cancel |
| 147 |
@tool.sensitive = destructible? || posting? |
| 148 |
cancel_post |
| 149 |
else |
| 150 |
destroy if destructible? |
| 151 |
end |
| 152 |
false |
| 153 |
end |
| 154 |
@tool |
| 155 |
end |
| 156 |
|
| 157 |
リック可否状態を更新する |
| 158 |
def refresh_buttons(refresh_brothers = true) |
| 159 |
if refresh_brothers and @options.has_key?(:postboxstorage) |
| 160 |
@options[:postboxstorage].children.each{ |brother| |
| 161 |
brother.refresh_buttons(false) } |
| 162 |
else |
| 163 |
widget_send.sensitive = postable? |
| 164 |
widget_tool.sensitive = destructible? || posting? end end |
| 165 |
|
| 166 |
ージの投稿中なら真を返す |
| 167 |
def posting? |
| 168 |
!!@posting end |
| 169 |
|
| 170 |
legateを作成するように設定されていれば真を返す |
| 171 |
def delegatable? |
| 172 |
@options[:delegate_other] end |
| 173 |
|
| 174 |
ォーカスを合わせる |
| 175 |
def active |
| 176 |
get_ancestor(Gtk::Window).set_focus(widget_post) if(get_ancestor(Gtk::Window)) end |
| 177 |
|
| 178 |
成功したら、self.destroyを呼んで自分自身を削除する |
| 179 |
|
| 180 |
:world_current フィルタの結果が使われる |
| 181 |
def post_it(world: target_world) |
| 182 |
if postable? |
| 183 |
return unless before_post(world: world || target_world) |
| 184 |
@posting = Plugin[:gtk3].compose( |
| 185 |
world || target_world, |
| 186 |
to_display_only? ? nil : @to.first, |
| 187 |
**compose_options |
| 188 |
).next{ |
| 189 |
destroy |
| 190 |
}.trap{ |err| |
| 191 |
warn err |
| 192 |
end_post |
| 193 |
} |
| 194 |
start_post |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
def destroy |
| 199 |
@@ringlock.synchronize{ |
| 200 |
if not(destroyed?) and not(frozen?) and parent |
| 201 |
parent.remove(self) |
| 202 |
@@postboxes.delete(self) |
| 203 |
super |
| 204 |
on_delete |
| 205 |
self.freeze end } end |
| 206 |
|
| 207 |
private |
| 208 |
|
| 209 |
def generate_box |
| 210 |
@reply_widgets = [] |
| 211 |
result = Gtk::Grid.new |
| 212 |
result.orientation = :horizontal |
| 213 |
result.add(widget_tool).add(widget_post).add(widget_remain).add(widget_send) |
| 214 |
w_replies = Gtk::Grid.new |
| 215 |
w_replies.orientation = :vertical |
| 216 |
w_replies.add(result) |
| 217 |
@to.select{|m|m.respond_to?(:description)}.each{ |message| |
| 218 |
w_reply = Gtk::Grid.new |
| 219 |
w_reply.orientation = :horizontal |
| 220 |
itv = Gtk::IntelligentTextview.new(message.description, { 'font' => :mumble_basic_font }) |
| 221 |
itv.hexpand = true |
| 222 |
itv.style_generator = lambda{ get_style_provider(message) } |
| 223 |
itv.bg_modifier |
| 224 |
ev = Gtk::EventBox.new |
| 225 |
ev.style_context.add_provider(get_style_provider(message), Gtk::StyleProvider::PRIORITY_APPLICATION) |
| 226 |
w_reply.add(Gtk::WebIcon.new(message.icon, 32, 32).set_valign(:start)) if message.respond_to?(:icon) |
| 227 |
w_replies.add(ev.add(w_reply.add(itv))) |
| 228 |
@reply_widgets << itv } |
| 229 |
w_replies end |
| 230 |
|
| 231 |
def gen_widget_post |
| 232 |
Gtk::TextView.new end |
| 233 |
|
| 234 |
def postable? |
| 235 |
not(widget_post.buffer.text.empty?) and (/[^\p{blank}]/ === widget_post.buffer.text) and Plugin[:gtk3].compose?(target_world, to_display_only? ? nil : @to.first, visibility: @visibility) |
| 236 |
end |
| 237 |
|
| 238 |
、そちらにフォーカスを回す |
| 239 |
|
| 240 |
current フィルタの結果が使われる |
| 241 |
|
| 242 |
を続ける |
| 243 |
投稿を開始した |
| 244 |
def before_post(world: nil) |
| 245 |
return false if delegate(world: world) |
| 246 |
@options[:before_post_hook]&.call(self) |
| 247 |
Plugin.call(:before_postbox_post, widget_post.buffer.text) |
| 248 |
true |
| 249 |
end |
| 250 |
|
| 251 |
def start_post |
| 252 |
if not(frozen? or destroyed?) |
| 253 |
widget_post.editable = false |
| 254 |
[widget_post, widget_send].compact.each{|widget| widget.sensitive = false } |
| 255 |
widget_tool.sensitive = true |
| 256 |
end |
| 257 |
end |
| 258 |
|
| 259 |
def end_post |
| 260 |
if not(frozen? or destroyed?) |
| 261 |
@posting = nil |
| 262 |
widget_post.editable = true |
| 263 |
[widget_post, widget_send].compact.each{|widget| widget.sensitive = true } end end |
| 264 |
|
| 265 |
投稿が中止された場合に呼ばれる |
| 266 |
def cancel_post |
| 267 |
if not(frozen? or destroyed?) |
| 268 |
if @options[:delegated_by] |
| 269 |
@options[:delegated_by].widget_post.buffer.text = widget_post.buffer.text |
| 270 |
destroy |
| 271 |
else |
| 272 |
end_post end end end |
| 273 |
|
| 274 |
def delegate(world: nil) |
| 275 |
if @options[:postboxstorage] && delegatable? |
| 276 |
options = { |
| 277 |
**all_options, |
| 278 |
delegate_other: false, |
| 279 |
delegated_by: self, |
| 280 |
target_world: world || target_world } |
| 281 |
if @options[:delegate_other].respond_to? :to_proc |
| 282 |
@options[:delegate_other].to_proc.(options) |
| 283 |
else |
| 284 |
@options[:postboxstorage].pack_start(Gtk::PostBox.new(nil, **options)).show_all |
| 285 |
end |
| 286 |
true |
| 287 |
end |
| 288 |
end |
| 289 |
|
| 290 |
def service |
| 291 |
target_world |
| 292 |
end |
| 293 |
|
| 294 |
private def target_world |
| 295 |
@target_world || current_world |
| 296 |
end |
| 297 |
|
| 298 |
private def current_world |
| 299 |
world, = Plugin.filtering(:world_current, nil) |
| 300 |
world |
| 301 |
end |
| 302 |
|
| 303 |
集前と同じ状態なら真を返す。 |
| 304 |
棄されている場合は、常に真を返す |
| 305 |
def post_is_empty? |
| 306 |
widget_post.destroyed? or widget_post.buffer.text.empty? or widget_post.buffer.text == @header + @footer end |
| 307 |
|
| 308 |
def brothers |
| 309 |
if(@options[:postboxstorage]) |
| 310 |
@options[:postboxstorage].children.find_all{|c| c.sensitive? } |
| 311 |
else |
| 312 |
[] end end |
| 313 |
|
| 314 |
def lonely? |
| 315 |
brothers.size <= 1 end |
| 316 |
|
| 317 |
ことによって削除して良いなら真を返す。 |
| 318 |
def destructible? |
| 319 |
if(@options.has_key?(:postboxstorage)) |
| 320 |
return false if lonely? or (brothers - [self]).all?{ |w| !w.delegatable? } |
| 321 |
post_is_empty? |
| 322 |
else |
| 323 |
true end end |
| 324 |
|
| 325 |
フォーカスが外れたら削除される設定の場合、このウィジェットを削除する |
| 326 |
def destroy_if_necessary(*related_widgets) |
| 327 |
if(not(frozen? or destroyed?) and not([widget_post, *related_widgets].compact.any?{ |w| w.focus? }) and destructible?) |
| 328 |
destroy |
| 329 |
true end end |
| 330 |
|
| 331 |
def on_delete(&block) |
| 332 |
if block |
| 333 |
@on_delete = block |
| 334 |
elsif defined? @on_delete |
| 335 |
@on_delete.call |
| 336 |
end |
| 337 |
end |
| 338 |
|
| 339 |
def reply? |
| 340 |
!@to.empty? and !to_display_only? end |
| 341 |
|
| 342 |
def register |
| 343 |
@@ringlock.synchronize{ |
| 344 |
@@postboxes << self } end |
| 345 |
|
| 346 |
つけるかどうかを返す |
| 347 |
|
| 348 |
|
| 349 |
def use_blind_footer? |
| 350 |
@use_blind_footer end |
| 351 |
|
| 352 |
def update_remain_charcount |
| 353 |
remain_charcount&.next{ |count| |
| 354 |
@remain.set_text((count || '---').to_s) if not @remain.destroyed? |
| 355 |
}&.trap { |
| 356 |
@remain.set_text('---') if not @remain.destroyed? |
| 357 |
} |
| 358 |
end |
| 359 |
|
| 360 |
def remain_charcount |
| 361 |
if not widget_post.destroyed? |
| 362 |
Plugin[:gtk3].spell(:remain_charcount, target_world, **compose_options) |
| 363 |
end |
| 364 |
end |
| 365 |
|
| 366 |
def focus_out_event(widget, event=nil) |
| 367 |
options = @options |
| 368 |
Delayer.new{ |
| 369 |
if(not(frozen? or destroyed?) and not(options.has_key?(:postboxstorage)) and post_is_empty?) |
| 370 |
destroy_if_necessary(widget_send, widget_tool, *@reply_widgets) end } |
| 371 |
false end |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
def get_backgroundcolor(message) |
| 376 |
if(message.from_me?) |
| 377 |
UserConfig[:mumble_self_bg] |
| 378 |
elsif(message.to_me?) |
| 379 |
UserConfig[:mumble_reply_bg] |
| 380 |
else |
| 381 |
UserConfig[:mumble_basic_bg] end end |
| 382 |
|
| 383 |
def get_style_provider(message) |
| 384 |
color = get_backgroundcolor(message) |
| 385 |
Gtk::CssProvider.new.tap do |provider| |
| 386 |
provider.load_from_data(<<~CSS) |
| 387 |
*, *:active, *:disabled, *:hover, *:focus { |
| 388 |
background-color: rgb(#{color[0] / 256}, #{color[1] / 256}, #{color[2] / 256}); |
| 389 |
} |
| 390 |
CSS |
| 391 |
end |
| 392 |
end |
| 393 |
|
| 394 |
def post_set_default_text(post) |
| 395 |
if @options[:delegated_by] |
| 396 |
post.buffer.text = @options[:delegated_by].post.buffer.text |
| 397 |
@options[:delegated_by].post.buffer.text = '' |
| 398 |
elsif !(@header.empty? and @footer.empty?) |
| 399 |
post.buffer.text = @header + @footer |
| 400 |
post.buffer.place_cursor(post.buffer.get_iter_at(offset: @header.size)) |
| 401 |
end |
| 402 |
post.accepts_tab = false |
| 403 |
end |
| 404 |
|
| 405 |
を生成した時に指定された全ての名前付き引数と値のペアを返す |
| 406 |
|
| 407 |
|
| 408 |
def all_options |
| 409 |
{ from: @from, |
| 410 |
to: @to, |
| 411 |
footer: @footer, |
| 412 |
to_display_only: to_display_only?, |
| 413 |
visibility: @visibility, |
| 414 |
**@options } end |
| 415 |
|
| 416 |
際のオプションを返す |
| 417 |
|
| 418 |
|
| 419 |
def compose_options |
| 420 |
text = widget_post.buffer.text |
| 421 |
text += UserConfig[:footer] if use_blind_footer? |
| 422 |
{ |
| 423 |
body: text, |
| 424 |
visibility: @visibility |
| 425 |
} |
| 426 |
end |
| 427 |
|
| 428 |
の下に表示するのみで、投稿時にリプライにしない |
| 429 |
|
| 430 |
|
| 431 |
def to_display_only? |
| 432 |
@to_display_only end |
| 433 |
|
| 434 |
end |
| 435 |
end |