| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
# Enthought library imports. |
| 11 |
from pyface.action.api import Action, ActionItem, Group |
| 12 |
from traits.api import Any, Instance, List, on_trait_change, Property, Str |
| 13 |
|
| 14 |
|
| 15 |
class TaskWindowToggleAction(Action): |
| 16 |
"""An action for activating an application window.""" |
| 17 |
|
| 18 |
#### 'Action' interface ################################################### |
| 19 |
|
| 20 |
name = Property(Str, observe="window.active_task.name") |
| 21 |
style = "toggle" |
| 22 |
|
| 23 |
#### 'TaskWindowToggleAction' interface ################################### |
| 24 |
|
| 25 |
# The window to use for this action. |
| 26 |
window = Instance("envisage.ui.tasks.task_window.TaskWindow") |
| 27 |
|
| 28 |
########################################################################### |
| 29 |
# 'Action' interface. |
| 30 |
########################################################################### |
| 31 |
|
| 32 |
def perform(self, event=None): |
| 33 |
if self.window: |
| 34 |
self.window.activate() |
| 35 |
|
| 36 |
########################################################################### |
| 37 |
# Private interface. |
| 38 |
########################################################################### |
| 39 |
|
| 40 |
def _get_name(self): |
| 41 |
if self.window.active_task: |
| 42 |
return self.window.active_task.name |
| 43 |
return "" |
| 44 |
|
| 45 |
@on_trait_change("window:activated") |
| 46 |
def _window_activated(self): |
| 47 |
self.checked = True |
| 48 |
|
| 49 |
@on_trait_change("window:deactivated") |
| 50 |
def _window_deactivated(self): |
| 51 |
self.checked = False |
| 52 |
|
| 53 |
|
| 54 |
class TaskWindowToggleGroup(Group): |
| 55 |
""" |
| 56 |
A Group for toggling the activation state of an application's windows. |
| 57 |
""" |
| 58 |
|
| 59 |
#### 'Group' interface #################################################### |
| 60 |
|
| 61 |
id = "TaskWindowToggleGroup" |
| 62 |
items = List |
| 63 |
|
| 64 |
#### 'TaskWindowToggleGroup' interface #################################### |
| 65 |
|
| 66 |
# The application that contains the group. |
| 67 |
application = Instance( |
| 68 |
"envisage.ui.tasks.tasks_application." "TasksApplication" |
| 69 |
) |
| 70 |
|
| 71 |
# The ActionManager to which the group belongs. |
| 72 |
manager = Any |
| 73 |
|
| 74 |
########################################################################### |
| 75 |
# 'Group' interface. |
| 76 |
########################################################################### |
| 77 |
|
| 78 |
def destroy(self): |
| 79 |
"""Called when the group is no longer required.""" |
| 80 |
super().destroy() |
| 81 |
if self.application: |
| 82 |
self.application.on_trait_change( |
| 83 |
self._rebuild, "window_opened, window_closed", remove=True |
| 84 |
) |
| 85 |
|
| 86 |
########################################################################### |
| 87 |
# Private interface. |
| 88 |
########################################################################### |
| 89 |
|
| 90 |
def _get_items(self): |
| 91 |
items = [] |
| 92 |
for window in self.application.windows: |
| 93 |
active = window == self.application.active_window |
| 94 |
action = TaskWindowToggleAction(window=window, checked=active) |
| 95 |
items.append(ActionItem(action=action)) |
| 96 |
return items |
| 97 |
|
| 98 |
def _rebuild(self): |
| 99 |
# Clear out the old group, then build the new one. |
| 100 |
for item in self.items: |
| 101 |
item.destroy() |
| 102 |
self.items = self._get_items() |
| 103 |
|
| 104 |
# Inform our manager that it needs to be rebuilt. |
| 105 |
self.manager.changed = True |
| 106 |
|
| 107 |
#### Trait initializers ################################################### |
| 108 |
|
| 109 |
def _application_default(self): |
| 110 |
return self.manager.controller.task.window.application |
| 111 |
|
| 112 |
def _items_default(self): |
| 113 |
self.application.on_trait_change( |
| 114 |
self._rebuild, "window_opened, window_closed" |
| 115 |
) |
| 116 |
return self._get_items() |
| 117 |
|
| 118 |
def _manager_default(self): |
| 119 |
manager = self |
| 120 |
while isinstance(manager, Group): |
| 121 |
manager = manager.parent |
| 122 |
return manager |