Made the alpha animators work on only one object.
[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, object)
85                         super(duration) do |v|
86                                 object.alpha = compute_alpha(v)
87                         end
88                 end
89
90                 def compute_alpha(v)
91                         (255 * v).to_i
92                 end
93         end
94
95         class InverseAlphaAnimator < AlphaAnimator
96                 def compute_alpha(v)
97                         super((1.0 - v).abs)
98                 end
99         end
100
101         class MoveAnimator < ZeroToOneAnimator
102                 def initialize(duration, movement, *objects)
103                         zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
104
105                         super(duration) do |v|
106                                 # decelerate
107                                 v = Math.sin(v * Math::PI / 2.0)
108
109                                 zipped.each do |(o, orig_pos)|
110                                         o.move(orig_pos.first,
111                                                orig_pos.last + (movement * v).to_i)
112                                 end
113                         end
114                 end
115         end
116
117         class MailboxIcon < Evas::Smart
118                 class FadeOutFinishedEvent < Ecore::Event
119                         attr_reader :icon
120
121                         def initialize(icon)
122                                 super()
123
124                                 @icon = icon
125                         end
126                 end
127
128                 attr_accessor :slot
129
130                 def initialize(evas, label)
131                         super(evas)
132
133                         self.name = label
134
135                         @slot = nil
136                         @alpha_anim = nil
137
138                         @img = Evas::Image.new(evas)
139                         @label = Evas::Text.new(evas)
140
141                         @objects = [@img, @label]
142                         @objects.each { |o| add_member(o) }
143
144                         set_color(255, 255, 255, 0)
145                         @img.set_color(255, 255, 255, 0)
146                         @label.set_color(255, 0, 0, 0)
147
148                         @img.set_file(ICON_FILE)
149                         @img.set_fill(0, 0, *@img.get_size)
150
151                         @label.text = name
152                         @label.set_font("VeraBd", 10)
153
154                         resize(*@img.get_size)
155                 end
156
157                 def fade_in
158                         show
159
160                         @alpha_anim ||= AlphaAnimator.new(2, self)
161                         @alpha_anim.on_finished { @alpha_anim = nil }
162                 end
163
164                 def fade_out
165                         @alpha_anim ||= InverseAlphaAnimator.new(2, self)
166                         @alpha_anim.on_finished do
167                                 @alpha_anim = nil
168                                 FadeOutFinishedEvent.raise(self)
169                         end
170                 end
171
172                 # smart callbacks
173                 def smart_show
174                         @objects.each { |o| o.show }
175                 end
176
177                 def smart_hide
178                         @objects.each { |o| o.hide }
179
180                         @alpha_anim && @alpha_anim.delete
181                         @alpha_anim = nil
182                 end
183
184                 def smart_delete
185                         @objects.each { |o| o.delete }
186                         @objects.clear
187
188                         @alpha_anim && @alpha_anim.delete
189
190                         @img = @label = @alpha_anim = nil
191                 end
192
193                 def smart_move(x, y)
194                         @objects.each { |o| o.move(x, y) }
195
196                         @label.center(self)
197                 end
198
199                 def smart_resize(w, h)
200                         @img.resize(w, h)
201                 end
202
203                 def smart_color_set(r, g, b, a)
204                         @objects.each { |o| o.alpha = a }
205                 end
206         end
207
208         class Container < Evas::Smart
209                 class ContainerError < StandardError; end
210                 class ContainerFullError < ContainerError; end
211                 class ContainerLockedError < ContainerError; end
212
213                 def initialize(evas)
214                         super
215
216                         @bg = Evas::Rectangle.new(evas)
217                         @bg.set_color(0, 0, 0, 8)
218
219                         add_member(@bg)
220
221                         @icons = []
222                         @animators = []
223
224                         @about_to_add = 0
225                         @add_lock_count = 0
226
227                         @handlers = [
228                                 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
229                                                         &method(:on_icon_fade_out_finished))
230                         ]
231                 end
232
233                 def can_add?
234                         !slots_left.zero? && @add_lock_count.zero?
235                 end
236
237                 def can_delete?
238                         @about_to_add.zero? && @add_lock_count.zero?
239                 end
240
241                 def <<(i)
242                         Kernel.raise(ContainerFullError) if slots_left.zero?
243                         Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
244
245                         i.move_relative(self, 0, 0)
246                         i.slot = next_slot
247                         i.clip = self
248                         i.fade_in
249
250                         # check whether we need to need to move this icon
251                         if slots_left == 1
252                                 @icons[i.slot] = i
253                                 return
254                         end
255
256                         movement = Main.instance.icon_height * (slots_left - 1)
257
258                         @about_to_add += 1
259
260                         move_time = 0.25 * slots_left
261                         @animators << MoveAnimator.new(move_time, movement, i)
262
263                         @animators.last.on_finished do |ani|
264                                 @animators.delete(ani)
265                                 @icons[i.slot] = i
266
267                                 @about_to_add -= 1
268                         end
269                 end
270
271                 def delete(icon)
272                         i = @icons.index(icon)
273                         return (block_given? ? yield : nil) if i.nil?
274
275                         delete_at(i)
276                 end
277
278                 def delete_at(i)
279                         Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
280                         Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
281
282                         @add_lock_count += 1
283                         @icons[i].fade_out
284                 end
285
286                 def on_icon_fade_out_finished(ev)
287                         i = @icons.index(ev.icon)
288                         ev.icon.delete
289                         @icons.delete_at(i)
290
291                         # icons that are placed above the one that's deleted need
292                         # to be moved. check whether are there any first
293                         if i == @icons.length
294                                 @add_lock_count -= 1
295                                 return
296                         end
297
298                         ar = @icons[i..-1]
299
300                         @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
301                         @animators.last.on_finished do |ani|
302                                 @animators.delete(ani)
303                                 @add_lock_count -= 1
304                         end
305                 end
306
307                 # smart callbacks
308                 def smart_show
309                         @bg.show
310                 end
311
312                 def smart_hide
313                         @bg.hide
314                 end
315
316                 def smart_delete
317                         @bg.delete
318                         @bg = nil
319                 end
320
321                 def smart_move(x, y)
322                         @bg.move(x, y)
323                 end
324
325                 def smart_resize(w, h)
326                         @bg.resize(w, h)
327                 end
328
329                 private
330                 def slots_left
331                         MAX_ICONS - @icons.nitems - @about_to_add
332                 end
333
334                 def next_slot
335                         @icons.nitems + @about_to_add
336                 end
337         end
338
339         class Main < Ecore::Evas::SoftwareX11
340                 include Singleton
341
342                 attr_reader :container
343
344                 def initialize
345                         super
346
347                         self.has_alpha = true
348                         self.title = "Embrace"
349                         self.borderless = true
350
351                         @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
352
353                         on_resize { @container.resize(*geometry[2, 3]) }
354
355                         @container = Container.new(evas)
356                         @container.move(0, 0)
357                         @container.layer = -1
358                         @container.show
359
360                         size = [@icon_dim.first, icon_height * MAX_ICONS]
361                         resize(*size)
362                         set_size_min(*size)
363                         set_size_max(*size)
364
365                         evas.font_path_append("/usr/lib/X11/fonts/TTF")
366
367                         @handlers = [
368                                 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
369                                                         &method(:on_mailbox_status)),
370                                 Ecore::EventHandler.new(IMAP::FinishedEvent,
371                                                         &method(:on_finished))
372                         ]
373
374                         s = File.expand_path("~/.e/apps/embrace/config.yaml")
375                         @config = YAML.load(File.read(s))
376
377                         @server = nil
378                         @timer = Ecore::Timer.new(30, &method(:on_timer))
379                         on_timer
380                 end
381
382                 def icon_height
383                         @icon_dim.last
384                 end
385
386                 def run
387                         Ecore.main_loop_begin
388                 end
389
390                 private
391                 def on_timer
392                         return unless @server.nil?
393
394                         @server = IMAP::Session.new(@config)
395
396                         true
397                 end
398
399                 def on_mailbox_status(ev)
400                         md = ev.name.match(/^Lists.(.+)$/)
401                         if md.nil?
402                                 lbl = ev.name
403                         else
404                                 lbl = md.captures.first
405                         end
406
407                         found = evas.find_object(lbl)
408
409                         if ev.count.zero? && !found.nil? && @container.can_delete?
410                                 @container.delete(found)
411                         elsif !ev.count.zero? && found.nil? && @container.can_add?
412                                 @container << MailboxIcon.new(evas, lbl)
413                         end
414
415                         false
416                 end
417
418                 def on_finished(ev)
419                         @server = nil
420                 end
421         end
422 end
423
424 Embrace::Main.instance.show
425 Embrace::Main.instance.run