Skip to content

subtitles

The community/plugin/subtitles directory contains the code for a custom subtitles plugin for Talon. This plugin displays subtitles of speech input on the screen. It allows customization of the subtitles' appearance, location, and duration.

The core functionality resides in these files:

  • on_phrase.py: This file hooks into Talon's speech system to capture spoken phrases. The on_pre_phrase function is registered to be called on every spoken phrase. It extracts the spoken words and calls the show_subtitle function from subtitles.py to display the subtitle on the screen. This file also contains logic to skip phrases in sleep mode.

  • subtitles.py: This file implements the subtitle display logic.

    • It defines various settings using the setting function decorator such as show, screens, size, color, color_outline, timeout_per_char, timeout_min, timeout_max, and y.
    • The show_subtitle function is the main entry point for displaying subtitles. It clears existing subtitles and creates a new canvas for each screen selected by the user, then calls show_text_on_screen to display the subtitle.
    • The get_screens function determines which screens to display the subtitles on, based on the user's subtitles_screens setting.
    • The show_text_on_screen creates a canvas on the given screen and uses on_draw to actually draw the text. It sets a timeout to clear the canvas.
    • The on_draw function is where the text is rendered using Skia. It calculates the position of the text, applies a drop shadow, sets the colors, and draws the text and its outline using settings defined in subtitles.talon.
    • The calculate_timeout function calculates how long the subtitle should be displayed on the screen using user defined settings.
    • The set_text_size_and_get_rect function calculates the size of text based on the screen size and scales the font down so that the text will fit within the screen bounds.
    • The clear_canvases function closes all open canvases and clears the list of canvases.
  • subtitles.talon: This file defines the default settings for the subtitles plugin. These settings include:

    • user.subtitles_show: A boolean to enable or disable the subtitles.
    • user.subtitles_screens: The screens on which to display subtitles.
    • user.subtitles_size: The size of the subtitle text.
    • user.subtitles_color: The color of the subtitle text.
    • user.subtitles_color_outline: The outline color of the subtitle text.
    • user.subtitles_timeout_per_char: The time to extend the timeout for each character.
    • user.subtitles_timeout_min: The minimum display time for a subtitle.
    • user.subtitles_timeout_max: The maximum display time for a subtitle.
    • user.subtitles_y: The vertical position of the subtitle.
  • README.md: This file provides documentation for the subtitles plugin, including how to enable/disable the subtitles and other configuration options in subtitles.talon.

In summary, the plugin works by intercepting spoken phrases, rendering those phrases as text on screen according to the configurable settings, and automatically removing those subtitles after a set time.