5 # Copyright (c) 2006 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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.
26 require "embrace/imap"
29 DATADIR = "/usr/local/share/#{PKG_NAME}/"
33 ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
35 class ZeroToOneAnimator < Ecore::Animator
36 def initialize(duration)
42 v = [(Time.now - started) / duration, 1.0].min
46 @finished.call(self) unless (v < 1.0) || @finished.nil?
57 def on_finished(&block)
62 # an animator that runs for the specified number of seconds,
63 # and yields values between 0 and 255
64 class AlphaAnimator < ZeroToOneAnimator
65 def initialize(duration, object)
66 super(duration) do |v|
68 object.set_color(a, a, a, a)
77 class InverseAlphaAnimator < AlphaAnimator
83 class MoveAnimator < ZeroToOneAnimator
84 def initialize(duration, movement, *objects)
85 zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
87 super(duration) do |v|
89 v = Math.sin(v * Math::PI / 2.0)
91 zipped.each do |(o, orig_pos)|
92 o.move(orig_pos.first,
93 orig_pos.last + (movement * v).to_i)
99 class MailboxIcon < Evas::Smart
100 class FadeOutFinishedEvent < Ecore::Event
112 def initialize(evas, label)
120 @img = Evas::Image.new(evas)
121 @label = Evas::Text.new(evas)
123 @objects = [@img, @label]
124 @objects.each { |o| add_member(o) }
126 set_color(0, 0, 0, 0)
128 @img.set_file(ICON_FILE)
129 @img.set_fill(0, 0, *@img.get_size)
132 @label.set_font("VeraBd", 10)
137 @label_offset_x = (b[0] / 2) - (a[2] / 2)
138 @label_offset_y = (b[1] / 2) - (a[3] / 2)
140 resize(*@img.get_size)
146 @alpha_anim ||= AlphaAnimator.new(2, self)
147 @alpha_anim.on_finished { @alpha_anim = nil }
151 @alpha_anim ||= InverseAlphaAnimator.new(2, self)
152 @alpha_anim.on_finished do
154 FadeOutFinishedEvent.raise(self)
160 @objects.each { |o| o.show }
164 @objects.each { |o| o.hide }
166 @alpha_anim && @alpha_anim.delete
171 @objects.each { |o| o.delete }
174 @alpha_anim && @alpha_anim.delete
176 @img = @label = @alpha_anim = nil
182 # center the label on the image
183 @label.move(x + @label_offset_x,
187 def smart_resize(w, h)
191 def smart_color_set(r, g, b, a)
192 @img.set_color(r, g, b, a)
193 @label.set_color(r, 0, 0, a)
197 class Container < Evas::Smart
198 class ContainerError < StandardError; end
199 class ContainerFullError < ContainerError; end
200 class ContainerLockedError < ContainerError; end
205 @bg = Evas::Rectangle.new(evas)
206 @bg.set_color(0, 0, 0, 0)
217 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
218 &method(:on_icon_fade_out_finished))
223 !slots_left.zero? && @add_lock_count.zero?
227 @about_to_add.zero? && @add_lock_count.zero?
231 Kernel.raise(ContainerFullError) if slots_left.zero?
232 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
237 geo[1] + geo[3] % Main.instance.icon_height)
243 # check whether we need to need to move this icon
249 movement = Main.instance.icon_height * (slots_left - 1)
253 move_time = 0.25 * slots_left
254 @animators << MoveAnimator.new(move_time, movement, i)
256 @animators.last.on_finished do |ani|
257 @animators.delete(ani)
265 i = @icons.index(icon)
266 return (block_given? ? yield : nil) if i.nil?
272 Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
273 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
279 def on_icon_fade_out_finished(ev)
280 i = @icons.index(ev.icon)
284 # icons that are placed above the one that's deleted need
285 # to be moved. check whether are there any first
286 if i == @icons.length
293 @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
294 @animators.last.on_finished do |ani|
295 @animators.delete(ani)
318 def smart_resize(w, h)
324 geometry.pop / Main.instance.icon_height
328 max_icons - @icons.nitems - @about_to_add
332 @icons.nitems + @about_to_add
336 class Main < Ecore::Evas::SoftwareX11
339 attr_reader :container
344 self.has_alpha = true
345 self.title = "Embrace"
346 self.borderless = true
348 @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
350 on_resize { @container.resize(*geometry[2, 3]) }
352 @container = Container.new(evas)
353 @container.move(0, 0)
354 @container.layer = -1
357 size = [@icon_dim.first]
361 size << icon_height * 11
370 evas.font_path_append("/usr/lib/X11/fonts/TTF")
373 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
374 &method(:on_mailbox_status)),
375 Ecore::EventHandler.new(IMAP::FinishedEvent,
376 &method(:on_finished))
379 s = File.expand_path("~/.e/apps/embrace/config.yaml")
380 @config = YAML.load(File.read(s))
383 @timer = Ecore::Timer.new(30, &method(:on_timer))
392 Ecore.main_loop_begin
397 return unless @server.nil?
399 @server = IMAP::Session.new(@config)
404 def on_mailbox_status(ev)
405 md = ev.name.match(/^Lists.(.+)$/)
409 lbl = md.captures.first
412 found = evas.find_object(lbl)
414 if ev.count.zero? && !found.nil? && @container.can_delete?
415 @container.delete(found)
416 elsif !ev.count.zero? && found.nil? && @container.can_add?
417 @container << MailboxIcon.new(evas, lbl)
429 Embrace::Main.instance.show
430 Embrace::Main.instance.run