How to show Unity launcher in only one workspace?

Like many things, it does not exist as far as I know, BUT, it can be done with a little creativity and the right tools.

enter image description here

How it can be done

Assuming you are on 14.04 (with python3), you can use a script to run in the background, that keeps track of your current viewport and sets the launcher to autohide or not, depending on that current viewport.

  • What you need to do first is install wmctrl:

    sudo apt-get install wmctrl
    

    We need wmctrl to get information about the total size of all viewports and to be able to read information about the current section we are in.

  • Once that is done, copy the script below into an empty file and safe it as autohide_launcher.py (keep the name like that) and make it executable(!).

    In the line hide_launcher, decide for wich viewports you want to autohide the launcher (set "True"), and use the correct number of entries, corresponding with your number of viewports. The list reads per viewport row, from left to right.

#!/usr/bin/env python3

import subprocess
import time

# set the hide-launcher values for your viewports; in rows/columns
hide_launcher = (False, True, True, True)

# don't change anything below (or it must be the time.sleep(2) interval)
key = " org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ "
pr_get = "gsettings get "; pr_set = "gsettings set "
check = pr_get+key+"launcher-hide-mode"
hide = pr_set+key+"launcher-hide-mode 1"
show = pr_set+key+"launcher-hide-mode 0"

def get_value(command):
    return subprocess.check_output(
        ["/bin/bash", "-c", command]).decode('utf-8').strip()

# get screen resolution
output = get_value("xrandr").split(); idf = output.index("current")
screen_res = (int(output[idf+1]), int(output[idf+3].replace(",", "")))

while True:
    # get total span size all viewports, position data
    wsp_info = get_value("wmctrl -d").strip()
    scr_data = [item.split("x") for item in wsp_info.split(" ") if "x" in item][0]
    # get current position (viewport coordinates)
    VP = eval(wsp_info[wsp_info.find("VP: "):].split(" ")[1])
    # calculated viewports rows / columns
    VP_hor = int(scr_data[0])/int(screen_res[0])
    VP_vert = int(scr_data[1])/int(screen_res[1])
    # calculated viewport positions
    range_hor = [i*screen_res[0] for i in range(int(VP_hor))]
    range_vert = [i*screen_res[1] for i in range(int(VP_vert))]
    viewports = [(h, range_vert[i])for i in range(len(range_vert)) for h in range_hor]
    current_viewport = viewports.index(VP); a_hide = get_value(check)
    if (hide_launcher[current_viewport], a_hide == "0") == (True, True):
        subprocess.Popen(["/bin/bash", "-c", hide])
    elif (hide_launcher[current_viewport], a_hide == "0") == (False, False):
        subprocess.Popen(["/bin/bash", "-c", show])
    else:
        pass
    time.sleep(1)
  • You can start the script by the command:

    /path/to/autohide_launcher.py
    

Toggle autohide-per-viewport on/of

However, it is more convenient to use the script below to have one command to toggle the script on/of.

Copy the script below into an empty file and save it as start_stop.py, in one and the same folder as the autohide_launcher.py script. Make it executable as well(!). Now you can toggle the autohide function with the command

/path/to/start_stop.py

The start/stop script:

#!/usr/bin/env python3

import os
import subprocess

script_dir = os.path.dirname(os.path.abspath(__file__))
cmd = "ps -ef | grep autohide_launcher.py"
run = subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").split("\n")
match = [line for line in run if script_dir+"/"+"autohide_launcher.py" in line]

if len(match) != 0:
    subprocess.Popen(["kill", match[0].split()[1]])
    subprocess.Popen(["notify-send", "autohide per viewport stopped..."])
else:
    subprocess.Popen(["/bin/bash", "-c", script_dir+"/"+"autohide_launcher.py"])
    subprocess.Popen(["notify-send", "autohide per viewport started..."])

enter image description here

Alternative ways to start or stop the script

There are several other ways to toggle the script in a convenient way:

Add the script to your startup apoplications

If you permanently want to run the script in the background:

  • Open Startup Applications and choose "Add".
  • Add the command:

    /path/to/autohide_launcher.py
    
  • Give it a name to your liking

Set a keyboard shortcut to toggle the script

  • Open System Settings and choose: "Keyboard" > "Shortcuts" > "Custom Shortcuts".
  • Create a new shortcut of your choice, with the command:

    /path/to/start_stop.py
    

    Now you can toggle autohide-per-viewport with the key combination.


posted on gist.gisthub


Introduction

The Unity Launcher and Compiz don't have a setting for only showing the launcher on specific viewport, however it is possible to use hide launcher setting to poll for change in viewport and alter that setting accordingly. The top left viewport would be coordinates 0,0 , thus there must be polling for whether or not we are on that viewport - otherwise unset launcher.

The scrip bellow does exactly that.

Getting the script

Source code can be copied from here directly or downloaded from my git repository by running the following commands ( obviously in terminal ):

  1. sudo apt-get install git
  2. cd /opt ; sudo git clone https://github.com/SergKolo/sergrep.git
  3. sudo chmod -R +x sergrep

The file is called toggle_unity_launcher.sh

How the script works

The script can be called without options ( to show launcher only in the top left viewport ) or with -x and -y options to set a specific viewport, like so

 toggle_unity_launcher.sh -x 1366 -y 0

The -h flag will print usage.

Making script run at login

The script can be started at login by adding full path to the file (including the -x and -y options) and arguments into the Startup Applications , like so

enter image description here

Source code

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: April 8th , 2016
# Purpose: Set Unity launcher to show up only on
#          specific viewport. By default - viewport 0,0
# Written for: http://askubuntu.com/q/349247/295286
# Tested on: Ubuntu 14.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

ARGV0="$0"
ARGC=$#

print_usage()
{
  cat << EOF

Copyright 2016 Serg Kolo
Usage: toggle_unity_launcher.sh [-x INT -y INT] [-h]

The script toggles Unity Launcher on user-defined viewport
By default - launcher appears only on 0, 0

-x and -y flags serve to set custom viewport 

Use 'xprop -root -notype _NET_DESKTOP_VIEWPORT' to find
the exact coordinates of a viewport you want to set
EOF
}

get_viewport() 
{
    xprop -root -notype _NET_DESKTOP_VIEWPORT | awk -F '=' '{printf "%s",substr($2,2)}'
} 

set_launcher_mode()
{
  dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode $1

}

poll_viewport_change()
{
  while [ "$(get_viewport)" = "$VIEWPORT" ]  
  do
    set_launcher_mode 0
    sleep 0.25
  done
}

parse_args()
{
  local OPTIND opt
  while getopts "x:y:h" opt
  do
   case ${opt} in
      x) XPOS=${OPTARG} 
        ;;
      y) YPOS=${OPTARG}
        ;;
      h) print_usage
         exit 0
        ;;
     \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    esac
  done
  shift $((OPTIND-1))
}

main()
{
 local XPOS=0
 local YPOS=0
 parse_args "$@"
 local VIEWPORT=$(printf "%s, %s" "$XPOS" "$YPOS"  )  
 while true
 do
  poll_viewport_change 
  set_launcher_mode 1 # happens only when 
                      # previous function exits
  sleep 0.25
 done
}

main "$@"