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
|
#------------------------------------------------------------------------------
# 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 atom.api import (
Atom, Str, Enum, Bool, Event, Coerced, Typed, ForwardTyped, set_default
)
from enaml.core.declarative import d_, observe
from enaml.icon import Icon
from enaml.layout.geometry import Pos, Rect, Size
from .close_event import CloseEvent
from .container import Container
from .widget import Widget, ProxyWidget
class ProxyWindow(ProxyWidget):
""" The abstract definition of a proxy Window object.
"""
#: A reference to the Window declaration.
declaration = ForwardTyped(lambda: Window)
def set_title(self, title):
raise NotImplementedError
def set_modality(self, modality):
raise NotImplementedError
def set_icon(self, icon):
raise NotImplementedError
def position(self):
raise NotImplementedError
def set_position(self, pos):
raise NotImplementedError
def size(self):
raise NotImplementedError
def set_size(self, size):
raise NotImplementedError
def geometry(self):
raise NotImplementedError
def set_geometry(self, rect):
raise NotImplementedError
def frame_geometry(self):
raise NotImplementedError
def minimize(self):
raise NotImplementedError
def is_minimized(self):
raise NotImplementedError
def maximize(self):
raise NotImplementedError
def is_maximized(self):
raise NotImplementedError
def restore(self):
raise NotImplementedError
def send_to_front(self):
raise NotImplementedError
def send_to_back(self):
raise NotImplementedError
def activate_window(self):
raise NotImplementedError
def center_on_screen(self):
raise NotImplementedError
def center_on_widget(self, other):
raise NotImplementedError
def close(self):
raise NotImplementedError
class Window(Widget):
""" A top-level Window component.
A Window component is represents of a top-level visible component
with a frame decoration. It may have at most one child widget which
is dubbed the 'central widget'. The central widget is an instance
of Container and is expanded to fit the size of the window.
A Window does not support features like MenuBars or DockPanes, for
such functionality, use a MainWindow widget.
"""
#: A static set of windows being used by the application. A window
#: adds itself to this list when it is initialized, and removes
#: itself when it is destroyed. This allows toplevel windows with
#: no parent to persist without any other strong references.
windows = set()
#: The titlebar text.
title = d_(Str())
#: The initial position of the window frame. A value of (-1, -1)
#: indicates that the toolkit should choose the initial position.
initial_position = d_(Coerced(Pos, (-1, -1)))
#: The initial size of the window client area. A value of (-1, -1)
#: indicates that the toolkit should choose the initial size.
initial_size = d_(Coerced(Size, (-1, -1)))
#: An enum which indicates the modality of the window. The default
#: value is 'non_modal'.
modality = d_(Enum('non_modal', 'application_modal', 'window_modal'))
#: If this value is set to True, the window will be destroyed on
#: the completion of the `closed` event.
destroy_on_close = d_(Bool(True))
#: The title bar icon.
icon = d_(Typed(Icon))
#: Whether the window stays on top of other windows on the desktop.
#: Changes to this value after the window is shown will be ignored.
always_on_top = d_(Bool(False))
#: An event fired when the user request the window to be closed.
#: This will happen when the user clicks on the "X" button in the
#: title bar button, or when the 'close' method is called. The
#: payload will be a CloseEvent object which will allow code to
#: veto the close event and prevent the window from closing.
closing = d_(Event(CloseEvent), writable=False)
#: An event fired when the window is closed.
closed = d_(Event(), writable=False)
#: Windows are invisible by default.
visible = set_default(False)
#: A reference to the ProxyWindow object.
proxy = Typed(ProxyWindow)
def initialize(self):
""" An overridden initializer method.
This method adds the window to the static set of Windows.
"""
super(Window, self).initialize()
Window.windows.add(self)
def destroy(self):
""" An overridden destructor method.
This method removes the window from the static set of Windows.
"""
super(Window, self).destroy()
Window.windows.discard(self)
#--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
def central_widget(self):
""" Get the central widget defined on the window.
The last `Container` child of the window is the central widget.
"""
for child in reversed(self.children):
if isinstance(child, Container):
return child
def position(self):
""" Get the position of the window frame.
Returns
-------
result : Pos
The current position of the window frame.
"""
if self.proxy_is_active:
return self.proxy.position()
return Pos(-1, -1)
def set_position(self, pos):
""" Set the position of the window frame.
Parameters
----------
pos : Pos
The desired position of the window the window frame.
"""
if self.proxy_is_active:
self.proxy.set_position(pos)
def size(self):
""" Get the size of the window client area.
Returns
-------
result : Size
The current size of the window client area.
"""
if self.proxy_is_active:
return self.proxy.size()
return Size(-1, -1)
def set_size(self, size):
""" Set the size of the window client area.
Parameters
----------
size : Size
The desired size of the window client area.
"""
if self.proxy_is_active:
self.proxy.set_size(size)
def geometry(self):
""" Get the geometry of the window client area.
Returns
-------
result : Rect
The current geometry of the window client area.
"""
if self.proxy_is_active:
return self.proxy.geometry()
return Rect(-1, -1, -1, -1)
def set_geometry(self, rect):
""" Set the geometry of the window client area.
Parameters
----------
rect : Rect
The desired geometry of the window client area.
"""
if self.proxy_is_active:
self.proxy.set_geometry(rect)
def frame_geometry(self):
""" Get the geometry of the window frame.
Returns
-------
result : Rect
The current geometry of the window frame.
"""
if self.proxy_is_active:
return self.proxy.frame_geometry()
return Rect(-1, -1, -1, -1)
def maximize(self):
""" Maximize the window.
"""
if self.proxy_is_active:
self.proxy.maximize()
def is_maximized(self):
""" Get whether the window is maximized.
"""
if self.proxy_is_active:
return self.proxy.is_maximized()
return False
def minimize(self):
""" Minimize the window.
"""
if self.proxy_is_active:
self.proxy.minimize()
def is_minimized(self):
""" Get whether the window is minimized.
"""
if self.proxy_is_active:
return self.proxy.is_minimized()
return False
def restore(self):
""" Restore the window from a maximized or minimized state.
"""
if self.proxy_is_active:
self.proxy.restore()
def send_to_front(self):
""" Send the window to the top of the Z-order.
This will only affect the Z-order of the window relative to the
Z-order of other windows in the same application.
"""
if self.proxy_is_active:
self.proxy.send_to_front()
def send_to_back(self):
""" Send the window to the bottom of the Z-order.
This will only affect the Z-order of the window relative to the
Z-order of other windows in the same application.
"""
if self.proxy_is_active:
self.proxy.send_to_back()
def activate_window(self):
""" Set this window to be the active application window.
This performs the same operation as clicking the mouse on the
title bar of the window, except that it will not effect the Z
order of the window.
On Windows, this will cause the taskbar icon to flash if the
window does not belong to the active application.
"""
if self.proxy_is_active:
self.proxy.activate_window()
def center_on_screen(self):
""" Center the window on the screen.
"""
if self.proxy_is_active:
self.proxy.center_on_screen()
def center_on_widget(self, other):
""" Center this window on another widget.
Parameters
----------
other : Widget
The widget onto which to center this window.
"""
assert isinstance(other, Widget)
if self.proxy_is_active and other.proxy_is_active:
self.proxy.center_on_widget(other)
def close(self):
""" Close the window.
This will cause the window to be hidden, the 'closed' event
to be fired, and the window subsequently destroyed.
"""
if self.proxy_is_active:
self.proxy.close()
def show(self):
""" Show the window to the screen.
This is a reimplemented parent class method which will init
and build the window hierarchy if needed.
"""
if not self.is_initialized:
self.initialize()
if not self.proxy_is_active:
self.activate_proxy()
super(Window, self).show()
#-------------------------------------------------------------------------
# Observers
#--------------------------------------------------------------------------
@observe('title', 'modality', 'icon')
def _update_proxy(self, change):
""" Update the ProxyWindow when the Window data changes.
"""
# The superclass handler implementation is sufficient.
super(Window, self)._update_proxy(change)
|