Skip to content

Commit

Permalink
fix: prevent root widgets from drawing/undrawing during __init__()
Browse files Browse the repository at this point in the history
When I made `__group` a private variable (#7927c9a), I had to modify the
`Root` constructor to call `_regroup()` instead of directly setting
`self._group` (#55c42ff).  However, this inadvertantly broke
`PanningGui`, because it resulting in `do_draw()` and `do_undraw()`
being called before the object was fully initialized.

Fixes #46.
  • Loading branch information
kalekundert committed Feb 27, 2021
1 parent d7ad302 commit 5165b50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion glooey/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def __init__(self, window, batch=None, group=None):

self.__window = window
self.__batch = batch or pyglet.graphics.Batch()
self._regroup(group)
self.__spurious_leave_event = False

self._init_group(group)

window.push_handlers(self)

def on_mouse_enter(self, x, y):
Expand Down
28 changes: 24 additions & 4 deletions glooey/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,10 +1477,18 @@ def _regroup(self, new_group):
"""
Change the `pyglet.graphics.Group` associated with this widget.
Three callbacks are invoked to allow the widget to react to this
change: `do_regroup()`, `do_draw()`, and `do_regroup_children()`. The
last initiates a recursive descent down the widget hierarchy updating
the groups of the widget's children and all of their children.
Four callbacks are invoked to allow the widget to react to this
change. In the following order:
- `do_regroup()`
- `do_undraw()`
- Called indirectly by `do_regroup()` and not by this method, so this
can be overridden.
- `do_draw()`
- `do_regroup_children()`
The last initiates a recursive descent down the widget hierarchy
updating the groups of the widget's children and all of their children.
"""
# Changing the group is often an expensive operation, so don't do
# anything unless we have to. It is assumed that do_regroup_children()
Expand Down Expand Up @@ -1530,6 +1538,18 @@ def _repack_and_regroup_children(self):
if self.__num_children > 0:
self.do_regroup_children()

def _init_group(self, group):
"""
Set the `pyglet.graphics.Group` associated with this object, without
invoking any of the callbacks that `_regroup()` would.
This method is only meant to be used in constructors---specifically
`Root.__init__()`---where drawing/regrouping children doesn't make
sense and can create complications (e.g. the need for `do_draw()` to
guard against uninitialized member variables).
"""
self.__group = group

def _attach_child(self, child):
"""
Add a child to this widget.
Expand Down

0 comments on commit 5165b50

Please sign in to comment.