@@ -17,7 +17,7 @@ use matrix_sdk_ui::timeline::{Profile, TimelineDetails};
1717use rangemap:: RangeSet ;
1818
1919use 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 {
335334impl 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
822851impl 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
0 commit comments