-
Notifications
You must be signed in to change notification settings - Fork 52
Convert dynamic on_trait_change to observe part 2 #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6cb6a5c
348d9be
cb8abb9
11bcb9c
267cf0c
a861827
c497ec0
23228ea
8694cea
194c3fc
68d9a9c
fa38e0b
6316309
a972587
e3434f1
8fa8527
021359e
0151354
5c17aee
a9c74b3
77a6e6c
ca4ca3e
0c77a63
8a4c0aa
e9b92e1
8a663b3
4e45c99
e79805b
c581b0b
c51318d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
from pyface.action.action import Action | ||
from traits.api import Any, Str | ||
from traits.observation.api import trait | ||
|
||
# Logging. | ||
logger = logging.getLogger(__name__) | ||
|
@@ -55,11 +56,15 @@ def destroy(self): | |
""" | ||
|
||
if self.object: | ||
self.object.on_trait_change( | ||
self._enabled_update, self.enabled_name, remove=True | ||
self.object.observe( | ||
self._enabled_update, | ||
trait(self.enabled_name, optional=True), | ||
remove=True | ||
) | ||
self.object.on_trait_change( | ||
self._visible_update, self.visible_name, remove=True | ||
self.object.observe( | ||
self._visible_update, | ||
trait(self.visible_name, optional=True), | ||
remove=True | ||
) | ||
|
||
def perform(self, event=None): | ||
|
@@ -102,32 +107,31 @@ def _enabled_name_changed(self, old, new): | |
obj = self.object | ||
if obj is not None: | ||
if old: | ||
obj.on_trait_change(self._enabled_update, old, remove=True) | ||
obj.observe(self._enabled_update, old, remove=True) | ||
if new: | ||
obj.on_trait_change(self._enabled_update, new) | ||
obj.observe(self._enabled_update, new) | ||
self._enabled_update() | ||
|
||
def _visible_name_changed(self, old, new): | ||
obj = self.object | ||
if obj is not None: | ||
if old: | ||
obj.on_trait_change(self._visible_update, old, remove=True) | ||
obj.observe(self._visible_update, old, remove=True) | ||
if new: | ||
obj.on_trait_change(self._visible_update, new) | ||
obj.observe(self._visible_update, new) | ||
self._visible_update() | ||
|
||
def _object_changed(self, old, new): | ||
for kind in ("enabled", "visible"): | ||
method = getattr(self, "_%s_update" % kind) | ||
name = getattr(self, "%s_name" % kind) | ||
if name: | ||
if old: | ||
old.on_trait_change(method, name, remove=True) | ||
if new: | ||
new.on_trait_change(method, name) | ||
if old: | ||
old.observe(method, trait(name, optional=True), remove=True) | ||
if new: | ||
new.observe(method, trait(name, optional=True)) | ||
Comment on lines
+128
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commenting on this to draw attention to a reviewer. Previously the observer was never getting hooked up if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
method() | ||
|
||
def _enabled_update(self): | ||
def _enabled_update(self, event=None): | ||
if self.enabled_name: | ||
if self.object: | ||
self.enabled = bool( | ||
|
@@ -138,7 +142,7 @@ def _enabled_update(self): | |
else: | ||
self.enabled = bool(self.object) | ||
|
||
def _visible_update(self): | ||
def _visible_update(self, event=None): | ||
if self.visible_name: | ||
if self.object: | ||
self.visible = bool( | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trait
here assumes the first argument is a trait name (see doc), so an extended name like"child.is_visible"
is going to fail because"child.is_visible"
as a trait name is not defined onobject
. Theoptional=True
may mask that lost behaviour?