Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Room list search bar #232

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/home/room_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ live_design! {
}

RoomPreview = {{RoomPreview}} {
height: 65.
// Wraps the RoomPreviewContent in an AdaptiveView
// to change the displayed content (and its layout) based on the available space in the sidebar.
adaptive_preview = <AdaptiveView> {
Expand Down
35 changes: 33 additions & 2 deletions src/home/rooms_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use matrix_sdk::ruma::{MilliSecondsSinceUnixEpoch, OwnedRoomId};

use crate::{app::AppState, sliding_sync::{submit_async_request, MatrixRequest, PaginationDirection}};

use super::room_preview::RoomPreviewAction;
use super::{room_preview::RoomPreviewAction, rooms_sidebar::{RoomsSideBarAction, RoomsSideBarFilter}};

/// Whether to pre-paginate visible rooms at least once in order to
/// be able to display the latest message in the room preview,
Expand Down Expand Up @@ -232,6 +232,25 @@ impl RoomsList {
format!("Loaded {} rooms.", self.all_rooms.len())
};
}
fn filter_rooms(&mut self, keywords: &str) {
let keywords = keywords.to_lowercase();

if keywords.is_empty() {
self.display_filter = RoomDisplayFilter::default();
self.displayed_rooms = self.all_rooms.keys().cloned().collect();
return;
}

self.display_filter = RoomDisplayFilter(Box::new(move |room| {
let room_name = room.room_name.as_ref().map(|n| n.to_lowercase());
room_name.as_ref().map_or(false, |n| n.contains(&keywords))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is okay for now as a rudimentary search, but in the future we'd want to fuzzy match the room name, room ID, room members, etc against one or more of the keywords (rather than just a straight-up "contains keywords" conditional)

}));

self.displayed_rooms = self.all_rooms.iter()
.filter(|(_, room)| (self.display_filter)(&room))
.map(|(room_id, _)| room_id.clone())
.collect();
}
}

impl Widget for RoomsList {
Expand Down Expand Up @@ -369,6 +388,7 @@ impl Widget for RoomsList {
self.redraw(cx);
}
}
self.widget_match_event(cx, event, scope);
}


Expand Down Expand Up @@ -397,7 +417,6 @@ impl Widget for RoomsList {

while let Some(item_id) = list.next_visible_item(cx) {
let mut scope = Scope::empty();

// Draw the room preview for each room in the `displayed_rooms` list.
let room_to_draw = self.displayed_rooms
.get(item_id)
Expand Down Expand Up @@ -443,3 +462,15 @@ impl Widget for RoomsList {
}

}

impl WidgetMatchEvent for RoomsList {
fn handle_actions(&mut self, cx: &mut Cx, actions:&Actions, _scope: &mut Scope) {
for action in actions {
if let RoomsSideBarAction::Filter { keywords, filter } = action.as_widget_action().cast() {
if filter == RoomsSideBarFilter::Rooms {
self.filter_rooms(&keywords);
}
}
}
}
}
138 changes: 138 additions & 0 deletions src/home/rooms_sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use makepad_widgets::*;

use crate::shared::search_bar::SearchBarAction;

live_design! {
import makepad_widgets::base::*;
import makepad_widgets::theme_desktop_dark::*;
Expand All @@ -10,6 +12,85 @@ live_design! {
import crate::shared::adaptive_view::AdaptiveView;

import crate::home::rooms_list::RoomsList;
import crate::shared::cached_widget::CachedWidget;

ICON_COLLAPSE = dep("crate://self/resources/icons/collapse.svg")
ICON_ADD = dep("crate://self/resources/icons/add.svg")

CollapsableTitle = <View> {
width: Fill, height: Fit
flow: Right, spacing: 10.
align: {x: 0.0, y: 0.5}
collapse_icon = <Icon> {
draw_icon: {
svg_file: (ICON_COLLAPSE),
uniform rotation_angle: -90.0,
fn get_color(self) -> vec4 {
// return #666;
return (COLOR_TEXT_IDLE);
}

// Support rotation of the icon
fn clip_and_transform_vertex(self, rect_pos: vec2, rect_size: vec2) -> vec4 {
let clipped: vec2 = clamp(
self.geom_pos * rect_size + rect_pos,
self.draw_clip.xy,
self.draw_clip.zw
)
self.pos = (clipped - rect_pos) / rect_size

// Calculate the texture coordinates based on the rotation angle
let angle_rad = self.rotation_angle * 3.14159265359 / 180.0;
let cos_angle = cos(angle_rad);
let sin_angle = sin(angle_rad);
let rot_matrix = mat2(
cos_angle, -sin_angle,
sin_angle, cos_angle
);
self.tex_coord1 = mix(
self.icon_t1.xy,
self.icon_t2.xy,
(rot_matrix * (self.pos.xy - vec2(0.5))) + vec2(0.5)
);

return self.camera_projection * (self.camera_view * (self.view_transform * vec4(
clipped.x,
clipped.y,
self.draw_depth + self.draw_zbias,
1.
)))
}
}
icon_walk: {width: 12, height: 12}
}

title = <Label> {
draw_text: {
color: #x0,
text_style: <TITLE_TEXT>{ font_size: 11}
}
}

<View> {
width: Fill
}

add_icon = <View> {
width: Fit
visible: false
padding: {right: 10}
align: {x: 0.5, y: 0.5}
<Icon> {
icon_walk: {width: 10, height: 10}
draw_icon: {
svg_file: (ICON_ADD),
fn get_color(self) -> vec4 {
return (COLOR_TEXT_IDLE);
}
}
}
}
}

RoomsView = {{RoomsView}} {
show_bg: true,
Expand All @@ -34,6 +115,11 @@ live_design! {
text_style: <TITLE_TEXT>{}
}
}
<SearchBar> {
input = {
empty_message: "Search for rooms..."
}
}
<CachedWidget> {
rooms_list = <RoomsList> {}
}
Expand All @@ -53,6 +139,25 @@ live_design! {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoomsSideBarFilter {
Copy link
Member

@kevinaboos kevinaboos Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're conflating filter kinds with room categories.... these are two separate things.

The filter categories would be things like:

  • search by room name
  • search by room ID
  • search by room member (user name and/or user ID)
  • search by anything (any keywords match)
  • etc

You could also have additional filter kinds for "is_direct" or "is_room" (not direct), but those wouldn't be expressible as enum variants because you'd only be able to select one. They would have to be bitflags/bitsets such that you could select multiple filter criteria at once. Or, alternatively, you could add a series of booleans to a "filter type" struct. The design is up to you, but try to think deeply about the possible valid combinations of filter kinds and search terms.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other possible filter dimensions include whether a room is marked as a favorite, whether a user has left a room, joined a room, or is just invited to a room, etc.

The list of built-in filter functions are here; perhaps you could just re-use most of them. But note that we do not want to use them with the RoomListDynamicEntriesController because that modifies the actual room_list_service's list of rooms, which I don't want to deal with.

https://matrix-org.github.io/matrix-rust-sdk/matrix_sdk_ui/room_list_service/filters/index.html#functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. I got it. So i will do that and then change this issue's label. Thanks

/// Filter all
All,
/// Filter people
People,
/// Filter rooms
Rooms,
}

#[derive(Debug, Clone, DefaultNone)]
pub enum RoomsSideBarAction {
/// Filter
Filter {
keywords: String,
filter: RoomsSideBarFilter,
},
None
}
#[derive(Widget, Live, LiveHook)]
pub struct RoomsView {
#[deref]
Expand All @@ -62,9 +167,42 @@ pub struct RoomsView {
impl Widget for RoomsView {
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
self.view.handle_event(cx, event, scope);
self.widget_match_event(cx, event, scope);
}

fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
self.view.draw_walk(cx, scope, walk)
}
}

impl WidgetMatchEvent for RoomsView {
fn handle_actions(&mut self, cx: &mut Cx, actions:&Actions, scope: &mut Scope) {
let widget_uid = self.widget_uid();

for action in actions.iter() {
match action.as_widget_action().cast() {
SearchBarAction::Search(keywords) => {
cx.widget_action(
widget_uid,
&scope.path,
RoomsSideBarAction::Filter {
keywords: keywords.clone(),
filter: RoomsSideBarFilter::Rooms,
}
);
},
SearchBarAction::ResetSearch => {
cx.widget_action(
widget_uid,
&scope.path,
RoomsSideBarAction::Filter {
keywords: "".to_string(),
filter: RoomsSideBarFilter::Rooms,
}
);
}
_ => {}
}
}
}
}