Skip to content

Commit bb546d5

Browse files
committed
added package
1 parent 293675c commit bb546d5

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

SimpleMonitorControlTray/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import configparser
2+
import os
3+
4+
import SimpleMonitorControlTray.monitorHandler as mH
5+
import SimpleMonitorControlTray.notificationHandler as nH
6+
import SimpleMonitorControlTray.trayHandler as tH
7+
8+
currentPath = os.getcwd()
9+
config_file_path = "config.ini"
10+
assets_folder = "assets"
11+
asset_iconEnabled = "assets\iconEnabled.png"
12+
asset_iconDisabled = "assets\iconDisabled.png"
13+
14+
fileNotFound = " file not found. Exiting."
15+
16+
MULTIMONITORTOOL_PATH, CSV_FILE_PATH, MM_CONFIG_FILE_PATH, MONITOR_NAME = (
17+
None,
18+
None,
19+
None,
20+
None,
21+
)
22+
23+
24+
def check_for_missing_files():
25+
26+
if not os.path.exists(MULTIMONITORTOOL_PATH):
27+
nH.sendError(MULTIMONITORTOOL_PATH + fileNotFound)
28+
tH.quitItemClicked()
29+
if not os.path.exists(os.path.join(currentPath, asset_iconEnabled)):
30+
nH.sendError(asset_iconDisabled + fileNotFound)
31+
tH.quitItemClicked()
32+
33+
if not os.path.exists(CSV_FILE_PATH):
34+
mH.saveMultiMonitorToolConfig()
35+
36+
multiMonitorToolOutputPath = os.path.join(currentPath, "MultiMonitorTool")
37+
38+
if not os.path.exists(multiMonitorToolOutputPath):
39+
os.makedirs(multiMonitorToolOutputPath)
40+
41+
nH.sendNotification(
42+
"If you do not have all Monitors enabled and configured as you like right now, please do so and then right click the tray icon and save the configuration",
43+
20,
44+
)
45+
46+
47+
def read_config():
48+
49+
global MULTIMONITORTOOL_PATH, CSV_FILE_PATH, MM_CONFIG_FILE_PATH, MONITOR_NAME
50+
51+
if not os.path.exists(config_file_path):
52+
nH.sendError(config_file_path + fileNotFound)
53+
tH.quitItemClicked()
54+
55+
config = configparser.ConfigParser()
56+
57+
config.read(config_file_path, encoding="utf-8")
58+
59+
MULTIMONITORTOOL_PATH = config.get("SETTINGS", "multimonitorpath")
60+
MONITOR_NAME = config.get("SETTINGS", "monitor_name")
61+
CSV_FILE_PATH = config.get("DEV", "mm_csv_export_path")
62+
MM_CONFIG_FILE_PATH = config.get("DEV", "mm_config_file_path")
63+
64+
check_for_missing_files()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import csv
2+
import subprocess
3+
4+
import SimpleMonitorControlTray.configHandler as ch
5+
6+
7+
def saveMultiMonitorToolConfig():
8+
subprocess.run(
9+
[
10+
ch.MULTIMONITORTOOL_PATH,
11+
"/SaveConfig",
12+
ch.MM_CONFIG_FILE_PATH,
13+
]
14+
)
15+
16+
17+
def updateMonitorsCSV():
18+
# Run MultiMonitorTool.exe with the /scomma option to save the monitors list into a CSV file
19+
subprocess.run(
20+
[
21+
ch.MULTIMONITORTOOL_PATH,
22+
"/scomma",
23+
ch.CSV_FILE_PATH,
24+
]
25+
)
26+
27+
28+
def enableMonitor():
29+
subprocess.run(
30+
[
31+
ch.MULTIMONITORTOOL_PATH,
32+
"/LoadConfig",
33+
ch.MM_CONFIG_FILE_PATH,
34+
]
35+
)
36+
37+
38+
def disableMonitor():
39+
# Run MultiMonitorTool.exe with the /disable option to disable the third monitor
40+
subprocess.run(
41+
[
42+
ch.MULTIMONITORTOOL_PATH,
43+
"/disable",
44+
ch.MONITOR_NAME[-1],
45+
]
46+
)
47+
48+
49+
def isMonitorEnabled():
50+
# Check if the third monitor is listed in the CSV file
51+
with open(ch.CSV_FILE_PATH, "r") as file:
52+
reader = csv.DictReader(file)
53+
54+
for row in reader:
55+
try:
56+
monitor_name = row["Name"]
57+
monitor_active = row["Active"]
58+
if monitor_name == ch.MONITOR_NAME and monitor_active.upper() == "YES":
59+
return True
60+
except KeyError:
61+
pass # Ignore rows with missing keys
62+
return False # No match found
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import threading
2+
3+
from win10toast import ToastNotifier
4+
5+
6+
def sendError(text):
7+
toaster = ToastNotifier()
8+
toaster.show_toast("Error", text, duration=7)
9+
10+
11+
def sendNotification(text, duration):
12+
# toaster.show_toast("It's your first startup!", text, duration=duration)
13+
pass

SimpleMonitorControlTray/registryHandler.py

Whitespace-only changes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
3+
import pystray
4+
from PIL import Image
5+
from pystray import MenuItem as item
6+
7+
import SimpleMonitorControlTray.configHandler as cH
8+
import SimpleMonitorControlTray.monitorHandler as mH
9+
10+
script_dir = os.getcwd()
11+
12+
imageIconEnabled = Image.open(os.path.join(script_dir, "assets\iconEnabled.png"))
13+
imageIconDisabled = Image.open(os.path.join(script_dir, "assets\iconDisabled.png"))
14+
15+
title = "SimpleMonitorControlTray"
16+
17+
icon = None
18+
19+
20+
def saveMultiMonitorToolConfigClicked():
21+
mH.saveMultiMonitorToolConfig()
22+
23+
24+
def iconTrayClicked():
25+
mH.updateMonitorsCSV()
26+
if mH.isMonitorEnabled():
27+
mH.disableMonitor()
28+
icon.icon = imageIconDisabled
29+
else:
30+
mH.enableMonitor()
31+
icon.icon = imageIconEnabled
32+
33+
34+
def quitItemClicked():
35+
icon.stop()
36+
37+
38+
def openConfigClicked():
39+
os.startfile(os.path.join(script_dir, cH.config_file_path))
40+
41+
42+
def initTray():
43+
global icon
44+
menu = (
45+
item(title, iconTrayClicked, default=True, visible=False),
46+
item(
47+
"Save current monitor layout (used when enabling)",
48+
saveMultiMonitorToolConfigClicked,
49+
),
50+
item("Open Config.ini", openConfigClicked),
51+
item("Quit", quitItemClicked),
52+
)
53+
54+
firstImageIcon = None
55+
56+
mH.updateMonitorsCSV()
57+
58+
if mH.isMonitorEnabled():
59+
firstImageIcon = imageIconEnabled
60+
if not os.path.exists(cH.MM_CONFIG_FILE_PATH):
61+
mH.saveMultiMonitorToolConfig()
62+
else:
63+
firstImageIcon = imageIconDisabled
64+
65+
icon = pystray.Icon(title, firstImageIcon, title, menu)
66+
icon.run()

0 commit comments

Comments
 (0)