Skip to content

Commit 130952f

Browse files
committed
Added regex
1 parent 0aab241 commit 130952f

8 files changed

Lines changed: 101 additions & 61 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bytesize = "2.0"
5252
bitflags = "2.6.0"
5353
indexmap = "2.6.0"
5454
blurhash = { version = "0.2.3", default-features = false }
55+
regex = "1.11.1"
5556

5657
tsp_sdk = { git = "https://github.com/openwallet-foundation-labs/tsp.git", optional = true, features = ["async", "resolve"] }
5758
# tsp_sdk = { version = "0.8.0", optional = true, default-features = false, features = ["async", "resolve"] }

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl MatchEvent for App {
359359
// }
360360
// _ => {}
361361
// }
362-
if let MessageSearchAction::Clicked(_) = action.as_widget_action().cast() {
362+
if let MessageSearchAction::Clicked = action.as_widget_action().cast() {
363363
cx.widget_action(
364364
self.ui.widget_uid(),
365365
&Scope::default().path,

src/home/home_screen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ live_design! {
6969
visible: false,
7070
align: {x: 1.0},
7171

72-
cw = <CachedWidget> {
72+
<CachedWidget> {
7373
message_search_input_bar = <MessageSearchInputBar> {
7474
width: 300,
7575
}

src/right_panel/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,10 @@ impl Widget for RightPanel {
7979

8080
impl WidgetMatchEvent for RightPanel {
8181
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions, _scope: &mut Scope) {
82-
if self.view.button(id!(back_button)).clicked(actions) {
83-
self.view.set_visible(cx, true);
84-
}
8582

8683
for action in actions.iter() {
8784
match action.as_widget_action().cast() {
88-
MessageSearchAction::Clicked(_) => {
85+
MessageSearchAction::Clicked => {
8986
self.view.set_visible(cx, true);
9087
self.view.stack_navigation(id!(view_stack)).pop_to_root(cx);
9188
self.view

src/right_panel/search_message.rs

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use matrix_sdk_ui::timeline::{Profile, TimelineDetails};
1717
use rangemap::RangeSet;
1818

1919
use crate::{
20-
app::AppState,
20+
app::{AppState, AppStateAction},
2121
home::{
2222
room_screen::{
2323
populate_text_message_content, ItemDrawnStatus, JumpToMessageRequest, MessageWidgetRefExt,
@@ -84,9 +84,9 @@ live_design! {
8484
draw_bg: {
8585
instance color: (COLOR_BUTTON_GREY)
8686
instance color_hover: #fef65b
87-
instance border_width: 1.5
88-
instance radius: 3.0
89-
instance hover: 0.0
87+
uniform border_width: 1.5
88+
uniform border_radius: 4.0
89+
uniform hover: 0.0
9090
fn get_color(self) -> vec4 {
9191
return mix(self.color, mix(self.color, self.color_hover, 0.2), self.hover)
9292
}
@@ -97,7 +97,7 @@ live_design! {
9797
self.border_width,
9898
self.rect_size.x - self.border_width * 2.0,
9999
self.rect_size.y - self.border_width * 2.0,
100-
max(1.0, self.radius)
100+
max(1.0, self.border_radius)
101101
)
102102
sdf.fill(self.get_color());
103103
return sdf.result;
@@ -283,7 +283,6 @@ pub fn highlight_search_terms_in_message(message: &mut RoomMessageEventContent,
283283
} else {
284284
let formatted_string = apply_highlights(text.body.clone(), highlights);
285285
// issue of returning one result
286-
println!("formatted_string {:?}", formatted_string);
287286
formatted_string
288287
};
289288
text.formatted = Some(FormattedBody::html(formatted));
@@ -335,8 +334,6 @@ struct SearchResults {
335334
impl Widget for SearchResults {
336335
/// Handles events and actions for the SearchResults widget and its inner Timeline view.
337336
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
338-
self.view.handle_event(cx, event, scope);
339-
340337
// Handle pagination when user scrolls to the top
341338
if let Event::Actions(actions) = event {
342339
let search_portal_list = self.portal_list(id!(searched_messages.list));
@@ -347,7 +344,7 @@ impl Widget for SearchResults {
347344
scope,
348345
);
349346
}
350-
347+
self.view.handle_event(cx, event, scope);
351348
self.widget_match_event(cx, event, scope);
352349
}
353350

@@ -434,11 +431,11 @@ impl SearchResults {
434431
portal_list: &PortalListRef,
435432
_scope: &mut Scope,
436433
) {
434+
let total_items = self.search_state.items.len();
435+
if total_items == 0 { return };
437436
if self.search_state.is_fully_paginated { return };
438437
if !portal_list.scrolled(actions) { return };
439-
440438
let first_index = portal_list.first_id();
441-
let total_items = self.search_state.items.len();
442439
let visible_items: usize = portal_list.visible_items();
443440
log!("Scrolled down from item {} --> {:?}, before sending room {:?} last index {:?}",
444441
self.search_state.last_scrolled_index, total_items, self.room_id, first_index + visible_items
@@ -530,11 +527,14 @@ impl SearchResults {
530527
fn handle_search_bar_action(&mut self, cx: &mut Cx, scope: &mut Scope, action: &Action) {
531528
match action.as_widget_action().cast() {
532529
MessageSearchAction::Changed(search_term) => {
530+
self.hide_bottom_space(cx);
533531
let search_result_summary_ref =
534532
self.search_result_summary(id!(search_result_plane));
535-
search_result_summary_ref.reset(cx);
536533
self.search_state = SearchState::default();
537534
if search_term.is_empty() {
535+
self.view
536+
.button_set(ids!(search_all_rooms_button, search_again_button))
537+
.set_enabled(cx, false);
538538
// Abort previous inflight search request regardless of message choice if search term is empty.
539539
submit_async_request(MatrixRequest::SearchMessages {
540540
criteria: create_search_criteria(search_term, MessageSearchChoice::AllRooms),
@@ -543,6 +543,9 @@ impl SearchResults {
543543
});
544544
return;
545545
}
546+
self.view
547+
.button_set(ids!(search_all_rooms_button, search_again_button))
548+
.set_enabled(cx, true);
546549
if let Some(selected_room) = {
547550
let app_state = scope.data.get::<AppState>().unwrap();
548551
app_state.selected_room.clone()
@@ -573,22 +576,30 @@ impl SearchResults {
573576
});
574577
}
575578
}
576-
MessageSearchAction::Clicked(search_term) => {
579+
MessageSearchAction::Clicked => {
577580
let search_result_summary_ref =
578581
self.search_result_summary(id!(search_result_plane));
579-
if let Some(selected_room) = {
580-
let app_state = scope.data.get::<AppState>().unwrap();
581-
app_state.selected_room.clone()
582-
} {
583-
let room_id = selected_room.room_id();
584-
let criteria = create_search_criteria(search_term, MessageSearchChoice::OneRoom(room_id.clone()));
585-
search_result_summary_ref.display_search_criteria(cx, scope, criteria);
582+
let message_search_input_bar_ref = cx.get_global::<MessageSearchInputBarRef>();
583+
let search_term = message_search_input_bar_ref.get_text();
584+
if search_term.is_empty() {
585+
search_result_summary_ref.display_instruction(cx);
586586
}
587587
}
588588
_ => {}
589589
}
590590
}
591591

592+
fn handle_room_focus_changed_action(&mut self, cx: &mut Cx, action: &Action) {
593+
match action.as_widget_action().cast() {
594+
AppStateAction::RoomFocused(_room_id) => {
595+
// Show the search again button
596+
self.view
597+
.button(id!(search_again_button))
598+
.set_visible(cx, true);
599+
}
600+
_ => { }
601+
}
602+
}
592603
/// Displays the loading view for backwards pagination for search result.
593604
fn display_bottom_space(&mut self, cx: &mut Cx) {
594605
self.view.view(id!(bottom_space)).set_visible(cx, true);
@@ -604,6 +615,7 @@ impl WidgetMatchEvent for SearchResults {
604615
fn handle_actions(&mut self, cx: &mut Cx, actions: &Actions, scope: &mut Scope) {
605616
for action in actions.iter() {
606617
self.handle_search_bar_action(cx, scope, action);
618+
self.handle_room_focus_changed_action(cx, action);
607619
match action.downcast_ref() {
608620
Some(SearchResultAction::Received(results)) => {
609621
self.process_search_results(cx, scope, results.clone());
@@ -625,17 +637,22 @@ impl WidgetMatchEvent for SearchResults {
625637
}
626638
let search_all_rooms_button = self.view.button(id!(search_all_rooms_button));
627639
if search_all_rooms_button.clicked(actions) {
640+
let message_search_input_bar_ref = cx.get_global::<MessageSearchInputBarRef>();
641+
let search_term = message_search_input_bar_ref.get_text();
642+
if search_term.is_empty() {
643+
continue;
644+
}
645+
self.search_state = SearchState::default();
628646
// Disable the button during search
629647
search_all_rooms_button.set_enabled(cx, false);
648+
let criteria = create_search_criteria(search_term, MessageSearchChoice::AllRooms);
630649
let search_result_summary_ref =
631650
self.search_result_summary(id!(search_result_plane));
632-
search_result_summary_ref.reset(cx);
651+
search_result_summary_ref.set_includes_all_rooms(cx, true);
652+
search_result_summary_ref.display_search_criteria(cx, scope, criteria.clone());
633653
self.display_bottom_space(cx);
634-
self.search_state = SearchState::default();
635-
let message_search_input_bar_ref = cx.get_global::<MessageSearchInputBarRef>();
636-
let search_term = message_search_input_bar_ref.get_text();
637654
submit_async_request(MatrixRequest::SearchMessages {
638-
criteria: create_search_criteria(search_term, MessageSearchChoice::AllRooms),
655+
criteria,
639656
next_batch: None,
640657
abort_previous_search: true,
641658
});
@@ -655,6 +672,10 @@ impl WidgetMatchEvent for SearchResults {
655672
} {
656673
let search_result_summary_ref =
657674
self.search_result_summary(id!(search_result_plane));
675+
let Some(previous_room_id) = &self.room_id else { continue; };
676+
if previous_room_id != selected_room.room_id() {
677+
self.search_state = SearchState::default();
678+
}
658679
let includes_all_rooms = search_result_summary_ref.includes_all_rooms();
659680
let room_id = selected_room.room_id();
660681
self.room_id = Some(room_id.clone());
@@ -673,11 +694,23 @@ impl WidgetMatchEvent for SearchResults {
673694
search_all_rooms_button.set_enabled(cx, false);
674695
let message_search_input_bar_ref = cx.get_global::<MessageSearchInputBarRef>();
675696
let search_term = message_search_input_bar_ref.get_text();
676-
submit_async_request(MatrixRequest::SearchMessages {
677-
criteria: create_search_criteria(search_term, MessageSearchChoice::OneRoom(room_id.clone())),
678-
next_batch: None,
679-
abort_previous_search: true,
680-
});
697+
if includes_all_rooms {
698+
let criteria = create_search_criteria(search_term, MessageSearchChoice::AllRooms);
699+
search_result_summary_ref.display_search_criteria(cx, scope, criteria.clone());
700+
submit_async_request(MatrixRequest::SearchMessages {
701+
criteria,
702+
next_batch: None,
703+
abort_previous_search: true,
704+
});
705+
} else {
706+
let criteria = create_search_criteria(search_term, MessageSearchChoice::OneRoom(room_id.clone()));
707+
search_result_summary_ref.display_search_criteria(cx, scope, criteria.clone());
708+
submit_async_request(MatrixRequest::SearchMessages {
709+
criteria,
710+
next_batch: None,
711+
abort_previous_search: true,
712+
});
713+
}
681714
}
682715
}
683716
}
@@ -755,7 +788,7 @@ impl SearchResultSummary {
755788
/// This function is used to display the search criteria in the top-right of the room screen.
756789
/// It is typically used when a new search is initiated.
757790
///
758-
fn display_search_criteria(&mut self, cx: &mut Cx, scope: &mut Scope, search_criteria: MatrixCriteria) {
791+
pub fn display_search_criteria(&mut self, cx: &mut Cx, scope: &mut Scope, search_criteria: MatrixCriteria) {
759792
self.room_name = scope.data.get::<AppState>().and_then(|f| {
760793
f.selected_room
761794
.as_ref()
@@ -784,7 +817,7 @@ impl SearchResultSummary {
784817
/// Display result summary.
785818
///
786819
/// This is used to display the number of search results and the search term at the top of the search result.
787-
fn display_result_summary(&mut self,
820+
pub fn display_result_summary(&mut self,
788821
cx: &mut Cx,
789822
results: &SearchResultReceived
790823
) {
@@ -809,17 +842,25 @@ impl SearchResultSummary {
809842
);
810843
}
811844

812-
/// Resets the search result summary and set the loading view back to visible.
813-
///
814-
/// This function clears the summary text and makes the loading indicator visible.
815-
/// It is typically used when a new search is initiated or search results are being cleared.
816-
fn reset(&mut self, cx: &mut Cx) {
817-
self.view.markdown(id!(summary_label)).set_text(cx, "");
818-
self.visible = false;
845+
/// Set whether the search criteria to include all rooms.
846+
pub fn set_includes_all_rooms(&mut self, _cx: &mut Cx, includes_all_rooms: bool) {
847+
self.includes_all_rooms = includes_all_rooms;
819848
}
820849
}
821850

822851
impl SearchResultSummaryRef {
852+
/// Display instruction to type to search.
853+
pub fn display_instruction(&self, cx: &mut Cx) {
854+
let Some(mut inner) = self.borrow_mut() else {
855+
return;
856+
};
857+
inner.view.markdown(id!(summary_label)).set_text(
858+
cx,
859+
"Type to search.",
860+
);
861+
inner.visible = true;
862+
}
863+
823864
/// See [`SearchResultSummary::display_search_criteria()`].
824865
pub fn display_search_criteria(&self, cx: &mut Cx, scope: &mut Scope, search_criteria: MatrixCriteria) {
825866
let Some(mut inner) = self.borrow_mut() else {
@@ -836,21 +877,21 @@ impl SearchResultSummaryRef {
836877
inner.display_result_summary(cx, results);
837878
}
838879

839-
/// See [`SearchResultSummary::reset()`].
840-
pub fn reset(&self, cx: &mut Cx) {
841-
let Some(mut inner) = self.borrow_mut() else {
842-
return;
843-
};
844-
inner.reset(cx);
845-
}
846-
847880
/// See [`SearchResultSummary::includes_all_rooms()`].
848881
pub fn includes_all_rooms(&self) -> bool {
849882
let Some(inner) = self.borrow() else {
850883
return false;
851884
};
852885
inner.includes_all_rooms
853886
}
887+
888+
/// See [`SearchResultSummary::set_includes_all_rooms()`].
889+
pub fn set_includes_all_rooms(&self, cx: &mut Cx, includes_all_rooms: bool) {
890+
let Some(mut inner) = self.borrow_mut() else {
891+
return;
892+
};
893+
inner.set_includes_all_rooms(cx, includes_all_rooms);
894+
}
854895
}
855896

856897
/// Search result as timeline item

src/shared/message_search_input_bar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub enum MessageSearchAction {
8383
/// Emitted when the user has changed the text (after debounce delay or clear button).
8484
Changed(String),
8585
/// Emitted when the user has clicked the input bar.
86-
Clicked(String),
86+
Clicked,
8787
None,
8888
}
8989

@@ -98,16 +98,16 @@ impl Widget for MessageSearchInputBar {
9898
);
9999
}
100100
}
101-
self.view.handle_event(cx, event, scope);
102101
let area = self.text_input(id!(input)).area();
103102
if let Hit::FingerDown(..) = event.hits(cx, area) {
104103
let widget_uid = self.widget_uid();
105104
cx.widget_action(
106105
widget_uid,
107106
&scope.path,
108-
MessageSearchAction::Clicked(self.view.text_input(id!(input)).text())
107+
MessageSearchAction::Clicked
109108
);
110109
}
110+
self.view.handle_event(cx, event, scope);
111111
self.widget_match_event(cx, event, scope);
112112
}
113113

src/sliding_sync.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ async fn async_worker(
11821182
Ok(response) => {
11831183
let next_batch =
11841184
response.search_categories.room_events.next_batch.clone();
1185-
let mut last_room_id = None;
1185+
let mut room_id_for_grouping = None;
11861186
let result = response.search_categories;
11871187
let mut items = Vector::new();
11881188
let mut profile_infos = BTreeMap::new();
@@ -1232,13 +1232,13 @@ async fn async_worker(
12321232
);
12331233
// Include all rooms in the search results.
12341234
if room_filter.is_none() {
1235-
if let Some(ref mut last_room_id) = last_room_id {
1236-
if last_room_id != &event_room_id {
1237-
*last_room_id = event_room_id.clone();
1235+
if let Some(ref mut room_id_for_grouping) = room_id_for_grouping {
1236+
if room_id_for_grouping != &event_room_id {
1237+
*room_id_for_grouping = event_room_id.clone();
12381238
items.push_back(right_panel::search_message::SearchResultItem::RoomHeader(event_room_id));
12391239
}
12401240
} else {
1241-
last_room_id = Some(event_room_id.clone());
1241+
room_id_for_grouping = Some(event_room_id.clone());
12421242
items.push_back(right_panel::search_message::SearchResultItem::RoomHeader(event_room_id));
12431243
}
12441244
}

0 commit comments

Comments
 (0)