Skip to content

Commit d85c604

Browse files
committed
search: auto-focus search field on navigate
I'm going to add a search changelog on this commit since I forgot to do so previously. Fixes: https://linear.app/damus/issue/DECK-538/auto-focus-search-field-on-search-view Changelog-Added: Added fulltext search ui Signed-off-by: William Casarin <[email protected]>
1 parent 8e0e42a commit d85c604

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

Diff for: crates/notedeck_columns/src/nav.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
edit_deck::{EditDeckResponse, EditDeckView},
1919
note::{PostAction, PostType},
2020
profile::EditProfileView,
21-
search::SearchView,
21+
search::{FocusState, SearchView},
2222
support::SupportView,
2323
RelayView, View,
2424
},
@@ -403,9 +403,23 @@ fn render_nav_body(
403403

404404
Route::Search => {
405405
let id = ui.id().with(("search", depth, col));
406+
let navigating = app
407+
.columns_mut(ctx.accounts)
408+
.column(col)
409+
.router()
410+
.navigating;
406411
let search_buffer = app.view_state.searches.entry(id).or_default();
407412
let txn = Transaction::new(ctx.ndb).expect("txn");
408413

414+
if navigating {
415+
search_buffer.focus_state = FocusState::Navigating
416+
} else if search_buffer.focus_state == FocusState::Navigating {
417+
// we're not navigating but our last search buffer state
418+
// says we were navigating. This means that navigating has
419+
// stopped. Let's make sure to focus the input field
420+
search_buffer.focus_state = FocusState::ShouldRequestFocus;
421+
}
422+
409423
SearchView::new(
410424
ctx.ndb,
411425
&txn,

Diff for: crates/notedeck_columns/src/route.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub enum Route {
2525
EditProfile(Pubkey),
2626
Support,
2727
NewDeck,
28-
/// Search screen
2928
Search,
3029
EditDeck(usize),
3130
}

Diff for: crates/notedeck_columns/src/ui/search/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::{error, info, warn};
1212

1313
mod state;
1414

15-
pub use state::{SearchQueryState, SearchState};
15+
pub use state::{FocusState, SearchQueryState, SearchState};
1616

1717
pub struct SearchView<'a> {
1818
query: &'a mut SearchQueryState,
@@ -57,7 +57,7 @@ impl<'a> SearchView<'a> {
5757
}
5858

5959
match self.query.state {
60-
SearchState::New => None,
60+
SearchState::New | SearchState::Navigating => None,
6161

6262
SearchState::Searched | SearchState::Typing => {
6363
if self.query.state == SearchState::Typing {
@@ -163,7 +163,7 @@ fn search_box(query: &mut SearchQueryState, ui: &mut egui::Ui) -> bool {
163163

164164
// Search input field
165165
//let font_size = notedeck::fonts::get_font_size(ui.ctx(), &NotedeckTextStyle::Body);
166-
ui.add_sized(
166+
let response = ui.add_sized(
167167
[ui.available_width(), search_height],
168168
TextEdit::singleline(&mut query.string)
169169
.hint_text(RichText::new("Search notes...").weak())
@@ -173,6 +173,11 @@ fn search_box(query: &mut SearchQueryState, ui: &mut egui::Ui) -> bool {
173173
.frame(false),
174174
);
175175

176+
if query.focus_state == FocusState::ShouldRequestFocus {
177+
response.request_focus();
178+
query.focus_state = FocusState::RequestedFocus;
179+
}
180+
176181
let after_len = query.string.len();
177182

178183
let changed = before_len != after_len;

Diff for: crates/notedeck_columns/src/ui/search/state.rs

+18
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ use std::time::Duration;
66
pub enum SearchState {
77
Typing,
88
Searched,
9+
Navigating,
910
New,
1011
}
1112

13+
#[derive(Debug, Eq, PartialEq)]
14+
pub enum FocusState {
15+
/// Get ready to focus
16+
Navigating,
17+
18+
/// We should request focus when we stop navigating
19+
ShouldRequestFocus,
20+
21+
/// We already focused, we don't need to do that again
22+
RequestedFocus,
23+
}
24+
1225
/// Search query state that exists between frames
1326
#[derive(Debug)]
1427
pub struct SearchQueryState {
@@ -20,6 +33,10 @@ pub struct SearchQueryState {
2033
/// again next frames
2134
pub state: SearchState,
2235

36+
/// A bit of context to know if we're navigating to the view. We
37+
/// can use this to know when to request focus on the textedit
38+
pub focus_state: FocusState,
39+
2340
/// When was the input updated? We use this to debounce searches
2441
pub debouncer: Debouncer,
2542

@@ -39,6 +56,7 @@ impl SearchQueryState {
3956
string: "".to_string(),
4057
state: SearchState::New,
4158
notes: TimelineTab::default(),
59+
focus_state: FocusState::Navigating,
4260
debouncer: Debouncer::new(Duration::from_millis(200)),
4361
}
4462
}

Diff for: crates/notedeck_columns/src/ui/side_panel.rs

-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ impl<'a> DesktopSidePanel<'a> {
324324
}
325325
SidePanelAction::Search => {
326326
// TODO
327-
info!("Clicked search button");
328327
if router.top() == &Route::Search {
329328
router.go_back();
330329
} else {

0 commit comments

Comments
 (0)