Skip to content

Instantly share code, notes, and snippets.

@rynbrd
Created August 10, 2012 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rynbrd/3316399 to your computer and use it in GitHub Desktop.
Save rynbrd/3316399 to your computer and use it in GitHub Desktop.
Toggle interface scaling in Gnome3 and Google Chrome
#!/bin/env python3
import os
import sys
import json
import subprocess
from gi.repository import Gio
class ScaleError(Exception):
"""An error to raise on scaling error."""
def __init__(self, message, cause=None):
Exception.__init__(self, message, cause)
self.message = message
self.cause = cause
class GnomeScale:
"""Adjust Gnome text scaling."""
def __init__(self, small=1.0, large=1.5):
"""
Initializes Gnome settings object and scaling factors.
small -- The scaling factor for a small interface.
large -- The scaling factor for a large interface.
"""
self.small = small
self.large = large
self.settings = Gio.Settings.new('org.gnome.desktop.interface')
def get(self):
"""Get the Gnome text scaling factor."""
return self.settings.get_double('text-scaling-factor')
def set(self, scale):
"""Set the Gnome text scaling factor."""
self.settings.set_double('text-scaling-factor', scale)
def set_small(self):
"""Scale Gnome to the large size."""
self.set(self.small)
def set_large(self):
"""Scale Gnome to the small size."""
self.set(self.large)
def toggle(self):
"""Toggle scaling between small and large."""
if self.get() == self.small:
self.set_large()
return self.large
else:
self.set_small()
return self.small
class ChromeScale:
"""Adjust Google Chrome zoom level."""
default_preferences = '.config/google-chrome/Default/Preferences'
def __init__(self, small=0.0, large=3.069389038663465, preferences=None, hostzoom=None):
"""
Initializes preferences and scaling factors.
small -- The scaling factor for a small interface.
large -- The scaling factor for a large interface.
preferences -- The path to the Chrome preferences file.
"""
if preferences is None:
preferences = self.default_preferences
if preferences[0] != '/':
preferences = '%s/%s' % (os.getenv("HOME"), preferences)
if hostzoom is None:
hostzoom = {}
self.preferences = preferences
self.hostzoom = hostzoom
self.data = None
self.small = small
self.large = large
self.load()
@classmethod
def is_running(cls):
"""Check if Chrome is running."""
cmd = "ps ax | awk '{print $5}' | grep -q '/chrome$'"
return subprocess.call(cmd, shell=True) == 0
def load(self):
"""Load the Chrome preferences."""
try:
self.data = json.load(open(self.preferences, 'r'))
except IOError as e:
raise ScaleError('Unable to open preferences for reading.', e)
def save(self):
"""Save the Chrome preferences."""
try:
json.dump(self.data, open(self.preferences, 'w'), indent=4)
except IOError as e:
raise ScaleError('Unable to open preferences for writing.')
def get(self):
"""Get the scaling factor."""
return self.data['profile']['default_zoom_level']
def set(self, scale):
"""Set the scaling factor."""
self.data['profile']['default_zoom_level'] = scale
def set_small(self):
"""Scale Chrome to the small size."""
self.set(self.small)
def set_large(self):
"""Scale Chrome to the large size."""
self.set(self.large)
def toggle(self):
"""Toggle between small and large interface sizes."""
if self.get() == self.small:
self.set_large()
else:
self.set_small()
def clear(self):
"""Clear unspecified per-host zoom levels."""
self.data['profile']['per_host_zoom_levels'] = self.hostzoom
class Scale:
"""Scale the UI."""
def __init__(self, hosts=None):
"""
Initialize the scalers.
hosts -- A dict of hostnames with zoom values to set in Chrome.
"""
self.gnome = GnomeScale()
self.chrome = ChromeScale(hostzoom=hosts)
def set_small(self):
"""Set all scalers to small."""
self.gnome.set_small()
if self.chrome.is_running():
sys.stderr.write("warning: Chrome is running, not setting zoom level.\n")
else:
self.chrome.set_small()
self.chrome.save()
def set_large(self):
"""Set all scalers to large."""
self.gnome.set_large()
if self.chrome.is_running():
sys.stderr.write("warning: Chrome is running, not setting zoom level.\n")
else:
self.chrome.set_large()
self.chrome.save()
def toggle(self):
"""Toggle all scalers between small and large."""
if self.gnome.get() == self.gnome.small:
self.set_large()
else:
self.set_small()
def scale(hosts, size=None):
"""
Scale the interface.
hosts -- A dict of hosts to scale a particular way in Chrome.
size -- The size to scale to. Either 'small' or 'large'. Otherwise will
toggle the scale between the two.
"""
scaler = Scale(hosts)
try:
if size == 'small':
scaler.set_small()
elif size == 'large':
scaler.set_large()
else:
scaler.toggle()
except ScaleError as e:
print(e.message)
return False
return True
if __name__ == '__main__':
size = None
hosts = {"grooveshark.com": 0.0}
if len(sys.argv) > 1:
size = sys.argv[1]
res = scale(hosts, size)
if not res:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment