Back to index

unity  6.0.0
workspace.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 
00013 from autopilot.globals import global_context
00014 from autopilot.keybindings import KeybindingsHelper
00015 from autopilot.utilities import (
00016     get_compiz_option,
00017     get_desktop_geometry,
00018     get_desktop_viewport,
00019     )
00020 
00021 
00022 class WorkspaceManager(KeybindingsHelper):
00023     """Class to manage switching to different workspaces."""
00024 
00025     def __init__(self):
00026         super(WorkspaceManager, self).__init__()
00027         self.refresh_workspace_information()
00028 
00029     @property
00030     def num_workspaces(self):
00031         """The number of workspaces configured."""
00032         return self._workspaces_wide * self._workspaces_high
00033 
00034     @property
00035     def current_workspace(self):
00036         """The current workspace number. 0 <= x < num_workspaces."""
00037         vx,vy = get_desktop_viewport()
00038         return self._coordinates_to_vp_number(vx, vy)
00039 
00040     def refresh_workspace_information(self):
00041         """Re-read information about available workspaces from compiz and X11."""
00042         self._workspaces_wide = get_compiz_option("core", "hsize")
00043         self._workspaces_high = get_compiz_option("core", "vsize")
00044         self._desktop_width, self.desktop_height = get_desktop_geometry()
00045         self._viewport_width = self._desktop_width / self._workspaces_wide
00046         self._viewport_height = self.desktop_height / self._workspaces_high
00047 
00048     def switch_to(self, workspace_num):
00049         """Switch to the workspace specified.
00050 
00051         ValueError is raised if workspace_num is outside 0<= workspace_num < num_workspaces.
00052 
00053         """
00054         if workspace_num < 0 or workspace_num >= self.num_workspaces:
00055             raise ValueError("Workspace number must be between 0 and %d" % self.num_workspaces)
00056 
00057         current_row, current_col = self._vp_number_to_row_col(self.current_workspace)
00058         target_row, target_col = self._vp_number_to_row_col(workspace_num)
00059         lefts = rights = ups = downs = 0
00060         if current_col > target_col:
00061             lefts = current_col - target_col
00062         else:
00063             rights = target_col - current_col
00064         if current_row > target_row:
00065             ups = current_row - target_row
00066         else:
00067             downs = target_row - current_row
00068 
00069         for i in range(lefts):
00070             self.keybinding("workspace/move_left")
00071         for i in range(rights):
00072             self.keybinding("workspace/move_right")
00073         for i in range(ups):
00074             self.keybinding("workspace/move_up")
00075         for i in range(downs):
00076             self.keybinding("workspace/move_down")
00077 
00078     def _coordinates_to_vp_number(self, vx, vy):
00079         """Translate viewport coordinates to a viewport number."""
00080         row,col = self._coordinates_to_row_col(vx, vy)
00081         return (row * self._workspaces_wide) + col
00082 
00083     def _coordinates_to_row_col(self, vx, vy):
00084         """Translate viewport coordinates to viewport row,col."""
00085         row = vy / self._viewport_height
00086         col = vx / self._viewport_width
00087         return (row,col)
00088 
00089     def _vp_number_to_row_col(self, vp_number):
00090         """Translate a viewport number to a viewport row/col."""
00091         row = vp_number / self._workspaces_wide
00092         col = vp_number % self._workspaces_wide
00093         return (row,col)