Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed May 31, 2024
0 parents commit 6e59d2a
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.build-result
.buildcmd
.direnv
/target
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2018"
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
resolver = "2"
members = ["egui-nav", "examples/*"]

[workspace.package]
edition = "2021"
license = "MIT"

[workspace.dependencies]
egui-nav = { path = "egui-nav" }
egui = "*"

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# egui-nav

A navigation control for egui

WIP! alpha!
11 changes: 11 additions & 0 deletions egui-nav/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "egui-nav"
version = "0.1.0"
edition = "2021"

[dependencies]
#egui = { path = "/home/jb55/dev/github/emilk/egui/crates/egui" }
#egui_extras = { path = "/home/jb55/dev/github/emilk/egui/crates/egui_extras" }
egui = "*"
egui_extras = "*"

38 changes: 38 additions & 0 deletions egui-nav/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use core::fmt::Display;

pub struct Nav<'r, T> {
route: &'r [T],
}

impl<'r, T> Nav<'r, T> {
/// Nav requires at least one route or it will panic
pub fn new(route: &'r [T]) -> Self {
// precondition: we must have at least one route. this simplifies
// the rest of the control, and it's easy to catchbb
assert!(route.len() > 0, "Nav routes cannot be empty");
Nav { route }
}

pub fn top_route(&self) -> &'r T {
&self.route[self.route.len() - 1]
}

/// Safer version of new if we're not sure if we will have non-empty routes
pub fn try_new(route: &'r [T]) -> Option<Self> {
if route.len() == 0 {
None
} else {
Some(Nav { route })
}
}

pub fn show<F, R>(&self, ui: &mut egui::Ui, show_route: F) -> R
where
F: Fn(&mut egui::Ui, &Nav<'_, T>) -> R,
T: Display,
{
let route = self.top_route();
ui.label(format!("< {}", route));
show_route(ui, self)
}
}
27 changes: 27 additions & 0 deletions examples/nav-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "nav-demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
egui-nav = { workspace = true }
egui = "0.27.2"
eframe = "0.27.2"
env_logger = "0.11.1"
log = "0.4"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tracing-subscriber = "0.3"

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
tracing-wasm = "0.2"
wasm-bindgen-futures = "*"

[dev-dependencies]
trunk = "0.20.0"

65 changes: 65 additions & 0 deletions examples/nav-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use eframe::egui;
use egui::{Direction, Frame, Layout};
use egui_nav::Nav;
use std::fmt;

#[cfg(not(target_arch = "wasm32"))]
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([600.0, 800.0]),
..Default::default()
};
eframe::run_native("Nav Demo", options, Box::new(|_cc| Box::<MyApp>::default()))
}

// When compiling to web using trunk:
#[cfg(target_arch = "wasm32")]
fn main() {
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();

let web_options = eframe::WebOptions::default();

wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|_cc| Box::<MyApp>::default()),
)
.await
.expect("failed to start eframe");
});
}

#[derive(Default)]
struct MyApp {}

enum Route {
Home,
Profile(String),
}

impl fmt::Display for Route {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Route::Home => write!(f, "Home"),
Route::Profile(name) => write!(f, "{}'s Profile", name),
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default()
.frame(Frame::none())
.show(ctx, |ui| {
let route = &[Route::Home, Route::Profile("bob".to_string())];
Nav::new(route).show(ui, |ui, nav| match nav.top_route() {
Route::Home => ui.label("Home body"),
Route::Profile(name) => ui.label("Profile body"),
});
});
}
}

0 comments on commit 6e59d2a

Please sign in to comment.