From 322f151344b4e8913ea823e49aef4262c1bb2118 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sun, 15 Nov 2026 15:20:33 -0500 Subject: [PATCH] Attempt to fix bugs where Cthulhu was reading previous entry in comobo boxes in addition to the current entry. --- internal/app/combo_presentation.go | 15 +++++++++ internal/app/combo_presentation_test.go | 32 +++++++++++++++++++ internal/app/event_presentation.go | 6 ++++ .../atspi/adapter_event_callback_native.go | 6 ++++ 4 files changed, 59 insertions(+) diff --git a/internal/app/combo_presentation.go b/internal/app/combo_presentation.go index 5c86732..c773f5d 100644 --- a/internal/app/combo_presentation.go +++ b/internal/app/combo_presentation.go @@ -37,3 +37,18 @@ func (history *comboPresentation) observe(event atspi.Event) (text string, updat } return text, update, duplicate } + +// echoesSelection reports a closed GTK combo's menu-item selection whose value +// the focused combo already presented. GTK sends the combo's name change first. +func (history *comboPresentation) echoesSelection(event atspi.Event) bool { + if event.Type != atspi.SelectionChangedEvent || event.Source.Role != "menu item" || + !history.focus.Valid() || atspi.NewObjectID(event.Source.BusName, event.OriginPath) != history.focus { + return false + } + text := event.Source.Name + duplicate := text != "" && text == history.value + if text != "" { + history.value = text + } + return duplicate +} diff --git a/internal/app/combo_presentation_test.go b/internal/app/combo_presentation_test.go index 9fc8896..8887d6a 100644 --- a/internal/app/combo_presentation_test.go +++ b/internal/app/combo_presentation_test.go @@ -8,6 +8,7 @@ import ( "context" "log/slog" "reflect" + "strconv" "testing" "git.stormux.org/storm/cthulhu/internal/atspi" @@ -58,3 +59,34 @@ func TestComboSelectionSpeaksValueWithoutRepeatingRoleSound(t *testing.T) { }) } } + +func TestClosedGTKComboSelectionSpeaksOnlyNewValue(t *testing.T) { + queue := &modeSoundQueue{speech: &fakeSpeechQueue{spoken: make(chan string, 32)}, sounds: make(chan sound.Cue, 32)} + adapter := &fakeAdapter{events: make(chan atspi.Event, 32)} + combo := atspi.Snapshot{BusName: ":1.70", Path: "/combo", Role: "combo box", Name: "Speech synthesizer", Description: "kokoro", Focused: true} + focus := atspi.NewEvent(atspi.FocusedStateEvent, combo) + focus.Detail1 = 1 + adapter.events <- focus + for index, value := range []string{"espeak-ng", "kokoro", "rhvoice"} { + // GTK renames the combo, selects the menu item, then restores the label. + combo.Description, combo.Focused = value, false + adapter.events <- atspi.NewEvent(atspi.AccessibleNameEvent, combo) + item := atspi.Snapshot{BusName: combo.BusName, Path: "/item" + strconv.Itoa(index), Role: "menu item", Name: value} + selection := atspi.NewEvent(atspi.SelectionChangedEvent, item) + selection.OriginPath = combo.Path + adapter.events <- selection + adapter.events <- atspi.NewEvent(atspi.AccessibleNameEvent, combo) + } + close(adapter.events) + options := presentation.DefaultOptions() + processEventsWithInput(context.Background(), slog.New(slog.DiscardHandler), queue, adapter, nil, nil, 0, nil, nil, nil, nil, options) + close(queue.speech.spoken) + var got []string + for text := range queue.speech.spoken { + got = append(got, text) + } + want := []string{"Speech synthesizer, combo box, kokoro", "espeak-ng", "kokoro", "rhvoice"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("speech = %q, want %q", got, want) + } +} diff --git a/internal/app/event_presentation.go b/internal/app/event_presentation.go index c763ba6..97908b8 100644 --- a/internal/app/event_presentation.go +++ b/internal/app/event_presentation.go @@ -62,6 +62,7 @@ func (presenter *eventPresenter) speak(event atspi.Event, capturedIntent ...inpu if comboDuplicate { return } + comboSelectionEcho := state.comboHistory.echoesSelection(event) sequence := recentInput(inputSource).Sequence intent := recentInput(inputSource).Intent if len(capturedIntent) != 0 { @@ -185,6 +186,11 @@ func (presenter *eventPresenter) speak(event atspi.Event, capturedIntent ...inpu } spoken, fallbackSpeech = prompt.focusSpeech(spoken.WithText(text), fallbackSpeech.WithText(fallbackText)) text, fallbackText = spoken.Text, fallbackSpeech.Text + if comboSelectionEcho { + // Keep the item's position cue without repeating the combo's value. + spoken, fallbackSpeech = spoken.WithText(""), fallbackSpeech.WithText("") + text, fallbackText = "", "" + } cueKey := strings.Join(cueParts, "\x00") // Adjacent characters can have the same text while the caret moves. // Treat each character caret event as a new review position. diff --git a/internal/atspi/adapter_event_callback_native.go b/internal/atspi/adapter_event_callback_native.go index cbf814f..ef296c2 100644 --- a/internal/atspi/adapter_event_callback_native.go +++ b/internal/atspi/adapter_event_callback_native.go @@ -110,6 +110,12 @@ func (adapter *Adapter) captureNativeEventSnapshot(event *C.AtspiEvent, target * // Document focus/load presentation needs only the title. Real caret // events still capture text, including carets hosted on document roots. captureCaret := !isDocumentLifecycleEvent(eventType) && eventType != BusyStateEvent + // GTK emits a combo's name change before later "changed" handlers can + // publish its new value as the description. The cached description is + // then the previous selection, so refresh only this combo's cache. + if eventType == AccessibleNameEvent && C.atspi_accessible_get_role(target, nil) == C.ATSPI_ROLE_COMBO_BOX { + C.atspi_accessible_clear_cache_single(target) + } if eventType == FocusEvent || eventType == FocusedStateEvent { role := C.atspi_accessible_get_role(target, nil) captureCaret = role != C.ATSPI_ROLE_DOCUMENT_WEB && role != C.ATSPI_ROLE_DOCUMENT_FRAME && role != C.ATSPI_ROLE_EMBEDDED