egui/egui_demo_lib/src/apps/demo/demo_app_windows.rs

243 lines
7.7 KiB
Rust
Raw Normal View History

2021-05-09 08:53:35 +00:00
use super::Demo;
use egui::{CtxRef, ScrollArea, Ui};
use std::collections::BTreeSet;
// ----------------------------------------------------------------------------
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
struct Demos {
#[cfg_attr(feature = "persistence", serde(skip))]
2021-05-09 08:53:35 +00:00
demos: Vec<Box<dyn Demo>>,
open: BTreeSet<String>,
}
2021-05-09 08:53:35 +00:00
impl Default for Demos {
fn default() -> Self {
2021-05-09 08:53:35 +00:00
Self::from_demos(vec![
New text layout (#682) This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`. This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor. One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley. ## Performance Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!). Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that. All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
Box::new(super::code_editor::CodeEditor::default()),
Box::new(super::dancing_strings::DancingStrings::default()),
Box::new(super::drag_and_drop::DragAndDropDemo::default()),
Box::new(super::font_book::FontBook::default()),
2021-05-09 08:53:35 +00:00
Box::new(super::MiscDemoWindow::default()),
Box::new(super::multi_touch::MultiTouch::default()),
Box::new(super::painting::Painting::default()),
2021-02-14 20:39:04 +00:00
Box::new(super::plot_demo::PlotDemo::default()),
Box::new(super::scrolling::Scrolling::default()),
Box::new(super::sliders::Sliders::default()),
Box::new(super::widget_gallery::WidgetGallery::default()),
Box::new(super::window_options::WindowOptions::default()),
2021-02-07 13:46:53 +00:00
Box::new(super::tests::WindowResizeTest::default()),
Box::new(super::window_with_panels::WindowWithPanels::default()),
2021-05-09 08:53:35 +00:00
])
}
}
impl Demos {
pub fn from_demos(demos: Vec<Box<dyn Demo>>) -> Self {
let mut open = BTreeSet::new();
open.insert(
super::widget_gallery::WidgetGallery::default()
.name()
.to_owned(),
);
Self { demos, open }
}
pub fn checkboxes(&mut self, ui: &mut Ui) {
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
ui.checkbox(&mut is_open, demo.name());
set_open(open, demo.name(), is_open);
}
}
pub fn windows(&mut self, ctx: &CtxRef) {
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
demo.show(ctx, &mut is_open);
set_open(open, demo.name(), is_open);
}
}
}
// ----------------------------------------------------------------------------
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
struct Tests {
#[cfg_attr(feature = "persistence", serde(skip))]
demos: Vec<Box<dyn Demo>>,
open: BTreeSet<String>,
}
impl Default for Tests {
fn default() -> Self {
Self::from_demos(vec![
2021-03-13 11:38:03 +00:00
Box::new(super::tests::CursorTest::default()),
Box::new(super::tests::IdTest::default()),
2021-02-06 15:54:38 +00:00
Box::new(super::tests::InputTest::default()),
Box::new(super::layout_test::LayoutTest::default()),
Box::new(super::tests::ManualLayoutTest::default()),
Box::new(super::tests::TableTest::default()),
2021-05-09 08:53:35 +00:00
])
}
}
2021-05-09 08:53:35 +00:00
impl Tests {
pub fn from_demos(demos: Vec<Box<dyn Demo>>) -> Self {
let mut open = BTreeSet::new();
open.insert(
super::widget_gallery::WidgetGallery::default()
.name()
.to_owned(),
);
2021-05-06 18:49:22 +00:00
Self { demos, open }
}
2021-05-09 08:53:35 +00:00
pub fn checkboxes(&mut self, ui: &mut Ui) {
2021-05-06 18:49:22 +00:00
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
ui.checkbox(&mut is_open, demo.name());
set_open(open, demo.name(), is_open);
}
}
2021-05-09 08:53:35 +00:00
pub fn windows(&mut self, ctx: &CtxRef) {
2021-05-06 18:49:22 +00:00
let Self { demos, open } = self;
for demo in demos {
let mut is_open = open.contains(demo.name());
demo.show(ctx, &mut is_open);
set_open(open, demo.name(), is_open);
}
}
}
2021-05-09 08:53:35 +00:00
// ----------------------------------------------------------------------------
fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {
if is_open {
if !open.contains(key) {
open.insert(key.to_owned());
}
} else {
open.remove(key);
}
}
// ----------------------------------------------------------------------------
/// A menu bar in which you can select different demo windows to show.
#[derive(Default)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
pub struct DemoWindows {
demos: Demos,
2021-05-09 08:53:35 +00:00
tests: Tests,
}
impl DemoWindows {
/// Show the app ui (menu bar and windows).
/// `sidebar_ui` can be used to optionally show some things in the sidebar
pub fn ui(&mut self, ctx: &CtxRef) {
let Self { demos, tests } = self;
2021-05-09 08:53:35 +00:00
egui::SidePanel::right("egui_demo_panel")
.min_width(150.0)
.default_width(190.0)
.show(ctx, |ui| {
egui::trace!(ui);
2021-05-09 08:53:35 +00:00
ui.vertical_centered(|ui| {
ui.heading("✒ egui demos");
2021-05-09 08:53:35 +00:00
});
ui.separator();
ScrollArea::vertical().show(ui, |ui| {
use egui::special_emojis::{GITHUB, OS_APPLE, OS_LINUX, OS_WINDOWS};
ui.label("egui is an immediate mode GUI library written in Rust.");
ui.label(format!(
"egui runs on the web, or natively on {}{}{}",
OS_APPLE, OS_LINUX, OS_WINDOWS,
));
ui.vertical_centered(|ui| {
ui.hyperlink_to(
format!("{} egui home page", GITHUB),
"https://github.com/emilk/egui",
);
});
ui.separator();
demos.checkboxes(ui);
ui.separator();
tests.checkboxes(ui);
ui.separator();
ui.vertical_centered(|ui| {
if ui.button("Organize windows").clicked() {
ui.ctx().memory().reset_areas();
}
});
2021-05-09 08:53:35 +00:00
});
});
2020-10-21 20:10:55 +00:00
egui::TopBottomPanel::top("menu_bar").show(ctx, |ui| {
2021-01-01 20:27:10 +00:00
show_menu_bar(ui);
});
{
let mut fill = ctx.style().visuals.extreme_bg_color;
if !cfg!(target_arch = "wasm32") {
// Native: WrapApp uses a transparent window, so let's show that off:
// NOTE: the OS compositor assumes "normal" blending, so we need to hack it:
let [r, g, b, _] = fill.to_array();
fill = egui::Color32::from_rgba_premultiplied(r, g, b, 180);
}
let frame = egui::Frame::none().fill(fill);
egui::CentralPanel::default().frame(frame).show(ctx, |_| {});
}
2021-02-03 00:08:23 +00:00
2021-01-01 23:13:34 +00:00
self.windows(ctx);
}
/// Show the open windows.
2021-01-01 23:13:34 +00:00
fn windows(&mut self, ctx: &CtxRef) {
let Self { demos, tests } = self;
2021-05-09 08:53:35 +00:00
demos.windows(ctx);
tests.windows(ctx);
}
}
// ----------------------------------------------------------------------------
2021-01-01 20:27:10 +00:00
fn show_menu_bar(ui: &mut Ui) {
trace!(ui);
use egui::*;
menu::bar(ui, |ui| {
menu::menu(ui, "File", |ui| {
if ui.button("Organize windows").clicked() {
ui.ctx().memory().reset_areas();
}
if ui
.button("Clear egui memory")
.on_hover_text("Forget scroll, positions, sizes etc")
.clicked()
{
*ui.ctx().memory() = Default::default();
}
});
});
}