1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
3
# Author: Thomi Richards
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
11
from time import sleep
13
from autopilot.keybindings import KeybindingsHelper
14
from autopilot.emulators.unity import get_state_by_path, make_introspection_object
15
from autopilot.emulators.X11 import Keyboard, Mouse
17
# even though we don't use these directly, we need to make sure they've been
18
# imported so the classes contained are registered with the introspection API.
19
from autopilot.emulators.unity.icons import *
21
logger = logging.getLogger(__name__)
24
# TODO: THis class needs to be ported to the new-style emulator classes.
25
# See launcher.py or dash.py for reference.
26
class Switcher(KeybindingsHelper):
27
"""Interact with the Unity switcher."""
30
super(Switcher, self).__init__()
31
self._keyboard = Keyboard()
35
"""Start the switcher with alt+tab."""
36
logger.debug("Initiating switcher with Alt+Tab")
37
self.keybinding_hold("switcher/reveal_normal")
38
self.keybinding_tap("switcher/reveal_normal")
41
def initiate_detail_mode(self):
42
"""Start detail mode with alt+`"""
43
logger.debug("Initiating switcher detail mode with Alt+`")
44
self.keybinding_hold("switcher/reveal_details")
45
self.keybinding_tap("switcher/reveal_details")
48
def initiate_all_mode(self):
49
"""Start switcher in 'all workspaces' mode.
51
Shows apps from all workspaces, instead of just the current workspace.
53
logger.debug("Initiating switcher in 'all workspaces' mode.")
54
self.keybinding_hold("switcher/reveal_all")
55
self.keybinding_tap("switcher/reveal_all")
58
def initiate_right_arrow(self):
59
"""Impropperly attempt to start switcher."""
60
logger.debug("Initiating switcher with Alt+Right (should not work)")
61
self.keybinding_hold("switcher/reveal_impropper")
62
self.keybinding_tap("switcher/right")
66
"""Stop switcher without activating the selected icon."""
67
logger.debug("Terminating switcher.")
68
self.keybinding("switcher/cancel")
69
self.keybinding_release("switcher/reveal_normal")
72
"""Stop switcher without activating the selected icon and releasing the keys."""
73
logger.debug("Cancelling switcher.")
74
self.keybinding("switcher/cancel")
77
"""Stop switcher and activate the selected icon."""
78
logger.debug("Stopping switcher")
79
self.keybinding_release("switcher/reveal_normal")
82
"""Move to the next icon."""
83
logger.debug("Selecting next item in switcher.")
84
self.keybinding("switcher/next")
86
def previous_icon(self):
87
"""Move to the previous icon."""
88
logger.debug("Selecting previous item in switcher.")
89
self.keybinding("switcher/prev")
91
def next_icon_mouse(self):
92
"""Move to the next icon using the mouse scroll wheel"""
93
logger.debug("Selecting next item in switcher with mouse scroll wheel.")
95
self._mouse.release(6)
97
def previous_icon_mouse(self):
98
"""Move to the previous icon using the mouse scroll wheel"""
99
logger.debug("Selecting previous item in switcher with mouse scroll wheel.")
101
self._mouse.release(7)
103
def show_details(self):
104
"""Show detail mode."""
105
logger.debug("Showing details view.")
106
self.keybinding("switcher/detail_start")
108
def hide_details(self):
109
"""Hide detail mode."""
110
logger.debug("Hiding details view.")
111
self.keybinding("switcher/detail_stop")
113
def next_detail(self):
114
"""Move to next detail in the switcher."""
115
logger.debug("Selecting next item in details mode.")
116
self.keybinding("switcher/detail_next")
118
def previous_detail(self):
119
"""Move to the previous detail in the switcher."""
120
logger.debug("Selecting previous item in details mode.")
121
self.keybinding("switcher/detail_prev")
123
def __get_icon(self, index):
124
return self.__get_model()['Children'][index][1][0]
127
def current_icon(self):
128
"""Get the currently-selected icon."""
129
if not self.get_is_visible:
131
model = self.__get_model()
132
sel_idx = self.get_selection_index()
134
return make_introspection_object(model['Children'][sel_idx])
138
def get_icon_name(self, index):
139
return self.__get_icon(index)['tooltip-text']
141
def get_icon_desktop_file(self, index):
143
return self.__get_icon(index)['desktop-file']
147
def get_model_size(self):
148
return len(self.__get_model()['Children'])
150
def get_selection_index(self):
151
return int(self.__get_model()['selection-index'])
153
def get_last_selection_index(self):
154
return bool(self.__get_model()['last-selection-index'])
156
def get_is_visible(self):
157
return bool(self.__get_controller()['visible'])
159
def get_is_in_details_mode(self):
160
"""Return True if the SwitcherView is in details mode."""
161
return bool(self.__get_model()['detail-selection'])
163
def get_switcher_icons(self):
164
"""Get all icons in the switcher model.
166
The switcher needs to be initiated in order to get the model.
170
model = get_state_by_path('//SwitcherModel')[0]
171
for child in model['Children']:
172
icon = make_introspection_object(child)
177
def __get_model(self):
178
return get_state_by_path('/Unity/SwitcherController/SwitcherModel')[0]
180
def __get_controller(self):
181
return get_state_by_path('/Unity/SwitcherController')[0]