Skip to content

Commit

Permalink
added install button for audiodevicecmdlets, removed restart needs
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne authored and Flora committed Jun 27, 2024
1 parent 6b675d6 commit 9b6a701
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 73 deletions.
66 changes: 52 additions & 14 deletions src/BigPictureTV.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ def set_audio_device(device_name, devices):
return False

def switch_audio(audio_output):
if constants.get('DisableAudioSwitch', False):
print("Audio switching is disabled.")
return

devices = get_audio_devices()
success = set_audio_device(audio_output, devices)

Expand All @@ -96,14 +92,20 @@ def switch_screen(mode):
def switch_mode(mode):
global current_mode
global tray_icon
global constants
constants = load_constants()
disabled_audio = constants.get('DisableAudioSwitch')
installed_audio = is_audio_device_cmdlets_installed()

if mode == Mode.GAMEMODE:
switch_screen(GAMEMODE_SCREEN)
switch_audio(constants['GAMEMODE_AUDIO'])
if not disabled_audio and installed_audio:
switch_audio(constants['GAMEMODE_AUDIO'])
current_mode = Mode.GAMEMODE
else:
switch_screen(DESKTOP_SCREEN)
switch_audio(constants['DESKTOP_AUDIO'])
if not disabled_audio and installed_audio:
switch_audio(constants['DESKTOP_AUDIO'])
current_mode = Mode.DESKTOP

write_current_mode(current_mode)
Expand Down Expand Up @@ -176,9 +178,7 @@ def create_tray_icon(current_mode):
def run_tray_icon():
global tray_icon
global current_mode
global constants

constants = load_constants()
current_mode = read_current_mode()
tray_icon = create_tray_icon(current_mode)
tray_icon.run()
Expand All @@ -193,6 +193,17 @@ def open_settings_window():
window.show()
app.exec_()

def is_audio_device_cmdlets_installed():
try:
cmd = 'powershell "Get-Module -ListAvailable -Name AudioDeviceCmdlets"'
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if "AudioDeviceCmdlets" in result.stdout:
return True
return False
except Exception as e:
print(f"Error checking AudioDeviceCmdlets installation: {e}")
return False

class SettingsWindow(QMainWindow):
def __init__(self):
super().__init__()
Expand All @@ -216,7 +227,15 @@ def __init__(self):

self.disableAudioCheckbox.stateChanged.connect(self.toggle_audio_fields)
self.helpButton.clicked.connect(self.open_help_dialog)
self.audioInstallButton.clicked.connect(self.install_audio_device_cmdlets)

if is_audio_device_cmdlets_installed():
self.audioInstallButton.setText("AudioDeviceCmdlets Installed")
self.audioInstallButton.setEnabled(False)
else:
self.disableAudioCheckbox.setEnabled(False)
self.desktopEntry.setEnabled(False)
self.gamemodeEntry.setEnabled(False)
self.set_stylesheet()

def toggle_audio_fields(self, state):
Expand Down Expand Up @@ -246,7 +265,7 @@ def save_settings(self):
with open(self.constants_path, 'w') as f:
json.dump(self.constants, f, indent=4)
QMessageBox.information(self, 'Success', 'Settings saved successfully.')
restart_main()
self.close()
except Exception as e:
QMessageBox.critical(self, 'Error', f'Failed to save settings: {e}')

Expand Down Expand Up @@ -284,9 +303,29 @@ def is_startup_shortcut_exist(self):
startup_folder = winshell.startup()
shortcut_path = os.path.join(startup_folder, 'BigPictureTV.lnk')
return os.path.exists(shortcut_path)

def restart_main():
os.execl(sys.executable, sys.executable, *sys.argv)

def install_audio_device_cmdlets(self):
try:
self.audioInstallButton.setText("Installing...")
self.audioInstallButton.setEnabled(False)
cmd = 'powershell Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser -Force -AllowClobber'
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
QMessageBox.information(self, 'Success', 'AudioDeviceCmdlets installed successfully.')
self.audioInstallButton.setText("AudioDeviceCmdlets Installed")
self.audioInstallButton.setEnabled(False)
self.disableAudioCheckbox.setEnabled(True)
self.desktopEntry.setEnabled(True)
self.gamemodeEntry.setEnabled(True)
else:
QMessageBox.critical(self, 'Error', 'Failed to install AudioDeviceCmdlets. See the console for more details.')
print(result.stderr)
self.audioInstallButton.setEnabled(True)
self.audioInstallButton.setText("Install AudioDeviceCmdlets")
except Exception as e:
QMessageBox.critical(self, 'Error', f'Failed to install AudioDeviceCmdlets: {e}')
self.audioInstallButton.setEnabled(True)
self.audioInstallButton.setText("Install AudioDeviceCmdlets")

class HelpDialog(QDialog):
def __init__(self, stylesheet=None):
Expand All @@ -303,9 +342,8 @@ def __init__(self, stylesheet=None):
self.setFixedSize(self.size())

if __name__ == '__main__':
constants = load_constants()
current_mode = read_current_mode()

constants = load_constants()
if current_mode != Mode.DESKTOP:
switch_mode(Mode.DESKTOP)

Expand Down
125 changes: 66 additions & 59 deletions src/ui/design.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>490</width>
<height>309</height>
<height>380</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -24,8 +24,18 @@
<item row="0" column="0">
<widget class="QFrame" name="gridFrame">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QCheckBox" name="startupCheckBox">
<item row="6" column="1">
<widget class="QLineEdit" name="gamemodeEntry"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Disable audio switching</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="disableAudioCheckbox">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
Expand All @@ -34,14 +44,58 @@
</property>
</widget>
</item>
<item row="0" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label">
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string>Steam Big Picture keywords</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Run at startup</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<item row="6" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>GAMEMODE audio device</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>DESKTOP audio device</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="startupCheckBox">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="systemThemeBox">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QFrame" name="buttonsFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -68,7 +122,7 @@
<item row="0" column="1">
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Save and restart</string>
<string>Save</string>
</property>
</widget>
</item>
Expand All @@ -82,70 +136,23 @@
</layout>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Use system theme for icon</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="desktopEntry"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label">
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string>Steam Big Picture keywords</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="gamemodeEntry"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>GAMEMODE audio device</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>DESKTOP audio device</string>
</property>
</widget>
<widget class="QLineEdit" name="desktopEntry"/>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QLineEdit" name="steamEntry"/>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="systemThemeBox">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="disableAudioCheckbox">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_6">
<item row="0" column="0" colspan="2">
<widget class="QPushButton" name="audioInstallButton">
<property name="text">
<string>Disable audio switching</string>
<string>Install AudioDeviceCmdlets</string>
</property>
</widget>
</item>
Expand Down
14 changes: 14 additions & 0 deletions src/ui/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ QPushButton {
margin: 8px;
}

QPushButton:hover {
background-color: #43474f;
border: 1px solid #3a3f47;
}

QPushButton:disabled {
background-color: #3a3f47;
color: #999999;
border: 1px solid #3a3f47;
border-radius: 4px;
padding: 5px;
margin: 8px;
}

QLabel {
color: white;
margin: 8px;
Expand Down

0 comments on commit 9b6a701

Please sign in to comment.