Search box doesn't pick up the newly selected text from the buffer #20868

Closed
opened 2026-01-31 07:26:23 +00:00 by claunia · 2 comments
Owner

Originally created by @tusharsnx on GitHub (Nov 22, 2023).

Windows Terminal version

1.19.3172.0

Windows build number

10.0.22631.0

Other Software

No response

Steps to reproduce

  1. Select a piece of text in the terminal
  2. Trigger Search Action.
  3. The search criteria (needle) is now the selected text.
  4. Close SearchBox
  5. Select a different piece of text.
  6. Trigger Search Action.
  7. The search criteria is still the old one.

Expected Behavior

At step 7, needle should be the newly selected text, instead of the old one.

Actual Behavior

At step 7, needle is still the previously selected text.

Originally created by @tusharsnx on GitHub (Nov 22, 2023). ### Windows Terminal version 1.19.3172.0 ### Windows build number 10.0.22631.0 ### Other Software _No response_ ### Steps to reproduce 1. Select a piece of text in the terminal 2. Trigger Search Action. 3. The search criteria (`needle`) is now the selected text. 4. Close SearchBox 5. Select a different piece of text. 6. Trigger Search Action. 7. The search criteria is still the old one. ### Expected Behavior At step 7, `needle` should be the newly selected text, instead of the old one. ### Actual Behavior At step 7, `needle` is still the previously selected text.
Author
Owner

@github-actions[bot] commented on GitHub (Nov 22, 2023):

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@github-actions[bot] commented on GitHub (Nov 22, 2023): Hi I'm an AI powered bot that finds similar issues based off the issue title. Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you! ### Closed similar issues: - [Selected text should populate search box (#8307)](https://github.com/microsoft/terminal/issues/8307), similarity score: 0.83 > Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Author
Owner

@tusharsnx commented on GitHub (Nov 22, 2023):

Regressed in #0cbde

It's a combination of these codes that creates the race condition. The VisibilityChanged event overwrites _selection before we can read the newly selected text using it.

63b3820a18/src/cascadia/TerminalControl/SearchBoxControl.cpp (L22-L32)

It may not be obvious but, if we look at the TermControl::CreateSearchBoxControl code, it fires two events when a selection is active.

63b3820a18/src/cascadia/TerminalControl/TermControl.cpp (L404-L433)

Once, when we change the visibility, and second when we populate the text box with the selected text. Both calls But by the time we call _core.SelectedText(), the visibility event handler has already overwritten CoreControl::_selection, and it now shows that the previous highlighted match is being selected 🫤

Trace:

  1. We still haven't populated the search box with the currently selected text, and we called _searchBox->Visibility(Visibility::Visible):
    VisibilityChanged -> SearchBoxControl::_SearchChangedHandlers() -> TermControl::_SearchChanged() -> Get the lock on Terminal -> ControlCore::Search() -> _searcher.SelectCurrent(). The selection now highlights previously selected text.

  2. Now when we call _searchBox->_core.SelectedText(true), we receive the previously selected text and populate the search box with this stale text. After this, SearchBoxControl::_SearchChangedHandlers() is invoked by the SearchBoxControl::TextBoxTextChanged handler like normally it would, but unfortunately, the needle isn't correct.

@tusharsnx commented on GitHub (Nov 22, 2023): Regressed in [#0cbde](https://github.com/microsoft/terminal/commit/0cbde94e4b2e530eac00acc7de1808fe2fd10273) It's a combination of these codes that creates the race condition. The VisibilityChanged event overwrites `_selection` before we can read the newly selected text using it. https://github.com/microsoft/terminal/blob/63b3820a18be096a4b1950e335c9605267440734/src/cascadia/TerminalControl/SearchBoxControl.cpp#L22-L32 It may not be obvious but, if we look at the `TermControl::CreateSearchBoxControl` code, it fires two events when a selection is active. https://github.com/microsoft/terminal/blob/63b3820a18be096a4b1950e335c9605267440734/src/cascadia/TerminalControl/TermControl.cpp#L404-L433 Once, when we change the visibility, and second when we populate the text box with the selected text. Both calls But by the time we call `_core.SelectedText()`, the visibility event handler has already overwritten `CoreControl::_selection`, and it now shows that the previous highlighted match is being selected 🫤 Trace: 1. We still haven't populated the search box with the currently selected text, and we called `_searchBox->Visibility(Visibility::Visible)`: VisibilityChanged -> `SearchBoxControl::_SearchChangedHandlers()` -> `TermControl::_SearchChanged()` -> Get the lock on `Terminal` -> `ControlCore::Search()` -> `_searcher.SelectCurrent()`. The selection now highlights previously selected text. 2. Now when we call `_searchBox->_core.SelectedText(true)`, we receive the previously selected text and populate the search box with this stale text. After this, `SearchBoxControl::_SearchChangedHandlers()` is invoked by the `SearchBoxControl::TextBoxTextChanged` handler like normally it would, but unfortunately, the needle isn't correct.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20868