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
|
#------------------------------------------------------------------------------
# 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 enaml.layout.api import HSplitLayout, InsertItem
from enaml.widgets.api import Container, DockArea, DockItem, Html
from enaml.workbench.api import Extension, PluginManifest
from enaml.workbench.core.api import Command
from enaml.workbench.ui.api import ActionItem, MenuItem
print('Imported Persistent View!')
class PickableDockItem(DockItem):
""" A custom pickable dock item class.
"""
def __getstate__(self):
""" Get the pickle state for the dock item.
This method saves the necessary state for the dock items used
in this example. Different applications will have different
state saving requirements.
The default __setstate__ method provided on the Atom base class
provides sufficient unpickling behavior.
"""
return {'name': self.name, 'title': self.title}
class PickableDockArea(DockArea):
""" A custom pickable dock area class.
"""
def get_save_items(self):
""" Get the list of dock items to save with this dock area.
"""
return [c for c in self.children if isinstance(c, PickableDockItem)]
def __getstate__(self):
""" Get the pickle state for the dock area.
This method saves the necessary state for the dock area used
in this example. Different applications will have different
state saving requirements.
"""
state = {
'name': self.name,
'layout': self.save_layout(),
'items': self.get_save_items(),
}
return state
def __setstate__(self, state):
""" Restore the state of the dock area.
"""
self.name = state['name']
self.layout = state['layout']
self.insert_children(None, state['items'])
enamldef Pane(PickableDockItem):
""" A pickable dock item with default Html content.
"""
Container:
Html:
source = '<h1><center>%s</center></h1>' % title
def create_new_area():
""" Create a new pickable dock area with two child panes.
"""
area = PickableDockArea(name='the_dock_area')
Pane(area, name='first', title='Pane 0')
Pane(area, name='second', title='Pane 1')
area.layout = HSplitLayout('first', 'second')
return area
def new_item(event):
""" The handler for the 'sample.persistent.new_item' command.
"""
ui = event.workbench.get_plugin('enaml.workbench.ui')
area = ui.workspace.content.find('the_dock_area')
count = len(area.dock_items())
name = '_pane_%d' % count
title = 'Pane %d' % count
item = Pane(area, name=name, title=title)
area.update_layout(InsertItem(item=item.name))
def destroy_items(event):
""" The handler for the 'sample.persistent.destroy_items' command.
"""
ui = event.workbench.get_plugin('enaml.workbench.ui')
area = ui.workspace.content.find('the_dock_area')
for item in area.dock_items():
item.destroy()
enamldef PersistentManifest(PluginManifest):
""" The manifest which is registered when the view is loaded.
This manifest contributes extra menu items to the menu bar and
new commands for manipulating the dock area items.
"""
id = 'sample.persistent'
Extension:
id = 'commands'
point = 'enaml.workbench.core.commands'
Command:
id = 'sample.persistent.new_item'
handler = new_item
Command:
id = 'sample.persistent.destroy_items'
handler = destroy_items
Extension:
id = 'actions'
point = 'enaml.workbench.ui.actions'
MenuItem:
path = '/debug'
label = 'Debug'
after = 'file'
MenuItem:
path = '/options'
label = 'Options'
MenuItem:
path = '/tools'
label = 'Tools'
before = 'workspace'
ActionItem:
path = '/debug/something'
label = 'Something'
ActionItem:
path = '/options/something_else'
label = 'Something Else'
ActionItem:
path = '/tools/destroy_items'
label = 'Destroy Items'
shortcut = 'Ctrl+X'
command = 'sample.persistent.destroy_items'
ActionItem:
path = '/file/new'
label = 'New'
shortcut = 'Ctrl+N'
group = 'user'
command = 'sample.persistent.new_item'
|