view enso/ui/__init__.py @ 50:51181fe08fea

The event manager is now passed-in to primary and mini-message implementations, which decouples the enso.ui.messages package from ui.events.
author Atul Varma <varmaa@toolness.com>
date Mon, 25 Feb 2008 12:39:19 -0600
parents 5e4c680f49a3
children 6d522a046fda
line wrap: on
line source

# Copyright (c) 2008, Humanized, Inc.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of Enso nor the names of its contributors may
#       be used to endorse or promote products derived from this
#       software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY Humanized, Inc. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Humanized, Inc. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# ----------------------------------------------------------------------------
#
#   enso.ui
#
# ----------------------------------------------------------------------------

"""
    The UserInterface module.

    This module encapsulates all parts of the Enso user interface,
    including (but not limited to) the quasimode.
"""

# ----------------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------------

from enso.ui import events
from enso.ui import messages
from enso.ui import quasimode
from enso.ui import commands
import enso.config


# ----------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------

_isUserInterfaceInited = False

def _onEnterEventLoop():
    """
    Executed as soon as we enter the main event loop; at this point,
    we know that the event manager has intialized and is running
    properly. This means that we can now start any threads and perform
    any actions that require the event manager to be running.
    """

    msgXml = enso.config.OPENING_MSG_XML

    if msgXml != "None":
        openingMsg = messages.Message( isPrimary = True,
                                       isMini = False,
                                       fullXml = msgXml )
        messages.messageManager.newMessage( openingMsg )


def _onExitEventLoop():
    """
    Executed as soon as we leave the main event loop.
    """

    pass


def run():
    """
    Runs the user interface and does not return until the user
    interface crashes or finishes properly.
    """

    # UI threads must be started upon the event manager's
    # initialization to ensure that the event manager is already
    # initialized and running when the other UI threads start.
    events.eventManager.registerResponder(
        _onEnterEventLoop,
        "init"
        )

    try:
        events.eventManager.run()
    finally:
        _onExitEventLoop()


def init():
    """
    Initializes the user interface module.  Do not call any functions
    in this module until this function has been called.
    """
    
    global _isUserInterfaceInited
    
    # Events must be initialized first, because other singletons may
    # need to register responders.
    events.init( enso.config.QUASIMODE_KEYCODE )

    commands.init()
    messages.init( events.eventManager )
    quasimode.init()

    _isUserInterfaceInited = True


def isInitialized():
    """
    Returns whether or not this module is initialized.
    """
    
    return _isUserInterfaceInited


def stop():
    """
    Stops the user interface. This can be called multiple times, and
    is also thread-safe.
    """
    
    events.eventManager.stop()


def shutdown():
    """
    Shuts down the user interface module.  Call this once you are done
    using any functions in this module.
    """

    assert isInitialized(), ".shutdown() called before .init() in "\
                            "UserInterface!"

    # Events must be shutdown first, because it may contain lingering
    # references to singletons that registered event responders.
    events.shutdown()
    
    quasimode.shutdown()
    messages.shutdown()
    commands.shutdown()

    global _isUserInterfaceInited
    _isUserInterfaceInited = False