Skip to content

Commit

Permalink
Storing and rendering Topics in app.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Aug 1, 2023
1 parent aff463d commit 461b57b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
59 changes: 20 additions & 39 deletions pubsubman_gui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::topic::Topic;

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// Example stuff:
label: String,

// this how you opt-out of serialization of a member
#[serde(skip)]
value: f32,
topics: Vec<Topic>,
}

impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
topics: vec![
Topic {
id: "test-topic-1".to_string(),
},
Topic {
id: "test-topic-2".to_string(),
},
],
}
}
}
Expand Down Expand Up @@ -45,12 +48,7 @@ impl eframe::App for TemplateApp {
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let Self { label, value } = self;

// Examples of how to create different panels and windows.
// Pick whichever suits you.
// Tip: a good default choice is to just keep the `CentralPanel`.
// For inspiration and more examples, go to https://emilk.github.io/egui
let Self { topics } = self;

#[cfg(not(target_arch = "wasm32"))] // no File->Quit on web pages!
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
Expand All @@ -64,33 +62,16 @@ impl eframe::App for TemplateApp {
});
});

egui::SidePanel::left("side_panel").show(ctx, |ui| {
ui.heading("Side Panel");

ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(label);
});

ui.add(egui::Slider::new(value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
*value += 1.0;
}
egui::SidePanel::left("side_panel")
.exact_width(250.0)
.resizable(false)
.show(ctx, |ui| {
ui.heading("Topics");

ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(" and ");
ui.hyperlink_to(
"eframe",
"https://github.com/emilk/egui/tree/master/crates/eframe",
);
ui.label(".");
});
for topic in topics.iter_mut() {
topic.show(ui);
}
});
});

egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
Expand Down
1 change: 1 addition & 0 deletions pubsubman_gui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::all, rust_2018_idioms)]

mod app;
mod topic;
pub use app::TemplateApp;
21 changes: 21 additions & 0 deletions pubsubman_gui/src/topic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub struct Topic {
pub id: String,
}

impl Topic {
pub fn show(&mut self, ui: &mut egui::Ui) {
let frame = egui::Frame::none()
.fill(ui.visuals().code_bg_color)
.inner_margin(egui::Margin::same(5.0))
.rounding(ui.visuals().window_rounding)
.show(ui, |ui| {
ui.set_width(ui.available_width());
ui.label(&self.id);
});

let _response = frame
.response
.on_hover_cursor(egui::CursorIcon::PointingHand)
.interact(egui::Sense::click_and_drag());
}
}

0 comments on commit 461b57b

Please sign in to comment.