-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6e59d2a
Showing
8 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.build-result | ||
.buildcmd | ||
.direnv | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
edition = "2018" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# egui-nav | ||
|
||
A navigation control for egui | ||
|
||
WIP! alpha! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
}); | ||
}); | ||
} | ||
} |