Skip to content

Commit

Permalink
Still learning
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Oct 17, 2023
1 parent 7ddc5da commit eb4a58a
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 43 deletions.
64 changes: 62 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust-version = "1.71"

[dependencies]
egui = "0.23.0"
egui_extras = "0.23.0"
eframe = { version = "0.23.0", default-features = false, features = [
"accesskit", # Make egui comptaible with screen readers. NOTE: adds a lot of dependencies.
"default_fonts", # Embed the default egui fonts.
Expand Down
193 changes: 193 additions & 0 deletions src/apps/app_windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
use std::collections::BTreeSet;

use egui::{Context, Modifiers, ScrollArea, Ui};

use super::App;
use super::View;

Check warning on line 6 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

unused import: `super::View`

Check warning on line 6 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `super::View`

Check failure on line 6 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `super::View`

Check warning on line 6 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `super::View`

// ----------------------------------------------------------------------------

#[derive(serde::Deserialize, serde::Serialize)]
struct Apps {
#[serde(skip)] // This how you opt-out of serialization of a field
apps: Vec<Box<dyn App>>,

open: BTreeSet<String>,
}

impl Default for Apps {
fn default() -> Self {
Self::from_apps(vec![Box::<super::scoreboard_app::ScoreBoardApp>::default()])
}
}

impl Apps {
pub fn from_apps(apps: Vec<Box<dyn App>>) -> Self {
let mut open = BTreeSet::new();
open.insert(
super::scoreboard_app::ScoreBoardApp::default()
.name()
.to_owned(),
);

Self { apps, open }
}

pub fn checkboxes(&mut self, ui: &mut Ui) {

Check warning on line 36 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

methods `checkboxes` and `windows` are never used
let Self { apps, open } = self;
for app in apps {
let mut is_open = open.contains(app.name());
ui.toggle_value(&mut is_open, app.name());
set_open(open, app.name(), is_open);
}
}

pub fn windows(&mut self, ctx: &Context) {
let Self { apps, open } = self;
for app in apps {
let mut is_open = open.contains(app.name());
app.show(ctx, &mut is_open);
set_open(open, app.name(), is_open);
}
}
}

// ----------------------------------------------------------------------------

fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {

Check warning on line 57 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

function `set_open` is never used
if is_open {
if !open.contains(key) {
open.insert(key.to_owned());
}
} else {
open.remove(key);
}
}

// ----------------------------------------------------------------------------

#[derive(serde::Deserialize, serde::Serialize)]
pub struct AppWindows {
about_is_open: bool,
apps: Apps,
}

impl Default for AppWindows {
fn default() -> Self {
Self {
about_is_open: true,
apps: Default::default(),
}
}
}

impl AppWindows {
/// Show the app ui (menu bar and windows).
pub fn ui(&mut self, ctx: &Context) {

Check warning on line 86 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

methods `ui`, `desktop_ui`, `show_windows`, and `app_list_ui` are never used
self.desktop_ui(ctx);
}

fn desktop_ui(&mut self, ctx: &Context) {
egui::SidePanel::right("egui_app_panel")
.resizable(false)
.default_width(150.0)
.show(ctx, |ui| {
ui.vertical_centered(|ui| {
ui.heading("🎯 apps");
});

ui.separator();

self.app_list_ui(ui);

ui.separator();

use egui::special_emojis::{GITHUB, TWITTER};

Check warning on line 105 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

unused import: `TWITTER`

Check warning on line 105 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `TWITTER`

Check failure on line 105 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `TWITTER`

Check warning on line 105 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `TWITTER`
ui.hyperlink_to(
format!("{GITHUB} GitHub"),
"https://github.com/bitbrain-za/judge_2331-rs",
);
ui.separator();
});

egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
file_menu_button(ui);
});
});

self.show_windows(ctx);
}

/// Show the open windows.
fn show_windows(&mut self, ctx: &Context) {
self.apps.windows(ctx);
}

fn app_list_ui(&mut self, ui: &mut egui::Ui) {
ScrollArea::vertical().show(ui, |ui| {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
// ui.toggle_value(&mut self.about_is_open, self.about.name());

ui.separator();
self.apps.checkboxes(ui);
ui.separator();
});
});
}
}

// ----------------------------------------------------------------------------

fn file_menu_button(ui: &mut Ui) {

Check warning on line 142 in src/apps/app_windows.rs

View workflow job for this annotation

GitHub Actions / Check

function `file_menu_button` is never used
let organize_shortcut =
egui::KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, egui::Key::O);
let reset_shortcut =
egui::KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, egui::Key::R);

// NOTE: we must check the shortcuts OUTSIDE of the actual "File" menu,
// or else they would only be checked if the "File" menu was actually open!

if ui.input_mut(|i| i.consume_shortcut(&organize_shortcut)) {
ui.ctx().memory_mut(|mem| mem.reset_areas());
}

if ui.input_mut(|i| i.consume_shortcut(&reset_shortcut)) {
ui.ctx().memory_mut(|mem| *mem = Default::default());
}

ui.menu_button("File", |ui| {
ui.set_min_width(220.0);
ui.style_mut().wrap = Some(false);

// On the web the browser controls the zoom
#[cfg(not(target_arch = "wasm32"))]
{
egui::gui_zoom::zoom_menu_buttons(ui, None);
ui.separator();
}

if ui
.add(
egui::Button::new("Organize Windows")
.shortcut_text(ui.ctx().format_shortcut(&organize_shortcut)),
)
.clicked()
{
ui.ctx().memory_mut(|mem| mem.reset_areas());
ui.close_menu();
}

if ui
.add(
egui::Button::new("Reset egui memory")
.shortcut_text(ui.ctx().format_shortcut(&reset_shortcut)),
)
.on_hover_text("Forget scroll, positions, sizes etc")
.clicked()
{
ui.ctx().memory_mut(|mem| *mem = Default::default());
ui.close_menu();
}
});
}
17 changes: 17 additions & 0 deletions src/apps/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub mod app_windows;
mod scoreboard_app;
pub use scoreboard_app::ScoreBoardApp;

/// Something to view in the demo windows
pub trait View {
fn ui(&mut self, ui: &mut egui::Ui);
}

/// Something to view
pub trait App {
/// `&'static` so we can also use it as a key to store open/close state.
fn name(&self) -> &'static str;

/// Show windows, etc
fn show(&mut self, ctx: &egui::Context, open: &mut bool);
}
Loading

0 comments on commit eb4a58a

Please sign in to comment.