code: ui updates

This commit is contained in:
Gabor Körber 2024-06-10 23:51:47 +02:00
parent 36d7a64148
commit e5b572fcc1
4 changed files with 75 additions and 7 deletions

View File

@ -1,5 +1,7 @@
pub mod handlers; pub mod handlers;
pub mod icon;
pub mod server; pub mod server;
pub mod soundclips; pub mod soundclips;
pub mod state; pub mod state;
pub mod ui;
pub mod vbplay; pub mod vbplay;

View File

@ -64,15 +64,20 @@ fn main() {
should_run: should_run.clone(), should_run: should_run.clone(),
}; };
let (ui_to_server_tx, ui_to_server_rx) = mpsc::channel(32); let (tx_ui_to_server, rx_ui_to_server) = mpsc::channel(32);
//let (server_to_ui_tx, server_to_ui_rx) = mpsc::channel(32); let (tx_server_to_ui, rx_server_to_ui) = mpsc::channel(32);
let server_handle = rt.spawn(async move { let server_handle = rt.spawn(async move {
server::run_server(app_state, ui_to_server_rx).await; server::run_server(app_state, rx_ui_to_server, tx_server_to_ui).await;
}); });
// Run the UI on the main thread // Run the UI on the main thread
ui::run_ui(server_settings, should_run, ui_to_server_tx); ui::run_ui(
server_settings,
should_run,
tx_ui_to_server,
rx_server_to_ui,
);
if let Err(e) = rt.block_on(server_handle) { if let Err(e) = rt.block_on(server_handle) {
eprintln!("Server task error: {:?}", e); eprintln!("Server task error: {:?}", e);

View File

@ -1,5 +1,6 @@
use crate::handlers; use crate::handlers;
use crate::state::AppState; use crate::state::AppState;
use crate::ui::UIMessage;
use axum::{routing, Router}; use axum::{routing, Router};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::net::SocketAddr; use std::net::SocketAddr;
@ -17,7 +18,11 @@ pub enum ServerMessage {
Shutdown, Shutdown,
} }
pub async fn run_server(app_state: AppState, mut rx: mpsc::Receiver<ServerMessage>) { pub async fn run_server(
app_state: AppState,
mut rx: mpsc::Receiver<ServerMessage>,
tx: mpsc::Sender<UIMessage>,
) {
loop { loop {
let should_run = { let should_run = {
let guard = app_state.should_run.lock().unwrap(); let guard = app_state.should_run.lock().unwrap();
@ -90,5 +95,6 @@ pub async fn run_server(app_state: AppState, mut rx: mpsc::Receiver<ServerMessag
} }
} }
} }
tx.try_send(UIMessage::ServerHasQuit).unwrap_or_default();
eprintln!("Exiting run_server."); eprintln!("Exiting run_server.");
} }

View File

@ -1,5 +1,6 @@
use eframe::egui; use eframe::egui;
use raw_window_handle::{HasWindowHandle, RawWindowHandle}; use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use std::borrow::BorrowMut;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::{ use std::sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
@ -18,6 +19,7 @@ pub fn run_ui(
settings: Arc<Mutex<ServerSettings>>, settings: Arc<Mutex<ServerSettings>>,
should_run: Arc<Mutex<bool>>, should_run: Arc<Mutex<bool>>,
tx: mpsc::Sender<ServerMessage>, tx: mpsc::Sender<ServerMessage>,
rx: mpsc::Receiver<UIMessage>,
) { ) {
// Initialize visibility as AtomicBool // Initialize visibility as AtomicBool
let visible = Arc::new(AtomicBool::new(true)); let visible = Arc::new(AtomicBool::new(true));
@ -80,8 +82,10 @@ pub fn run_ui(
let my_tx = tx.clone(); let my_tx = tx.clone();
tray.add_menu_item("Quit", move || { tray.add_menu_item("Quit", move || {
my_tx.try_send(ServerMessage::Shutdown).unwrap(); my_tx.try_send(ServerMessage::Shutdown).unwrap_or_else(|_| {
std::process::exit(0); eprintln!("Failed to send Shutdown message");
});
//std::process::exit(0);
}) })
.unwrap(); .unwrap();
@ -89,6 +93,7 @@ pub fn run_ui(
settings, settings,
should_run, should_run,
tx, tx,
rx,
tray, tray,
}; };
@ -103,17 +108,67 @@ struct NativeApp {
#[allow(dead_code)] #[allow(dead_code)]
should_run: Arc<Mutex<bool>>, should_run: Arc<Mutex<bool>>,
tx: mpsc::Sender<ServerMessage>, tx: mpsc::Sender<ServerMessage>,
rx: mpsc::Receiver<UIMessage>,
#[allow(dead_code)] #[allow(dead_code)]
tray: TrayItem, tray: TrayItem,
} }
impl eframe::App for NativeApp { impl eframe::App for NativeApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
if let Ok(message) = self.rx.try_recv() {
match message {
UIMessage::ServerHasQuit => {
eprintln!("Server has quit!!!?");
}
_ => {}
}
}
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
egui::TopBottomPanel::top("top_panel")
.resizable(true)
.min_height(32.0)
.show_inside(ui, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Soundboard");
});
});
});
egui::SidePanel::left("left_panel")
.resizable(true)
.default_width(180.0)
.width_range(80.0..=220.0)
.show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
ui.vertical(|ui| {
ui.heading("Menu");
if ui.button("Status").clicked() {}
if ui.button("Devices").clicked() {}
if ui.button("Pages").clicked() {}
if ui.button("Server Settings").clicked() {}
if ui.button("Sound Settings").clicked() {}
});
});
egui::ScrollArea::vertical().show(ui, |ui| {});
});
let mut settings = self.settings.lock().unwrap(); let mut settings = self.settings.lock().unwrap();
ui.heading("Server Settings"); ui.heading("Server Settings");
egui::Frame {
inner_margin: 12.0.into(),
outer_margin: 24.0.into(),
rounding: 14.0.into(),
shadow: egui::epaint::Shadow::NONE,
fill: egui::Color32::from_rgba_unmultiplied(97, 0, 255, 128),
stroke: egui::Stroke::new(1.0, egui::Color32::GRAY),
}
.show(ui, |ui| {
ui.label(egui::RichText::new("3311").color(egui::Color32::WHITE));
});
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Port:"); ui.label("Port:");
if ui.text_edit_singleline(&mut settings.port).changed() {} if ui.text_edit_singleline(&mut settings.port).changed() {}