Debian Codegraph @

plugin/command/command.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 require_relative 'conditions'
4
5 Plugin.create :command do
6
7 on_gui_child_activated do |i_window, i_pane|
8 if i_window.is_a?(Plugin::GUI::Window) and i_pane.is_a?(Plugin::GUI::Pane)
9 last_active_pane[i_window.slug] = i_pane end end
10
11 command(:copy_selected_region,
12 name: _('コピー'),
13 condition: ->opt{ opt.messages.size == 1 && opt.widget&.selected_text(opt.messages.first)},
14 visible: true,
15 icon: Skin[:copy],
16 role: :timeline) do |opt|
17 Plugin.call(:clipboard_write, opt.widget.selected_text(opt.messages.first))
18 end
19
20 command(:copy_description,
21 name: _('本文をコピー'),
22 condition: ->opt{ opt.messages.size == 1 },
23 visible: true,
24 icon: Skin[:copy_all],
25 role: :timeline) do |opt|
26 Plugin.call(:clipboard_write, opt.messages.first.description)
27 end
28
29 command(:reply,
30 name: _('返信'),
31 condition: ->opt{ !opt.messages.empty? && compose?(opt.world, to: opt.messages) },
32 visible: true,
33 icon: Skin[:reply],
34 role: :timeline) do |opt|
35 messages = opt.messages
36 opt.widget.create_postbox(to: messages,
37 header: messages.select{|x|x.respond_to?(:idname)}.map{|x| "@#{x.idname}"}.uniq.join(' ') + ' ',
38 use_blind_footer: !UserConfig[:footer_exclude_reply]) end
39
40 command(:reply_all,
41 name: _('全員に返信'),
42 condition: ->opt{ !opt.messages.empty? && compose?(opt.world, to: opt.messages) },
43 visible: true,
44 icon: Skin[:reply],
45 role: :timeline) do |opt|
46 messages = opt.messages.map{ |m| m.ancestors.to_a }.flatten
47 opt.widget.create_postbox(to: messages,
48 header: messages.map(&:user).uniq.reject(&:me?).select{|x|x.respond_to?(:idname)}.map{|x| "@#{x.idname}"}.join(' ') + ' ',
49 use_blind_footer: !UserConfig[:footer_exclude_reply]) end
50
51 command(:retweet,
52 name: _('リツイート'),
53 condition: ->opt{ opt.messages.any?{|m| share?(opt.world, m) && !shared?(opt.world, m) } },
54 visible: true,
55 icon: Skin[:retweet],
56 role: :timeline) do |opt|
57 target = opt.messages.select{|m| share?(m, opt.world) }.reject{|m| shared?(m, opt.world) }.map(&:introducer)
58 target.deach { |m|
59 if m.from_me?([opt.world])
60 +dialog(_('リツイート')) {
61 label _('過去の栄光にすがりますか?')
62 link m
63 }.next {
64 share(m, opt.world)
65 }
66 else
67 share(m, opt.world)
68 end
69 }
70 end
71
72 command(:delete_retweet,
73 name: _('リツイートをキャンセル'),
74 condition: ->opt{ !opt.messages.empty? && opt.messages.all?{|m| destroy_share?(opt.world, m) } },
75 visible: true,
76 icon: Skin[:retweet_cancel],
77 role: :timeline) do |opt|
78 Delayer::Deferred.when(
79 opt.messages.map{|m| destroy_share(opt.world, m) }
80 ).terminate(_('リツイートをキャンセルしている途中でエラーが発生しました'))
81 end
82
83 command(:favorite,
84 name: _('ふぁぼふぁぼする'),
85 condition: ->opt{ opt.messages.any?{|m| favorite?(opt.world, m) && !favorited?(opt.world, m) } },
86 visible: true,
87 icon: Skin[:unfav],
88 role: :timeline) do |opt|
89 Delayer::Deferred.when(
90 opt.messages.select{|m|
91 favorite?(opt.world, m) && !favorited?(opt.world, m)
92 }.map{|m|
93 favorite(opt.world, m)
94 }
95 ).terminate(_('ふぁぼふぁぼしている途中でエラーが発生しました'))
96 end
97
98 command(:delete_favorite,
99 name: _('あんふぁぼ'),
100 condition: ->opt{ !opt.messages.empty? && opt.messages.all?{|m| unfavorite?(opt.world, m) } },
101 visible: true,
102 icon: Skin[:fav],
103 role: :timeline) do |opt|
104 Delayer::Deferred.when(
105 opt.messages.map{|m| unfavorite(opt.world, m) }
106 ).terminate(_('あんふぁぼしている途中でエラーが発生しました'))
107 end
108
109 command(:delete,
110 name: _('削除'),
111 condition: ->opt{ !opt.messages.empty? && opt.messages.all?{|m| destroy?(opt.world, m) } },
112 visible: true,
113 icon: Skin[:close],
114 role: :timeline) do |opt|
115 opt.messages.deach { |m|
116 +dialog(_('削除')) {
117 label _('失った信頼はもう戻ってきませんが、本当にこのつぶやきを削除しますか?')
118 link m
119 }.next {
120 destroy(opt.world, m)
121 }
122 }
123 end
124
125 command(:aboutuser,
126 name: ->(opt) {
127 if opt
128 (_("%{title}について") % { title: opt.messages&.first&.user&.title })
129 else
130 _('投稿者について')
131 end
132 },
133 condition: ->(opt) { !opt.messages.empty? && compose?(opt.world, to: opt.messages) },
134 visible: true,
135 icon: ->(opt) { opt && opt.messages.first.user.icon },
136 role: :timeline) do |opt|
137 Plugin.call(:open, opt.messages.first.user) end
138
139 command(:select_prev,
140 name: _('一つ上のメッセージを選択'),
141 condition: :itself.to_proc,
142 visible: false,
143 role: :timeline) do |opt|
144 Plugin.call(:gui_timeline_move_cursor_to, opt.widget, :prev) end
145
146 command(:select_next,
147 name: _('一つ下のメッセージを選択'),
148 condition: :itself.to_proc,
149 visible: false,
150 role: :timeline) do |opt|
151 Plugin.call(:gui_timeline_move_cursor_to, opt.widget, :next) end
152
153 command(:post_it,
154 name: _('投稿する'),
155 condition: ->opt{ opt.widget&.editable? },
156 visible: true,
157 icon: Skin[:post],
158 role: :postbox) do |opt|
159 opt.widget.post_it!(world: opt.world)
160 end
161
162 command(:google_search,
163 name: _('ggrks'),
164 condition: ->opt{ opt.messages.size == 1 && opt.widget.selected_text(opt.messages.first) },
165 visible: true,
166 icon: "https://www.google.co.jp/images/google_favicon_128.png",
167 role: :timeline) do |opt|
168 Plugin.call(:open, "http://www.google.co.jp/search?" + URI.encode_www_form({'q'=>opt.widget.selected_text(opt.messages.first).to_s}))
169 end
170
171 command(:open_in_browser,
172 name: _('ブラウザで開く'),
173 condition: ->opt{ opt.messages.size == 1 && opt.messages.first.perma_link },
174 visible: true,
175 role: :timeline) do |opt|
176 Plugin.call(:open, Diva::Model(:web)&.new(perma_link: opt.messages.first.perma_link)) end
177
178 command(:open_link,
179 name: _('リンクを開く'),
180 condition: ->opt{
181 opt.messages.size == 1 &&
182 opt.messages[0].entity.any? { |u| %i[urls media].include?(u[:slug]) }
183 },
184 visible: true,
185 role: :timeline) do |opt|
186 opt.messages[0].entity.each do |u|
187 url =
188 case u[:slug]
189 when :urls
190 u[:expanded_url] || u[:url]
191 when :media
192 u[:media_url]
193 end
194 Plugin.call(:open, url) if url
195 end
196 end
197
198 command(:copy_link,
199 name: _('リンクをコピー'),
200 condition: ->opt{
201 opt.messages.size == 1 &&
202 opt.messages[0].entity.any? {|u| u[:slug] == :urls }
203 },
204 visible: true,
205 role: :timeline) do |opt|
206 opt.messages[0].entity.each do |u|
207 Plugin.call(:clipboard_write, u[:url]) if u[:slug] == :urls
208 end
209 end
210
211 command(:new_pane,
212 name: _('新規ペインに移動'),
213 condition: ->opt{
214 pane = opt.widget.parent
215 pane.is_a?(Plugin::GUI::Pane) && pane.children.size != 1
216 },
217 visible: true,
218 role: :tab) do |opt|
219 tab = opt.widget.is_a?(Plugin::GUI::Tab) ? opt.widget : opt.widget.ancestor_of(Plugin::GUI::Tab)
220 window = tab.ancestor_of(Plugin::GUI::Window)
221 if window
222 pane = Plugin::GUI::Pane.instance
223 pane << tab
224 window << pane
225 else
226 error "window not found."
227 end
228 end
229
230 command(:close,
231 name: _('タブを閉じる'),
232 condition: ->opt{
233 opt.widget.deletable
234 },
235 visible: true,
236 icon: Skin[:close],
237 role: :tab) do |opt|
238 opt.widget.destroy
239 end
240
241 command(:focus_right_tab,
242 name: _('次のタブを選択'),
243 condition: :itself.to_proc,
244 visible: false,
245 role: :tab) do |opt|
246 focus_move_widget(opt.widget, 1)
247 end
248
249 command(:focus_left_tab,
250 name: _('前のタブを選択'),
251 condition: :itself.to_proc,
252 visible: false,
253 role: :tab) do |opt|
254 focus_move_widget(opt.widget, -1)
255 end
256
257 command(:focus_right_pane,
258 name: _('右のペインを選択'),
259 condition: :itself.to_proc,
260 visible: false,
261 role: :pane) do |opt|
262 focus_move_widget(opt.widget, -1)
263 end
264
265 command(:focus_left_pane,
266 name: _('左のペインを選択'),
267 condition: :itself.to_proc,
268 visible: false,
269 role: :pane) do |opt|
270 focus_move_widget(opt.widget, 1)
271 end
272
273 command(:focus_to_postbox,
274 name: _('投稿ボックスにフォーカス'),
275 condition: ->opt{
276 if opt.widget.respond_to? :active_chain
277 not opt.widget.active_chain.last.is_a? Plugin::GUI::Postbox
278 else
279 opt.widget.is_a? Plugin::GUI::Postbox end
280 },
281 visible: false,
282 role: :window) do |opt|
283 focus_move_to_nearest_postbox(opt.widget.active_chain.last)
284 end
285
286 command(:focus_to_tab,
287 name: _('タブにフォーカス'),
288 condition: :itself.to_proc,
289 visible: false,
290 role: :postbox) do |opt|
291 focus_move_to_latest_widget(opt.widget)
292 end
293
294 command(:timeline_scroll_to_top,
295 name: _('タイムラインの一番上にジャーンプ!'),
296 condition: :itself.to_proc,
297 visible: false,
298 role: :timeline) do |opt|
299 Plugin.call :gui_timeline_scroll, opt.widget, :top
300 end
301
302 command(:scroll_up,
303 name: _('上にスクロール'),
304 condition: :itself.to_proc,
305 visible: false,
306 role: :timeline) do |opt|
307 Plugin.call :gui_timeline_scroll, opt.widget, :up
308 end
309
310 command(:scroll_down,
311 name: _('下にスクロール'),
312 condition: :itself.to_proc,
313 visible: false,
314 role: :timeline) do |opt|
315 Plugin.call :gui_timeline_scroll, opt.widget, :down
316 end
317
318 # フォーカスを _widget_ から _distance_ に移動する
319 # ==== Args
320 # [widget] 起点となるウィジェット
321 # [distance] 移動距離
322 def focus_move_widget(widget, distance)
323 type_strict widget => Plugin::GUI::HierarchyParent
324 type_strict widget => Plugin::GUI::HierarchyChild
325 children = widget.parent.children.select{ |w| w.is_a? widget.class }
326 index = children.index(widget)
327 term = children[(index + distance) % children.size]
328 term = term.active_chain.last if term.respond_to? :active_chain
329 term.active! if term
330 src_tl = widget.active_chain.last
331 if widget.is_a?(Plugin::GUI::Pane) and src_tl.is_a?(Plugin::GUI::Timeline) and term.is_a?(Plugin::GUI::Timeline)
332 slide_timeline_focus(src_tl, term) end end
333
334 # タイムライン _src_ で選択されているディスプレイ上のy座標が同じ _dest_ のツイートに
335 # フォーカスを移動する
336 # ==== Args
337 # [src] フォーカスを取得するタイムライン
338 # [dest] フォーカスを設定するタイムライン
339 def slide_timeline_focus(src, dest)
340 type_strict src => Plugin::GUI::Timeline, dest => Plugin::GUI::Timeline
341 y = Plugin.filtering(:gui_timeline_cursor_position, src, nil).last
342 if y
343 Plugin.call(:gui_timeline_move_cursor_to, dest, y) end end
344
345 # 一番近い postbox にフォーカスを与える
346 # ==== Args
347 # [widget] 基準となるウィジェット
348 def focus_move_to_nearest_postbox(widget)
349 if widget.is_a? Plugin::GUI::HierarchyParent
350 postbox = widget.children.find{ |w| w.is_a? Plugin::GUI::Postbox }
351 if postbox
352 return postbox.active! end end
353 if widget.is_a? Plugin::GUI::HierarchyChild
354 focus_move_to_nearest_postbox(widget.parent) end end
355
356 # 最後にアクティブだったペインにフォーカスを与える。
357 # 親タイムラインがあれば、それにフォーカスを与える。
358 # ==== Args
359 # [postbox] 基準となるウィジェット
360 def focus_move_to_latest_widget(postbox)
361 if postbox.parent.is_a? Plugin::GUI::Window
362 pane = last_active_pane[postbox.parent.slug]
363 pane.active! if pane
364 else
365 postbox.parent.active! end end
366
367 def last_active_pane
368 @last_active_pane ||= {} end
369
370 end