[caribou] factored out opacity code from window.py



commit e3b9436d90d6dc50f5d713dc23002f45127f63c0
Author: Eitan Isaacson <eitan monotonous org>
Date:   Mon Dec 7 23:08:34 2009 -0800

    factored out opacity code from window.py

 src/caribou/animation.py |    8 +++-
 src/caribou/opacity.py   |   60 +++++++++++++++++++++
 src/caribou/window.py    |  131 ++++++++++++----------------------------------
 3 files changed, 100 insertions(+), 99 deletions(-)
---
diff --git a/src/caribou/animation.py b/src/caribou/animation.py
index a5b7278..837a91b 100644
--- a/src/caribou/animation.py
+++ b/src/caribou/animation.py
@@ -1,7 +1,12 @@
-import clutter, gtk
+import clutter
 
 class AnimatedWindowBase(object):
     def __init__(self, ease=clutter.EASE_IN_QUAD):
+        if self.__class__ == AnimatedWindowBase:
+            raise TypeError, \
+                "AnimatedWindowBase is an abstract class, " \
+                "must be subclassed with a gtk.Window"
+
         self._actor = clutter.Rectangle()
         self.ease = ease
 
@@ -21,6 +26,7 @@ class AnimatedWindowBase(object):
         return animation
         
 if __name__ == "__main__":
+    import gtk
     class AnimatedWindow(gtk.Window, AnimatedWindowBase):
         def __init__(self):
             gtk.Window.__init__(self)
diff --git a/src/caribou/opacity.py b/src/caribou/opacity.py
new file mode 100644
index 0000000..a6f976f
--- /dev/null
+++ b/src/caribou/opacity.py
@@ -0,0 +1,60 @@
+import gtk, glib
+from math import sqrt
+
+class ProximityWindowBase(object):
+    def __init__(self, min_alpha=1.0, max_alpha=1.0, max_distance=100):
+        if self.__class__ == ProximityWindowBase:
+            raise TypeError, \
+                "ProximityWindowBase is an abstract class, " \
+                "must be subclassed with a gtk.Window"
+        self.connect('map-event', self.__onmapped)
+        self.max_distance = max_distance
+        if max_alpha < min_alpha:
+            raise ValueError, "min_alpha can't be larger than max_alpha"
+        self.min_alpha = min_alpha
+        self.max_alpha = max_alpha
+
+    def __onmapped(self, obj, event):
+        if self.is_composited():
+            self.set_opacity(self.max_alpha)
+            if self.max_alpha != self.min_alpha:
+                # Don't waste CPU if the max and min are equal.
+                glib.timeout_add(80, self._proximity_check)
+
+    def _proximity_check(self):
+        x, y = self.get_pointer()
+        
+        distance =  self._get_distance_to_bbox(x, y, self.allocation)
+
+        opacity = (self.max_alpha - self.min_alpha) * \
+            (1 - min(distance, self.max_distance)/self.max_distance)
+        opacity += self.min_alpha
+
+        self.set_opacity(opacity)
+        return self.props.visible
+
+    def _get_distance_to_bbox(self, x, y, bbox):
+        if x < bbox.x:
+            x_distance = bbox.x - x
+        elif x > bbox.width + bbox.x:
+            x_distance = bbox.width + bbox.x - x
+        else:
+            x_distance = 0
+
+        if y < bbox.y:
+            y_distance = bbox.y - y
+        elif y > bbox.height + bbox.y:
+            y_distance = bbox.height + bbox.y - y
+        else:
+            y_distance = 0
+
+        if y_distance == 0 and x_distance == 0:
+            return 0.0
+        elif y_distance != 0 and x_distance == 0:
+            return abs(float(y_distance))
+        elif y_distance == 0 and x_distance != 0:
+            return abs(float(x_distance))
+        else:
+            x2 = bbox.x if x_distance > 0 else bbox.x + bbox.width
+            y2 = bbox.y if y_distance > 0 else bbox.y + bbox.height
+            return sqrt((x - x2)**2 + (y - y2)**2)
diff --git a/src/caribou/window.py b/src/caribou/window.py
index 35a5650..7464db9 100644
--- a/src/caribou/window.py
+++ b/src/caribou/window.py
@@ -21,11 +21,11 @@
 import gtk
 import gtk.gdk as gdk
 import glib
-from math import sqrt
 import keyboard
 from keyboards import qwerty
 import gconf
 import animation
+import opacity
 
 class CaribouWindow(gtk.Window):
     __gtype_name__ = "CaribouWindow"
@@ -47,14 +47,6 @@ class CaribouWindow(gtk.Window):
         self._default_placement = default_placement or \
             CaribouWindowPlacement()
         
-        # Alpha and proximity stuff
-        self.connect('map-event', self._onmapped)
-        self.max_distance = max_distance
-        if max_alpha < min_alpha:
-            raise ValueError, "min_alpha can't be larger than max_alpha"
-        self.min_alpha = min_alpha
-        self.max_alpha = max_alpha
-
     def set_cursor_location(self, cursor_location):
         self._cursor_location = cursor_location
         self._update_position()
@@ -70,7 +62,28 @@ class CaribouWindow(gtk.Window):
     def _get_root_bbox(self):
         root_window = gdk.get_default_root_window()
         args = root_window.get_position() + root_window.get_size()
-        return gdk.Rectangle(*args)
+
+        root_bbox = gdk.Rectangle(*args)
+
+        current_screen = gtk.gdk.screen_get_default().get_number()
+        for panel in self._gconf_client.all_dirs('/apps/panel/toplevels'):
+            orientation = self._gconf_client.get_string(panel+'/orientation')
+            size = self._gconf_client.get_int(panel+'/size')
+            screen = self._gconf_client.get_int(panel+'/screen')
+            if screen != current_screen:
+                continue
+            if orientation == 'top':
+                root_bbox.y += size
+                root_bbox.height -= size
+            elif orientation == 'bottom':
+                root_bbox.height -= size
+            elif orientation == 'right':
+                root_bbox.x += size
+                root_bbox.width -= size
+            elif orientation == 'left':
+                root_bbox.x -= size
+        
+        return root_bbox
 
     def _calculate_position(self, placement=None):
         root_bbox = self._get_root_bbox()
@@ -113,67 +126,10 @@ class CaribouWindow(gtk.Window):
 
         return offset
 
-    def _onmapped(self, obj, event):
-        if self.is_composited():
-            self.set_opacity(self.max_alpha)
-            if self.max_alpha != self.min_alpha:
-                # Don't waste CPU if the max and min are equal.
-                glib.timeout_add(80, self._proximity_check)
-
-    def _proximity_check(self):
-        x, y = self.get_pointer()
-        abs_x, abs_y = self.get_position()
-        x += abs_x
-        y += abs_y
-        
-        distance =  self._get_distance_to_bbox(
-            x, y, gtk.gdk.Rectangle(abs_x, abs_y,
-                                    self.allocation.width, 
-                                    self.allocation.height))
-
-        if CaribouWindowPlacement.SCREEN != \
-                self._default_placement.x.stickto or \
-                CaribouWindowPlacement.SCREEN != \
-                self._default_placement.y.stickto:
-            if self._entry_location != gtk.gdk.Rectangle(0, 0, 0, 0):
-                distance2 = self._get_distance_to_bbox(x, y, 
-                                                       self._entry_location)
-                distance = min(distance, distance2)
-
-        opacity = (self.max_alpha - self.min_alpha) * \
-            (1 - min(distance, self.max_distance)/self.max_distance)
-        opacity += self.min_alpha
-
-        self.set_opacity(opacity)
-        return self.props.visible
-
-    def _get_distance_to_bbox(self, x, y, bbox):
-        if x < bbox.x:
-            x_distance = bbox.x - x
-        elif x > bbox.width + bbox.x:
-            x_distance = bbox.width + bbox.x - x
-        else:
-            x_distance = 0
-
-        if y < bbox.y:
-            y_distance = bbox.y - y
-        elif y > bbox.height + bbox.y:
-            y_distance = bbox.height + bbox.y - y
-        else:
-            y_distance = 0
-
-        if y_distance == 0 and x_distance == 0:
-            return 0.0
-        elif y_distance != 0 and x_distance == 0:
-            return abs(float(y_distance))
-        elif y_distance == 0 and x_distance != 0:
-            return abs(float(x_distance))
-        else:
-            x2 = bbox.x if x_distance > 0 else bbox.x + bbox.width
-            y2 = bbox.y if y_distance > 0 else bbox.y + bbox.height
-            return sqrt((x - x2)**2 + (y - y2)**2)
-
-class CaribouWindowDocked(CaribouWindow, animation.AnimatedWindowBase):
+
+class CaribouWindowDocked(CaribouWindow, 
+                          animation.AnimatedWindowBase,
+                          opacity.ProximityWindowBase):
     __gtype_name__ = "CaribouWindowDocked"
     
     def __init__(self):
@@ -184,36 +140,15 @@ class CaribouWindowDocked(CaribouWindow, animation.AnimatedWindowBase):
             ystickto=CaribouWindowPlacement.SCREEN,
             xgravitate=CaribouWindowPlacement.INSIDE)
 
-        CaribouWindow.__init__(
-            self, placement, min_alpha=0.8, max_alpha=0.8)
-
+        CaribouWindow.__init__(self, placement)
         animation.AnimatedWindowBase.__init__(self)
-        self._gconf_client = gconf.client_get_default()
+        opacity.ProximityWindowBase.__init__(
+            self, min_alpha=0.5, max_alpha=0.8)
 
-    def _get_root_bbox(self):
-        root_bbox = CaribouWindow._get_root_bbox(self)
-        current_screen = gtk.gdk.screen_get_default().get_number()
-        for panel in self._gconf_client.all_dirs('/apps/panel/toplevels'):
-            orientation = self._gconf_client.get_string(panel+'/orientation')
-            size = self._gconf_client.get_int(panel+'/size')
-            screen = self._gconf_client.get_int(panel+'/screen')
-            if screen != current_screen:
-                continue
-            if orientation == 'top':
-                root_bbox.y += size
-                root_bbox.height -= size
-            elif orientation == 'bottom':
-                root_bbox.height -= size
-            elif orientation == 'right':
-                root_bbox.x += size
-                root_bbox.width -= size
-            elif orientation == 'left':
-                root_bbox.x -= size
-        
-        return root_bbox
+        self._gconf_client = gconf.client_get_default()
+        self.connect('map-event', self.__onmapped)
 
-    def _onmapped(self, obj, event):
-        CaribouWindow._onmapped(self, obj, event)
+    def __onmapped(self, obj, event):
         self._roll_in()
 
     def _roll_in(self):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]