Skip to content

Commit

Permalink
Added copy popup for right clicking on message payloads.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Aug 28, 2023
1 parent 3ec42c8 commit 3f3e57b
Showing 1 changed file with 70 additions and 5 deletions.
75 changes: 70 additions & 5 deletions pubsubman_gui/src/ui/messages_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct MessagesView {
pub stream_messages_enabled: bool,
pub stream_messages_cancel_token: Option<CancellationToken>,
pub search_query: String,
copy_popup: Option<(egui::Pos2, String, Value)>,
}

impl MessagesView {
Expand All @@ -32,6 +33,8 @@ impl MessagesView {
column_settings: &mut ColumnSettings,
messages: &[PubsubMessage],
) {
self.show_copy_popup(ui);

let search_query = self.search_query.to_ascii_lowercase();
let filtered_messages = messages
.iter()
Expand Down Expand Up @@ -150,25 +153,87 @@ impl MessagesView {
&search_query,
);

if search_query_changed {
for response in responses {
for (response, value) in responses {
if search_query_changed {
response.reset_expanded(ui);
}

if let Some((response, path)) = response.inner {
if response.secondary_clicked() {
self.copy_popup = Some((
response
.interact_pointer_pos()
.unwrap_or(response.rect.left_bottom()),
path,
value,
));
}
}
}
});
});
}
});
}

fn show_copy_popup(&mut self, ui: &mut egui::Ui) {
let popup_id = ui.make_persistent_id("copy_popup");
let mut should_close_popup = false;

if let Some((pos, path, value)) = &self.copy_popup {
let area_response = egui::Area::new(popup_id)
.order(egui::Order::Foreground)
.constrain(true)
.fixed_pos(*pos)
.pivot(egui::Align2::LEFT_TOP)
.show(ui.ctx(), |ui| {
egui::Frame::popup(ui.style()).show(ui, |ui| {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
ui.set_width(150.0);

if !path.is_empty()
&& ui
.add(egui::Button::new("Copy property path").frame(false))
.clicked()
{
ui.output_mut(|o| o.copied_text = path.clone());
should_close_popup = true;
}

if ui
.add(egui::Button::new("Copy contents").frame(false))
.clicked()
{
if let Some(val) = value.pointer(path) {
if let Ok(pretty_str) = serde_json::to_string_pretty(val) {
ui.output_mut(|o| o.copied_text = pretty_str);
}
}
should_close_popup = true;
}
});
});
})
.response;

if area_response.clicked_elsewhere() {
should_close_popup = true;
}
}

if should_close_popup {
self.copy_popup = None;
}
}
}

fn render_messages_table<'a, I>(
ui: &mut egui::Ui,
selected_topic: &TopicName,
column_settings: &ColumnSettings,
messages: I,
search_term: &String,
) -> Vec<JsonTreeResponse>
search_term: &str,
) -> Vec<(JsonTreeResponse, Value)>
where
I: Iterator<Item = &'a PubsubMessage>,
{
Expand Down Expand Up @@ -239,7 +304,7 @@ where
let response = JsonTree::new(&message.id, &value)
.show(ui, DefaultExpand::SearchResults(search_term));

json_tree_responses.push(response);
json_tree_responses.push((response, value));

ui.end_row();
}
Expand Down

0 comments on commit 3f3e57b

Please sign in to comment.