Skip to content
This repository has been archived by the owner on Oct 27, 2019. It is now read-only.

Commit

Permalink
Redone icon selection mechanism to fix the KDE > 5.8 issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebrait committed Oct 23, 2016
1 parent 6d84f83 commit a5ff3c5
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions usr/lib/prime-indicator/prime-indicator
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ from gi.repository import AppIndicator3

APP_NAME = "PRIME Indicator"
HOME_DIR = os.getenv("HOME") # type:str
LIB_PATH = "/usr/lib/prime-indicator"
SCRIPT_CMD = "sudo " + LIB_PATH + "/gpuswitcher"
LIB_PATH = "/usr/lib/prime-indicator/"
SCRIPT_CMD = "sudo " + LIB_PATH + "gpuswitcher"
STD_ICONS_FOLDER = LIB_PATH + "icons/"
USER_ICONS_FOLDER = HOME_DIR + "/.config/prime-indicator/icons/"
CONFIG_PATH = HOME_DIR + "/.config/prime-indicator/prime-indicator.cfg"
TEMP_ICON_PATH = HOME_DIR + "/.config/prime-indicator/icons"
PRIME_SELECT_PATH = "/usr/bin/prime-select"
NVIDIA_SETTINGS_PATH = "/usr/bin/nvidia-settings"
PRIME_INDICATOR_PREFIX = "prime-indicator-"
SYMBOLIC_SUFFIX = "-symbolic"
THEMED_SUFFIX = "-themed"

NVIDIA = "nvidia"
INTEL = "intel"

NVIDIA_COLOR = STD_ICONS_FOLDER + "prime-indicator-nvidia.svg"
INTEL_COLOR = STD_ICONS_FOLDER + "prime-indicator-intel.svg"
NVIDIA_SYMBOLIC = USER_ICONS_FOLDER + "prime-indicator-nvidia-symbolic-themed.svg"
INTEL_SYMBOLIC = USER_ICONS_FOLDER + "prime-indicator-intel-symbolic-themed.svg"


class Indicator:
def __init__(self):
Expand All @@ -68,14 +72,22 @@ class Indicator:
self.custom_color = None

if self.active_gpu in [INTEL, NVIDIA]:
self.icon_name = self.active_gpu
if self.theme_icons == "color":
self.icon_name = PRIME_INDICATOR_PREFIX + self.icon_name
if self.theme_icons == "theme-default":
theme = Gtk.IconTheme.get_default()
if theme.has_icon(self.active_gpu):
self.icon_name = theme.lookup_icon(self.active_gpu, max(theme.get_icon_sizes(self.active_gpu)),
0).get_filename()
else:
self.icon_name = INTEL_COLOR
print("ALERT: GTK Icon Theme does not provide an icon for " + (
"Intel" if self.active_gpu == INTEL else "NVIDIA") + ". Using default color icon instead!")
elif self.theme_icons == "color":
self.icon_name = INTEL_COLOR if self.active_gpu == INTEL else NVIDIA_COLOR
else:
self.icon_name = PRIME_INDICATOR_PREFIX + self.icon_name + SYMBOLIC_SUFFIX + THEMED_SUFFIX
if self.theme_icons.startswith("custom"):
self.custom_color = re.search("custom\((.*)\)", self.theme_icons).group(1)
self.create_themed_icons()
self.icon_name = INTEL_SYMBOLIC if self.active_gpu == INTEL else NVIDIA_SYMBOLIC
self.icon_tooltip_text = "Active graphics card: " + ("Intel" if self.active_gpu == INTEL else "NVIDIA")
else:
self.icon_name = "dialog-error"
Expand Down Expand Up @@ -135,9 +147,8 @@ class Indicator:
item.show()
self.menu.append(item)

self.icon = AppIndicator3.Indicator.new_with_path(APP_NAME, "",
AppIndicator3.IndicatorCategory.APPLICATION_STATUS,
self.icon_path)
self.icon = AppIndicator3.Indicator.new(APP_NAME, "",
AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
self.icon.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.icon.set_icon(self.icon_name)
self.icon.set_title(self.icon_tooltip_text)
Expand All @@ -156,20 +167,16 @@ class Indicator:
g=int(style_fg_color[1].green * 255),
b=int(style_fg_color[1].blue * 255))

os.makedirs(TEMP_ICON_PATH, exist_ok=True)
with open(LIB_PATH + "/icons/" + PRIME_INDICATOR_PREFIX + NVIDIA + SYMBOLIC_SUFFIX + ".svg", "r") as fin:
with open(TEMP_ICON_PATH + "/" + PRIME_INDICATOR_PREFIX + NVIDIA + SYMBOLIC_SUFFIX + THEMED_SUFFIX + ".svg",
"w") as fout:
os.makedirs(USER_ICONS_FOLDER, exist_ok=True)
with open(STD_ICONS_FOLDER + "prime-indicator-nvidia-symbolic.svg", "r") as fin:
with open(NVIDIA_SYMBOLIC, "w") as fout:
for line in fin:
fout.write(line.replace("#bebebe", fg_color_str))
with open(LIB_PATH + "/icons/" + PRIME_INDICATOR_PREFIX + INTEL + SYMBOLIC_SUFFIX + ".svg", "r") as fin:
with open(TEMP_ICON_PATH + "/" + PRIME_INDICATOR_PREFIX + INTEL + SYMBOLIC_SUFFIX + THEMED_SUFFIX + ".svg",
"w") as fout:
with open(STD_ICONS_FOLDER + "prime-indicator-intel-symbolic.svg", "r") as fin:
with open(INTEL_SYMBOLIC, "w") as fout:
for line in fin:
fout.write(line.replace("#bebebe", fg_color_str))

self.icon_path = TEMP_ICON_PATH

def write_default_config(self):
os.makedirs(os.path.dirname(CONFIG_PATH), exist_ok=True)
self.config.clear()
Expand All @@ -189,7 +196,7 @@ class Indicator:
iconset = self.config.get("Appearance", "iconset")
if iconset not in ["symbolic", "color", "theme-default"] and \
not re.search('^custom\(#([a-fA-F0-9]{2}){3}\)$', iconset) or \
self.config.get("PowerManagement", "enabled") not in ["true", "false"]:
self.config.get("PowerManagement", "enabled") not in ["true", "false"]:
self.write_default_config()
except (configparser.NoSectionError, configparser.NoOptionError):
self.write_default_config()
Expand Down Expand Up @@ -330,10 +337,6 @@ def run() -> None:
Indicator().execute()


def restart() -> None:
os.execv(__file__, sys.argv)


if __name__ == "__main__":

# If nvidia-prime isn't installed or isn't supported, exit cleanly
Expand Down

0 comments on commit a5ff3c5

Please sign in to comment.