From 774c558b98f11288d2ddfd18bb4e0127da62877e Mon Sep 17 00:00:00 2001 From: Eitan Isaacson Date: Wed, 2 Dec 2009 13:48:11 -0800 Subject: Made special CaribouWindowEntry class to demonstrate more sophisticated placement policies --- src/caribou.py | 9 ++------- src/keyboard.py | 52 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/caribou.py b/src/caribou.py index 6c8c93b..bc5eb64 100644 --- a/src/caribou.py +++ b/src/caribou.py @@ -175,13 +175,8 @@ if __name__ == "__main__": pyatspi.Registry.registerKeystrokeListener(test.on_key_down, mask = None, kind = (pyatspi.KEY_PRESSED_EVENT,)) # TODO: move text entry detection to its own file - placement = keyboard.CaribouKeyboardPlacement( - xalign=keyboard.CaribouKeyboardPlacement.START, - xstickto=keyboard.CaribouKeyboardPlacement.ENTRY, - ystickto=keyboard.CaribouKeyboardPlacement.ENTRY, - xgravitate=keyboard.CaribouKeyboardPlacement.INSIDE, - ygravitate=keyboard.CaribouKeyboardPlacement.INSIDE) - cp = keyboard.CaribouHoverWindow(placement) + + cp = keyboard.CaribouWindowEntry() cp.hide_all() gtk.main() diff --git a/src/keyboard.py b/src/keyboard.py index b4943f3..ae0244c 100644 --- a/src/keyboard.py +++ b/src/keyboard.py @@ -130,20 +130,14 @@ gobject.type_register(CaribouKeyboard) class CaribouWindow(gtk.Window): __gtype_name__ = "CaribouWindow" - def __init__(self): + def __init__(self, default_placement=None): super(CaribouWindow, self).__init__(gtk.WINDOW_POPUP) self.set_name("CaribouWindow") self._vbox = gtk.VBox() self.add(self._vbox) - self._vbox.pack_start(CaribouKeyboard(qwerty)) - -class CaribouHoverWindow(CaribouWindow): - __gtype_name__ = "CaribouHoverWindow" - - def __init__(self, default_placement=None): - super(CaribouHoverWindow, self).__init__() + self._vbox.pack_start(CaribouKeyboard(qwerty)) self.connect("size-allocate", lambda w, a: self._update_position()) @@ -151,7 +145,6 @@ class CaribouHoverWindow(CaribouWindow): self._entry_location = gtk.gdk.Rectangle() self._default_placement = default_placement or \ CaribouKeyboardPlacement() - def set_cursor_location(self, cursor_location): self._cursor_location = cursor_location @@ -177,16 +170,18 @@ class CaribouHoverWindow(CaribouWindow): x = self._calculate_axis(placement.x, root_bbox) y = self._calculate_axis(placement.y, root_bbox) - proposed_position = \ - gdk.Rectangle(x, y, self.allocation.width, self.allocation.height) - - x += placement.x.adjust_to_bounds(root_bbox, proposed_position) - y += placement.y.adjust_to_bounds(root_bbox, proposed_position) - + return x, y def _update_position(self): - self.move(*self._calculate_position()) + x, y = self._calculate_position() + root_bbox = self._get_root_bbox() + proposed_position = \ + gdk.Rectangle(x, y, self.allocation.width, self.allocation.height) + + x += self._default_placement.x.adjust_to_bounds(root_bbox, proposed_position) + y += self._default_placement.y.adjust_to_bounds(root_bbox, proposed_position) + self.move(x, y) def _calculate_axis(self, axis_placement, root_bbox): bbox = root_bbox @@ -210,6 +205,29 @@ class CaribouHoverWindow(CaribouWindow): return offset +class CaribouWindowEntry(CaribouWindow): + __gtype_name__ = "CaribouWindowEntry" + + def __init__(self): + placement = CaribouKeyboardPlacement( + xalign=CaribouKeyboardPlacement.START, + xstickto=CaribouKeyboardPlacement.ENTRY, + ystickto=CaribouKeyboardPlacement.ENTRY, + xgravitate=CaribouKeyboardPlacement.INSIDE, + ygravitate=CaribouKeyboardPlacement.OUTSIDE) + + CaribouWindow.__init__(self, placement) + + def _calculate_axis(self, axis_placement, root_bbox): + offset = CaribouWindow._calculate_axis(self, axis_placement, root_bbox) + + if axis_placement.axis == 'y': + if offset + self.allocation.height > root_bbox.height + root_bbox.y: + new_axis_placement = axis_placement.copy(align=CaribouKeyboardPlacement.START) + offset = CaribouWindow._calculate_axis(self, new_axis_placement, root_bbox) + + return offset + class CaribouKeyboardPlacement(object): START = 'start' END = 'end' @@ -269,7 +287,7 @@ class CaribouKeyboardPlacement(object): ygravitate or self.OUTSIDE) if __name__ == "__main__": - ckbd = CaribouHoverWindow() + ckbd = CaribouWindow() ckbd.show_all() gtk.main() -- cgit v1.2.1