Skip to content

Commit

Permalink
refactor(accessible_output): encapsulate accessible output handling a…
Browse files Browse the repository at this point in the history
…nd improve logging

- Refactored `accessible_output` handling in `ConversationTab` by centralizing logic into a `_handle_accessible_output` method.
- Enhanced initialization logging in `accessible_output.py` by adding a logger.
- Removed redundant focus checks before handling accessible output.
- Improved accessibility support by adding optional Braille output capability.
- Simplified conditions for message focusing based on accessibility settings.
  • Loading branch information
AAClause committed Nov 4, 2024
1 parent 05453e1 commit 948a575
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 4 additions & 0 deletions basilisk/accessible_output.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import logging
import re
from functools import cache

import accessible_output3.outputs.auto

log = logging.getLogger(__name__)


@cache
def get_accessible_output():
log.info("Initializing Accessible Output")
return accessible_output3.outputs.auto.Auto()


Expand Down
47 changes: 27 additions & 20 deletions basilisk/gui/conversation_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ def navigate_message(self, previous: bool):
else:
start, end = self.get_range_for_current_message()
current_message = self.messages.GetRange(start, end)
if config.conf().conversation.use_accessible_output:
accessible_output.speak(current_message)
self._handle_accessible_output(current_message)

def go_to_previous_message(self, event: wx.CommandEvent = None):
self.navigate_message(True)
Expand All @@ -559,16 +558,14 @@ def move_to_start_of_message(self, event: wx.CommandEvent = None):
self.message_segment_manager.absolute_position = cursor_pos
self.message_segment_manager.focus_content_block()
self.messages.SetInsertionPoint(self.message_segment_manager.start)
if config.conf().conversation.use_accessible_output:
accessible_output.output(_("Start of message."))
self._handle_accessible_output(_("Start of message."))

def move_to_end_of_message(self, event: wx.CommandEvent = None):
cursor_pos = self.messages.GetInsertionPoint()
self.message_segment_manager.absolute_position = cursor_pos
self.message_segment_manager.focus_content_block()
self.messages.SetInsertionPoint(self.message_segment_manager.end - 1)
if config.conf().conversation.use_accessible_output:
accessible_output.output(_("End of message."))
self._handle_accessible_output(_("End of message."))

def get_range_for_current_message(self) -> tuple[int, int]:
cursor_pos = self.messages.GetInsertionPoint()
Expand Down Expand Up @@ -609,8 +606,7 @@ def on_copy_message(self, event: wx.CommandEvent = None):
self.select_current_message()
self.messages.Copy()
self.messages.SetInsertionPoint(cursor_pos)
if config.conf().conversation.use_accessible_output:
accessible_output.output(_("Message copied to clipboard."))
self._handle_accessible_output(_("Message copied to clipboard."))

def on_remove_message_block(self, event: wx.CommandEvent = None):
cursor_pos = self.messages.GetInsertionPoint()
Expand All @@ -622,8 +618,10 @@ def on_remove_message_block(self, event: wx.CommandEvent = None):
self.conversation.messages.remove(message_block)
self.refresh_messages()
self.messages.SetInsertionPoint(cursor_pos)
if config.conf().conversation.use_accessible_output:
accessible_output.output(_("Message block removed."))
self._handle_accessible_output(
_("Message block removed."), braille=True
)

else:
wx.Bell()

Expand Down Expand Up @@ -996,7 +994,7 @@ def on_transcription_received(self, transcription):
stop_sound()
self.SetStatusText(_("Ready"))
self.prompt.AppendText(transcription.text)
if config.conf().conversation.use_accessible_output:
if self.prompt.HasFocus() and self.GetTopLevelParent().IsShown():
self._handle_accessible_output(transcription.text)
self.prompt.SetInsertionPointEnd()
self.prompt.SetFocus()
Expand Down Expand Up @@ -1146,27 +1144,33 @@ def _handle_completion_with_stream(self, chunk: str):
if re.match(r".+[\n:.?!]\s?$", self.stream_buffer):
self._flush_stream_buffer()
if not self._messages_already_focused:
self.messages.SetFocus()
if not config.conf().conversation.use_accessible_output:
self.messages.SetFocus()
self._messages_already_focused = True
new_time = time.time()
if new_time - self.last_time > 4:
play_sound("chat_response_pending")
self.last_time = new_time

def _handle_accessible_output(self, text: str):
def _handle_accessible_output(
self, text: str, braille: bool = False, force: bool = False
):
if (
text.strip()
and config.conf().conversation.use_accessible_output
and self.prompt.HasFocus()
(not force and not config.conf().conversation.use_accessible_output)
or not isinstance(text, str)
or not text.strip()
):
accessible_output.speak(clear_for_speak(text))
self._messages_already_focused = True
return
if braille:
accessible_output.braille(text)
accessible_output.speak(clear_for_speak(text))

def _flush_stream_buffer(self):
pos = self.messages.GetInsertionPoint()
text = self.stream_buffer
self.messages.AppendText(text)
self._handle_accessible_output(text)
if self.prompt.HasFocus() and self.GetTopLevelParent().IsShown():
self._handle_accessible_output(text)
self.stream_buffer = ""
self.messages.SetInsertionPoint(pos)

Expand All @@ -1191,7 +1195,10 @@ def _post_completion_without_stream(self, new_block: MessageBlock):
self._end_task()

def _end_task(self, success: bool = True):
if not self._messages_already_focused:
if (
not self._messages_already_focused
and not config.conf().conversation.use_accessible_output
):
self.messages.SetFocus()
task = self.task
task.join()
Expand Down
1 change: 0 additions & 1 deletion basilisk/main_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def OnInit(self) -> bool:
initialize_sound_manager()
log.info("sound manager initialized")
get_accessible_output()
log.info("accessible output initialized")
from basilisk.gui.main_frame import MainFrame

frame_style = wx.DEFAULT_FRAME_STYLE
Expand Down

0 comments on commit 948a575

Please sign in to comment.