Misc code cleanup, docs fixes, etc

This commit is contained in:
Emil Ernerfeldt 2022-02-19 20:51:51 +01:00
parent e49245fae5
commit 89d19860b8
15 changed files with 67 additions and 41 deletions

View file

@ -128,20 +128,20 @@
.catch(on_wasm_error); .catch(on_wasm_error);
function on_wasm_loaded() { function on_wasm_loaded() {
console.debug("wasm loaded. starting egui app…"); console.debug("wasm loaded. starting app…");
// This call installs a bunch of callbacks and then returns: // This call installs a bunch of callbacks and then returns:
wasm_bindgen.start("the_canvas_id"); wasm_bindgen.start("the_canvas_id");
console.debug("egui app started."); console.debug("app started.");
document.getElementById("center_text").remove(); document.getElementById("center_text").remove();
} }
function on_wasm_error(error) { function on_wasm_error(error) {
console.error("Failed to start egui: " + error); console.error("Failed to start: " + error);
document.getElementById("center_text").innerHTML = ` document.getElementById("center_text").innerHTML = `
<p> <p>
An error occurred loading egui An error occurred during loading:
</p> </p>
<p style="font-family:Courier New"> <p style="font-family:Courier New">
${error} ${error}

View file

@ -22,19 +22,17 @@ impl epi::App for MyApp {
} }
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) { fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
let Self { name, age } = self;
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application"); ui.heading("My egui Application");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Your name: "); ui.label("Your name: ");
ui.text_edit_singleline(name); ui.text_edit_singleline(&mut self.name);
}); });
ui.add(egui::Slider::new(age, 0..=120).text("age")); ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() { if ui.button("Click each year").clicked() {
*age += 1; self.age += 1;
} }
ui.label(format!("Hello '{}', age {}", name, age)); ui.label(format!("Hello '{}', age {}", self.name, self.age));
}); });
// Resize the native window to be just the size we need it to be: // Resize the native window to be just the size we need it to be:

View file

@ -2,7 +2,7 @@
name = "egui" name = "egui"
version = "0.16.1" version = "0.16.1"
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"] authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
description = "Simple, portable immediate mode GUI library for Rust" description = "An easy-to-use immediate mode GUI that runs on both web and native"
edition = "2021" edition = "2021"
rust-version = "1.56" rust-version = "1.56"
homepage = "https://github.com/emilk/egui" homepage = "https://github.com/emilk/egui"

View file

@ -9,6 +9,8 @@
//! The order in which you add panels matter! //! The order in which you add panels matter!
//! The first panel you add will always be the outermost, and the last you add will always be the innermost. //! The first panel you add will always be the outermost, and the last you add will always be the innermost.
//! //!
//! You must never open one top-level panel from within another panel. Add one panel, then the next.
//!
//! Always add any [`CentralPanel`] last. //! Always add any [`CentralPanel`] last.
//! //!
//! Add your [`Window`]:s after any top-level panels. //! Add your [`Window`]:s after any top-level panels.

View file

@ -165,18 +165,27 @@ pub enum Event {
/// ///
/// When the user presses enter/return, do not send a `Text` (just [`Key::Enter`]). /// When the user presses enter/return, do not send a `Text` (just [`Key::Enter`]).
Text(String), Text(String),
/// A key was pressed or released.
Key { Key {
key: Key, key: Key,
/// Was it pressed or released?
pressed: bool, pressed: bool,
/// The state of the modifier keys at the time of the event.
modifiers: Modifiers, modifiers: Modifiers,
}, },
/// The mouse or touch moved to a new place.
PointerMoved(Pos2), PointerMoved(Pos2),
/// A mouse button was pressed or released (or a touch started or stopped).
PointerButton { PointerButton {
/// Where is the pointer?
pos: Pos2, pos: Pos2,
/// What mouse button? For touches, use [`PointerButton::Primary`].
button: PointerButton, button: PointerButton,
/// Was it the button/touch pressed this frame, or released?
pressed: bool, pressed: bool,
/// The state of the modifier keys at the time of the event /// The state of the modifier keys at the time of the event.
modifiers: Modifiers, modifiers: Modifiers,
}, },
/// The mouse left the screen, or the last/primary touch input disappeared. /// The mouse left the screen, or the last/primary touch input disappeared.
@ -217,6 +226,7 @@ pub enum Event {
/// Unique identifier of a finger/pen. Value is stable from touch down /// Unique identifier of a finger/pen. Value is stable from touch down
/// to lift-up /// to lift-up
id: TouchId, id: TouchId,
/// One of: start move end cancel.
phase: TouchPhase, phase: TouchPhase,
/// Position of the touch (or where the touch was last detected) /// Position of the touch (or where the touch was last detected)
pos: Pos2, pos: Pos2,

View file

@ -146,7 +146,7 @@ impl Widget for &mut epaint::TessellationOptions {
epsilon: _, epsilon: _,
} = self; } = self;
ui.checkbox(anti_alias, "Antialias") ui.checkbox(anti_alias, "Antialias")
.on_hover_text("Turn off for small performance gain."); .on_hover_text("Apply feathering to smooth out the edges of shapes. Turn off for small performance gain.");
ui.add( ui.add(
crate::widgets::Slider::new(bezier_tolerance, 0.0001..=10.0) crate::widgets::Slider::new(bezier_tolerance, 0.0001..=10.0)
.logarithmic(true) .logarithmic(true)

View file

@ -23,6 +23,7 @@ pub enum Order {
/// Debug layer, always painted last / on top /// Debug layer, always painted last / on top
Debug, Debug,
} }
impl Order { impl Order {
const COUNT: usize = 6; const COUNT: usize = 6;
const ALL: [Order; Self::COUNT] = [ const ALL: [Order; Self::COUNT] = [

View file

@ -447,7 +447,7 @@ impl Response {
/// ///
/// If `align` is `None`, it'll scroll enough to bring the UI into view. /// If `align` is `None`, it'll scroll enough to bring the UI into view.
/// ///
/// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to`]. /// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to_rect`].
/// ///
/// ``` /// ```
/// # egui::__run_test_ui(|ui| { /// # egui::__run_test_ui(|ui| {

View file

@ -908,7 +908,7 @@ impl Ui {
/// ///
/// If `align` is `None`, it'll scroll enough to bring the cursor into view. /// If `align` is `None`, it'll scroll enough to bring the cursor into view.
/// ///
/// See also: [`Response::scroll_to_me`], [`Ui::scroll_to`]. /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to_rect`].
/// ///
/// ``` /// ```
/// # use egui::Align; /// # use egui::Align;
@ -933,7 +933,7 @@ impl Ui {
/// ///
/// If `align` is not provided, it'll scroll enough to bring the cursor into view. /// If `align` is not provided, it'll scroll enough to bring the cursor into view.
/// ///
/// See also: [`Response::scroll_to_me`], [`Ui::scroll_to`]. /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to_rect`].
/// ///
/// ``` /// ```
/// # use egui::Align; /// # use egui::Align;

View file

@ -613,6 +613,7 @@ impl PlotItem for Polygon {
} }
/// Text inside the plot. /// Text inside the plot.
#[derive(Clone)]
pub struct Text { pub struct Text {
pub(super) text: WidgetText, pub(super) text: WidgetText,
pub(super) position: Value, pub(super) position: Value,
@ -1079,6 +1080,7 @@ impl PlotItem for Arrows {
} }
/// An image in the plot. /// An image in the plot.
#[derive(Clone)]
pub struct PlotImage { pub struct PlotImage {
pub(super) position: Value, pub(super) position: Value,
pub(super) texture_id: TextureId, pub(super) texture_id: TextureId,

View file

@ -24,6 +24,6 @@ Check out [eframe_template](https://github.com/emilk/eframe_template) for an exa
* No integration with browser settings for colors and fonts. * No integration with browser settings for colors and fonts.
* On Linux and Mac, Firefox will copy the WebGL render target from GPU, to CPU and then back again (https://bugzilla.mozilla.org/show_bug.cgi?id=1010527#c0), slowing down egui. * On Linux and Mac, Firefox will copy the WebGL render target from GPU, to CPU and then back again (https://bugzilla.mozilla.org/show_bug.cgi?id=1010527#c0), slowing down egui.
The suggested use for `egui_web` is for experiments, personal projects and web games. Using egui for a serious web page is probably a bad idea.
In many ways, `egui_web` is trying to make the browser do something it wasn't designed to do (though there are many things browser vendors could do to improve how well libraries like egui work). In many ways, `egui_web` is trying to make the browser do something it wasn't designed to do (though there are many things browser vendors could do to improve how well libraries like egui work).
The suggested use for `egui_web` are for web apps where performance and responsiveness are more important than accessability and mobile text editing.

View file

@ -31,6 +31,20 @@ pub enum Shape {
CubicBezier(CubicBezierShape), CubicBezier(CubicBezierShape),
} }
impl From<Vec<Shape>> for Shape {
#[inline(always)]
fn from(shapes: Vec<Shape>) -> Self {
Self::Vec(shapes)
}
}
impl From<Mesh> for Shape {
#[inline(always)]
fn from(mesh: Mesh) -> Self {
Self::Mesh(mesh)
}
}
/// ## Constructors /// ## Constructors
impl Shape { impl Shape {
/// A line between two points. /// A line between two points.
@ -59,25 +73,25 @@ impl Shape {
/// Turn a line into equally spaced dots. /// Turn a line into equally spaced dots.
pub fn dotted_line( pub fn dotted_line(
points: &[Pos2], path: &[Pos2],
color: impl Into<Color32>, color: impl Into<Color32>,
spacing: f32, spacing: f32,
radius: f32, radius: f32,
) -> Vec<Self> { ) -> Vec<Self> {
let mut shapes = Vec::new(); let mut shapes = Vec::new();
points_from_line(points, spacing, radius, color.into(), &mut shapes); points_from_line(path, spacing, radius, color.into(), &mut shapes);
shapes shapes
} }
/// Turn a line into dashes. /// Turn a line into dashes.
pub fn dashed_line( pub fn dashed_line(
points: &[Pos2], path: &[Pos2],
stroke: impl Into<Stroke>, stroke: impl Into<Stroke>,
dash_length: f32, dash_length: f32,
gap_length: f32, gap_length: f32,
) -> Vec<Self> { ) -> Vec<Self> {
let mut shapes = Vec::new(); let mut shapes = Vec::new();
dashes_from_line(points, stroke.into(), dash_length, gap_length, &mut shapes); dashes_from_line(path, stroke.into(), dash_length, gap_length, &mut shapes);
shapes shapes
} }
@ -495,16 +509,15 @@ impl From<TextShape> for Shape {
/// Creates equally spaced filled circles from a line. /// Creates equally spaced filled circles from a line.
fn points_from_line( fn points_from_line(
line: &[Pos2], path: &[Pos2],
spacing: f32, spacing: f32,
radius: f32, radius: f32,
color: Color32, color: Color32,
shapes: &mut Vec<Shape>, shapes: &mut Vec<Shape>,
) { ) {
let mut position_on_segment = 0.0; let mut position_on_segment = 0.0;
line.windows(2).for_each(|window| { path.windows(2).for_each(|window| {
let start = window[0]; let (start, end) = (window[0], window[1]);
let end = window[1];
let vector = end - start; let vector = end - start;
let segment_length = vector.length(); let segment_length = vector.length();
while position_on_segment < segment_length { while position_on_segment < segment_length {
@ -518,7 +531,7 @@ fn points_from_line(
/// Creates dashes from a line. /// Creates dashes from a line.
fn dashes_from_line( fn dashes_from_line(
line: &[Pos2], path: &[Pos2],
stroke: Stroke, stroke: Stroke,
dash_length: f32, dash_length: f32,
gap_length: f32, gap_length: f32,
@ -526,9 +539,8 @@ fn dashes_from_line(
) { ) {
let mut position_on_segment = 0.0; let mut position_on_segment = 0.0;
let mut drawing_dash = false; let mut drawing_dash = false;
line.windows(2).for_each(|window| { path.windows(2).for_each(|window| {
let start = window[0]; let (start, end) = (window[0], window[1]);
let end = window[1];
let vector = end - start; let vector = end - start;
let segment_length = vector.length(); let segment_length = vector.length();

View file

@ -160,15 +160,15 @@ pub struct FontTweak {
/// Shift font downwards by this fraction of the font size (in points). /// Shift font downwards by this fraction of the font size (in points).
/// ///
/// A positive value shifts the text upwards. /// A positive value shifts the text downwards.
/// A negative value shifts it downwards. /// A negative value shifts it upwards.
/// ///
/// Example value: `-0.2`. /// Example value: `-0.2`.
pub y_offset_factor: f32, pub y_offset_factor: f32,
/// Shift font downwards by this amount of logical points. /// Shift font downwards by this amount of logical points.
/// ///
/// Example value: `-1.0`. /// Example value: `2.0`.
pub y_offset: f32, pub y_offset: f32,
} }

View file

@ -372,27 +372,27 @@ pub struct WebInfo {
pub struct Location { pub struct Location {
/// The full URL (`location.href`) without the hash. /// The full URL (`location.href`) without the hash.
/// ///
/// Example: "http://www.example.com:80/index.html?foo=bar". /// Example: `"http://www.example.com:80/index.html?foo=bar"`.
pub url: String, pub url: String,
/// `location.protocol` /// `location.protocol`
/// ///
/// Example: "http:". /// Example: `"http:"`.
pub protocol: String, pub protocol: String,
/// `location.host` /// `location.host`
/// ///
/// Example: "example.com:80". /// Example: `"example.com:80"`.
pub host: String, pub host: String,
/// `location.hostname` /// `location.hostname`
/// ///
/// Example: "example.com". /// Example: `"example.com"`.
pub hostname: String, pub hostname: String,
/// `location.port` /// `location.port`
/// ///
/// Example: "80". /// Example: `"80"`.
pub port: String, pub port: String,
/// The "#fragment" part of "www.example.com/index.html?query#fragment". /// The "#fragment" part of "www.example.com/index.html?query#fragment".
@ -415,7 +415,7 @@ pub struct Location {
/// `location.origin` /// `location.origin`
/// ///
/// Example: "http://www.example.com:80" /// Example: `"http://www.example.com:80"`.
pub origin: String, pub origin: String,
} }

View file

@ -52,6 +52,7 @@ cargo doc --document-private-items --no-deps --all-features
# For finding bloat: # For finding bloat:
# cargo bloat --release --bin demo_glium -n 200 | rg egui # cargo bloat --release --bin demo_glium -n 200 | rg egui
# Also try https://github.com/google/bloaty
# what compiles slowly? # what compiles slowly?
# https://fasterthanli.me/articles/why-is-my-rust-build-so-slow # https://fasterthanli.me/articles/why-is-my-rust-build-so-slow