Skip to content

Commit

Permalink
FIX: properly destroy tasks actions
Browse files Browse the repository at this point in the history
A few tasks-related actions did not properly dispose
of listeners to other object when they were destroyed.
  • Loading branch information
pberkes committed Nov 14, 2012
1 parent 28e8de3 commit d2328f0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
22 changes: 17 additions & 5 deletions pyface/tasks/action/dock_pane_toggle_group.py
@@ -1,13 +1,13 @@
# Enthought library imports.
from pyface.action.api import Action, ActionItem, Group
from traits.api import Bool, Instance, List, Property, Unicode, on_trait_change
from traits.api import Instance, List, Property, Unicode, on_trait_change

# Local imports.
from pyface.tasks.i_dock_pane import IDockPane


class DockPaneToggleAction(Action):
""" An Action for toggling the visibily of a dock pane.
""" An Action for toggling the visibility of a dock pane.
"""

#### 'DockPaneToggleAction' interface #####################################
Expand All @@ -24,6 +24,14 @@ class DockPaneToggleAction(Action):
# 'Action' interface.
###########################################################################

def destroy(self):
super(DockPaneToggleAction, self).destroy()

# Make sure that we are not listening to changes to the pane anymore.
# In traits style, we will set the basic object to None and have the
# listener check that if it is still there.
self.dock_pane = None

def perform(self, event=None):
if self.dock_pane:
self.dock_pane.visible = not self.dock_pane.visible
Expand All @@ -33,18 +41,22 @@ def perform(self, event=None):
###########################################################################

def _get_name(self):
if self.dock_pane is None:
return 'DELETED DOCK TOGGLE'
return self.dock_pane.name

def _get_tooltip(self):
return u'Toggles the visibilty of the %s pane.' % self.name
return u'Toggles the visibility of the %s pane.' % self.name

@on_trait_change('dock_pane.visible')
def _update_checked(self):
self.checked = self.dock_pane.visible
if self.dock_pane:
self.checked = self.dock_pane.visible

@on_trait_change('dock_pane.closable')
def _update_visible(self):
self.visible = self.dock_pane.closable
if self.dock_pane:
self.visible = self.dock_pane.closable


class DockPaneToggleGroup(Group):
Expand Down
15 changes: 15 additions & 0 deletions pyface/tasks/action/listening_action.py
Expand Up @@ -34,6 +34,21 @@ class ListeningAction(Action):
# 'Action' interface.
###########################################################################

def destroy(self):
""" Called when the action is no longer required.
Remove all the task listeners.
"""

if self.object:
self.object.on_trait_change(
self._enabled_update, self.enabled_name, remove=True
)
self.object.on_trait_change(
self._visible_update, self.visible_name, remove=True
)

def perform(self, event=None):
""" Call the appropriate function.
"""
Expand Down
19 changes: 15 additions & 4 deletions pyface/tasks/action/task_toggle_group.py
@@ -1,7 +1,6 @@
# Enthought library imports.
from pyface.action.api import Action, ActionItem, Group
from traits.api import Any, Bool, List, Instance, Property, Unicode, \
on_trait_change
from traits.api import Any, List, Instance, Property, Unicode, on_trait_change

# Local imports.
from pyface.tasks.task import Task
Expand All @@ -26,6 +25,14 @@ class TaskToggleAction(Action):
# 'Action' interface.
###########################################################################

def destroy(self):
super(TaskToggleAction, self).destroy()

# Make sure that we are not listening to changes in the task anymore
# In traits style, we will set the basic object to None and have the
# listener check that if it is still there.
self.task = None

def perform(self, event=None):
window = self.task.window
window.activate_task(self.task)
Expand All @@ -35,15 +42,19 @@ def perform(self, event=None):
###########################################################################

def _get_name(self):
if self.task is None:
return 'DELETED TASK TOGGLE'
return self.task.name

def _get_tooltip(self):
return u'Switch to the %s task.' % self.name

@on_trait_change('task.window.active_task')
def _update_checked(self):
window = self.task.window
self.checked = window is not None and window.active_task == self.task
if self.task:
window = self.task.window
self.checked = (window is not None
and window.active_task == self.task)


class TaskToggleGroup(Group):
Expand Down

0 comments on commit d2328f0

Please sign in to comment.