Created
January 20, 2015 20:45
-
-
Save jason-s/7252d9181e9f2facb0cf to your computer and use it in GitHub Desktop.
enaml popups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
os.environ['QT_API'] = 'pyside' | |
from PySide.QtCore import QTimer, QObject | |
from atom.api import Atom, Event, Bool | |
from enaml.core.api import Include | |
from enaml.widgets.api import ( | |
Window, Container, PopupView, Form, Label, PushButton | |
) | |
class Model(Atom): | |
busy = Bool() | |
class SandwichController(object): | |
steps = ( | |
('Preparing bread',1.2), | |
('Adding meat to the sandwich',2.0), | |
('Adding cheese to the sandwich',2.0), | |
('Toasting the sandwich',5.0), | |
('Adding vegetables to the sandwich',2.0), | |
('Your sandwich is ready',0) | |
) | |
def __init__(self, view): | |
self.view = view | |
self.popup = self._newPopup() | |
def _newPopup(self): | |
p = NotificationPopup(self.view, window_type='window') | |
p.timeout = 1.0 | |
return p | |
def step(self): | |
msg, time = self.steps[self.state] | |
self.displayMessage(msg) | |
def timerdone(): | |
self.state += 1 | |
self.step() | |
if time > 0: | |
QTimer.singleShot(int(time*1000), timerdone) | |
else: | |
self.done() | |
def start(self): | |
self.view.model.busy = True | |
self.state = 0 | |
self.step() | |
def displayMessage(self, msg): | |
if self.popup.visible: | |
self.popup.close() | |
self.popup = self._newPopup() | |
self.popup.msg = msg | |
self.popup.show() | |
def done(self): | |
self.view.model.busy = False | |
enamldef NotificationPopup(PopupView): | |
attr msg = '' | |
foreground = 'white' | |
background = 'rgba(30, 30, 30, 0.85)' | |
window_type = 'tool_tip' | |
parent_anchor = (1.0, 1.0) | |
anchor = (1.0, 1.0) | |
offset = (-10, -10) | |
timeout = 5 | |
fade_in_duration = 500 | |
fade_out_duration = 500 | |
Container: | |
Label: | |
foreground = 'white' | |
text << msg | |
align = 'center' | |
def make_sandwich(view): | |
if not view.model.busy: | |
SandwichController(view).start() | |
enamldef Main(Window): win: | |
initial_size = (400, 400) | |
attr model = Model() | |
Container: | |
PushButton: | |
text = 'Make Sandwich' | |
enabled << not model.busy | |
clicked :: make_sandwich(win) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment