From 2a87c72364a8ae3b51d8387cef0318eaa052c4b7 Mon Sep 17 00:00:00 2001 From: GruiaChiscop <166289528+GruiaChiscop@users.noreply.github.com> Date: Fri, 20 Jun 2025 23:09:25 +0300 Subject: [PATCH] Add previous voice toggle functionality Introduces a script and supporting method to switch to the previous voice in the voice settings. Updates gesture descriptions and ensures proper index handling for both next and previous voice toggling. --- addon/globalPlugins/voiceToggle/__init__.py | 25 ++++++++++++++++--- .../globalPlugins/voiceToggle/voiceToggle.py | 11 ++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/addon/globalPlugins/voiceToggle/__init__.py b/addon/globalPlugins/voiceToggle/__init__.py index 19c29c0..75d2aaf 100644 --- a/addon/globalPlugins/voiceToggle/__init__.py +++ b/addon/globalPlugins/voiceToggle/__init__.py @@ -31,12 +31,31 @@ def __terminate__(self): gui.settingsDialogs.NVDASettingsDialog.categoryClasses.remove(OptionsPanel) @scriptHandler.script( - gesture="kb:NVDA+Alt+V", # Translators: Gesture description for the Input gestures settings dialog - description=_("Toggles to the next voice."), + description=_("Switches to the next voice."), + category = _("Voice Toggle"), ) def script_toggleVoice(self, gesture): if not self.isSynthDoneSpeakingRegistered: synthDoneSpeaking.register(self.app.handleDoneSpeaking) self.isSynthDoneSpeakingRegistered = True - self.app.toggleVoice() + if len(self.app.voiceSettings) == 0: + return + nextIndex = self.app.getNextVoiceSettingsIndex(self.app.currentVoiceSettingsIndex) + newIndex = self.app.changeVoice(nextIndex) + self.app.currentVoiceSettingsIndex = newIndex + + @scriptHandler.script( + # Translators: Gesture description for the Input gestures settings dialog + description=_("Switches to the previous voice."), + category = _("Voice Toggle"), + ) + def script_previousVoice(self, gesture): + if not self.isSynthDoneSpeakingRegistered: + synthDoneSpeaking.register(self.app.handleDoneSpeaking) + self.isSynthDoneSpeakingRegistered = True + if len(self.app.voiceSettings) == 0: + return + previousIndex = self.app.getPreviousVoiceSettingsIndex(self.app.currentVoiceSettingsIndex) + newIndex = self.app.changeVoice(previousIndex) + self.app.currentVoiceSettingsIndex = newIndex diff --git a/addon/globalPlugins/voiceToggle/voiceToggle.py b/addon/globalPlugins/voiceToggle/voiceToggle.py index 3641d5b..7afaf3e 100644 --- a/addon/globalPlugins/voiceToggle/voiceToggle.py +++ b/addon/globalPlugins/voiceToggle/voiceToggle.py @@ -260,6 +260,17 @@ def getNextVoiceSettingsIndex(self, index): newIndex = (index + 1) % voiceSettingsLength return newIndex + def getPreviousVoiceSettingsIndex(self, index): + voiceSettingsLength = len(self.voiceSettings) + if voiceSettingsLength == 0: + return -1 + + # If index is out of list bounds and list is not empty, return zero + if index < 0 or index >= voiceSettingsLength: + return 0 + newIndex = (index - 1) % voiceSettingsLength + return newIndex + def changeVoice(self, newIndex, announceChange=True): voiceSettingsLength = len(self.voiceSettings) if (newIndex >= voiceSettingsLength) or (newIndex < 0):