Back to index

unity  6.0.0
switcher.py
Go to the documentation of this file.
00001 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
00002 # Copyright 2012 Canonical
00003 # Author: Thomi Richards
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License version 3, as published
00007 # by the Free Software Foundation.
00008 #
00009 
00010 from __future__ import absolute_import
00011 
00012 import logging
00013 
00014 from autopilot.emulators.X11 import Mouse
00015 from autopilot.keybindings import KeybindingsHelper
00016 
00017 from unity.emulators import UnityIntrospectionObject
00018 # even though we don't use these directly, we need to make sure they've been
00019 # imported so the classes contained are registered with the introspection API.
00020 from unity.emulators.icons import *
00021 
00022 
00023 logger = logging.getLogger(__name__)
00024 
00025 class SwitcherMode():
00026     """Define the possible modes the switcher can be in"""
00027     NORMAL = 0
00028     ALL = 1
00029     DETAIL = 2
00030 
00031 
00032 class Switcher(KeybindingsHelper):
00033     """A class for interacting with the switcher.
00034 
00035     Abstracts out switcher implementation, and makes the necessary functionality available
00036     to consumer code.
00037 
00038     """
00039 
00040     def __init__(self):
00041         super(Switcher, self).__init__()
00042         self._mouse = Mouse()
00043         controllers = SwitcherController.get_all_instances()
00044         assert(len(controllers) == 1)
00045         self.controller = controllers[0]
00046 
00047     @property
00048     def visible(self):
00049         """Is the switcher currently visible
00050 
00051         """
00052         return self.controller.visible
00053 
00054     @property
00055     def icons(self):
00056         """The set of icons in the switcher model
00057 
00058         """
00059         return self.controller.model.icons
00060 
00061     @property
00062     def current_icon(self):
00063         """The currently selected switcher icon"""
00064         return self.icons[self.selection_index]
00065 
00066     @property
00067     def selection_index(self):
00068         """The index of the currently selected icon"""
00069         return self.controller.model.selection_index
00070 
00071     @property
00072     def mode(self):
00073         """Returns the SwitcherMode that the switcher is currently in."""
00074         if not self.visible:
00075             return None
00076         if self.controller.model.detail_selection and not self.controller.model.only_detail_on_viewport:
00077             return SwitcherMode.DETAIL, SwitcherMode.ALL
00078         elif self.controller.model.detail_selection:
00079             return SwitcherMode.DETAIL
00080         elif not self.controller.model.only_detail_on_viewport:
00081             return SwitcherMode.ALL
00082         else:
00083             return SwitcherMode.NORMAL
00084 
00085     def initiate(self, mode=SwitcherMode.NORMAL):
00086         """Initiates the switcher in designated mode. Defaults to NORMAL"""
00087         if mode == SwitcherMode.NORMAL:
00088             logger.debug("Initiating switcher with Alt+Tab")
00089             self.keybinding_hold_part_then_tap("switcher/reveal_normal")
00090             self.visible.wait_for(True)
00091         elif mode == SwitcherMode.DETAIL:
00092             logger.debug("Initiating switcher detail mode with Alt+`")
00093             self.keybinding_hold_part_then_tap("switcher/reveal_details")
00094             self.visible.wait_for(True)
00095         elif mode == SwitcherMode.ALL:
00096             logger.debug("Initiating switcher in 'all workspaces' mode. Ctrl+Alt+Tab")
00097             self.keybinding_hold_part_then_tap("switcher/reveal_all")
00098             self.controller.model.only_detail_on_viewport.wait_for(False)
00099 
00100     def next_icon(self):
00101         """Move to the next icon."""
00102         logger.debug("Selecting next item in switcher.")
00103         self.keybinding("switcher/next")
00104 
00105     def previous_icon(self):
00106         """Move to the previous icon."""
00107         logger.debug("Selecting previous item in switcher.")
00108         self.keybinding("switcher/prev")
00109 
00110     def cancel(self):
00111         """Stop switcher without activating the selected icon and releasing the keys.
00112 
00113         NOTE: Does not release Alt.
00114 
00115         """
00116         logger.debug("Cancelling switcher.")
00117         self.keybinding("switcher/cancel")
00118         self.controller.visible.wait_for(False)
00119 
00120     def terminate(self):
00121         """Stop switcher without activating the selected icon."""
00122         logger.debug("Terminating switcher.")
00123         self.keybinding("switcher/cancel")
00124         self.keybinding_release("switcher/reveal_normal")
00125         self.controller.visible.wait_for(False)
00126 
00127     def select(self):
00128         """Stop switcher and activate the selected icon."""
00129         logger.debug("Stopping switcher")
00130         self.keybinding_release("switcher/reveal_normal")
00131         self.controller.visible.wait_for(False)
00132 
00133     def next_via_mouse(self):
00134         """Move to the next icon using the mouse scroll wheel."""
00135         logger.debug("Selecting next item in switcher with mouse scroll wheel.")
00136         self._mouse.press(6)
00137         self._mouse.release(6)
00138 
00139     def previous_via_mouse(self):
00140         """Move to the previous icon using the mouse scroll wheel."""
00141         logger.debug("Selecting previous item in switcher with mouse scroll wheel.")
00142         self._mouse.press(7)
00143         self._mouse.release(7)
00144 
00145     @property
00146     def detail_selection_index(self):
00147         """The index of the currently selected detail"""
00148         return self.controller.model.detail_selection_index
00149 
00150     @property
00151     def detail_current_count(self):
00152         """The number of shown details"""
00153         return self.controller.model.detail_current_count
00154 
00155     def show_details(self):
00156         """Show detail mode."""
00157         logger.debug("Showing details view.")
00158         self.keybinding("switcher/detail_start")
00159         self.controller.model.detail_selection.wait_for(True)
00160 
00161     def hide_details(self):
00162         """Hide detail mode."""
00163         logger.debug("Hiding details view.")
00164         self.keybinding("switcher/detail_stop")
00165         self.controller.model.detail_selection.wait_for(False)
00166 
00167     def next_detail(self):
00168         """Move to next detail in the switcher."""
00169         logger.debug("Selecting next item in details mode.")
00170         self.keybinding("switcher/detail_next")
00171 
00172     def previous_detail(self):
00173         """Move to the previous detail in the switcher."""
00174         logger.debug("Selecting previous item in details mode.")
00175         self.keybinding("switcher/detail_prev")
00176 
00177 
00178 class SwitcherController(UnityIntrospectionObject):
00179     """An emulator class for interacting with the switcher controller."""
00180 
00181     @property
00182     def view(self):
00183         return self.get_children_by_type(SwitcherView)[0]
00184 
00185     @property
00186     def model(self):
00187         return self.get_children_by_type(SwitcherModel)[0]
00188 
00189 
00190 class SwitcherView(UnityIntrospectionObject):
00191     """An emulator class for interacting with with SwitcherView."""
00192 
00193 
00194 class SwitcherModel(UnityIntrospectionObject):
00195     """An emulator class for interacting with the SwitcherModel."""
00196 
00197     @property
00198     def icons(self):
00199         return self.get_children_by_type(SimpleLauncherIcon)
00200