~ci-train-bot/autopilot/autopilot-ubuntu-yakkety-landing-063

« back to all changes in this revision

Viewing changes to autopilot/emulators/unity/workspace.py

  • Committer: Thomi Richards
  • Date: 2012-05-06 22:45:27 UTC
  • Revision ID: thomi.richards@canonical.com-20120506224527-xh6wixqiw0rarkmh
Imported code from unity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2012 Canonical
 
3
# Author: Thomi Richards
 
4
#
 
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.
 
8
#
 
9
 
 
10
from compizconfig import Plugin, Setting
 
11
 
 
12
from autopilot.globals import global_context
 
13
from autopilot.keybindings import KeybindingsHelper
 
14
from autopilot.utilities import get_desktop_viewport, get_desktop_geometry
 
15
 
 
16
 
 
17
class WorkspaceManager(KeybindingsHelper):
 
18
    """Class to manage switching to different workspaces."""
 
19
 
 
20
    def __init__(self):
 
21
        super(WorkspaceManager, self).__init__()
 
22
        self.refresh_workspace_information()
 
23
 
 
24
    @property
 
25
    def num_workspaces(self):
 
26
        """The number of workspaces configured."""
 
27
        return self._workspaces_wide * self._workspaces_high
 
28
 
 
29
    @property
 
30
    def current_workspace(self):
 
31
        """The current workspace number. 0 <= x < num_workspaces."""
 
32
        vx,vy = get_desktop_viewport()
 
33
        return self._coordinates_to_vp_number(vx, vy)
 
34
 
 
35
    def refresh_workspace_information(self):
 
36
        """Re-read information about available workspaces from compiz and X11."""
 
37
        self._workspaces_wide = self._get_compiz_option("core", "hsize")
 
38
        self._workspaces_high = self._get_compiz_option("core", "vsize")
 
39
        self._desktop_width, self.desktop_height = get_desktop_geometry()
 
40
        self._viewport_width = self._desktop_width / self._workspaces_wide
 
41
        self._viewport_height = self.desktop_height / self._workspaces_high
 
42
 
 
43
    def switch_to(self, workspace_num):
 
44
        """Switch to the workspace specified.
 
45
 
 
46
        ValueError is raised if workspace_num is outside 0<= workspace_num < num_workspaces.
 
47
 
 
48
        """
 
49
        if workspace_num < 0 or workspace_num >= self.num_workspaces:
 
50
            raise ValueError("Workspace number must be between 0 and %d" % self.num_workspaces)
 
51
 
 
52
        current_row, current_col = self._vp_number_to_row_col(self.current_workspace)
 
53
        target_row, target_col = self._vp_number_to_row_col(workspace_num)
 
54
        lefts = rights = ups = downs = 0
 
55
        if current_col > target_col:
 
56
            lefts = current_col - target_col
 
57
        else:
 
58
            rights = target_col - current_col
 
59
        if current_row > target_row:
 
60
            ups = current_row - target_row
 
61
        else:
 
62
            downs = target_row - current_row
 
63
 
 
64
        for i in range(lefts):
 
65
            self.keybinding("workspace/move_left")
 
66
        for i in range(rights):
 
67
            self.keybinding("workspace/move_right")
 
68
        for i in range(ups):
 
69
            self.keybinding("workspace/move_up")
 
70
        for i in range(downs):
 
71
            self.keybinding("workspace/move_down")
 
72
 
 
73
    def _coordinates_to_vp_number(self, vx, vy):
 
74
        """Translate viewport coordinates to a viewport number."""
 
75
        row,col = self._coordinates_to_row_col(vx, vy)
 
76
        return (row * self._workspaces_wide) + col
 
77
 
 
78
    def _coordinates_to_row_col(self, vx, vy):
 
79
        """Translate viewport coordinates to viewport row,col."""
 
80
        row = vy / self._viewport_height
 
81
        col = vx / self._viewport_width
 
82
        return (row,col)
 
83
 
 
84
    def _vp_number_to_row_col(self, vp_number):
 
85
        """Translate a viewport number to a viewport row/col."""
 
86
        row = vp_number / self._workspaces_wide
 
87
        col = vp_number % self._workspaces_wide
 
88
        return (row,col)
 
89
 
 
90
    def _get_compiz_option(self, plugin_name, setting_name):
 
91
        plugin = Plugin(global_context, plugin_name)
 
92
        setting = Setting(plugin, setting_name)
 
93
        return setting.Value