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}/"
32 ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
34 class ZeroToOneAnimator < Ecore::Animator
35 def initialize(duration)
41 v = [(Time.now - started) / duration, 1.0].min
45 @finished.call(self) unless (v < 1.0) || @finished.nil?
56 def on_finished(&block)
61 # an animator that runs for the specified number of seconds,
62 # and yields values between 0 and 255
63 class AlphaAnimator < ZeroToOneAnimator
64 def initialize(duration, object)
65 super(duration) do |v|
67 object.set_color(a, a, a, a)
76 class InverseAlphaAnimator < AlphaAnimator
82 class MoveAnimator < ZeroToOneAnimator
83 def initialize(duration, movement, *objects)
84 zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
86 super(duration) do |v|
88 v = Math.sin(v * Math::PI / 2.0)
90 zipped.each do |(o, orig_pos)|
91 o.move(orig_pos.first,
92 orig_pos.last + (movement * v).to_i)
98 class MailboxIcon < Evas::Smart
99 class FadeOutFinishedEvent < Ecore::Event
111 def initialize(evas, label)
119 @img = Evas::Image.new(evas)
120 @label = Evas::Text.new(evas)
122 @objects = [@img, @label]
123 @objects.each { |o| add_member(o) }
125 set_color(0, 0, 0, 0)
127 @img.set_file(ICON_FILE)
128 @img.set_fill(0, 0, *@img.get_size)
131 @label.set_font("VeraBd", 10)
136 @label_offset_x = (b[0] / 2) - (a[2] / 2)
137 @label_offset_y = (b[1] / 2) - (a[3] / 2)
139 resize(*@img.get_size)
145 @alpha_anim ||= AlphaAnimator.new(2, self)
146 @alpha_anim.on_finished { @alpha_anim = nil }
150 @alpha_anim ||= InverseAlphaAnimator.new(2, self)
151 @alpha_anim.on_finished do
153 FadeOutFinishedEvent.raise(self)
159 @objects.each { |o| o.show }
163 @objects.each { |o| o.hide }
165 @alpha_anim && @alpha_anim.delete
170 @objects.each { |o| o.delete }
173 @alpha_anim && @alpha_anim.delete
175 @img = @label = @alpha_anim = nil
181 # center the label on the image
182 @label.move(x + @label_offset_x,
186 def smart_resize(w, h)
190 def smart_color_set(r, g, b, a)
191 @img.set_color(r, g, b, a)
192 @label.set_color(r, 0, 0, a)
196 class Container < Evas::Smart
197 class ContainerError < StandardError; end
198 class ContainerFullError < ContainerError; end
199 class ContainerLockedError < ContainerError; end
204 @bg = Evas::Rectangle.new(evas)
205 @bg.set_color(0, 0, 0, 8)
216 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
217 &method(:on_icon_fade_out_finished))
222 !slots_left.zero? && @add_lock_count.zero?
226 @about_to_add.zero? && @add_lock_count.zero?
230 Kernel.raise(ContainerFullError) if slots_left.zero?
231 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
236 geo[1] + geo[3] % Main.instance.icon_height)
242 # check whether we need to need to move this icon
248 movement = Main.instance.icon_height * (slots_left - 1)
252 move_time = 0.25 * slots_left
253 @animators << MoveAnimator.new(move_time, movement, i)
255 @animators.last.on_finished do |ani|
256 @animators.delete(ani)
264 i = @icons.index(icon)
265 return (block_given? ? yield : nil) if i.nil?
271 Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
272 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
278 def on_icon_fade_out_finished(ev)
279 i = @icons.index(ev.icon)
283 # icons that are placed above the one that's deleted need
284 # to be moved. check whether are there any first
285 if i == @icons.length
292 @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
293 @animators.last.on_finished do |ani|
294 @animators.delete(ani)
317 def smart_resize(w, h)
323 geometry.pop / Main.instance.icon_height
327 max_icons - @icons.nitems - @about_to_add
331 @icons.nitems + @about_to_add
335 class Main < Ecore::Evas::SoftwareX11
338 attr_reader :container
343 self.has_alpha = true
344 self.title = "Embrace"
345 self.borderless = true
347 @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
349 on_resize { @container.resize(*geometry[2, 3]) }
351 @container = Container.new(evas)
352 @container.move(0, 0)
353 @container.layer = -1
356 size = [@icon_dim.first]
360 size << icon_height * 11
369 evas.font_path_append("/usr/lib/X11/fonts/TTF")
372 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
373 &method(:on_mailbox_status)),
374 Ecore::EventHandler.new(IMAP::FinishedEvent,
375 &method(:on_finished))
378 s = File.expand_path("~/.e/apps/embrace/config.yaml")
379 @config = YAML.load(File.read(s))
382 @timer = Ecore::Timer.new(30, &method(:on_timer))
391 Ecore.main_loop_begin
396 return unless @server.nil?
398 @server = IMAP::Session.new(@config)
403 def on_mailbox_status(ev)
404 md = ev.name.match(/^Lists.(.+)$/)
408 lbl = md.captures.first
411 found = evas.find_object(lbl)
413 if ev.count.zero? && !found.nil? && @container.can_delete?
414 @container.delete(found)
415 elsif !ev.count.zero? && found.nil? && @container.can_add?
416 @container << MailboxIcon.new(evas, lbl)
428 Embrace::Main.instance.show
429 Embrace::Main.instance.run