reworked icon handling
[embrace.git] / bin / embrace
1 #!/usr/bin/env ruby
2
3 # vim:syn=ruby
4 #
5 # Copyright (c) 2006 Tilman Sauerbeck (tilman at code-monkey de)
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of version 2 of the GNU General Public License as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 require "ecore"
21 require "ecore_x"
22 require "ecore_evas"
23 require "singleton"
24 require "yaml"
25 require "embrace/imap"
26
27 PKG_NAME = "embrace"
28 DATADIR = "/usr/local/share/#{PKG_NAME}/"
29
30 class Evas::EvasObject
31         def move_relative(obj, x, y)
32                 # FIXME investigate whether there's an easier way
33                 move(*obj.geometry[0..1].zip([x, y]).map { |(a, b)| a + b })
34         end
35
36         def center(obj)
37                 a = geometry
38                 b = obj.geometry
39
40                 move_relative(obj, (b[2] / 2) - (a[2] / 2),
41                                    (b[3] / 2) - (a[3] / 2))
42         end
43
44         def alpha=(alpha)
45                 set_color(*(get_color[0..-2] << alpha))
46         end
47 end
48
49 module Embrace
50         VERSION = "0.0.1"
51         ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
52         MAX_ICONS = 11
53
54         class ZeroToOneAnimator < Ecore::Animator
55                 def initialize(duration)
56                         @finished = nil
57
58                         started = Time.now
59
60                         super() do
61                                 v = [(Time.now - started) / duration, 1.0].min
62
63                                 yield v
64
65                                 @finished.call(self) unless (v < 1.0) || @finished.nil?
66                                 v < 1.0
67                         end
68                 end
69
70                 def delete
71                         super
72
73                         @finished = nil
74                 end
75
76                 def on_finished(&block)
77                         @finished = block
78                 end
79         end
80
81         # an animator that runs for the specified number of seconds,
82         # and yields values between 0 and 255
83         class AlphaAnimator < ZeroToOneAnimator
84                 def initialize(duration, *objects)
85                         super(duration) do |v|
86                                 objects.each { |o| o.alpha = (255 * v).to_i }
87                         end
88                 end
89         end
90
91         class MoveAnimator < ZeroToOneAnimator
92                 def initialize(duration, movement, *objects)
93                         zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
94
95                         super(duration) do |v|
96                                 # decelerate
97                                 v = Math.sin(v * Math::PI / 2.0)
98
99                                 zipped.each do |(o, orig_pos)|
100                                         o.move(orig_pos.first,
101                                                orig_pos.last + (movement * v).to_i)
102                                 end
103                         end
104                 end
105         end
106
107         class MailboxIcon < Evas::Smart
108                 attr_accessor :slot
109
110                 def initialize(evas, label)
111                         super(evas)
112
113                         @slot = nil
114                         @alpha_anim = nil
115
116                         @img = Evas::Image.new(evas)
117                         @label = Evas::Text.new(evas)
118
119                         @objects = [@img, @label]
120                         @objects.each { |o| add_member(o) }
121
122                         @img.set_color(255, 255, 255, 0)
123                         @label.set_color(255, 0, 0, 0)
124
125                         @img.set_file(ICON_FILE)
126                         @img.set_fill(0, 0, *@img.get_size)
127
128                         @label.text = label
129                         @label.set_font("VeraBd", 10)
130
131                         resize(*@img.get_size)
132                 end
133
134                 def label
135                         @label.text
136                 end
137
138                 # smart callbacks
139                 def on_show
140                         @objects.each { |o| o.show }
141
142                         @alpha_anim ||= AlphaAnimator.new(2, @img, @label)
143                         @alpha_anim.on_finished { @alpha_anim = nil }
144                 end
145
146                 def on_hide
147                         @objects.each { |o| o.hide }
148
149                         @alpha_anim && @alpha_anim.delete
150                         @alpha_anim = nil
151                 end
152
153                 def on_delete
154                         @objects.each { |o| o.delete }
155                         @objects.clear
156
157                         @alpha_anim && @alpha_anim.delete
158
159                         @img = @label = @alpha_anim = nil
160                 end
161
162                 def on_move(x, y)
163                         @objects.each { |o| o.move(x, y) }
164
165                         @label.center(self)
166                 end
167
168                 def on_resize(w, h)
169                         @img.resize(w, h)
170                 end
171         end
172
173         class Container < Evas::Smart
174                 class ContainerError < StandardError; end
175                 class ContainerFullError < ContainerError; end
176                 class ContainerLockedError < ContainerError; end
177
178                 include Enumerable
179
180                 def initialize(evas)
181                         super
182
183                         @bg = Evas::Rectangle.new(evas)
184                         @bg.set_color(0, 0, 0, 255)
185
186                         add_member(@bg)
187
188                         @icons = []
189                         @animators = []
190
191                         @about_to_add = 0
192                         @add_lock_count = 0
193                 end
194
195                 def each
196                         @icons.each { |i| yield i }
197                 end
198
199                 def <<(i)
200                         Kernel.raise(ContainerFullError) if slots_left.zero?
201                         Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
202
203                         i.move_relative(self, 0, 0)
204                         i.slot = next_slot
205                         i.clip = self
206                         i.show
207
208                         # check whether we need to need to move this icon
209                         if slots_left == 1
210                                 @icons[i.slot] = i
211                                 return
212                         end
213
214                         movement = Main.instance.icon_height * (slots_left - 1)
215
216                         @about_to_add += 1
217
218                         move_time = 0.25 * slots_left
219                         @animators << MoveAnimator.new(move_time, movement, i)
220
221                         @animators.last.on_finished do |ani|
222                                 @animators.delete(ani)
223                                 @icons[i.slot] = i
224
225                                 @about_to_add -= 1
226                         end
227                 end
228
229                 def delete(icon)
230                         i = @icons.index(icon)
231                         return (block_given? ? yield : nil) if i.nil?
232
233                         delete_at(i)
234                 end
235
236                 def delete_at(i)
237                         Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
238
239                         # icons that are placed above the one that's deleted need
240                         # to be moved
241                         ar = @icons[(i + 1)..-1]
242
243                         @icons[i].delete
244                         @icons.delete_at(i)
245
246                         return if ar.empty?
247
248                         @add_lock_count += 1
249
250                         @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
251                         @animators.last.on_finished do |ani|
252                                 @animators.delete(ani)
253                                 @add_lock_count -= 1
254                         end
255                 end
256
257                 # smart callbacks
258                 def on_show
259                         @bg.show
260                 end
261
262                 def on_hide
263                         @bg.hide
264                 end
265
266                 def on_delete
267                         @bg.delete
268                         @bg = nil
269                 end
270
271                 def on_move(x, y)
272                         @bg.move(x, y)
273                 end
274
275                 def on_resize(w, h)
276                         @bg.resize(w, h)
277                 end
278
279                 private
280                 def slots_left
281                         MAX_ICONS - @icons.nitems - @about_to_add
282                 end
283
284                 def next_slot
285                         @icons.nitems + @about_to_add
286                 end
287         end
288
289         class Main < Ecore::Evas::SoftwareX11
290                 include Singleton
291
292                 attr_reader :container
293
294                 def initialize
295                         super
296
297                         self.title = "Embrace"
298                         self.borderless = true
299
300                         @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
301
302                         on_resize { @container.resize(*geometry[2, 3]) }
303
304                         @container = Container.new(evas)
305                         @container.move(0, 0)
306                         @container.layer = -1
307                         @container.show
308
309                         size = [@icon_dim.first, icon_height * MAX_ICONS]
310                         resize(*size)
311                         set_size_min(*size)
312                         set_size_max(*size)
313
314                         evas.font_path_append("/usr/lib/X11/fonts/TTF")
315
316                         @handlers = [
317                                 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
318                                                         &method(:on_mailbox_status)),
319                                 Ecore::EventHandler.new(IMAP::FinishedEvent,
320                                                         &method(:on_finished))
321                         ]
322
323                         s = File.expand_path("~/.e/apps/embrace/config.yaml")
324                         @config = YAML.load(File.read(s))
325
326                         @server = nil
327                         @timer = Ecore::Timer.new(30, &method(:on_timer))
328                         on_timer
329                 end
330
331                 def icon_height
332                         @icon_dim.last
333                 end
334
335                 def run
336                         Ecore.main_loop_begin
337                 end
338
339                 private
340                 def add_icon(name)
341                         @container << MailboxIcon.new(evas, name)
342                 end
343
344                 def on_timer
345                         return unless @server.nil?
346
347                         @server = IMAP::Session.new(@config)
348
349                         true
350                 end
351
352                 def on_mailbox_status(ev)
353                         md = ev.name.match(/^Lists.(.+)$/)
354                         if md.nil?
355                                 lbl = ev.name
356                         else
357                                 lbl = md.captures.first
358                         end
359
360                         found = @container.find { |i| i.label == lbl }
361
362                         begin
363                                 if ev.count.zero?
364                                         unless found.nil?
365                                                 #puts "removing icon #{lbl}"
366                                                 @container.delete(found)
367                                         else
368                                                 #puts "count == 0, but icon not found (#{lbl})"
369                                         end
370                                 elsif found.nil?
371                                         #puts "adding icon #{lbl}"
372                                         add_icon(lbl)
373                                 else
374                                         #puts "count > 0, but already there (#{lbl})"
375                                 end
376                         rescue Exception => e
377                                 puts e.message
378                         end
379
380                         false
381                 end
382
383                 def on_finished(ev)
384                         @server = nil
385                 end
386         end
387 end
388
389 Embrace::Main.instance.show
390 Embrace::Main.instance.run