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))
47 ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
50 class ZeroToOneAnimator < Ecore::Animator
51 def initialize(duration)
57 v = [(Time.now - started) / duration, 1.0].min
61 @finished.call(self) unless (v < 1.0) || @finished.nil?
72 def on_finished(&block)
77 # an animator that runs for the specified number of seconds,
78 # and yields values between 0 and 255
79 class AlphaAnimator < ZeroToOneAnimator
80 def initialize(duration, object)
81 super(duration) do |v|
83 object.set_color(a, a, a, a)
92 class InverseAlphaAnimator < AlphaAnimator
98 class MoveAnimator < ZeroToOneAnimator
99 def initialize(duration, movement, *objects)
100 zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
102 super(duration) do |v|
104 v = Math.sin(v * Math::PI / 2.0)
106 zipped.each do |(o, orig_pos)|
107 o.move(orig_pos.first,
108 orig_pos.last + (movement * v).to_i)
114 class MailboxIcon < Evas::Smart
115 class FadeOutFinishedEvent < Ecore::Event
127 def initialize(evas, label)
135 @img = Evas::Image.new(evas)
136 @label = Evas::Text.new(evas)
138 @objects = [@img, @label]
139 @objects.each { |o| add_member(o) }
141 set_color(0, 0, 0, 0)
143 @img.set_file(ICON_FILE)
144 @img.set_fill(0, 0, *@img.get_size)
147 @label.set_font("VeraBd", 10)
149 resize(*@img.get_size)
155 @alpha_anim ||= AlphaAnimator.new(2, self)
156 @alpha_anim.on_finished { @alpha_anim = nil }
160 @alpha_anim ||= InverseAlphaAnimator.new(2, self)
161 @alpha_anim.on_finished do
163 FadeOutFinishedEvent.raise(self)
169 @objects.each { |o| o.show }
173 @objects.each { |o| o.hide }
175 @alpha_anim && @alpha_anim.delete
180 @objects.each { |o| o.delete }
183 @alpha_anim && @alpha_anim.delete
185 @img = @label = @alpha_anim = nil
189 @objects.each { |o| o.move(x, y) }
194 def smart_resize(w, h)
198 def smart_color_set(r, g, b, a)
199 @img.set_color(r, g, b, a)
200 @label.set_color(r, 0, 0, a)
204 class Container < Evas::Smart
205 class ContainerError < StandardError; end
206 class ContainerFullError < ContainerError; end
207 class ContainerLockedError < ContainerError; end
212 @bg = Evas::Rectangle.new(evas)
213 @bg.set_color(0, 0, 0, 8)
224 Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
225 &method(:on_icon_fade_out_finished))
230 !slots_left.zero? && @add_lock_count.zero?
234 @about_to_add.zero? && @add_lock_count.zero?
238 Kernel.raise(ContainerFullError) if slots_left.zero?
239 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
241 i.move_relative(self, 0, 0)
246 # check whether we need to need to move this icon
252 movement = Main.instance.icon_height * (slots_left - 1)
256 move_time = 0.25 * slots_left
257 @animators << MoveAnimator.new(move_time, movement, i)
259 @animators.last.on_finished do |ani|
260 @animators.delete(ani)
268 i = @icons.index(icon)
269 return (block_given? ? yield : nil) if i.nil?
275 Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
276 Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
282 def on_icon_fade_out_finished(ev)
283 i = @icons.index(ev.icon)
287 # icons that are placed above the one that's deleted need
288 # to be moved. check whether are there any first
289 if i == @icons.length
296 @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
297 @animators.last.on_finished do |ani|
298 @animators.delete(ani)
321 def smart_resize(w, h)
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, icon_height * MAX_ICONS]
361 evas.font_path_append("/usr/lib/X11/fonts/TTF")
364 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
365 &method(:on_mailbox_status)),
366 Ecore::EventHandler.new(IMAP::FinishedEvent,
367 &method(:on_finished))
370 s = File.expand_path("~/.e/apps/embrace/config.yaml")
371 @config = YAML.load(File.read(s))
374 @timer = Ecore::Timer.new(30, &method(:on_timer))
383 Ecore.main_loop_begin
388 return unless @server.nil?
390 @server = IMAP::Session.new(@config)
395 def on_mailbox_status(ev)
396 md = ev.name.match(/^Lists.(.+)$/)
400 lbl = md.captures.first
403 found = evas.find_object(lbl)
405 if ev.count.zero? && !found.nil? && @container.can_delete?
406 @container.delete(found)
407 elsif !ev.count.zero? && found.nil? && @container.can_add?
408 @container << MailboxIcon.new(evas, lbl)
420 Embrace::Main.instance.show
421 Embrace::Main.instance.run