summaryrefslogtreecommitdiff
path: root/caribou/ui/main.py
blob: 1a6ac9ef71760887cc63aec7259b51efa5ee53d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
from caribou.common.settings_manager import SettingsManager
from caribou.ui.i18n import _
import caribou.common.const as const
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,
                 window_factory=CaribouWindowEntry):
        if not self._get_a11y_enabled():
            raise Exception, "AT-SPI 1 or 2 needs to be enabled."
        self.__current_acc = None
        self.window_factory = window_factory
        self.kb_factory = kb_factory
        kb = kb_factory()
        self.window = window_factory(kb)
        self._register_event_listeners()
        SettingsManager.layout.connect("value-changed",
                                       self._on_layout_changed)

        # Scanning
        self.scan_master = ScanMaster(self.window, kb)
        SettingsManager.scan_enabled.connect("value-changed",
                                             self._on_scan_toggled)
        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")
        pyatspi.Registry.registerEventListener(self.on_focus, "focus")
        pyatspi.Registry.registerEventListener(
            self.on_text_caret_moved, "object:text-caret-moved")

    def _deregister_event_listeners(self):
        pyatspi.Registry.deregisterEventListener(
            self.on_focus, "object:state-changed:focused")
        pyatspi.Registry.deregisterEventListener(self.on_focus, "focus")
        pyatspi.Registry.deregisterEventListener(
            self.on_text_caret_moved, "object:text-caret-moved")

    def _on_scan_toggled(self, setting, val):
        if val:
            self.scan_master.start()
        else:
            self.scan_master.stop()

    def _on_layout_changed(self, setting, val):
        self._deregister_event_listeners()
        self.window.destroy()
        self._update_window()
        self._register_event_listeners()

    def _update_window(self):
        kb = self.kb_factory()
        self.scan_master.set_keyboard(kb)
        self.window = self.window_factory(kb)

    def _get_a11y_enabled(self):
        try:
            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(
                    "/desktop/gnome/interface/accessibility")
                atspi2 = gconfc.get_bool(
                    "/desktop/gnome/interface/accessibility2")
                return atspi1 or atspi2
        except:
            raise
            return False

    def on_text_caret_moved(self, event):
        if self.__current_acc == event.source:
            self.__set_location(event.source)
            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()
        [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(
            *text.getCharacterExtents(text.caretOffset,
                                      pyatspi.DESKTOP_COORDS))

        component = acc.queryComponent()
        entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS)

        if cursor_bb == Rectangle(0, 0, 0, 0):
            cursor_bb = entry_bb

        self.window.set_cursor_location(cursor_bb)
        self.window.set_entry_location(entry_bb)

        self.window.show_all()

    def on_focus(self, event):
        acc = event.source
        source_role = acc.getRole()
        if acc.getState().contains(pyatspi.STATE_EDITABLE) or \
                source_role == pyatspi.ROLE_TERMINAL:
            if source_role in (pyatspi.ROLE_TEXT,
                               pyatspi.ROLE_PARAGRAPH,
                               pyatspi.ROLE_PASSWORD_TEXT,
                               pyatspi.ROLE_TERMINAL):
                if event.type.startswith("focus") or event.detail1 == 1:
                    self.__set_text_location(acc)
                    self.__current_acc = event.source
                    self.__set_location = self.__set_text_location
                    if debug == True:
                        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.__set_location = None
                    if debug == True:
                        print "leave text widget in", event.host_application.name

            elif source_role == pyatspi.ROLE_ENTRY:
                if event.type.startswith("focus") or event.detail1 == 1:
                    self.__set_entry_location(acc)
                    self.__current_acc = event.source
                    self.__set_location = self.__set_entry_location
                    if debug == True:
                        print "enter entry widget in", event.host_application.name
                elif event.detail1 == 0:
                    self.window.hide()
                    self.__current_acc = None
                    self.__set_location = None
                    if debug == True:
                        print "leave entry widget in", event.host_application.name
            else:
                if debug == True:
                    print _("WARNING - Caribou: unhandled editable widget:"), event.source

        # Firefox does not report leave entry widget events.
        # This could be a way to get the entry widget leave events.
        #else:
        #    if event.detail1 == 1:
        #        self.window.hide()
        #        print "--> LEAVE EDITABLE TEXT <--"

    def clean_exit(self):
        self.scan_master.stop()
        self._deregister_event_listeners()