Fix speech preview ordering and use shared quick voice profiles

This commit is contained in:
Storm Dragon committed 2026-11-15 17:56:36 -05:00
1 parent 7aadaf6602
commit e20319f776
17 files changed
+321 -222

No files matched your search

+11
View File
@@ -139,6 +139,9 @@ func (a *application) restore_override_result(done func(error)) bool {
// A form opened under the application must never outlive that context:
// its previews and save draft belong to the old effective configuration.
a.ui.close()
if a.preview.busy {
return false
}
if a.overrideCurrent != nil {
current.temporary = a.capture_override(current.terminal)
}
@@ -192,6 +195,11 @@ func (a *application) select_override(id string) {
// the ID and busy/terminal gates before entering this path.
func (a *application) select_override_result(id string, done func(string)) {
terminal := a.lastTerminal
a.ui.close()
if a.preview.busy {
done("Settings are busy; try again after the current operation")
return
}
a.overrideRestoreFailed = false // An explicit selection retries restoration.
a.restore_override_result(func(err error) {
if err != nil {
@@ -278,6 +286,9 @@ func (a *application) observe_override(next screen.Snapshot) (applicationoverrid
return applicationoverrides.Frame{}, false
}
a.ui.close()
if a.preview.busy {
return applicationoverrides.Frame{}, false
}
if !selected.initialized && selected.profile == nil {
a.load_application_profile(selected)
return applicationoverrides.Frame{}, false
+8 -33
View File
@@ -35,10 +35,6 @@ import (
"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"
type quickSettings struct {
selectionChanged bool
menu *quicksettings.Menu
@@ -133,17 +129,7 @@ func (h quickHost) Value(entry quicksettings.Entry) quicksettings.Value {
func (h quickHost) Options(entry quicksettings.Entry) []quicksettings.Option {
if entry.ID == "voice-profile" {
options := []quicksettings.Option{}
if h.quick.voice_profile_choice(*h.settings) == "" {
options = append(options, quicksettings.Option{ID: "", Label: "Custom"})
}
if h.quick.savedVoice != nil {
options = append(options, quicksettings.Option{ID: quickSavedVoice, Label: "Default"})
}
for _, p := range h.settings.VoiceProfiles.Profiles {
options = append(options, quicksettings.Option{ID: p.Name, Label: p.Name, Literal: true})
}
return options
return h.settings.VoiceProfiles.Quick_options(config.Primary_voice(*h.settings), h.quick.savedVoice)
}
definition := h.quick.definitions[entry.ID]
choices := definition.Choices(*h.settings)
@@ -152,7 +138,7 @@ func (h quickHost) Options(entry quicksettings.Entry) []quicksettings.Option {
}
options := make([]quicksettings.Option, 0, len(choices))
for _, choice := range choices {
options = append(options, quicksettings.Option{ID: choice.ID, Label: choice.Label})
options = append(options, quicksettings.Option{ID: choice.ID, Label: choice.Label, Literal: speech_choice(definition.ChoiceSource) && choice.ID != ""})
}
return options
}
@@ -207,11 +193,10 @@ func (q *quickSettings) command(id command.ID, settings *config.Settings, voice
return false
}
fresh := new_quick_settings(settings.Presentation.Speech.Driver)
if len(settings.VoiceProfiles.Profiles) > 0 {
for index := range fresh.categories {
if fresh.categories[index].ID == "presentation.speech" {
fresh.categories[index].Entries = append(fresh.categories[index].Entries, quicksettings.Entry{ID: "voice-profile", Label: "Voice profile", Kind: quicksettings.Choice})
}
// Always offered so "Default" can restore the saved voice without profiles.
for index := range fresh.categories {
if fresh.categories[index].ID == "presentation.speech" {
fresh.categories[index].Entries = append(fresh.categories[index].Entries, quicksettings.Entry{ID: "voice-profile", Label: "Voice profile", Kind: quicksettings.Choice})
}
}
q.definitions = fresh.definitions
@@ -226,10 +211,7 @@ func (q *quickSettings) command(id command.ID, settings *config.Settings, voice
return true
}
if result.Entry.ID == "voice-profile" {
p, ok := settings.VoiceProfiles.Find(result.Value.Choice)
if result.Value.Choice == quickSavedVoice && q.savedVoice != nil {
p, ok = *q.savedVoice, true
}
p, ok := settings.VoiceProfiles.Quick_profile(result.Value.Choice, q.savedVoice)
if !ok {
voice.Say(i18n.T("Choose a saved voice profile"))
return true
@@ -376,12 +358,5 @@ func (s *settingsSaver) result(err error, voice Speaker) {
// voice_profile_choice names the matching profile, then the saved voice, and
// otherwise reports a custom session voice.
func (q *quickSettings) voice_profile_choice(settings config.Settings) string {
primary := config.Primary_voice(settings)
if name := settings.VoiceProfiles.Matching(primary); name != "" {
return name
}
if q.savedVoice != nil && voiceprofiles.Same_voice(*q.savedVoice, primary) {
return quickSavedVoice
}
return ""
return settings.VoiceProfiles.Quick_choice(config.Primary_voice(settings), q.savedVoice)
}
+27 -1
View File
@@ -30,10 +30,34 @@ import (
"git.stormux.org/storm/fenrir/internal/config"
"git.stormux.org/storm/fenrir/internal/input"
"git.stormux.org/storm/fenrir/internal/interaction"
"git.stormux.org/storm/fenrir/internal/preferences"
"git.stormux.org/storm/fenrir/internal/screen"
"git.stormux.org/storm/fenrir/internal/speech"
"git.stormux.org/storm/screenreader-common/quicksettings"
)
func TestQuickSpeechNamesBypassTranslation(t *testing.T) {
settings := config.Defaults()
settings.Presentation.Speech.Module = "all"
settings.Presentation.Speech.Language = "en"
settings.Presentation.Speech.Voice = "default"
menu := new_quick_settings("speech-dispatcher")
menu.discovery = preferences.Discovery{Catalog: speech.Catalog{Modules: []string{"all"}, Voices: map[string][]speech.Voice{"all": {{Name: "default", Language: "en"}}}}}
menu.menu.Messages.Translate = func(text string) string { return "translated " + text }
host := quickHost{quick: menu, settings: &settings}
for _, id := range []string{"module", "language", "voice"} {
entry := quicksettings.Entry{ID: "presentation.speech." + id, Kind: quicksettings.Choice}
value := host.Value(entry)
options := host.Options(entry)
if got := menu.menu.Describe(entry, value, options); got != value.Choice {
t.Fatalf("%s name translated: %q", id, got)
}
if got := menu.menu.Describe(entry, quicksettings.Value{}, options); got != "translated default" {
t.Fatalf("%s default label not translated: %q", id, got)
}
}
}
func TestQuickSpeechSelectionAdjustmentAndBounds(t *testing.T) {
settings := config.Defaults()
settings.Presentation.Speech.Rate = 80 // Exercise the upper rate bound.
@@ -57,7 +81,9 @@ func TestQuickSpeechSelectionAdjustmentAndBounds(t *testing.T) {
{command.CurrentQuickValue, "-10"},
{command.NextQuickEntry, "speech volume 100"},
{command.PreviousQuickValue, "90"},
{command.NextQuickEntry, "Voice profile Custom"},
{command.NextQuickEntry, "Speech Dispatcher module default"},
{command.PreviousQuickEntry, "Voice profile Custom"},
{command.PreviousQuickEntry, "speech volume 90"},
{command.PreviousQuickEntry, "speech pitch -10"},
{command.PreviousQuickEntry, "speech rate 100"},
@@ -93,7 +119,7 @@ func TestHardwareQuickSettingsOnlyOfferSupportedControls(t *testing.T) {
menu.selectSpeech = func(config.Speech, func(error, bool)) {
t.Fatal("hardware numeric adjustment tried reopening the device")
}
for _, want := range []string{"speech pitch 0", "speech volume 100", "speech rate 0"} {
for _, want := range []string{"speech pitch 0", "speech volume 100", "Voice profile Custom", "speech rate 0"} {
menu.command(command.NextQuickEntry, &settings, voice)
if got := voice.messages[len(voice.messages)-1]; got != want {
t.Fatal(driver, got, want)
+1 -1
View File
@@ -40,7 +40,7 @@ func previewable_selection(driverID string) bool {
// Hardware synthesizers keep their explicit voice test instead.
func (a *application) preview_menu_speech(selected config.Speech) {
live := a.settings.live
if !a.settings.speechAvailable || selected.Driver != live.Presentation.Speech.Driver {
if !a.settings.speechAvailable || a.speechPending || a.saver.busy || selected.Driver != live.Presentation.Speech.Driver {
return
}
parameters := speech.Parameters{Rate: selected.Rate, Pitch: selected.Pitch, Volume: selected.Volume}
+65
View File
@@ -173,3 +173,68 @@ func TestMenuAnnouncementWaitsForPreviewedVoice(t *testing.T) {
t.Fatal("idle announcement was held", voice.messages)
}
}
func TestSpeechChangeWaitsForClosingPreview(t *testing.T) {
a, voice, _, prepared := preview_application(t)
selected := a.settings.live.Presentation.Speech
selected.Module = "preview"
a.preview_menu_speech(selected)
<-prepared
a.restore_speech_preview()
next := selected
next.Module = "requested"
a.change_speech(next, func(err error) {
if err != nil {
t.Error(err)
}
})
if !a.speechPending {
t.Fatal("deferred selection did not retain admission")
}
if len(a.preview.idle) != 1 {
t.Fatal("selection did not wait for preview restoration")
}
select {
case got := <-prepared:
t.Fatal("selection overtook the closing preview", got)
case <-a.speechResults:
t.Fatal("selection completed before the closing preview")
default:
}
finish_preview(t, a, voice)
if got := <-prepared; got.Module != a.settings.live.Presentation.Speech.Module {
t.Fatal("preview did not restore first", got)
}
finish_preview(t, a, voice)
if got := <-prepared; got.Module != "requested" {
t.Fatal("requested selection was not last", got)
}
finish_selection(t, a)
}
func TestOverrideRestorationWaitsForMenuPreview(t *testing.T) {
a, voice, _, prepared := preview_application(t)
override_read(t, a, override_screen(1, "ninjam"))
a.select_override("ninjam")
override_read(t, a, override_screen(1, "ninjam"))
current := a.overrideCurrent
if current == nil {
t.Fatal("application override did not activate")
}
selected := a.settings.live.Presentation.Speech
selected.Module = "preview"
a.preview_menu_speech(selected)
<-prepared
a.ui.active = true
a.ui.onClosed = a.restore_speech_preview
completed := false
if a.restore_override(func() { completed = true }) || completed || a.overrideCurrent != current {
t.Fatal("application context changed before its preview restored")
}
finish_preview(t, a, voice)
<-prepared
finish_preview(t, a, voice)
if !a.restore_override(func() { completed = true }) || !completed || a.overrideCurrent != nil {
t.Fatal("application context did not restore after its preview")
}
}
+17 -10
View File
@@ -58,16 +58,23 @@ func (a *application) change_speech(selected config.Speech, done func(error)) {
return
}
a.speechPending = true
go func() {
prepared, err := a.prepareSpeech(selected)
if err == nil {
err = a.replace_speech(prepared)
}
select {
case a.speechResults <- speechChangeResult{err: err, done: done}:
case <-a.ctx.Done():
}
}()
start := func() {
go func() {
prepared, err := a.prepareSpeech(selected)
if err == nil {
err = a.replace_speech(prepared)
}
select {
case a.speechResults <- speechChangeResult{err: err, done: done}:
case <-a.ctx.Done():
}
}()
}
// Closing a menu may still be restoring its old transport. Keep admission
// while it drains so that restoration cannot overwrite this selection.
if !a.after_speech_preview(start) {
start()
}
}
func speech_choice(source string) bool {
+13
View File
@@ -225,3 +225,16 @@ func TestQuickVoiceProfileDefaultRestoresSavedVoice(t *testing.T) {
t.Fatal("Default unavailable from a custom voice", settings.Presentation.Speech)
}
}
func TestQuickVoiceProfileOfferedWithoutProfiles(t *testing.T) {
settings := config.Defaults()
saved := config.Primary_voice(settings)
menu, voice := new_quick_settings("speech-dispatcher"), &fakeVoice{}
menu.savedVoice = &saved
for range 6 {
menu.command(command.NextQuickEntry, &settings, voice)
}
if got := voice.messages[len(voice.messages)-1]; got != "Voice profile Default" {
t.Fatal("saved voice without profiles not reported as Default", got)
}
}