Debian Codegraph @

plugin/gtk3/widget/timeline.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 require 'mui/gtk_postbox'
4
5 module Plugin::Gtk3
6 # 投稿ボックスとスクロール可能のリストビューを備えたウィジェット
7 class Timeline < Gtk::Grid
8 class << self
9 attr_accessor :current
10
11 def update_rows(model)
12 @instances.each do |instance|
13 instance.bulk_add([model]) if instance.include?(model)
14 end
15 end
16
17 def remove_rows(models)
18 @instances.each do |instance|
19 instance.bulk_remove(models)
20 end
21 end
22
23 def miracle_painters_of(message)
24 Enumerator.new do |yielder|
25 @instances.each do |instance|
26 detect = instance.find_miracle_painter_by_message(message)
27 yielder << detect if detect
28 end
29 end
30 end
31
32 def new(*)
33 instance = super
34 (@instances ||= []).push(instance)
35 @current ||= instance
36 instance
37 end
38 end
39
40 type_register
41
42 attr_reader :postbox, :order, :imaginary
43
44 def initialize(imaginary=nil)
45 super()
46
47 self.name = 'timeline'
48 self.orientation = :vertical
49
50 @uri_mp_dict = {} # { String (model uri) => MiraclePainter }
51 @imaginary = imaginary
52 @order = ->(m) { m.modified.to_i }
53 @postbox = Gtk::Grid.new.tap do |grid|
54 grid.orientation = :vertical
55 end
56 @listbox = Gtk::ListBox.new.tap do |listbox|
57 listbox.selection_mode = :multiple
58 listbox.activate_on_single_click = false
59 listbox.set_sort_func do |a, b|
60 @order.(b.model) <=> @order.(a.model)
61 end
62 end
63 ssc(:destroy) do
64 @imaginary.destroy
65 end
66
67 add @postbox
68 add(Gtk::ScrolledWindow.new.tap do |sw|
69 sw.set_policy :never, :automatic
70 sw.overlay_scrolling = false
71 sw.expand = true
72 sw.add @listbox
73 end)
74 end
75
76 def order=(order)
77 @order = order
78 @listbox.invalidate_sort
79 end
80
81 def include?(message)
82 @uri_mp_dict.has_key?(message.uri.to_s)
83 end
84
85 def active
86 @imaginary.active!(true, true)
87
88 if self.class.current && self.class.current != self && !self.class.current.destroyed?
89 self.class.current.unselect_all
90 end
91 self.class.current = self
92 end
93
94 def keypress(keyname)
95 Plugin::GUI.keypress keyname, @imaginary
96 end
97
98 def bulk_add(models)
99 update_ordinal = false
100 filtered_models = Plugin.filtering(:show_filter, [*models]).first
101 filtered_models.each do |message_or_share|
102 message = message_or_share.retweet_source || message_or_share
103 mp = find_miracle_painter_by_message(message)
104 if mp
105 update_ordinal |= @order.(mp.model) != @order.(message)
106 mp.message = message
107 else
108 row = MiraclePainter.new(message)
109 row.show_all
110 @listbox.add(row)
111 @uri_mp_dict[message.uri.to_s] = row
112 end
113 end
114 @listbox.invalidate_sort if update_ordinal
115 overflow = @listbox.children[1000..]
116 bulk_remove(overflow.map(&:message)) if overflow
117 end
118
119 def bulk_remove(messages)
120 messages.map { |message|
121 @uri_mp_dict.delete(message.uri.to_s)
122 }.compact.each(&@listbox.method(:remove))
123 end
124
125 def clear
126 raise NotImplementedError
127 end
128
129 def size
130 @uri_mp_dict.size
131 end
132
133 def select_row(row)
134 @listbox.select_row(row) unless selected_rows.include?(row)
135 end
136
137 def select_row_at_index(index)
138 unselect_all
139 @listbox.select_row @listbox.get_row_at_index index
140 end
141
142 def unselect_all
143 selected_rows.each do |row|
144 @listbox.unselect_row row
145 end
146 end
147
148 def jump_to(direction)
149 case direction
150 when :top
151 @listbox.adjustment.value = @listbox.adjustment.lower
152 select_row_at_index(0)
153 when :up
154 @listbox.adjustment.value -= @listbox.adjustment.page_increment
155 when :down
156 @listbox.adjustment.value += @listbox.adjustment.page_increment
157 end
158 end
159
160 def selected_rows
161 @listbox.selected_rows
162 end
163
164 def popup_menu(event)
165 menu = Gtk::Menu.new
166 menu.attach_to_widget self
167
168 ev, menus = Plugin::GUI::Command.get_menu_items @imaginary
169 Gtk::ContextMenu.new(*menus).build!(self, ev, menu)
170
171 menu.show_all
172 menu.popup_at_pointer event
173
174 # TODO: gtk3 参照を保持しておかないとGCされてしまう
175 @menu = menu
176 end
177
178 def add_postbox(i_postbox)
179 # ずっと表示される(投稿しても消えない)PostBoxの処理
180 # 既にprocっぽいものが入っているときはそのままにしておく
181 options = i_postbox.options.dup
182 if options[:delegate_other] && !options[:delegate_other].respond_to?(:to_proc)
183 i_timeline = i_postbox.ancestor_of(Plugin::GUI::Timeline)
184 options[:delegate_other] = postbox_delegation_generator(i_timeline)
185 options[:postboxstorage] = postbox
186 end
187 create_postbox(options)
188 end
189
190 def find_miracle_painter_by_message(message)
191 @uri_mp_dict[message.uri.to_s]
192 end
193
194 private
195
196 def create_postbox(options, &block)
197 options = options.dup
198 options[:before_post_hook] = ->(_this) {
199 get_ancestor(Gtk::Window).set_focus(self) unless destroyed?
200 }
201 pb = Gtk::PostBox.new(**options).show_all
202 postbox << pb
203 pb.on_delete(&block) if block
204 get_ancestor(Gtk::Window).set_focus(pb.post)
205 pb
206 end
207
208 def postbox_delegation_generator(i_timeline)
209 ->(params) { i_timeline.create_postbox(params) }
210 end
211 end
212 end