Skip to content

Commit

Permalink
Creating backend instance in GUI. Handling topic refreshes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Aug 2, 2023
1 parent 1815997 commit 6587129
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pubsubman_gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ log = "0.4"
serde = { version = "1", features = ["derive"] }

env_logger = "0.10"
tokio = { version = "1.29.1", features = ["full"] }
pubsubman_backend = { version = "0.1.0", path = "../pubsubman_backend" }
82 changes: 47 additions & 35 deletions pubsubman_gui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,73 @@
use pubsubman_backend::{
message::{BackendMessage, FrontendMessage},
Backend,
};
use tokio::sync::mpsc::{Receiver, Sender};

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 {
#[serde(skip)]
topics: Vec<Topic>,
front_tx: Sender<FrontendMessage>,
back_rx: Receiver<BackendMessage>,
}

impl Default for TemplateApp {
fn default() -> Self {
impl TemplateApp {
/// Called once before the first frame.
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
let (front_tx, front_rx) = tokio::sync::mpsc::channel(2);
let (back_tx, back_rx) = tokio::sync::mpsc::channel(10);

tokio::spawn(async move {
Backend::new(back_tx, front_rx).await.init();
});

let front_tx_clone = front_tx.clone();
tokio::spawn(async move {
let _ = front_tx_clone
.send(FrontendMessage::RefreshTopicsRequest)
.await;
});

Self {
topics: vec![
Topic {
id: "test-topic-1".to_string(),
},
Topic {
id: "test-topic-2".to_string(),
},
],
topics: vec![],
front_tx,
back_rx,
}
}
}

impl TemplateApp {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.

// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
}
fn handle_backend_message(&mut self) {
match self.back_rx.try_recv() {
Ok(message) => match message {
BackendMessage::TopicsUpdated(topics) => {
self.topics = topics.into_iter().map(|id| Topic { id }).collect();

Default::default()
let front_tx = self.front_tx.clone();
tokio::spawn(async move {
let _ = front_tx.send(FrontendMessage::RefreshTopicsRequest).await;
});
}
},
Err(err) => println!("{:?}", err),
}
}
}

impl eframe::App for TemplateApp {
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
}

/// 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 { topics } = self;
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
ctx.request_repaint();
self.handle_backend_message();

let Self { topics, .. } = self;

egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
_frame.close();
frame.close();
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion pubsubman_gui/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

fn main() -> eframe::Result<()> {
#[tokio::main]
async fn main() -> eframe::Result<()> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).

let native_options = eframe::NativeOptions::default();
Expand Down
1 change: 1 addition & 0 deletions pubsubman_gui/src/topic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug)]
pub struct Topic {
pub id: String,
}
Expand Down

0 comments on commit 6587129

Please sign in to comment.