summaryrefslogtreecommitdiff
path: root/caribou/antler/keyboard_view.py
blob: 13e02bae330670bce9c8f3ad8bb9b2d4257077d3 (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
from caribou.settings.preferences_window import PreferencesDialog
from caribou.settings import CaribouSettings
from antler_settings import AntlerSettings
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Caribou
import gobject
import glib
import os

PRETTY_LABELS = {
    "BackSpace" : u'\u232b',
    "space" : u' ',
    "Return" : u'\u23ce',
    'Caribou_Prefs' : u'\u2328',
    'Caribou_ShiftUp' : u'\u2b06',
    'Caribou_ShiftDown' : u'\u2b07',
    'Caribou_Emoticons' : u'\u263a',
    'Caribou_Symbols' : u'123',
    'Caribou_Symbols_More' : u'{#*',
    'Caribou_Alpha' : u'Abc'
}

class AntlerKey(Gtk.Button):
    def __init__(self, key):
        gobject.GObject.__init__(self)
        self.caribou_key = key
        self.connect("pressed", self._on_pressed)
        self.connect("released", self._on_released)
        self.set_label(self._get_key_label())

        label = self.get_child()
        label.set_use_markup(True)
        label.props.margin = 6

        ctx = self.get_style_context()
        ctx.add_class("antler-keyboard-button")

        if key.props.name == "Caribou_Prefs":
            key.connect("key-clicked", self._on_prefs_clicked)
        if key.get_extended_keys ():
            self._sublevel = AntlerSubLevel(self)

    def _on_prefs_clicked(self, key):
        p = PreferencesDialog(AntlerSettings())
        p.populate_settings(CaribouSettings())
        p.show_all()
        p.run()
        p.destroy()

    def _get_key_label(self):
        label = self.caribou_key.props.name
        if PRETTY_LABELS.has_key(self.caribou_key.props.name):
            label = PRETTY_LABELS[self.caribou_key.props.name]
        elif self.caribou_key.props.name.startswith('Caribou_'):
            label = self.caribou_key.name.replace('Caribou_', '')
        else:
            unichar = unichr(Gdk.keyval_to_unicode(self.caribou_key.props.keyval))
            if not unichar.isspace() and unichar != u'\x00':
                label = unichar

        return "<b>%s</b>" % glib.markup_escape_text(label.encode('utf-8'))

    def _on_pressed(self, button):
        self.caribou_key.press()

    def _on_released(self, button):
        self.caribou_key.release()

    def do_get_preferred_width_for_height(self, w):
        return (w, w)

    def do_get_request_mode(self):
        return Gtk.SizeRequestMode.WIDTH_FOR_HEIGHT

class AntlerSubLevel(Gtk.Window):
    def __init__(self, key):
        gobject.GObject.__init__(self, type=Gtk.WindowType.POPUP)

        self.set_decorated(False)
        self.set_resizable(False)
        self.set_accept_focus(False)
        self.set_position(Gtk.WindowPosition.MOUSE)
        self.set_type_hint(Gdk.WindowTypeHint.DIALOG)

        ctx = self.get_style_context()
        ctx.add_class("antler-keyboard-window")

        key.caribou_key.connect("notify::show-subkeys", self._on_show_subkeys)
        self._key = key

        layout = AntlerLayout()
        layout.add_row(key.caribou_key.get_extended_keys())
        self.add(layout)

    def _on_show_subkeys(self, key, prop):
        parent = self._key.get_toplevel()
        if key.props.show_subkeys:
            self.set_transient_for(parent)
            parent.set_sensitive(False)
            self.show_all()
        else:
            parent.set_sensitive(True)
            self.hide()

class AntlerLayout(Gtk.Grid):
    KEY_SPAN = 4

    def __init__(self, level=None):
        gobject.GObject.__init__(self)
        self.set_column_homogeneous(True)
        self.set_row_homogeneous(True)
        self.set_row_spacing(6)
        self.set_column_spacing(6)

        ctx = self.get_style_context()
        ctx.add_class("antler-keyboard-layout")

        if level:
            self.load_rows(level.get_rows ())

    def add_row(self, row, row_num=0):
        col_num = 0
        for i, key in enumerate(row):
            antler_key = AntlerKey(key)
            ctx = antler_key.get_style_context()
            ctx.add_class("antler-keyboard-row%d" % row_num)
            self.attach(antler_key,
                        col_num + int(key.props.margin_left * self.KEY_SPAN),
                        row_num * self.KEY_SPAN,
                        int(self.KEY_SPAN * key.props.width),
                        self.KEY_SPAN)
            col_num += int((key.props.width + key.props.margin_left ) * self.KEY_SPAN)

    def load_rows(self, rows):
        for row_num, row in enumerate(rows):
            self.add_row(row.get_keys(), row_num)

class AntlerKeyboardView(Gtk.Notebook):
    def __init__(self):
        gobject.GObject.__init__(self)
        self.set_show_tabs(False)
        self.keyboard_model = Caribou.KeyboardModel()
        self.keyboard_model.connect("notify::active-group", self._on_group_changed)

        self.layers = {}


        settings = AntlerSettings()
        use_system = settings.use_system
        use_system.connect("value-changed", self._on_use_system_theme_changed)

        self._app_css_provider = Gtk.CssProvider()
        self._load_style(
            self._app_css_provider, "style.css",
            [glib.get_user_data_dir()] + list(glib.get_system_data_dirs()))

        if not use_system.value:
            Gtk.StyleContext.add_provider_for_screen(
                Gdk.Screen.get_default(), self._app_css_provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)


        self._scan_css_provider = Gtk.CssProvider()
        self._load_style(
            self._scan_css_provider, "scan-style.css",
            [glib.get_user_data_dir()] + list(glib.get_system_data_dirs()))
        Gtk.StyleContext.add_provider_for_screen(
                Gdk.Screen.get_default(), self._scan_css_provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        self._user_css_provider = Gtk.CssProvider()
        self._load_style(self._user_css_provider, "user-style.css",
                         [glib.get_user_data_dir()])
        Gtk.StyleContext.add_provider_for_screen(
                Gdk.Screen.get_default(), self._user_css_provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + 1)

        for gname in self.keyboard_model.get_groups():
            group = self.keyboard_model.get_group(gname)
            self.layers[gname] = {}
            group.connect("notify::active-level", self._on_level_changed)
            for lname in group.get_levels():
                level = group.get_level(lname)
                layout = AntlerLayout(level)
                layout.show()
                self.layers[gname][lname] = self.append_page(layout, None)

        self._set_to_active_layer()

    def _on_use_system_theme_changed(self, setting, value):
        if value:
            Gtk.StyleContext.remove_provider_for_screen(
                Gdk.Screen.get_default(), self._app_css_provider)
        else:
            Gtk.StyleContext.add_provider_for_screen(
                Gdk.Screen.get_default(), self._app_css_provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    def _load_style(self, provider, filename, search_path):
        spath = search_path[:]
        if os.environ.has_key("ANTLER_THEME_PATH"):
            spath.insert(0, os.environ["ANTLER_THEME_PATH"])

        for directory in spath:
            fn = os.path.join(directory, "antler", filename)
            if os.path.exists(fn):
                provider.load_from_path(fn)
                break

    def _on_level_changed(self, group, prop):
        self._set_to_active_layer()

    def _on_group_changed(self, kb, prop):
        self._set_to_active_layer()

    def _set_to_active_layer(self):
        active_group_name = self.keyboard_model.props.active_group
        active_group = self.keyboard_model.get_group(active_group_name)
        active_level_name = active_group.props.active_level

        self.set_current_page(self.layers[active_group_name][active_level_name])


if __name__ == "__main__":
    import signal
    signal.signal(signal.SIGINT, signal.SIG_DFL)

    w = Gtk.Window()
    w.set_accept_focus(False)

    kb = AntlerKeyboardView()
    w.add(kb)

    w.show_all()

    Gtk.main()