Skip to content

Commit

Permalink
Merge pull request #88 from nucleic/feature-stay-on-top
Browse files Browse the repository at this point in the history
implement always on top and activate window
  • Loading branch information
sccolbert committed Nov 21, 2013
2 parents c0214ce + dc93c44 commit 3ac3e69
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
9 changes: 5 additions & 4 deletions enaml/qt/qt_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from enaml.widgets.dialog import ProxyDialog

from .QtCore import Signal
from .QtCore import Qt, Signal
from .QtGui import QDialog

from .q_window_base import QWindowBase
Expand All @@ -26,7 +26,7 @@ class QWindowDialog(QDialog, QWindowBase):
# it is here only for API compatibility with the base class.
closed = Signal()

def __init__(self, parent=None):
def __init__(self, parent=None, flags=Qt.Widget):
""" Initialize a QWindowDialog.
Parameters
Expand All @@ -35,7 +35,7 @@ def __init__(self, parent=None):
The parent of the dialog.
"""
super(QWindowDialog, self).__init__(parent)
super(QWindowDialog, self).__init__(parent, flags)


class QtDialog(QtWindow, ProxyDialog):
Expand All @@ -48,7 +48,8 @@ def create_widget(self):
""" Create the underlying QFileDialog widget.
"""
self.widget = QWindowDialog(self.parent_widget())
flags = self.creation_flags()
self.widget = QWindowDialog(self.parent_widget(), flags)

def init_widget(self):
""" Initialize the underlying widget.
Expand Down
3 changes: 2 additions & 1 deletion enaml/qt/qt_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def create_widget(self):
""" Create the underlying widget QMainWindow widget.
"""
widget = QCustomMainWindow(self.parent_widget())
flags = self.creation_flags()
widget = QCustomMainWindow(self.parent_widget(), flags)
widget.setDocumentMode(True)
widget.setDockNestingEnabled(True)
self.widget = widget
Expand Down
28 changes: 25 additions & 3 deletions enaml/qt/qt_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
import sys

from atom.api import Typed

from enaml.layout.geometry import Pos, Rect, Size
Expand Down Expand Up @@ -33,7 +35,7 @@ class QWindow(QWindowBase):
on its central widget, unless the user explicitly changes them.
"""
def __init__(self, parent=None):
def __init__(self, parent=None, flags=Qt.Widget):
""" Initialize a QWindow.
Parameters
Expand All @@ -42,7 +44,7 @@ def __init__(self, parent=None):
The parent of the window.
"""
super(QWindow, self).__init__(parent, Qt.Window)
super(QWindow, self).__init__(parent, Qt.Window | flags)

def closeEvent(self, event):
""" Handle the QCloseEvent from the window system.
Expand All @@ -65,11 +67,21 @@ class QtWindow(QtWidget, ProxyWindow):
#--------------------------------------------------------------------------
# Initialization API
#--------------------------------------------------------------------------
def creation_flags(self):
""" A convenience function for getting the creation flags.
"""
flags = Qt.Widget
if self.declaration.always_on_top:
flags |= Qt.WindowStaysOnTopHint
return flags

def create_widget(self):
""" Create the QWindow widget.
"""
self.widget = QWindow(self.parent_widget())
flags = self.creation_flags()
self.widget = QWindow(self.parent_widget(), flags)

def init_widget(self):
""" Initialize the widget.
Expand Down Expand Up @@ -245,6 +257,16 @@ def send_to_back(self):
"""
self.widget.lower()

def activate_window(self):
""" Activate the underlying window widget.
"""
self.widget.activateWindow()
if sys.platform == 'win32':
# For some reason, this needs to be called twice on Windows
# in order to get the taskbar entry to flash.
self.widget.activateWindow()

def center_on_screen(self):
""" Center the window on the screen.
Expand Down
30 changes: 27 additions & 3 deletions enaml/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def send_to_front(self):
def send_to_back(self):
raise NotImplementedError

def activate_window(self):
raise NotImplementedError

def center_on_screen(self):
raise NotImplementedError

Expand Down Expand Up @@ -121,7 +124,8 @@ class Window(Widget):
#: The title bar icon.
icon = d_(Typed(Icon))

#: Whether or not the window remains on top of all others.
#: Whether the window styas 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 window is closed. This event is triggered
Expand Down Expand Up @@ -275,19 +279,39 @@ def restore(self):
self.proxy.restore()

def send_to_front(self):
""" Send the window to the top of the Z order.
""" 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.
""" 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.
Expand Down
3 changes: 2 additions & 1 deletion enaml/wx/wx_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ def create_widget(self):
""" Create the underlying widget wxMainWindow widget.
"""
self.widget = wxMainWindow(self.parent_widget())
style = self.creation_style()
self.widget = wxMainWindow(self.parent_widget(), style=style)

def init_layout(self):
""" Initialize the layout for the underlying widget.
Expand Down
19 changes: 18 additions & 1 deletion enaml/wx/wx_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,21 @@ class WxWindow(WxWidget, ProxyWindow):
#--------------------------------------------------------------------------
# Initialization API
#--------------------------------------------------------------------------
def creation_style(self):
""" A convenience function for getting the creation style.
"""
style = wx.DEFAULT_FRAME_STYLE
if self.declaration.always_on_top:
style |= wx.STAY_ON_TOP
return style

def create_widget(self):
""" Create the underlying wxCustomWindow widget.
"""
self.widget = wxCustomWindow(self.parent_widget())
style = self.creation_style()
self.widget = wxCustomWindow(self.parent_widget(), style=style)

def init_widget(self):
""" Initialize the window control.
Expand Down Expand Up @@ -316,6 +326,13 @@ def send_to_back(self):
"""
self.widget.Lower()

def activate_window(self):
""" Activate the underlying window widget.
"""
# wx makes no distinction between raise and activate
self.widget.Raise()

def center_on_screen(self):
""" Center the window on the screen.
Expand Down

0 comments on commit 3ac3e69

Please sign in to comment.