removed unused smart callbacks
[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
121                         @img.set_color(255, 255, 255, 0)
122                         @label.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 FixedSizeArray < Array
174                 def initialize(siz)
175                         super
176                 end
177
178                 def each
179                         super { |item| yield item unless item.nil? }
180                 end
181
182                 def delete_at(i)
183                         self[i] = nil
184                 end
185
186                 undef :push
187                 undef :<<
188                 undef :unshift
189
190                 undef :pop
191                 undef :shift
192                 undef :delete
193         end
194
195         class Container < Evas::Smart
196                 class ContainerError < StandardError; end
197                 class ContainerFullError < ContainerError; end
198                 class ContainerLockedError < ContainerError; end
199
200                 include Enumerable
201
202                 def initialize(evas)
203                         super
204
205                         @bg = Evas::Rectangle.new(evas)
206                         @bg.set_color(0, 0, 0, 255)
207
208                         @icons = FixedSizeArray.new(MAX_ICONS)
209                         @about_to_add = []
210                         @animators = []
211
212                         @lock_count = 0
213                 end
214
215                 def each
216                         @icons.each { |i| yield i unless i.nil? }
217                 end
218
219                 def <<(i)
220                         Kernel.raise(ContainerFullError) if slots_left.zero?
221                         Kernel.raise(ContainerLockedError) if @lock_count > 0
222
223                         i.move_relative(self, 0, 0)
224                         i.slot = next_slot
225                         i.clip = self
226                         i.show
227
228                         # check whether we need to need to move this icon
229                         if slots_left == 1
230                                 @icons[i.slot] = i
231                                 return
232                         end
233
234                         movement = Main.instance.icon_height * (slots_left - 1)
235
236                         @about_to_add << i
237
238                         move_time = 0.25 * slots_left
239                         @animators << MoveAnimator.new(move_time, movement, i)
240
241                         @animators.last.on_finished do |ani|
242                                 @animators.delete(ani)
243                                 @icons[i.slot] = i
244
245                                 # FIXME check whether we can always shift the array instead
246                                 #puts "really added #{i.label} now (slot #{i.slot})"
247                                 @about_to_add.delete(i)
248                         end
249                 end
250
251                 def delete(icon)
252                         # icons that are placed above the one that's deleted need
253                         # to be moved
254                         i = @icons.index(icon)
255                         return (block_given? ? yield : nil) if i.nil?
256
257                         delete_at(i)
258                 end
259
260                 def delete_at(i)
261                         @icons[i].delete
262                         @icons.delete_at(i)
263
264                         ar = @icons[i..-1].reject { |i| i.nil? }
265                         return if ar.nil?
266
267                         @lock_count += 1
268
269                         @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
270                         @animators.last.on_finished do |ani|
271                                 @animators.delete(ani)
272                                 @lock_count -= 1
273                         end
274                 end
275
276                 def length
277                         @icons.nitems
278                 end
279
280                 # smart callbacks
281                 def on_show
282                         @bg.show
283                 end
284
285                 def on_hide
286                         @bg.hide
287                 end
288
289                 def on_delete
290                         @bg.delete
291                         @bg = nil
292                 end
293
294                 def on_move(x, y)
295                         @bg.move(x, y)
296                 end
297
298                 def on_resize(w, h)
299                         @bg.resize(w, h)
300                 end
301
302                 private
303                 def slots_left
304                         MAX_ICONS - @icons.nitems - @about_to_add.length
305                 end
306
307                 def next_slot
308                         @icons.nitems + @about_to_add.length
309                 end
310         end
311
312         class Main < Ecore::Evas::SoftwareX11
313                 include Singleton
314
315                 attr_reader :container
316
317                 def initialize
318                         super
319
320                         self.title = "blah"
321                         self.borderless = true
322
323                         @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
324
325                         self.on_resize { @container.resize(*geometry[2, 3]) }
326
327                         @container = Container.new(evas)
328                         @container.move(0, 0)
329                         @container.layer = -1
330                         @container.show
331
332                         size = [@icon_dim.first, icon_height * MAX_ICONS]
333                         resize(*size)
334                         set_size_min(*size)
335                         set_size_max(*size)
336
337                         evas.font_path_append("/usr/lib/X11/fonts/TTF")
338
339                         @handlers = [
340                                 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
341                                                         &method(:on_mailbox_status)),
342                                 Ecore::EventHandler.new(IMAP::FinishedEvent,
343                                                         &method(:on_finished))
344                         ]
345
346                         s = File.expand_path("~/.e/apps/embrace/config.yaml")
347                         @config = YAML.load(File.read(s))
348
349                         @server = nil
350                         @timer = Ecore::Timer.new(30, &method(:on_timer))
351                         on_timer
352                 end
353
354                 def icon_height
355                         @icon_dim.last
356                 end
357
358                 def run
359                         Ecore.main_loop_begin
360                 end
361
362                 private
363                 def add_icon(name)
364                         @container << MailboxIcon.new(evas, name)
365                 end
366
367                 def on_timer
368                         return unless @server.nil?
369
370                         @server = IMAP::Session.new(@config)
371
372                         true
373                 end
374
375                 def on_mailbox_status(ev)
376                         md = ev.name.match(/^Lists.(.+)$/)
377                         if md.nil?
378                                 lbl = ev.name
379                         else
380                                 lbl = md.captures.first
381                         end
382
383                         found = @container.find { |i| i.label == lbl }
384
385                         begin
386                                 if ev.count.zero?
387                                         unless found.nil?
388                                                 #puts "removing icon #{lbl}"
389                                                 @container.delete(found)
390                                         else
391                                                 #puts "count == 0, but icon not found (#{lbl})"
392                                         end
393                                 elsif found.nil?
394                                         #puts "adding icon #{lbl}"
395                                         add_icon(lbl)
396                                 else
397                                         #puts "count > 0, but already there (#{lbl})"
398                                 end
399                         rescue Exception => e
400                                 puts e.message
401                         end
402
403                         false
404                 end
405
406                 def on_finished(ev)
407                         @server = nil
408                 end
409         end
410 end
411
412 Embrace::Main.instance.show
413 Embrace::Main.instance.run