Skip to content

Commit

Permalink
Extracted json context menu rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Oct 14, 2023
1 parent efab04c commit 8924755
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
12 changes: 11 additions & 1 deletion pubsubman/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
exit_state::{ExitState, SubscriptionCleanupState},
notifications::Notifications,
settings::Settings,
ui::{render_topic_name, MessagesView, PublishView},
ui::{render_topic_name, show_json_context_menu, MessagesView, PublishView},
};

#[derive(Default, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -251,6 +251,9 @@ impl App {
&message.data_json,
)
.default_expand(DefaultExpand::All)
.response_callback(show_json_context_menu(
&message.data_json,
))
.show(ui);
});

Expand All @@ -273,6 +276,13 @@ impl App {
)),
)
.default_expand(egui_json_tree::DefaultExpand::All)
.response_callback(show_json_context_menu(
&Value::Object(Map::from_iter(
message.attributes.iter().map(|(k, v)| {
(k.to_owned(), Value::String(v.clone()))
}),
)),
))
.show(ui);
}
});
Expand Down
38 changes: 38 additions & 0 deletions pubsubman/src/ui/json_ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use serde_json::Value;

pub fn show_json_context_menu(value: &Value) -> impl FnMut(egui::Response, &String) + '_ {
|response, pointer| {
response
.on_hover_cursor(egui::CursorIcon::ContextMenu)
.context_menu(|ui| {
show_json_context_menu_impl(ui, pointer, value);
});
}
}

fn show_json_context_menu_impl(ui: &mut egui::Ui, pointer: &String, value: &Value) {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
ui.set_width(150.0);

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

if ui
.add(egui::Button::new("Copy contents").frame(false))
.clicked()
{
if let Some(val) = value.pointer(pointer) {
if let Ok(pretty_str) = serde_json::to_string_pretty(val) {
ui.output_mut(|o| o.copied_text = pretty_str);
}
}
ui.close_menu();
}
});
}
38 changes: 3 additions & 35 deletions pubsubman/src/ui/messages_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use pubsubman_backend::{
message::FrontendMessage,
model::{PubsubMessage, SubscriptionName, TopicName},
};
use serde_json::Value;
use tokio::sync::mpsc::Sender;
use tokio_util::sync::CancellationToken;

Expand All @@ -13,6 +12,8 @@ use crate::{
column_settings::ColumnSettings,
};

use super::show_json_context_menu;

#[derive(Default)]
pub struct MessagesView {
pub stream_messages_enabled: bool,
Expand Down Expand Up @@ -220,13 +221,7 @@ fn render_messages_table<'a, I>(

let response = JsonTree::new(&message.id, &message.data_json)
.default_expand(DefaultExpand::SearchResults(search_term))
.response_callback(|response, pointer| {
response
.on_hover_cursor(egui::CursorIcon::ContextMenu)
.context_menu(|ui| {
show_context_menu(ui, pointer, &message.data_json);
});
})
.response_callback(show_json_context_menu(&message.data_json))
.show(ui);

if search_query_changed {
Expand All @@ -237,30 +232,3 @@ fn render_messages_table<'a, I>(
}
});
}

fn show_context_menu(ui: &mut egui::Ui, pointer: &String, value: &Value) {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
ui.set_width(150.0);

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

if ui
.add(egui::Button::new("Copy contents").frame(false))
.clicked()
{
if let Some(val) = value.pointer(pointer) {
if let Ok(pretty_str) = serde_json::to_string_pretty(val) {
ui.output_mut(|o| o.copied_text = pretty_str);
}
}
ui.close_menu();
}
});
}
2 changes: 2 additions & 0 deletions pubsubman/src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod json_ui;
mod messages_view;
mod modal;
mod publish_view;
mod topic_name;
mod validity_frame;

pub use json_ui::show_json_context_menu;
pub use messages_view::MessagesView;
pub use modal::Modal;
pub use publish_view::PublishView;
Expand Down

0 comments on commit 8924755

Please sign in to comment.