provide a way to get back to the default voice profile.

This commit is contained in:
Storm Dragon committed 2026-11-15 17:14:56 -05:00
1 parent 322f151344
commit cad3360bd8
8 files changed
+107 -14

No files matched your search

+4 -1
View File
@@ -17,6 +17,7 @@ import (
"git.stormux.org/storm/cthulhu/internal/tentacle"
"git.stormux.org/storm/cthulhu/internal/tentaclewire"
"git.stormux.org/storm/screenreader-common/quicksettings"
"git.stormux.org/storm/screenreader-common/voiceprofiles"
)
type speechSettingsUpdater interface {
@@ -48,6 +49,7 @@ type preferencesWorker struct {
globalSettings config.Settings
appSettings map[string]config.Settings
quickSpeech quickSpeechSettings
savedVoice *voiceprofiles.Profile // Quick settings "Default"; nil when unknown.
laptopLayout bool
activeApp string
editApp string
@@ -96,7 +98,8 @@ func (worker *preferencesWorker) setGlobalSettings(settings config.Settings) {
}
func newPreferencesWorker(logger *slog.Logger, settings config.Settings, updater any) *preferencesWorker {
return &preferencesWorker{logger: logger, settings: settings, globalSettings: settings, appSettings: make(map[string]config.Settings), updater: updater,
saved := config.Primary_voice(settings)
return &preferencesWorker{savedVoice: &saved, logger: logger, settings: settings, globalSettings: settings, appSettings: make(map[string]config.Settings), updater: updater,
extensionRequests: make(chan readerSettingsCall, 16), extensions: make(map[string]*readerExtension), speechCommands: make(chan input.Command, 16), remoteChanges: make(chan remoteSettingsChange, 16), discover: speech.Discover, readSpeechDefaults: speech.ReadDefaults, open: make(chan string, 1), applicationChanges: make(chan string, 1), done: make(chan struct{})}
}
@@ -48,6 +48,7 @@ func TestApplicationPreferencesSwitchAndPersistIndependently(t *testing.T) {
if worker.settings.TimeFormat != "24-hour" || worker.live.Load().TimeFormat != "24-hour" {
t.Fatal("application settings were not restored on return")
}
selectQuickSetting(t, worker, "speech.rate")
worker.handleSpeechCommand(ctx, input.IncreaseSpeechSetting)
worker.handleSpeechCommand(ctx, input.SaveSpeechSettings)
if !worker.beginEdit(ctx, "") {
+1
View File
@@ -84,6 +84,7 @@ func (worker *preferencesWorker) applyNext(ctx context.Context, next config.Sett
// An explicit preferences Apply replaces the session's quick speech choices.
if worker.editingActive {
worker.quickSpeech = quickSpeechSettings{}
worker.remember_saved_voice(next)
}
if worker.editingActive {
worker.editing = next
+40 -7
View File
@@ -18,8 +18,13 @@ import (
"git.stormux.org/storm/cthulhu/internal/tentaclewire"
"git.stormux.org/storm/screenreader-common/quicksettings"
sd "git.stormux.org/storm/screenreader-common/speechdispatcher"
"git.stormux.org/storm/screenreader-common/voiceprofiles"
)
// quickSavedVoice is the voice-profile choice for the saved speech settings.
// Profile names are visible text, so this identifier cannot collide.
const quickSavedVoice = "\x00saved"
// Use the settings worker's bounded mailbox, keeping discovery, disk writes,
// and speech connection replacement off the input and focus paths.
func (worker *preferencesWorker) SpeechCommand(command input.Command) {
@@ -156,11 +161,9 @@ func (host *quickSettingsHost) hardware() bool {
}
func (host *quickSettingsHost) categories() []quicksettings.Category {
voice := []quicksettings.Entry{
{ID: "speech.rate", Label: "Rate", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep},
{ID: "speech.pitch", Label: "Pitch", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep},
{ID: "speech.volume", Label: "Volume", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep},
}
// Match the preferences order: synthesizer and voice, then rate, pitch
// and volume, then saved profiles.
var voice []quicksettings.Entry
if host.hardware() {
voice = append(voice, quicksettings.Entry{ID: "speech.module", Label: "Hardware synthesizer", Kind: quicksettings.Choice})
} else {
@@ -169,6 +172,10 @@ func (host *quickSettingsHost) categories() []quicksettings.Category {
quicksettings.Entry{ID: "speech.language", Label: "Speech-dispatcher language", Kind: quicksettings.Choice, EmptyIsDefault: true},
quicksettings.Entry{ID: "speech.voice", Label: "Speech-dispatcher voice", Kind: quicksettings.Choice, EmptyIsDefault: true})
}
voice = append(voice,
quicksettings.Entry{ID: "speech.rate", Label: "Rate", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep},
quicksettings.Entry{ID: "speech.pitch", Label: "Pitch", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep},
quicksettings.Entry{ID: "speech.volume", Label: "Volume", Kind: quicksettings.Range, Min: -100, Max: 100, Step: quickVoiceStep})
if len(host.settings.VoiceProfiles.Profiles) > 0 {
voice = append(voice, quicksettings.Entry{ID: "speech.profile", Label: "Voice profile", Kind: quicksettings.Choice})
}
@@ -222,7 +229,7 @@ func (host *quickSettingsHost) Value(entry quicksettings.Entry) quicksettings.Va
}
switch entry.ID {
case "speech.profile":
return quicksettings.Value{Choice: settings.VoiceProfiles.Matching(config.Primary_voice(settings))}
return quicksettings.Value{Choice: host.voice_profile_choice(settings)}
case "speech.rate":
return number(settings.DefaultVoice.Rate, 0)
case "speech.pitch":
@@ -273,9 +280,12 @@ func (host *quickSettingsHost) Options(entry quicksettings.Entry) []quicksetting
switch entry.ID {
case "speech.profile":
options := []quicksettings.Option{}
if host.settings.VoiceProfiles.Matching(config.Primary_voice(host.settings)) == "" {
if host.voice_profile_choice(host.settings) == "" {
options = append(options, quicksettings.Option{ID: "", Label: "Custom"})
}
if host.worker != nil && host.worker.savedVoice != nil {
options = append(options, quicksettings.Option{ID: quickSavedVoice, Label: "Default"})
}
for _, p := range host.settings.VoiceProfiles.Profiles {
options = append(options, quicksettings.Option{ID: p.Name, Label: p.Name, Literal: true})
}
@@ -413,6 +423,9 @@ func (worker *preferencesWorker) apply_quick_setting(ctx context.Context, host *
var speechChanged bool
if result.Entry.ID == "speech.profile" {
p, ok := next.VoiceProfiles.Find(result.Value.Choice)
if result.Value.Choice == quickSavedVoice && worker.savedVoice != nil {
p, ok = *worker.savedVoice, true
}
if !ok {
worker.announceSpeechSetting("Choose a saved voice profile")
return false
@@ -530,3 +543,23 @@ func quickSpeechVoices(module string, voices []speech.Voice) []speech.Voice {
}
return speech.LocaleVoices(voices)
}
// voice_profile_choice names the matching profile, then the saved voice, and
// otherwise reports a custom session voice.
func (host *quickSettingsHost) voice_profile_choice(settings config.Settings) string {
primary := config.Primary_voice(settings)
if name := settings.VoiceProfiles.Matching(primary); name != "" {
return name
}
if host.worker != nil && host.worker.savedVoice != nil && voiceprofiles.Same_voice(*host.worker.savedVoice, primary) {
return quickSavedVoice
}
return ""
}
// remember_saved_voice records the voice that quick settings "Default"
// restores: the one in the settings at startup or the last explicit Apply.
func (worker *preferencesWorker) remember_saved_voice(settings config.Settings) {
saved := config.Primary_voice(worker.effective_settings(settings))
worker.savedVoice = &saved
}
+6 -3
View File
@@ -56,6 +56,7 @@ func TestSpeechShortcutsApplySaveAndFailure(t *testing.T) {
message := ""
worker.announce = func(text string) { message = text }
command := func(c input.Command) { t.Helper(); worker.handleSpeechCommand(context.Background(), c) }
selectQuickSetting(t, worker, "speech.rate")
command(input.IncreaseSpeechSetting)
if message != "60" || applied.Rate == nil || *applied.Rate != 60 || *worker.live.Load().DefaultVoice.Rate != 60 || rate != 50 {
t.Fatalf("rate: message %q, applied %+v, original %g", message, applied, rate)
@@ -91,7 +92,7 @@ func TestSpeechShortcutsApplySaveAndFailure(t *testing.T) {
}
command(input.SelectNextSpeechSetting)
if message != "Rate 60" {
t.Fatal("selector did not wrap to the first setting", message)
t.Fatal("selector did not move from voice to rate", message)
}
command(input.SaveSpeechSettings)
if message != "Speech settings saved" {
@@ -151,7 +152,7 @@ func TestQuickSettingsCategoriesReadingAndKeyboard(t *testing.T) {
{input.SelectNextSpeechSetting, "Character echo unchecked"},
{input.SelectNextSpeechSetting, "Word echo unchecked"},
{input.IncreaseSpeechSetting, "checked"},
{input.SelectNextSpeechCategory, "Speech. Rate 0"},
{input.SelectNextSpeechCategory, "Speech. Speech-dispatcher module default"},
{input.SelectPreviousSpeechCategory, "Keyboard. Word echo checked"},
}
for _, step := range steps {
@@ -319,6 +320,7 @@ func TestQuickSpeechFailureAndPreferencesReplacement(t *testing.T) {
worker.appSettings["Example Editor"] = application
ctx := t.Context()
worker.switchApplication(ctx, "Presentation")
selectQuickSetting(t, worker, "speech.rate")
worker.handleSpeechCommand(ctx, input.IncreaseSpeechSetting)
worker.handleSpeechCommand(ctx, input.SelectNextSpeechSetting)
worker.handleSpeechCommand(ctx, input.IncreaseSpeechSetting)
@@ -373,6 +375,7 @@ func TestSpeechShortcutReadsAndSavesRoundedNumbers(t *testing.T) {
worker := newPreferencesWorker(slog.New(slog.DiscardHandler), settings, nil)
message := ""
worker.announce = func(text string) { message = text }
selectQuickSetting(t, worker, "speech.rate")
for _, step := range []struct {
command input.Command
want string
@@ -450,7 +453,7 @@ func TestHardwareQuickSettingsOfferSynthesizersWithoutVoices(t *testing.T) {
}
var messages []string
worker.announce = func(text string) { messages = append(messages, text) }
for _, want := range []string{"Pitch 0", "Volume 100", "Hardware synthesizer doubletalk", "Rate 0"} {
for _, want := range []string{"Rate 0", "Pitch 0", "Volume 100", "Hardware synthesizer doubletalk"} {
worker.handleSpeechCommand(t.Context(), input.SelectNextSpeechSetting)
if got := messages[len(messages)-1]; got != want {
t.Fatalf("got %q, want %q", got, want)
+25 -2
View File
@@ -28,9 +28,12 @@ func TestQuickVoiceProfilesFailurePersistenceAndApplicationInheritance(t *testin
t.Fatal("failed profile changed current voice")
}
failure = nil
for _, name := range []string{"Reading", "Presentation", "Reading"} {
saved := config.Primary_voice(settings)
// After the last profile, Default restores the saved voice.
for _, name := range []string{"Reading", "Presentation", "", "Reading"} {
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if got := worker.settings.VoiceProfiles.Matching(config.Primary_voice(worker.settings)); got != name {
primary := config.Primary_voice(worker.settings)
if got := worker.settings.VoiceProfiles.Matching(primary); got != name || name == "" && !voiceprofiles.Same_voice(primary, saved) {
t.Fatal(got, name, worker.settings.DefaultVoice)
}
}
@@ -48,3 +51,23 @@ func TestQuickVoiceProfilesFailurePersistenceAndApplicationInheritance(t *testin
t.Fatal("profile changed auxiliary voices")
}
}
func TestQuickVoiceProfileDefaultRestoresSavedVoiceFromCustom(t *testing.T) {
settings := config.DefaultSettings()
settings.Path = filepath.Join(t.TempDir(), "settings.toml")
settings.VoiceProfiles.Profiles = []voiceprofiles.Profile{{Name: "Reading", Driver: "speech-dispatcher", Rate: 30, Volume: 80}}
saved := config.Primary_voice(settings)
worker := newPreferencesWorker(slog.New(slog.DiscardHandler), settings, settingsUpdateFunc(func(context.Context, speech.Settings) error { return nil }))
var messages []string
worker.announce = func(text string) { messages = append(messages, text) }
selectQuickSetting(t, worker, "speech.rate")
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
selectQuickSetting(t, worker, "speech.profile")
if got := messages[len(messages)-1]; got != "Voice profile Custom" {
t.Fatal("session voice not reported as Custom", got)
}
worker.handleSpeechCommand(t.Context(), input.IncreaseSpeechSetting)
if got := messages[len(messages)-1]; got != "Default" || !voiceprofiles.Same_voice(config.Primary_voice(worker.settings), saved) {
t.Fatal("Default did not restore the saved voice", messages, worker.settings.DefaultVoice)
}
}
+3
View File
@@ -20,6 +20,8 @@ void prefs_profiles_init(void) {
profilePage = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
gtk_container_set_border_width(GTK_CONTAINER(profilePage), 12);
profileCombo = gtk_combo_box_text_new();
// Focus the internal combo button so Tab and Shift+Tab leave it normally.
gtk_widget_set_can_focus(profileCombo, FALSE);
atk_object_set_name(gtk_widget_get_accessible(profileCombo), "Voice Profiles");
gtk_box_pack_start(GTK_BOX(profilePage), profileCombo, FALSE, FALSE, 0);
GtkWidget **buttons[] = {&profileAdd, &profileEdit, &profileRemove, &profileUse};
@@ -86,6 +88,7 @@ void prefs_profile_control(const char *id, const char *label, int kind, int row,
gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(widget), TRUE);
} else {
widget = gtk_combo_box_text_new();
gtk_widget_set_can_focus(widget, FALSE);
g_object_set_data_full(G_OBJECT(widget), "profile-label", g_strdup(label), g_free);
g_signal_connect(widget, "changed", G_CALLBACK(profile_combo_changed), NULL);
}
+27 -1
View File
@@ -112,6 +112,26 @@ func set_profile_control(c ui.Control) {
C.prefs_profile_set(id, C.int(c.Kind), text, C.double(c.Number), C.int(c.Selected), boolean(c.Hidden))
}
// set_profile_state updates a combo's selection and visibility in place.
func set_profile_state(c ui.Control) {
id, text := C.CString(c.ID), C.CString(c.Text)
defer C.free(unsafe.Pointer(id))
defer C.free(unsafe.Pointer(text))
C.prefs_profile_set(id, C.int(c.Kind), text, C.double(c.Number), C.int(c.Selected), boolean(c.Hidden))
}
func same_choices(a, b []ui.Choice) bool {
if len(a) != len(b) {
return false
}
for index := range a {
if a[index].ID != b[index].ID || a[index].Label != b[index].Label {
return false
}
}
return true
}
func (session *uiSession) profile_tick() bool {
if session.profileEditor == nil {
return false
@@ -135,7 +155,13 @@ func (session *uiSession) profile_tick() bool {
if c.Kind == ui.Entry {
c.Text = values[voiceprofiles.PageID+"."+c.ID].Text
}
set_profile_control(c)
// Rebuilding the combo the user is changing moves GTK focus out of
// it. Like the main speech page, refill only lists that changed.
if old := values[voiceprofiles.PageID+"."+c.ID]; c.Kind != ui.ComboBox || !same_choices(old.Choices, c.Choices) {
set_profile_control(c)
} else if old.Selected != c.Selected || old.Hidden != c.Hidden {
set_profile_state(c)
}
values[voiceprofiles.PageID+"."+c.ID] = c
for index, old := range session.profileControls {
if old.ID == c.ID {