diff --git a/msi-keyboard-color-selector.py b/msi-keyboard-color-selector.py new file mode 100755 index 0000000..c484148 --- /dev/null +++ b/msi-keyboard-color-selector.py @@ -0,0 +1,194 @@ +#!/usr/bin/python + +import sys +import os +import re +from PySide6.QtSvg import QSvgRenderer +from PySide6.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QInputDialog, QWidget, QVBoxLayout, QPushButton, QLabel +from PySide6.QtGui import QIcon, QAction, QPixmap, QPainter, QColor +from PySide6.QtCore import Qt, QTimer +import base64 + +class NotificationWindow(QWidget): + def __init__(self): + super().__init__() + self.setWindowTitle("Notification") + self.setGeometry(200, 200, 400, 200) + layout = QVBoxLayout() + self.label = QLabel("You clicked the tray icon!") + ok_button = QPushButton("OK") + ok_button.clicked.connect(self.close) + layout.addWidget(self.label) + layout.addWidget(ok_button) + self.setLayout(layout) + +class SystemTrayApp: + + # Only these models are supported for now + # GE63, GE73, GE75, GS63, GS73, GS75, GX63, GT63, GL63, GS65 + msi_model = "GS65" + + def __init__(self): + self.app = QApplication(sys.argv) + self.app.setQuitOnLastWindowClosed(False) + + self.notification_window = NotificationWindow() + + # Create the system tray icon + self.tray = QSystemTrayIcon() + + # - To load from a file + #self.tray.setIcon(QIcon("Linux-icon.png")) + + # - To load from embedded svg file + renderer = QSvgRenderer() + renderer.load(SystemTrayApp.svg_systray_icon.encode('utf-8')) + pixmap = QPixmap(24, 24) + pixmap.fill(QColor(0,0,0,0)) # QColor(0,0,0,0) is for transparency + painter = QPainter(pixmap) + renderer.render(painter) + painter.end() + self.tray.setIcon(QIcon(pixmap)) + + self.tray.setToolTip("MSI Keyboard Color Selector") + self.tray.setVisible(True) + + # Create the menu + self.menu = QMenu() + + # Add 10 options to the menu + self.create_actions() + + # Set the menu for the system tray icon + self.tray.setContextMenu(self.menu) + + # When the icon is cliced call tray_icon_activated method. + self.tray.activated.connect(self.tray_icon_activated) + + def create_actions(self): + # These presets are defined in the msi-perkeyrgb program + #aqua chakra default disco drain freeway plain rainbow-split roulette + actions = [ + ("Aqua", self.aqua), + ("Chakra", self.chakra), + ("Default", self.default), + ("Disco", self.disco), + ("Drain", self.drain), + ("Freeway", self.freeway), + ("Plain", self.plain), + ("Rainbow-Split", self.rainbow_split), + ("Roulette", self.roulette), + ("Custom Hex HTML Color", self.html_color), + ("Quit", self.app.quit) + ] + + for label, func in actions: + action = QAction(label, self.tray) + action.triggered.connect(func) + self.menu.addAction(action) + + def aqua(self): + print("Aqua preset selected") + self.tray.showMessage("Notification", "Aqua Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p aqua" + os.system(command) + + def chakra(self): + print("Chakra preset selected") + self.tray.showMessage("Notification", "Chakra Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p chakra" + os.system(command) + + def default(self): + print("Default preset selected") + self.tray.showMessage("Notification", "Default Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p default" + os.system(command) + + def disco(self): + print("Disco preset selected") + self.tray.showMessage("Notification", "Disco Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p disco" + os.system(command) + + def drain(self): + print("Drain preset selected") + self.tray.showMessage("Notification", "Drain Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p drain" + os.system(command) + + def freeway(self): + print("Freeway preset selected") + self.tray.showMessage("Notification", "Freeway Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p freeway" + os.system(command) + + def plain(self): + print("Plain preset selected") + self.tray.showMessage("Notification", "Plain Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p plain" + os.system(command) + + def rainbow_split(self): + print("Rainbow-Split preset selected") + self.tray.showMessage("Notification", "Rainbow-Split Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p rainbow-split" + os.system(command) + + def roulette(self): + print("Roulette preset selected") + self.tray.showMessage("Notification", "Roulette Preset Selected", QSystemTrayIcon.Information, 3000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -p roulette" + os.system(command) + + def html_color(self): + text, ok = QInputDialog.getText(None, "Input", "Enter a Hex HTML color: Example: 00ff00 for Green:") + if ok: + pattern = r'^[0-9A-Fa-f]{6}$' + if (re.match(pattern, text)): + print("Custom Hex HTML color: {text} selected") + notification_message = "Custom Hex HTML Color '" + text + "' selected" + self.tray.showMessage("Notification", notification_message, QSystemTrayIcon.Information, 4000) + command = "msi-perkeyrgb -m " + SystemTrayApp.msi_model + " -s " + text.lower() + os.system(command) + else: + print("Invalid Hexadecimal HTML color entered") + print("Just enter a RGB color in hexadecimal without the #") + print("Forexample:") + print("FF0000 = Red") + print("00FF00 = Green") + print("0000FF = Blue") + + def tray_icon_activated(self, reason): + print("tray_icon_activated has been called") + # if reason == QSystemTrayIcon.Trigger: + # self.notification_window.show() + + def run(self): + sys.exit(self.app.exec()) + + svg_systray_icon = """ + + + + + + + + + + + + + + + + + + + + """ + +if __name__ == "__main__": + tray_app = SystemTrayApp() + tray_app.run()