1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from __future__ import unicode_literals
from atom.api import Typed
from enaml.application import Application
from enaml.workbench.extension import Extension
from enaml.workbench.plugin import Plugin
from .action_item import ActionItem
from .autostart import Autostart
from .branding import Branding
from .menu_helper import create_menus
from .menu_item import MenuItem
from .window_model import WindowModel
from .workspace import Workspace
import enaml
with enaml.imports():
from .workbench_window import WorkbenchWindow
ACTIONS_POINT = 'enaml.workbench.ui.actions'
APPLICATION_FACTORY_POINT = 'enaml.workbench.ui.application_factory'
BRANDING_POINT = 'enaml.workbench.ui.branding'
WINDOW_FACTORY_POINT = 'enaml.workbench.ui.window_factory'
WORKSPACES_POINT = 'enaml.workbench.ui.workspaces'
AUTOSTART_POINT = 'enaml.workbench.ui.autostart'
class UIPlugin(Plugin):
""" The main UI plugin class for the Enaml studio.
The ui plugin manages the extension points for user contributions
to the main window.
"""
def start(self):
""" Start the plugin life-cycle.
This method is called by the framework at the appropriate time.
It should never be called by user code.
"""
self._create_application()
self._create_model()
self._create_window()
self._bind_observers()
self._start_autostarts()
def stop(self):
""" Stop the plugin life-cycle.
This method is called by the framework at the appropriate time.
It should never be called by user code.
"""
self._stop_autostarts()
self._unbind_observers()
self._destroy_window()
self._release_model()
self._release_application()
@property
def window(self):
""" Get a reference to the primary window.
"""
return self._window
@property
def workspace(self):
""" Get a reference to the currently active workspace.
"""
return self._model.workspace
def show_window(self):
""" Ensure the underlying window object is shown.
"""
self._window.show()
def hide_window(self):
""" Ensure the underlying window object is hidden.
"""
self._window.hide()
def start_application(self):
""" Start the application event loop.
"""
self._application.start()
def stop_application(self):
""" Stop the application event loop.
"""
self._application.stop()
def close_window(self):
""" Close the underlying workbench window.
"""
self._window.close()
def close_workspace(self):
""" Close and dispose of the currently active workspace.
"""
self._workspace_extension = None
self._model.workspace.stop()
self._model.workspace.workbench = None
self._model.workspace = Workspace()
def select_workspace(self, extension_id):
""" Select and start the workspace for the given extension id.
The current workspace will be stopped and released.
"""
target = None
workbench = self.workbench
point = workbench.get_extension_point(WORKSPACES_POINT)
for extension in point.extensions:
if extension.qualified_id == extension_id:
target = extension
break
if target is None:
msg = "'%s' is not a registered workspace extension"
raise ValueError(msg % extension_id)
if target is self._workspace_extension:
return
old_workspace = self._model.workspace
old_workspace.stop()
old_workspace.workbench = None
self._workspace_extension = target
new_workspace = self._create_workspace(target)
new_workspace.workbench = workbench
new_workspace.start()
self._model.workspace = new_workspace
#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
#: The application provided by an ApplicationFactory extension.
_application = Typed(Application)
#: The window object provided by a WindowFactory extension.
_window = Typed(WorkbenchWindow)
#: The view model object used to drive the window.
_model = Typed(WindowModel)
#: The currently activate branding extension object.
_branding_extension = Typed(Extension)
#: The currently activate workspace extension object.
_workspace_extension = Typed(Extension)
#: The currently active action extension objects.
_action_extensions = Typed(dict, ())
def _create_application(self):
""" Create the Application object for the ui.
This will load the highest ranking extension to the application
factory extension point, and use it to create the instance.
If an application object already exists, that application will
be used instead of any defined by a factory, since there can be
only one application per-process.
"""
if Application.instance() is not None:
self._application = Application.instance()
return
workbench = self.workbench
point = workbench.get_extension_point(APPLICATION_FACTORY_POINT)
extensions = point.extensions
if not extensions:
msg = "no contributions to the '%s' extension point"
raise RuntimeError(msg % APPLICATION_FACTORY_POINT)
extension = extensions[-1]
if extension.factory is None:
msg = "extension '%s' does not declare an application factory"
raise ValueError(msg % extension.qualified_id)
application = extension.factory()
if not isinstance(application, Application):
msg = "extension '%s' created non-Application type '%s'"
args = (extension.qualified_id, type(application).__name__)
raise TypeError(msg % args)
self._application = application
def _create_model(self):
""" Create and initialize the model which drives the window.
"""
self._model = WindowModel()
self._refresh_branding()
self._refresh_actions()
def _create_window(self):
""" Create the WorkbenchWindow object for the workbench.
This will load the highest ranking extension to the window
factory extension point, and use it to create the instance.
"""
workbench = self.workbench
point = workbench.get_extension_point(WINDOW_FACTORY_POINT)
extensions = point.extensions
if not extensions:
msg = "no contributions to the '%s' extension point"
raise RuntimeError(msg % WINDOW_FACTORY_POINT)
extension = extensions[-1]
if extension.factory is None:
msg = "extension '%s' does not declare a window factory"
raise ValueError(msg % extension.qualified_id)
window = extension.factory(workbench)
if not isinstance(window, WorkbenchWindow):
msg = "extension '%s' created non-WorkbenchWindow type '%s'"
args = (extension.qualified_id, type(window).__name__)
raise TypeError(msg % args)
window.workbench = workbench
window.window_model = self._model
self._window = window
def _create_workspace(self, extension):
""" Create the Workspace object for the given extension.
Parameters
----------
extension : Extension
The extension object of interest.
Returns
-------
result : Workspace
The workspace object for the given extension.
"""
if extension.factory is None:
msg = "extension '%s' does not declare a workspace factory"
raise ValueError(msg % extension.qualified_id)
workspace = extension.factory(self.workbench)
if not isinstance(workspace, Workspace):
msg = "extension '%s' created non-Workspace type '%s'"
args = (extension.qualified_id, type(workspace).__name__)
raise TypeError(msg % args)
return workspace
def _create_action_items(self, extension):
""" Create the action items for the extension.
"""
workbench = self.workbench
menu_items = extension.get_children(MenuItem)
action_items = extension.get_children(ActionItem)
if extension.factory:
for item in extension.factory(workbench):
if isinstance(item, MenuItem):
menu_items.append(item)
elif isinstance(item, ActionItem):
action_items.append(item)
else:
msg = "action extension created invalid action type '%s'"
raise TypeError(msg % type(item).__name__)
return menu_items, action_items
def _destroy_window(self):
""" Destroy and release the underlying window object.
"""
self._window.hide()
self._window.destroy()
self._window = None
def _release_model(self):
""" Release the underlying window model object.
"""
self._model.workspace.stop()
self._model = None
def _release_application(self):
""" Stop and release the underlyling application object.
"""
self._application.stop()
self._application = None
def _refresh_branding(self):
""" Refresh the branding object for the window model.
"""
workbench = self.workbench
point = workbench.get_extension_point(BRANDING_POINT)
extensions = point.extensions
if not extensions:
self._branding_extension = None
self._model.branding = Branding()
return
extension = extensions[-1]
if extension is self._branding_extension:
return
if extension.factory:
branding = extension.factory(workbench)
if not isinstance(branding, Branding):
msg = "extension '%s' created non-Branding type '%s'"
args = (extension.qualified_id, type(branding).__name__)
raise TypeError(msg % args)
else:
branding = extension.get_child(Branding, reverse=True)
branding = branding or Branding()
self._branding_extension = extension
self._model.branding = branding
def _refresh_actions(self):
""" Refresh the actions for the workbench window.
"""
workbench = self.workbench
point = workbench.get_extension_point(ACTIONS_POINT)
extensions = point.extensions
if not extensions:
self._action_extensions.clear()
self._model.menus = []
return
menu_items = []
action_items = []
new_extensions = {}
old_extensions = self._action_extensions
for extension in extensions:
if extension in old_extensions:
m_items, a_items = old_extensions[extension]
else:
m_items, a_items = self._create_action_items(extension)
new_extensions[extension] = (m_items, a_items)
menu_items.extend(m_items)
action_items.extend(a_items)
menus = create_menus(workbench, menu_items, action_items)
self._action_extensions = new_extensions
self._model.menus = menus
def _get_autostarts(self):
""" Get the autostart extension objects.
"""
workbench = self.workbench
point = workbench.get_extension_point(AUTOSTART_POINT)
extensions = sorted(point.extensions, key=lambda ext: ext.rank)
autostarts = []
for extension in extensions:
autostarts.extend(extension.get_children(Autostart))
return autostarts
def _start_autostarts(self):
""" Start the plugins for the autostart extension point.
"""
workbench = self.workbench
for autostart in self._get_autostarts():
workbench.get_plugin(autostart.plugin_id)
def _stop_autostarts(self):
""" Stop the plugins for the autostart extension point.
"""
workbench = self.workbench
for autostart in reversed(self._get_autostarts()):
plugin = workbench.get_plugin(autostart.plugin_id)
plugin.stop()
def _on_branding_updated(self, change):
""" The observer for the branding extension point.
"""
self._refresh_branding()
def _on_actions_updated(self, change):
""" The observer for the actions extension point.
"""
self._refresh_actions()
def _bind_observers(self):
""" Setup the observers for the plugin.
"""
workbench = self.workbench
point = workbench.get_extension_point(BRANDING_POINT)
point.observe('extensions', self._on_branding_updated)
point = workbench.get_extension_point(ACTIONS_POINT)
point.observe('extensions', self._on_actions_updated)
def _unbind_observers(self):
""" Remove the observers for the plugin.
"""
workbench = self.workbench
point = workbench.get_extension_point(BRANDING_POINT)
point.unobserve('extensions', self._on_branding_updated)
point = workbench.get_extension_point(ACTIONS_POINT)
point.unobserve('extensions', self._on_actions_updated)
|