Going in for egui

this has to work
This commit is contained in:
2024-09-15 14:32:59 -04:00
Unverified
parent 505236278c
commit aeb24f1cb4
7 changed files with 162 additions and 4 deletions

66
client/src/app.rs Normal file
View File

@@ -0,0 +1,66 @@
use crate::ui::panels;
/// 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:
pub label: String,
pub selected: bool,
pub selected_serverid: Option<String>,
pub selected_roomid: Option<String>,
#[serde(skip)] // This how you opt-out of serialization of a field
pub value: f32,
}
impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
selected: false,
selected_serverid: None,
selected_roomid: None,
value: 2.7,
}
}
}
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();
}
Default::default()
}
}
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
// File -> Quit
panels::top_panel(ctx);
panels::servers(self, ctx);
panels::rooms(self, ctx);
panels::messages(self, ctx)
}
/// 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);
}
}

View File

@@ -1,2 +1,4 @@
pub mod client;
pub mod types;
pub mod types;
pub mod app;
pub mod ui;

View File

@@ -1,3 +1,20 @@
fn main() {
println!("Hello, world!");
fn main() -> eframe::Result {
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([720.0, 500.0])
.with_min_inner_size([500.0, 300.0])
.with_icon(
// NOTE: Adding an icon is optional
eframe::icon_data::from_png_bytes(&include_bytes!("../assets/icon-256.png")[..])
.expect("Failed to load icon"),
),
..Default::default()
};
eframe::run_native(
"Realm",
native_options,
Box::new(|cc| Ok(Box::new(realm_client::app::TemplateApp::new(cc)))),
)
}

1
client/src/ui/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod panels;

64
client/src/ui/panels.rs Normal file
View File

@@ -0,0 +1,64 @@
use egui::{Context, SelectableLabel};
use crate::app::TemplateApp;
pub fn top_panel(ctx: &Context) {
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| {
// NOTE: no File->Quit on web pages!
let is_web = cfg!(target_arch = "wasm32");
if !is_web {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
});
ui.add_space(16.0);
}
egui::widgets::global_dark_light_mode_buttons(ui);
});
});
}
pub fn servers(app: &mut TemplateApp, ctx: &Context) {
egui::SidePanel::left("servers").show(ctx, |ui| {
ui.heading("Servers");
ui.separator();
if ui.add(SelectableLabel::new(app.selected, "server")).clicked() {
app.selected = !app.selected;
}
});
}
pub fn rooms(app: &mut TemplateApp, ctx: &Context) {
egui::SidePanel::left("rooms").show(ctx, |ui| {
ui.heading("Rooms");
ui.separator();
if ui.add(SelectableLabel::new(app.selected, "room")).clicked() {
app.selected = !app.selected;
}
});
}
pub fn messages(app: &mut TemplateApp, ctx: &Context) {
egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("eframe template");
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut app.label);
});
ui.add(egui::Slider::new(&mut app.value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
app.value += 1.0;
}
ui.separator();
});
}