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.
25 require "embrace/imap"
28 DATADIR = "/usr/local/share/#{PKG_NAME}/"
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 })
40 move_relative(obj, (b[2] / 2) - (a[2] / 2),
41 (b[3] / 2) - (a[3] / 2))
45 set_color(*(get_color[0..-2] << alpha))
51 ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
54 class ZeroToOneAnimator < Ecore::Animator
55 def initialize(duration)
61 v = [(Time.now - started) / duration, 1.0].min
65 @finished.call(self) unless (v < 1.0) || @finished.nil?
76 def on_finished(&block)
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|
87 objects.each { |o| o.alpha = a }
96 class InverseAlphaAnimator < AlphaAnimator
102 class MoveAnimator < ZeroToOneAnimator
103 def initialize(duration, movement, *objects)
104 zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
106 super(duration) do |v|
108 v = Math.sin(v * Math::PI / 2.0)
110 zipped.each do |(o, orig_pos)|
111 o.move(orig_pos.first,
112 orig_pos.last + (movement * v).to_i)
118 class MailboxIcon < Evas::Smart
119 class FadeOutFinishedEvent < Ecore::Event
131 def initialize(evas, label)
139 @img = Evas::Image.new(evas)
140 @label = Evas::Text.new(evas)
142 @objects = [@img, @label]
143 @objects.each { |o| add_member(o) }
145 set_color(255, 255, 255, 0)
146 @img.set_color(255, 255, 255, 0)
147 @label.set_color(255, 0, 0, 0)
149 @img.set_file(ICON_FILE)
150 @img.set_fill(0, 0, *@img.get_size)
153 @label.set_font("VeraBd", 10)
155 resize(*@img.get_size)
161 @alpha_anim ||= AlphaAnimator.new(2, self)
162 @alpha_anim.on_finished { @alpha_anim = nil }
166 @alpha_anim ||= InverseAlphaAnimator.new(2, self)
167 @alpha_anim.on_finished do
169 FadeOutFinishedEvent.raise(self)
175 @objects.each { |o| o.show }
179 @objects.each { |o| o.hide }
181 @alpha_anim && @alpha_anim.delete
186 @objects.each { |o| o.delete }
189 @alpha_anim && @alpha_anim.delete
191 @img = @label = @alpha_anim = nil
195 @objects.each { |o| o.move(x, y) }
200 def smart_resize(w, h)
204 def smart_color_set(r, g, b, a)
205 @objects.each { |o| o.alpha = a }
209 class Container < Evas::Smart
210 class ContainerError < StandardError; end
211 class ContainerFullError < ContainerError; end
212 class ContainerLockedError < ContainerError; end
217 @bg = Evas::Rectangle.new(evas)
218 @bg.set_color(0, 0, 0, 8)
229 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
230 &method(:on_icon_fade_out_finished))
235 !slots_left.zero? && @add_lock_count.zero?
239 @about_to_add.zero? && @add_lock_count.zero?
243 Kernel.raise(ContainerFullError) if slots_left.zero?
244 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
246 i.move_relative(self, 0, 0)
251 # check whether we need to need to move this icon
257 movement = Main.instance.icon_height * (slots_left - 1)
261 move_time = 0.25 * slots_left
262 @animators << MoveAnimator.new(move_time, movement, i)
264 @animators.last.on_finished do |ani|
265 @animators.delete(ani)
273 i = @icons.index(icon)
274 return (block_given? ? yield : nil) if i.nil?
280 Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
281 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
287 def on_icon_fade_out_finished(ev)
288 i = @icons.index(ev.icon)
292 # icons that are placed above the one that's deleted need
293 # to be moved. check whether are there any first
294 if i == @icons.length
301 @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
302 @animators.last.on_finished do |ani|
303 @animators.delete(ani)
326 def smart_resize(w, h)
332 MAX_ICONS - @icons.nitems - @about_to_add
336 @icons.nitems + @about_to_add
340 class Main < Ecore::Evas::SoftwareX11
343 attr_reader :container
348 self.has_alpha = true
349 self.title = "Embrace"
350 self.borderless = true
352 @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
354 on_resize { @container.resize(*geometry[2, 3]) }
356 @container = Container.new(evas)
357 @container.move(0, 0)
358 @container.layer = -1
361 size = [@icon_dim.first, icon_height * MAX_ICONS]
366 evas.font_path_append("/usr/lib/X11/fonts/TTF")
369 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
370 &method(:on_mailbox_status)),
371 Ecore::EventHandler.new(IMAP::FinishedEvent,
372 &method(:on_finished))
375 s = File.expand_path("~/.e/apps/embrace/config.yaml")
376 @config = YAML.load(File.read(s))
379 @timer = Ecore::Timer.new(30, &method(:on_timer))
388 Ecore.main_loop_begin
393 return unless @server.nil?
395 @server = IMAP::Session.new(@config)
400 def on_mailbox_status(ev)
401 md = ev.name.match(/^Lists.(.+)$/)
405 lbl = md.captures.first
408 found = evas.find_object(lbl)
410 if ev.count.zero? && !found.nil? && @container.can_delete?
411 @container.delete(found)
412 elsif !ev.count.zero? && found.nil? && @container.can_add?
413 @container << MailboxIcon.new(evas, lbl)
425 Embrace::Main.instance.show
426 Embrace::Main.instance.run