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

Add search modes #5

Merged
merged 5 commits into from
Oct 24, 2023
Merged
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
8 changes: 6 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
on: [push, pull_request]

name: CI

on:
push:
branches:
- master
pull_request:

env:
# This is required to enable the web_sys clipboard API which egui_web uses
# https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Clipboard.html
Expand Down
80 changes: 69 additions & 11 deletions pubsubman/src/ui/messages_view.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use chrono::{DateTime, Local};
use egui_json_tree::{DefaultExpand, JsonTree};
use pubsubman_backend::{
Expand All @@ -18,7 +20,8 @@ use super::show_json_context_menu;
pub struct MessagesView {
pub stream_messages_enabled: bool,
pub stream_messages_cancel_token: Option<CancellationToken>,
pub search_query: String,
search_query: String,
search_mode: SearchMode,
}

impl MessagesView {
Expand All @@ -34,9 +37,14 @@ impl MessagesView {
on_message_id_click: impl FnMut(usize),
) {
let search_query = self.search_query.to_ascii_lowercase();
let filtered_messages = messages
.iter()
.filter(|msg| msg.data.to_ascii_lowercase().contains(&search_query));
let search_mode = self.search_mode;
let filtered_messages = messages.iter().filter(|msg| {
let source = match search_mode {
SearchMode::Data => &msg.data,
SearchMode::Id => &msg.id,
};
source.to_ascii_lowercase().contains(&search_query)
});

egui::TopBottomPanel::top("messages_top_panel")
.frame(egui::Frame::side_top_panel(ui.style()).inner_margin(8.0))
Expand Down Expand Up @@ -102,7 +110,7 @@ impl MessagesView {
ui.label("Pull or Stream new messages to retrieve the latest.");
});
} else {
let mut search_query_changed = false;
let mut should_reset_expanded = false;

ui.horizontal(|ui| {
ui.visuals_mut().extreme_bg_color = egui::Color32::from_gray(32);
Expand All @@ -114,15 +122,41 @@ impl MessagesView {
);

if search_query_edit_response.changed() {
search_query_changed = true;
should_reset_expanded = true;
}

let search_mode_changed =
egui::ComboBox::from_id_source("search_mode_combo_box")
.selected_text(format!("{}", self.search_mode))
.width(50.0)
.show_ui(ui, |ui| {
ui.selectable_value(
&mut self.search_mode,
SearchMode::Data,
"Data",
)
.changed()
|| ui
.selectable_value(
&mut self.search_mode,
SearchMode::Id,
"ID",
)
.changed()
})
.inner
.unwrap_or_default();

if search_mode_changed {
should_reset_expanded = true;
}

ui.visuals_mut().widgets.inactive.weak_bg_fill =
egui::Color32::from_gray(32);

if ui.button("✖").clicked() && !self.search_query.is_empty() {
self.search_query.clear();
search_query_changed = true;
should_reset_expanded = true;
}

ui.with_layout(
Expand Down Expand Up @@ -154,7 +188,8 @@ impl MessagesView {
column_settings,
filtered_messages,
&search_query,
search_query_changed,
should_reset_expanded,
self.search_mode,
on_message_id_click,
);
});
Expand All @@ -164,13 +199,15 @@ impl MessagesView {
}
}

#[allow(clippy::too_many_arguments)]
fn render_messages_table<'a, I>(
ui: &mut egui::Ui,
selected_topic: &TopicName,
column_settings: &ColumnSettings,
messages: I,
search_term: &str,
search_query_changed: bool,
should_reset_expanded: bool,
search_mode: SearchMode,
mut on_message_id_click: impl FnMut(usize),
) where
I: Iterator<Item = &'a PubsubMessage>,
Expand Down Expand Up @@ -219,16 +256,37 @@ fn render_messages_table<'a, I>(
}
}

let default_expand = match search_mode {
SearchMode::Data => DefaultExpand::SearchResults(search_term),
SearchMode::Id => DefaultExpand::None,
};

let response = JsonTree::new(&message.id, &message.data_json)
.default_expand(DefaultExpand::SearchResults(search_term))
.default_expand(default_expand)
.response_callback(show_json_context_menu(&message.data_json))
.show(ui);

if search_query_changed {
if should_reset_expanded {
response.reset_expanded(ui);
}

ui.end_row();
}
});
}

#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
enum SearchMode {
#[default]
Data,
Id,
}

impl Display for SearchMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SearchMode::Data => write!(f, "Data"),
SearchMode::Id => write!(f, "ID"),
}
}
}
Loading