-
Notifications
You must be signed in to change notification settings - Fork 1
/
scaler.py
executable file
·128 lines (100 loc) · 4.62 KB
/
scaler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
import json
import os
import platform
import sys
from pathlib import Path
import subprocess
version = '2.0.0'
def error(message):
print(message)
sys.exit(1)
def apply_gnome(gnome_settings):
display_scale = 'display_scale' in gnome_settings and gnome_settings['display_scale']
font_scale = 'font_scale' in gnome_settings and gnome_settings['font_scale']
icon_size = 'icon_size' in gnome_settings and gnome_settings['icon_size']
cursor_size = 'cursor_size' in gnome_settings and gnome_settings['cursor_size']
if display_scale:
if 0 == os.system(f"gsettings set org.gnome.desktop.interface scaling-factor {display_scale}"):
print(f"Gnome display scale set to {display_scale}")
else:
error(
f"Error: Gnome display scale could not be set to {display_scale}")
if font_scale:
if 0 == os.system(f"gsettings set org.gnome.desktop.interface text-scaling-factor {font_scale}"):
print(f"Gnome font scale set to {font_scale}")
else:
error(
f"Error: Gnome font scale could not be set to {font_scale}")
if icon_size:
if 0 == os.system(f"gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size {icon_size}"):
print(f"Gnome icon size set to {icon_size} px")
else:
error(
f"Error: Gnome icon size could not be set to {icon_size}")
if cursor_size:
if 0 == os.system(f"gsettings set org.gnome.desktop.interface cursor-size {cursor_size}"):
print(f"Gnome cursor size set to {cursor_size} px")
else:
error(
f"Error: Gnome cursor size could not be set to {cursor_size}")
def apply_firefox(firefox_settings, firefox_config):
firefox_profile_dir = f"{Path.home()}/snap/firefox/common/.mozilla/firefox/{firefox_config['profile_folder']}/"
user_js_path = firefox_profile_dir + 'user.js'
new_line = f"user_pref('layout.css.devPixelsPerPx', '{firefox_settings['scale']}');\n"
lines = []
if not os.path.isfile(user_js_path):
lines.append(new_line)
else:
with open(user_js_path, 'r') as user_js_file:
lines = user_js_file.readlines()
if len(lines) == 0:
lines.append(new_line)
else:
for index, line in enumerate(lines):
if line.startswith('user_pref'):
lines[index] = new_line
with open(user_js_path, 'w+') as user_js_file:
user_js_file.writelines(lines)
print(f"Firefox scaling set to {firefox_settings['scale']}")
def apply_chromium(chromium_settings):
chromium_path = '/var/lib/snapd/desktop/applications/chromium_chromium.desktop'
chromium_result = subprocess.run(
['sudo', 'sed', '-i', f"s/\/snap\/bin\/chromium.*%U/\/snap\/bin\/chromium --force-device-scale-factor={chromium_settings['scale']} %U/", '/var/lib/snapd/desktop/applications/chromium_chromium.desktop'])
if chromium_result.returncode == 0:
print(
f"Chromium scaling set to {chromium_settings['scale']}")
else:
error(
f"Error setting chromium scale in: {chromium_path}")
if __name__ == '__main__':
envVars = os.environ.copy()
if(
platform.system() != 'Linux'
or 'XDG_CURRENT_DESKTOP' not in envVars
or (envVars['XDG_CURRENT_DESKTOP'] != 'Unity' and "GNOME" not in envVars['XDG_CURRENT_DESKTOP'])
):
error('This program is only useful on Linux with Gnome desktop.')
settings_path = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'settings.json')
if not os.path.isfile(settings_path):
error('Error: No settings.json found.')
if len(sys.argv) < 2:
error('Error: You did not specify a preset')
chosen_preset = sys.argv[1]
print(f"gnome-app-scaler v. {version}\n")
with open(settings_path, 'r') as settings_file:
settings_parsed = json.loads(settings_file.read())
presets = settings_parsed['presets']
config = settings_parsed['config']
if chosen_preset not in presets:
error('Error: Preset does not exist.')
for app_name in presets[chosen_preset]:
if app_name == 'gnome':
apply_gnome(presets[chosen_preset]['gnome'])
elif app_name == 'firefox':
apply_firefox(firefox_settings=presets[chosen_preset]['firefox'],
firefox_config=config['firefox'])
elif app_name == 'chromium':
apply_chromium(presets[chosen_preset]['chromium'])
print('\nRestart Firefox & Chromium to see the changes.')