Start work on server

This commit is contained in:
Michael Mikovsky
2025-11-28 18:39:14 -07:00
parent 598068d3ee
commit fcb8c6f6f5
13 changed files with 1800 additions and 613 deletions
+42 -49
View File
@@ -1,12 +1,16 @@
use egui::Frame;
use egui_tiles::Tree;
use crate::app::{AppState, windows::WindowWrapper};
use crate::{
app::{AppState, windows::WindowWrapper},
auth::Auth,
};
/// 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 {
auth: Auth,
// tab: Tab,
state: AppState,
tree: Tree<WindowWrapper>,
@@ -14,22 +18,8 @@ pub struct TemplateApp {
impl Default for TemplateApp {
fn default() -> Self {
// let mut tiles = egui_tiles::Tiles::default();
// let invis_1 = tiles.insert_pane(WindowWrapper {
// nr: 0,
// window: AppWindow::None,
// });
// let invis_2 = tiles.insert_pane(WindowWrapper {
// nr: 0,
// window: AppWindow::None,
// });
// tiles.set_visible(invis_1, false);
// tiles.set_visible(invis_2, false);
// let root = tiles.insert_horizontal_tile(vec![invis_1]);
Self {
auth: Auth::default(),
state: AppState::default(),
tree: egui_tiles::Tree::new_horizontal("tree_root", Vec::new()),
}
@@ -38,17 +28,17 @@ impl Default for TemplateApp {
impl TemplateApp {
/// Called once before the first frame.
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
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 {
// eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
// } else {
Default::default()
// }
if let Some(storage) = cc.storage {
eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default()
} else {
Default::default()
}
}
}
@@ -60,40 +50,43 @@ impl eframe::App for TemplateApp {
/// Called each time the UI needs repainting, which may be many times per second.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Put your widgets into a `SidePanel`, `TopBottomPanel`, `CentralPanel`, `Window` or `Area`.
// For inspiration and more examples, go to https://emilk.github.io/egui
if !self.auth.logged_in() {
egui::CentralPanel::default()
.frame(Frame::central_panel(&ctx.style()).inner_margin(0))
.show(ctx, |ui| self.auth.update(ui));
} else {
egui::TopBottomPanel::top("tab_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::TopBottomPanel::top("tab_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::MenuBar::new().ui(ui, |ui| {
ui.menu_button("File", |ui| {
ui.label("File");
});
egui::MenuBar::new().ui(ui, |ui| {
ui.menu_button("File", |ui| {
ui.label("File");
});
ui.menu_button("View", |ui| {
ui.label("View");
self.state.labels(&mut self.tree, ui);
ui.menu_button("View", |ui| {
ui.label("View");
self.state.labels(&mut self.tree, ui);
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
egui::widgets::global_theme_preference_switch(ui);
});
// ui.style
});
});
egui::TopBottomPanel::bottom("tab_panel").show(ctx, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
egui::widgets::global_theme_preference_switch(ui);
ui.label(format!("UnShell UI {}", env!("CARGO_PKG_VERSION")));
egui::warn_if_debug_build(ui);
});
// ui.style
});
});
egui::TopBottomPanel::bottom("tab_panel").show(ctx, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(format!("UnShell UI {}", env!("CARGO_PKG_VERSION")));
egui::warn_if_debug_build(ui);
});
});
egui::CentralPanel::default()
.frame(Frame::central_panel(&ctx.style()).inner_margin(0))
.show(ctx, |ui| self.tree.ui(&mut self.state, ui));
egui::CentralPanel::default()
.frame(Frame::central_panel(&ctx.style()).inner_margin(0))
.show(ctx, |ui| self.tree.ui(&mut self.state, ui));
}
}
}
+28 -74
View File
@@ -3,11 +3,9 @@ mod windows;
use std::collections::HashMap;
use crate::{app::windows::WindowWrapper, config::Config, flowchart::FlowChart};
pub use app::TemplateApp;
use egui_tiles::{TileId, Tree};
use log::info;
use crate::{app::windows::WindowWrapper, config::Config, flowchart::FlowChart};
#[derive(Default, serde::Deserialize, serde::Serialize)]
struct AppState {
@@ -29,87 +27,44 @@ impl AppState {
let enabled = self.open_windows.contains_key(&key);
if ui.selectable_label(enabled, *name).clicked() {
// if enabled {
// let tid = *self.open_windows.get(&key).unwrap();
// tree.remove_recursively(tid);
// tree.tiles.remove(tid);
// self.open_windows.remove(&key);
if enabled {
let tid = *self.open_windows.get(&key).unwrap();
tree.remove_recursively(tid);
tree.tiles.remove(tid);
self.open_windows.remove(&key);
// // if self.open_windows.is_empty()
// } else {
let tid = tree.tiles.insert_pane(WindowWrapper {
nr: i + 1,
window: *key,
});
// if self.open_windows.is_empty()
} else {
let tid = tree.tiles.insert_pane(WindowWrapper {
nr: i + 1,
window: *key,
});
match self.open_windows.len() {
0 => {
tree.root = Some(tid);
}
1 => {
let old_root = tree.root.unwrap();
let tab_id = tree.tiles.insert_tab_tile(vec![old_root, tid]);
tree.root = Some(tab_id);
}
_ => {
let pid = tree.tiles.insert_tab_tile(vec![tid]);
tree.move_tile_to_container(pid, tree.root.unwrap().clone(), 0, true);
match self.open_windows.len() {
0 => {
tree.root = Some(tid);
}
1 => {
let old_root = tree.root.unwrap();
let tab_id = tree.tiles.insert_tab_tile(vec![old_root, tid]);
tree.root = Some(tab_id);
tree.make_active(|t, _| t == tid);
}
_ => {
let root = tree.root().unwrap();
let n = tree.tiles.get_container(root).unwrap().num_children();
tree.move_tile_to_container(tid, tree.root.unwrap().clone(), n, true);
}
}
self.open_windows.insert(key.clone(), tid);
}
self.open_windows.insert(key.clone(), tid);
}
}
// if ui
// .selectable_label(
// self.open_windows.contains_key(&AppWindow::Flowchart),
// "Network",
// )
// .clicked()
// {
// // self.open_windows. = Tab::Flowchart;
// }
// if ui
// .selectable_label(self.open_windows.contains_key(&AppWindow::Config), "Config")
// .clicked()
// {
// // self.open_windows. = Tab::Flowchart;
// }
// if ui
// .selectable_label(self.tab == Tab::Test, self.config.title())
// .clicked()
// {
// self.tab = Tab::Test;
// }
}
// fn contains(&self, key: &AppWindow)
// fn toggle(&mut self, key: &AppWindow) {
// if self.
// }
}
// impl Default for AppState {
// fn default() -> Self {
// Self {
// open_windows: HashMap::from([
// (AppWindow::Flowchart, false),
// (AppWindow::Config, false),
// ]),
// flowchart: Default::default(),
// config: Default::default(),
// }
// }
// }
#[derive(Clone, Copy, serde::Deserialize, serde::Serialize, PartialEq, Eq, Hash)]
enum AppWindow {
None,
Flowchart,
Config,
}
@@ -117,7 +72,6 @@ enum AppWindow {
impl AppWindow {
fn update(&self, state: &mut AppState, ui: &mut egui::Ui) {
match self {
AppWindow::None => {}
AppWindow::Flowchart => state.flowchart.paint(ui),
AppWindow::Config => state.config.update(ui),
}
+129
View File
@@ -0,0 +1,129 @@
use std::{collections::HashMap, sync::Arc};
use egui::{Align2, Area, Frame, Order, Sense, UiKind, Vec2, mutex::Mutex};
use wasm_bindgen::prelude::Closure;
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Auth {
// Auth Stuff
token: Option<Token>,
#[serde(skip)]
auth_state: Arc<Mutex<AuthState>>,
// UI Stuff
username: String,
password: String,
show_password: bool,
}
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
struct Token {
access_token: String,
token_type: String,
}
#[derive(Debug, PartialEq, Eq)]
enum AuthState {
NotLoggedIn,
RequestSent,
Authorised(Token),
Error(String),
}
impl Default for AuthState {
fn default() -> Self {
Self::NotLoggedIn
}
}
impl Auth {
pub fn logged_in(&mut self) -> bool {
if self.token.is_some() {
true
} else {
match *self.auth_state.lock() {
AuthState::Authorised(ref token) => {
self.token = Some(token.clone());
true
}
_ => false,
}
}
// self.auth_state.lock().eq(&AuthState::Authorised)
}
pub fn update(&mut self, ui: &mut egui::Ui) {
Area::new("Auth".into())
.kind(UiKind::Modal)
.sense(Sense::hover())
.anchor(Align2::CENTER_CENTER, Vec2::ZERO)
.order(Order::Foreground)
.interactable(true)
.show(ui.ctx(), |ui| {
Frame::popup(ui.style()).show(ui, |ui| {
ui.heading("UnShell Login");
ui.horizontal(|ui| {
ui.label("Username");
ui.text_edit_singleline(&mut self.username);
});
ui.horizontal(|ui| {
ui.label("Password");
let _ = ui.add(
// [ui.available_width(), 24.],
egui::TextEdit::singleline(&mut self.password)
.password(!self.show_password),
);
self.show_password = ui.button("Show").is_pointer_button_down_on();
// ui.toggle_value(&mut self.show_password, "Show");
});
if ui.button("Login").clicked() {
let json = serde_json::to_string(&HashMap::from([
("client_id", self.username.clone()),
("client_secret", self.password.clone()),
]))
.unwrap();
let state = self.auth_state.clone();
*(state.lock()) = AuthState::RequestSent;
crate::httpPost(
"/auth",
&json,
Closure::once_into_js(move |response: String| {
*(state.lock()) =
if let Ok(token) = serde_json::from_str::<Token>(&response) {
AuthState::Authorised(token)
} else {
AuthState::Error("Malformed Response".into())
}
// self.logged_in
// .store(true, std::sync::atomic::Ordering::Relaxed);
// self.logged_in();
}),
);
}
});
});
// });
// });
}
}
impl Default for Auth {
fn default() -> Self {
Self {
token: Default::default(),
auth_state: Arc::new(Mutex::new(AuthState::NotLoggedIn)),
username: Default::default(),
password: Default::default(),
show_password: Default::default(),
}
}
}
+3 -4
View File
@@ -1,6 +1,5 @@
use egui::{TextStyle, Ui};
use egui_extras::{Column, TableBuilder};
use unshell_lib::config::RuntimeConfig;
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Config {
@@ -22,7 +21,7 @@ impl Default for Config {
current_payloads: vec![PayloadConfig {
name: "Test".to_string(),
components: vec!["server".to_string()],
runtimes: Vec::new(),
// runtimes: Vec::new(),
}],
}
}
@@ -217,7 +216,7 @@ impl Config {
struct PayloadConfig {
name: String,
components: Vec<String>,
runtimes: Vec<RuntimeConfig>,
// runtimes: Vec<RuntimeConfig>,
}
impl PayloadConfig {
@@ -225,7 +224,7 @@ impl PayloadConfig {
Self {
name: "New Payload".to_string(),
components: Vec::new(),
runtimes: Vec::new(),
// runtimes: Vec::new(),
}
}
}
+25
View File
@@ -4,5 +4,30 @@
extern crate log;
pub mod app;
mod auth;
mod config;
mod flowchart;
// mod JsFunc {
// use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(text: &str);
}
#[wasm_bindgen(module = "/assets/sw.js")]
extern "C" {
pub fn httpGet(url: &str, callback: JsValue);
pub fn httpPost(url: &str, data: &str, callback: JsValue);
}
// }
// #[cfg(not(target_arch = "wasm32"))]
// mod JsFunc {
// pub fn httpGet(url: &str, callback: fn() -> {}) {}
// }
+15 -14
View File
@@ -1,24 +1,23 @@
#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use unshell_gui::app::TemplateApp;
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result {
pretty_env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
// pretty_env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([400.0, 300.0])
.with_min_inner_size([300.0, 220.0]),
..Default::default()
};
eframe::run_native(
"eframe template",
native_options,
Box::new(|cc| Ok(Box::new(TemplateApp::new(cc)))),
)
// let native_options = eframe::NativeOptions {
// viewport: egui::ViewportBuilder::default()
// .with_inner_size([400.0, 300.0])
// .with_min_inner_size([300.0, 220.0]),
// ..Default::default()
// };
// eframe::run_native(
// "eframe template",
// native_options,
// Box::new(|cc| Ok(Box::new(TemplateApp::new(cc)))),
// )
todo!()
}
// When compiling to web using trunk:
@@ -26,6 +25,8 @@ fn main() -> eframe::Result {
fn main() {
use eframe::wasm_bindgen::JsCast as _;
use unshell_gui::app::TemplateApp;
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();