[caribou: 4/5] Use CSS theming for color options.



commit 6c5d8df157e084fc930be8a0b94522d1111296c8
Author: Eitan Isaacson <eitan monotonous org>
Date:   Tue Mar 8 23:39:28 2011 -0500

    Use CSS theming for color options.

 caribou/ui/keyboard.py |   73 ++++++++++++++++++-----------------
 caribou/ui/main.py     |  100 +++++++++++++++++++++++++++++++++++++++++++----
 caribou/ui/scan.py     |   16 +++----
 3 files changed, 136 insertions(+), 53 deletions(-)
---
diff --git a/caribou/ui/keyboard.py b/caribou/ui/keyboard.py
index 2dc924b..1fb43ba 100644
--- a/caribou/ui/keyboard.py
+++ b/caribou/ui/keyboard.py
@@ -90,9 +90,8 @@ class BaseKey(object):
             else:
                 self.set_label(self.label)
 
-        for name in ["normal_color", "mouse_over_color", "default_colors"]:
-            getattr(SettingsManager, name).connect("value-changed",
-                                                   self._colors_changed)
+        ctx = self.get_style_context()
+        ctx.add_class("caribou-keyboard-button")
 
         for name in ["default_font", "key_font"]:
             getattr(SettingsManager, name).connect("value-changed",
@@ -101,30 +100,26 @@ class BaseKey(object):
         if not SettingsManager.default_font.value:
             self._key_font_changed(None, None)
 
-        if not SettingsManager.default_colors.value:
-            self._colors_changed(None, None)
-
-    def _colors_changed(self, setting, value):
-        if SettingsManager.default_colors.value:
-            self._normal_color = None
-            self._mouse_over_color = None
-            self.reset_color()
-        else:
-            self._normal_color = SettingsManager.normal_color.value
-            self._mouse_over_color = SettingsManager.mouse_over_color.value
-            self.set_color(self._normal_color, self._mouse_over_color)
-
     def _key_font_changed(self, setting, value):
         if SettingsManager.default_font.value:
             self.reset_font()
         else:
             self.set_font(SettingsManager.key_font.value)
 
-    def scan_highlight(self, color):
-        if color is None:
-            self._colors_changed(None, None)
-        else:
-            self.set_color(color)
+    def scan_highlight_key(self):
+        raise NotImplemented
+
+    def scan_highlight_row(self):
+        raise NotImplemented
+
+    def scan_highlight_block(self):
+        raise NotImplemented
+
+    def scan_highlight_cancel(self):
+        raise NotImplemented
+
+    def scan_highlight_clear(self):
+        raise NotImplemented
 
     def _on_image_key_mapped(self, key):
         print
@@ -152,12 +147,6 @@ class BaseKey(object):
     def reset_font(self):
         raise NotImplemented
 
-    def set_color(self, normal_color, mouse_over_color):
-        raise NotImplemented
-
-    def reset_color(self):
-        raise NotImplemented
-
     def _get_value(self):
         return self._value
 
@@ -201,15 +190,29 @@ class Key(Gtk.Button, BaseKey):
             return
         label.modify_font(None)
 
-    def set_color(self, normal_color, mouse_over_color=None):
-        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(normal_color)[1])
-        if mouse_over_color:
-            self.modify_bg(Gtk.StateType.PRELIGHT,
-                           Gdk.color_parse(mouse_over_color)[1])
+    def _replace_scan_class_style(self, scan_class=None):
+        ctx = self.get_style_context()
+        for cls in ctx.list_classes():
+            if cls.startswith('caribou-scan'):
+                ctx.remove_class(cls)
+        if scan_class:
+            ctx.add_class(scan_class)
+        self.queue_draw()
+
+    def scan_highlight_key(self):
+        self._replace_scan_class_style("caribou-scan-key")
+
+    def scan_highlight_row(self):
+        self._replace_scan_class_style("caribou-scan-row")
+
+    def scan_highlight_block(self):
+        self._replace_scan_class_style("caribou-scan-block")
+
+    def scan_highlight_cancel(self):
+        self._replace_scan_class_style("caribou-scan-cancel")
 
-    def reset_color(self):
-        self.modify_bg(Gtk.StateType.NORMAL, None)
-        self.modify_bg(Gtk.StateType.PRELIGHT, None)
+    def scan_highlight_clear(self):
+        self._replace_scan_class_style()
 
 class ModifierKey(Gtk.ToggleButton, Key):
     pass
diff --git a/caribou/ui/main.py b/caribou/ui/main.py
index 3a7d19a..1a6ac9e 100644
--- a/caribou/ui/main.py
+++ b/caribou/ui/main.py
@@ -2,6 +2,7 @@ import pyatspi
 from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import Gio
+from string import Template
 
 from window import CaribouWindowEntry, Rectangle
 from keyboard import CaribouKeyboard
@@ -12,6 +13,40 @@ from scan import ScanMaster
 
 debug = False
 
+CSS_TEMPLATE = """
+.caribou-keyboard-button {
+background-image: none;
+background-color: $normal_bg;
+}
+
+.caribou-keyboard-button:hover {
+background-image: none;
+background-color: $mouseover_bg;
+}
+"""
+
+SCAN_CSS_TEMPLATE = """
+.caribou-scan-key {
+background-image: none;
+background-color: $button_scan;
+}
+
+.caribou-scan-row {
+background-image: none;
+background-color: $row_scan;
+}
+
+.caribou-scan-block {
+background-image: none;
+background-color: $block_scan;
+}
+
+.caribou-scan-cancel {
+background-image: none;
+background-color: $cancel_scan;
+}
+"""
+
 class Caribou:
     def __init__(self,
                  kb_factory=CaribouKeyboard,
@@ -34,6 +69,50 @@ class Caribou:
         if SettingsManager.scan_enabled.value:
             self.scan_master.start()
 
+        self._custom_css_provider = Gtk.CssProvider()
+
+        for name in ["normal_color", "mouse_over_color", "default_colors"]:
+            getattr(SettingsManager, name).connect("value-changed",
+                                                   self._colors_changed)
+        self._colors_changed(None, None)
+
+        self._scan_css_provider = Gtk.CssProvider()
+        Gtk.StyleContext.add_provider_for_screen(
+            Gdk.Screen.get_default(),
+            self._scan_css_provider,
+            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
+
+        for name in ["button_scanning_color",
+                     "row_scanning_color",
+                     "block_scanning_color",
+                     "cancel_scanning_color"]:
+            getattr(SettingsManager, name).connect("value-changed",
+                                                   self._scan_colors_changed)
+        self._scan_colors_changed(None, None)
+
+    def _colors_changed(self, setting, value):
+        if SettingsManager.default_colors.value:
+            Gtk.StyleContext.remove_provider_for_screen(
+                Gdk.Screen.get_default(),
+                self._custom_css_provider)
+        else:
+            Gtk.StyleContext.add_provider_for_screen(
+                Gdk.Screen.get_default(),
+                self._custom_css_provider,
+                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
+            self._custom_css_provider.load_from_data(
+                Template(CSS_TEMPLATE).substitute(
+                    normal_bg=SettingsManager.normal_color.value,
+                    mouseover_bg=SettingsManager.mouse_over_color.value), -1)
+
+    def _scan_colors_changed(self, setting, value):
+        self._scan_css_provider.load_from_data(Template(SCAN_CSS_TEMPLATE).substitute(
+                button_scan=SettingsManager.button_scanning_color.value,
+                row_scan=SettingsManager.row_scanning_color.value,
+                block_scan=SettingsManager.block_scanning_color.value,
+                cancel_scan=SettingsManager.cancel_scanning_color.value), -1)
+
+
     def _register_event_listeners(self):
         pyatspi.Registry.registerEventListener(
             self.on_focus, "object:state-changed:focused")
@@ -70,8 +149,10 @@ class Caribou:
             try:
                 settings = Gio.Settings('org.gnome.desktop.interface')
                 atspi = settings.get_boolean("toolkit-accessibility")
+                print "->", atspi
                 return atspi
             except:
+                raise
                 from gi.repository import GConf
                 gconfc = GConf.Client.get_default()
                 atspi1 = gconfc.get_bool(
@@ -80,6 +161,7 @@ class Caribou:
                     "/desktop/gnome/interface/accessibility2")
                 return atspi1 or atspi2
         except:
+            raise
             return False
 
     def on_text_caret_moved(self, event):
@@ -88,17 +170,17 @@ class Caribou:
             if debug == True:
                 print "object:text-caret-moved in", event.host_application.name,
                 print event.detail1, event.source.description
-    
+
     def __set_text_location(self, acc):
-        text = acc.queryText() 
+        text = acc.queryText()
         [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS)
         self.window.set_cursor_location(Rectangle(x, y, width, height))
-        
+
         component = acc.queryComponent()
         entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)
         self.window.set_entry_location(entry_bb)
         self.window.show_all()
-       
+
     def __set_entry_location(self, acc):
         text = acc.queryText()
         cursor_bb = Rectangle(
@@ -115,7 +197,7 @@ class Caribou:
         self.window.set_entry_location(entry_bb)
 
         self.window.show_all()
-       
+
     def on_focus(self, event):
         acc = event.source
         source_role = acc.getRole()
@@ -133,7 +215,7 @@ class Caribou:
                         print "enter text widget in", event.host_application.name
                 elif event.detail1 == 0 and acc == self.__current_acc:
                     self.window.hide()
-                    self.__current_acc = None 
+                    self.__current_acc = None
                     self.__set_location = None
                     if debug == True:
                         print "leave text widget in", event.host_application.name
@@ -147,7 +229,7 @@ class Caribou:
                         print "enter entry widget in", event.host_application.name
                 elif event.detail1 == 0:
                     self.window.hide()
-                    self.__current_acc = None 
+                    self.__current_acc = None
                     self.__set_location = None
                     if debug == True:
                         print "leave entry widget in", event.host_application.name
@@ -165,5 +247,5 @@ class Caribou:
     def clean_exit(self):
         self.scan_master.stop()
         self._deregister_event_listeners()
-        
-        
+
+
diff --git a/caribou/ui/scan.py b/caribou/ui/scan.py
index 16680fb..94aa8c2 100644
--- a/caribou/ui/scan.py
+++ b/caribou/ui/scan.py
@@ -59,7 +59,7 @@ class ScanMaster():
         self._ungrab_mouse_events()
 
         if self._last_block is not None:
-            self._multi_map(lambda x: x.reset_color(), self._last_block)
+            self._multi_map(lambda x: x.scan_highlight_clear(), self._last_block)
 
         if self._timer != 0:
             gobject.source_remove(self._timer)
@@ -154,25 +154,23 @@ class ScanMaster():
         if self._scan_path is None: return True
 
         if self._last_block is not None:
-            self._multi_map(lambda x: x.scan_highlight(None),
-                                       self._last_block)
+            self._multi_map(lambda x: x.scan_highlight_clear(), self._last_block)
 
         if SettingsManager.reverse_scanning.value:
             self._cancel, next_block = self._get_next_reverse_block()
         else:
             self._cancel, next_block = self._get_next_block()
 
-        color = SettingsManager.button_scanning_color.value
-
         if self._cancel:
-            color = SettingsManager.cancel_scanning_color.value
+            self._multi_map(lambda x: x.scan_highlight_cancel(), next_block)
         elif isinstance(next_block, list):
             if SettingsManager.scanning_type.value == ROW:
-                color = SettingsManager.row_scanning_color.value
+                self._multi_map(lambda x: x.scan_highlight_row(), next_block)
             else:
-                color = SettingsManager.block_scanning_color.value
+                self._multi_map(lambda x: x.scan_highlight_block(), next_block)
+        else:            
+            self._multi_map(lambda x: x.scan_highlight_key(), next_block)
 
-        self._multi_map(lambda x: x.scan_highlight(color), next_block)
         self._last_block = next_block
         return True
 



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