Accept an optional argument that's the height of the Embrace window.
[embrace.git] / bin / embrace
index 9772596e4280c26e21eeb6148f706d9049379765..79d57c892ae374d3498066c19d2e5072e936c11e 100755 (executable)
@@ -27,29 +27,9 @@ require "embrace/imap"
 PKG_NAME = "embrace"
 DATADIR = "/usr/local/share/#{PKG_NAME}/"
 
-class Evas::EvasObject
-       def move_relative(obj, x, y)
-               # FIXME investigate whether there's an easier way
-               move(*obj.geometry[0..1].zip([x, y]).map { |(a, b)| a + b })
-       end
-
-       def center(obj)
-               a = geometry
-               b = obj.geometry
-
-               move_relative(obj, (b[2] / 2) - (a[2] / 2),
-                                  (b[3] / 2) - (a[3] / 2))
-       end
-
-       def alpha=(alpha)
-               set_color(*(get_color[0..-2] << alpha))
-       end
-end
-
 module Embrace
        VERSION = "0.0.1"
        ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
-       MAX_ICONS = 11
 
        class ZeroToOneAnimator < Ecore::Animator
                def initialize(duration)
@@ -81,11 +61,22 @@ module Embrace
        # an animator that runs for the specified number of seconds,
        # and yields values between 0 and 255
        class AlphaAnimator < ZeroToOneAnimator
-               def initialize(duration, *objects)
+               def initialize(duration, object)
                        super(duration) do |v|
-                               objects.each { |o| o.alpha = (255 * v).to_i }
+                               a = compute_alpha(v)
+                               object.set_color(a, a, a, a)
                        end
                end
+
+               def compute_alpha(v)
+                       (255 * v).to_i
+               end
+       end
+
+       class InverseAlphaAnimator < AlphaAnimator
+               def compute_alpha(v)
+                       super((1.0 - v).abs)
+               end
        end
 
        class MoveAnimator < ZeroToOneAnimator
@@ -105,6 +96,16 @@ module Embrace
        end
 
        class MailboxIcon < Evas::Smart
+               class FadeOutFinishedEvent < Ecore::Event
+                       attr_reader :icon
+
+                       def initialize(icon)
+                               super()
+
+                               @icon = icon
+                       end
+               end
+
                attr_accessor :slot
 
                def initialize(evas, label)
@@ -121,8 +122,7 @@ module Embrace
                        @objects = [@img, @label]
                        @objects.each { |o| add_member(o) }
 
-                       @img.set_color(255, 255, 255, 0)
-                       @label.set_color(255, 0, 0, 0)
+                       set_color(0, 0, 0, 0)
 
                        @img.set_file(ICON_FILE)
                        @img.set_fill(0, 0, *@img.get_size)
@@ -130,25 +130,43 @@ module Embrace
                        @label.text = name
                        @label.set_font("VeraBd", 10)
 
+                       a = @label.geometry
+                       b = *@img.get_size
+
+                       @label_offset_x = (b[0] / 2) - (a[2] / 2)
+                       @label_offset_y = (b[1] / 2) - (a[3] / 2)
+
                        resize(*@img.get_size)
                end
 
-               # smart callbacks
-               def on_show
-                       @objects.each { |o| o.show }
+               def fade_in
+                       show
 
-                       @alpha_anim ||= AlphaAnimator.new(2, @img, @label)
+                       @alpha_anim ||= AlphaAnimator.new(2, self)
                        @alpha_anim.on_finished { @alpha_anim = nil }
                end
 
-               def on_hide
+               def fade_out
+                       @alpha_anim ||= InverseAlphaAnimator.new(2, self)
+                       @alpha_anim.on_finished do
+                               @alpha_anim = nil
+                               FadeOutFinishedEvent.raise(self)
+                       end
+               end
+
+               # smart callbacks
+               def smart_show
+                       @objects.each { |o| o.show }
+               end
+
+               def smart_hide
                        @objects.each { |o| o.hide }
 
                        @alpha_anim && @alpha_anim.delete
                        @alpha_anim = nil
                end
 
-               def on_delete
+               def smart_delete
                        @objects.each { |o| o.delete }
                        @objects.clear
 
@@ -157,15 +175,22 @@ module Embrace
                        @img = @label = @alpha_anim = nil
                end
 
-               def on_move(x, y)
-                       @objects.each { |o| o.move(x, y) }
+               def smart_move(x, y)
+                       @img.move(x, y)
 
-                       @label.center(self)
+                       # center the label on the image
+                       @label.move(x + @label_offset_x,
+                                   y + @label_offset_y)
                end
 
-               def on_resize(w, h)
+               def smart_resize(w, h)
                        @img.resize(w, h)
                end
+
+               def smart_color_set(r, g, b, a)
+                       @img.set_color(r, g, b, a)
+                       @label.set_color(r, 0, 0, a)
+               end
        end
 
        class Container < Evas::Smart
@@ -177,7 +202,7 @@ module Embrace
                        super
 
                        @bg = Evas::Rectangle.new(evas)
-                       @bg.set_color(0, 0, 0, 255)
+                       @bg.set_color(0, 0, 0, 8)
 
                        add_member(@bg)
 
@@ -186,16 +211,33 @@ module Embrace
 
                        @about_to_add = 0
                        @add_lock_count = 0
+
+                       @handlers = [
+                               Ecore::EventHandler.new(MailboxIcon::FadeOutFinishedEvent,
+                                                       &method(:on_icon_fade_out_finished))
+                       ]
+               end
+
+               def can_add?
+                       !slots_left.zero? && @add_lock_count.zero?
+               end
+
+               def can_delete?
+                       @about_to_add.zero? && @add_lock_count.zero?
                end
 
                def <<(i)
                        Kernel.raise(ContainerFullError) if slots_left.zero?
                        Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
 
-                       i.move_relative(self, 0, 0)
+                       geo = geometry
+
+                       i.move(geo[0],
+                              geo[1] + geo[3] % Main.instance.icon_height)
+
                        i.slot = next_slot
                        i.clip = self
-                       i.show
+                       i.fade_in
 
                        # check whether we need to need to move this icon
                        if slots_left == 1
@@ -229,16 +271,23 @@ module Embrace
                        Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
                        Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
 
-                       # icons that are placed above the one that's deleted need
-                       # to be moved
-                       ar = @icons[(i + 1)..-1]
+                       @add_lock_count += 1
+                       @icons[i].fade_out
+               end
 
-                       @icons[i].delete
+               def on_icon_fade_out_finished(ev)
+                       i = @icons.index(ev.icon)
+                       ev.icon.delete
                        @icons.delete_at(i)
 
-                       return if ar.empty?
+                       # icons that are placed above the one that's deleted need
+                       # to be moved. check whether are there any first
+                       if i == @icons.length
+                               @add_lock_count -= 1
+                               return
+                       end
 
-                       @add_lock_count += 1
+                       ar = @icons[i..-1]
 
                        @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
                        @animators.last.on_finished do |ani|
@@ -248,30 +297,34 @@ module Embrace
                end
 
                # smart callbacks
-               def on_show
+               def smart_show
                        @bg.show
                end
 
-               def on_hide
+               def smart_hide
                        @bg.hide
                end
 
-               def on_delete
+               def smart_delete
                        @bg.delete
                        @bg = nil
                end
 
-               def on_move(x, y)
+               def smart_move(x, y)
                        @bg.move(x, y)
                end
 
-               def on_resize(w, h)
+               def smart_resize(w, h)
                        @bg.resize(w, h)
                end
 
                private
+               def max_icons
+                       geometry.pop / Main.instance.icon_height
+               end
+
                def slots_left
-                       MAX_ICONS - @icons.nitems - @about_to_add
+                       max_icons - @icons.nitems - @about_to_add
                end
 
                def next_slot
@@ -287,6 +340,7 @@ module Embrace
                def initialize
                        super
 
+                       self.has_alpha = true
                        self.title = "Embrace"
                        self.borderless = true
 
@@ -299,7 +353,15 @@ module Embrace
                        @container.layer = -1
                        @container.show
 
-                       size = [@icon_dim.first, icon_height * MAX_ICONS]
+                       size = [@icon_dim.first]
+
+                       arg = ARGV.shift
+                       if arg.nil?
+                               size << icon_height * 11
+                       else
+                               size << arg.to_i
+                       end
+
                        resize(*size)
                        set_size_min(*size)
                        set_size_max(*size)
@@ -330,10 +392,6 @@ module Embrace
                end
 
                private
-               def add_icon(name)
-                       @container << MailboxIcon.new(evas, name)
-               end
-
                def on_timer
                        return unless @server.nil?
 
@@ -352,22 +410,10 @@ module Embrace
 
                        found = evas.find_object(lbl)
 
-                       begin
-                               if ev.count.zero?
-                                       unless found.nil?
-                                               #puts "removing icon #{lbl}"
-                                               @container.delete(found)
-                                       else
-                                               #puts "count == 0, but icon not found (#{lbl})"
-                                       end
-                               elsif found.nil?
-                                       #puts "adding icon #{lbl}"
-                                       add_icon(lbl)
-                               else
-                                       #puts "count > 0, but already there (#{lbl})"
-                               end
-                       rescue Exception => e
-                               puts e.message
+                       if ev.count.zero? && !found.nil? && @container.can_delete?
+                               @container.delete(found)
+                       elsif !ev.count.zero? && found.nil? && @container.can_add?
+                               @container << MailboxIcon.new(evas, lbl)
                        end
 
                        false