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, object)
85 super(duration) do |v|
86 object.alpha = compute_alpha(v)
95 class InverseAlphaAnimator < AlphaAnimator
101 class MoveAnimator < ZeroToOneAnimator
102 def initialize(duration, movement, *objects)
103 zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
105 super(duration) do |v|
107 v = Math.sin(v * Math::PI / 2.0)
109 zipped.each do |(o, orig_pos)|
110 o.move(orig_pos.first,
111 orig_pos.last + (movement * v).to_i)
117 class MailboxIcon < Evas::Smart
118 class FadeOutFinishedEvent < Ecore::Event
130 def initialize(evas, label)
138 @img = Evas::Image.new(evas)
139 @label = Evas::Text.new(evas)
141 @objects = [@img, @label]
142 @objects.each { |o| add_member(o) }
144 set_color(255, 255, 255, 0)
145 @img.set_color(255, 255, 255, 0)
146 @label.set_color(255, 0, 0, 0)
148 @img.set_file(ICON_FILE)
149 @img.set_fill(0, 0, *@img.get_size)
152 @label.set_font("VeraBd", 10)
154 resize(*@img.get_size)
160 @alpha_anim ||= AlphaAnimator.new(2, self)
161 @alpha_anim.on_finished { @alpha_anim = nil }
165 @alpha_anim ||= InverseAlphaAnimator.new(2, self)
166 @alpha_anim.on_finished do
168 FadeOutFinishedEvent.raise(self)
174 @objects.each { |o| o.show }
178 @objects.each { |o| o.hide }
180 @alpha_anim && @alpha_anim.delete
185 @objects.each { |o| o.delete }
188 @alpha_anim && @alpha_anim.delete
190 @img = @label = @alpha_anim = nil
194 @objects.each { |o| o.move(x, y) }
199 def smart_resize(w, h)
203 def smart_color_set(r, g, b, a)
204 @objects.each { |o| o.alpha = a }
208 class Container < Evas::Smart
209 class ContainerError < StandardError; end
210 class ContainerFullError < ContainerError; end
211 class ContainerLockedError < ContainerError; end
216 @bg = Evas::Rectangle.new(evas)
217 @bg.set_color(0, 0, 0, 8)
228 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
229 &method(:on_icon_fade_out_finished))
234 !slots_left.zero? && @add_lock_count.zero?
238 @about_to_add.zero? && @add_lock_count.zero?
242 Kernel.raise(ContainerFullError) if slots_left.zero?
243 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
245 i.move_relative(self, 0, 0)
250 # check whether we need to need to move this icon
256 movement = Main.instance.icon_height * (slots_left - 1)
260 move_time = 0.25 * slots_left
261 @animators << MoveAnimator.new(move_time, movement, i)
263 @animators.last.on_finished do |ani|
264 @animators.delete(ani)
272 i = @icons.index(icon)
273 return (block_given? ? yield : nil) if i.nil?
279 Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
280 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
286 def on_icon_fade_out_finished(ev)
287 i = @icons.index(ev.icon)
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
300 @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
301 @animators.last.on_finished do |ani|
302 @animators.delete(ani)
325 def smart_resize(w, h)
331 MAX_ICONS - @icons.nitems - @about_to_add
335 @icons.nitems + @about_to_add
339 class Main < Ecore::Evas::SoftwareX11
342 attr_reader :container
347 self.has_alpha = true
348 self.title = "Embrace"
349 self.borderless = true
351 @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
353 on_resize { @container.resize(*geometry[2, 3]) }
355 @container = Container.new(evas)
356 @container.move(0, 0)
357 @container.layer = -1
360 size = [@icon_dim.first, icon_height * MAX_ICONS]
365 evas.font_path_append("/usr/lib/X11/fonts/TTF")
368 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
369 &method(:on_mailbox_status)),
370 Ecore::EventHandler.new(IMAP::FinishedEvent,
371 &method(:on_finished))
374 s = File.expand_path("~/.e/apps/embrace/config.yaml")
375 @config = YAML.load(File.read(s))
378 @timer = Ecore::Timer.new(30, &method(:on_timer))
387 Ecore.main_loop_begin
392 return unless @server.nil?
394 @server = IMAP::Session.new(@config)
399 def on_mailbox_status(ev)
400 md = ev.name.match(/^Lists.(.+)$/)
404 lbl = md.captures.first
407 found = evas.find_object(lbl)
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)
424 Embrace::Main.instance.show
425 Embrace::Main.instance.run