From 7b04e086f744aa88939d8ab7b3c5f7573c7484fb Mon Sep 17 00:00:00 2001 From: Eitan Isaacson Date: Tue, 1 Dec 2009 14:16:48 -0800 Subject: Fixed more positioning things. --- src/caribou.py | 32 +++++++++++++++++++++++-------- src/keyboard.py | 58 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/src/caribou.py b/src/caribou.py index e6a5bd5..f52f8d1 100644 --- a/src/caribou.py +++ b/src/caribou.py @@ -42,17 +42,27 @@ class Test: text = acc.queryText() [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS) cp.set_cursor_location(gtk.gdk.Rectangle(x, y, width, height)) + + component = acc.queryComponent() + entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS) + cp.set_entry_location(entry_bb) cp.show_all() def __set_entry_location(self, acc): text = acc.queryText() - [x, y, width, height] = text.getCharacterExtents(text.caretOffset, pyatspi.DESKTOP_COORDS) - if x == 0 and y == 0 and width == 0 and height == 0: - component = acc.queryComponent() - bb = component.getExtents(pyatspi.DESKTOP_COORDS) - cp.set_cursor_location(gtk.gdk.Rectangle(bb.x, bb.y, bb.width, bb.height)) - else: - cp.set_cursor_location(gtk.gdk.Rectangle(x, y, width, height)) + cursor_bb = gdk.Rectangle( + *text.getCharacterExtents(text.caretOffset, + pyatspi.DESKTOP_COORDS)) + + component = acc.queryComponent() + entry_bb = component.getExtents(pyatspi.DESKTOP_COORDS) + + if cursor_bb == gdk.Rectangle(0, 0, 0, 0): + cursor_bb = entry_bb + + cp.set_cursor_location(cursor_bb) + cp.set_entry_location(entry_bb) + cp.show_all() def on_state_changed_focused(self, event): @@ -160,7 +170,13 @@ 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 - cp = keyboard.CaribouHoverWindow() + 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.hide_all() gtk.main() diff --git a/src/keyboard.py b/src/keyboard.py index 0c8dc2b..c132421 100644 --- a/src/keyboard.py +++ b/src/keyboard.py @@ -144,15 +144,21 @@ class CaribouHoverWindow(CaribouWindow): return gdk.Rectangle(*args) def _update_position(self, placement=None): + root_bbox = self._get_root_bbox() placement = placement or self._default_placement - x = self._calculate_axis(placement.x) - y = self._calculate_axis(placement.y) + 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) self.move(x, y) - def _calculate_axis(self, axis_placement): - root_bbox = self._get_root_bbox() + def _calculate_axis(self, axis_placement, root_bbox): bbox = root_bbox if axis_placement.stickto == CaribouKeyboardPlacement.CURSOR: @@ -164,7 +170,12 @@ class CaribouHoverWindow(CaribouWindow): if axis_placement.align == CaribouKeyboardPlacement.END: offset += axis_placement.get_length(bbox) - elif axis_placement.halign == CaribouKeyboardPlacement.CENTER: + if axis_placement.gravitate == CaribouKeyboardPlacement.INSIDE: + offset -= axis_placement.get_length(self.allocation) + elif axis_placement.align == CaribouKeyboardPlacement.START: + if axis_placement.gravitate == CaribouKeyboardPlacement.OUTSIDE: + offset -= axis_placement.get_length(self.allocation) + elif axis_placement.align == CaribouKeyboardPlacement.CENTER: offset += axis_placement.get_length(bbox)/2 return offset @@ -182,11 +193,11 @@ class CaribouKeyboardPlacement(object): OUTSIDE = 'outside' class _AxisPlacement(object): - def __init__(self, axis, align=None, stickto=None, gravitate=None): + def __init__(self, axis, align, stickto, gravitate): self.axis = axis - self.align = align or CaribouKeyboardPlacement.END - self.stickto = stickto or CaribouKeyboardPlacement.CURSOR - self.gravitate = gravitate or CaribouKeyboardPlacement.OUTSIDE + self.align = align + self.stickto = stickto + self.gravitate = gravitate def copy(self, align=None, stickto=None, gravitate=None): return self.__class__(self.axis, @@ -200,9 +211,32 @@ class CaribouKeyboardPlacement(object): def get_length(self, bbox): return bbox.width if self.axis == 'x' else bbox.height - def __init__(self, x=None, y=None): - self.x = x or self._AxisPlacement('x') - self.y = y or self._AxisPlacement('y') + def adjust_to_bounds(self, root_bbox, child_bbox): + child_vector_start = self.get_offset(child_bbox) + child_vector_end = self.get_length(child_bbox) + child_vector_start + root_vector_start = self.get_offset(root_bbox) + root_vector_end = self.get_length(root_bbox) + root_vector_start + + if root_vector_end < child_vector_end: + return root_vector_end - child_vector_end + + if root_vector_start > child_vector_start: + return root_vector_start - child_vector_start + + return 0 + + + def __init__(self, + xalign=None, xstickto=None, xgravitate=None, + yalign=None, ystickto=None, ygravitate=None): + self.x = self._AxisPlacement('x', + xalign or self.END, + xstickto or self.CURSOR, + xgravitate or self.OUTSIDE) + self.y = self._AxisPlacement('y', + yalign or self.END, + ystickto or self.CURSOR, + ygravitate or self.OUTSIDE) if __name__ == "__main__": ckbd = CaribouHoverWindow() -- cgit v1.2.1