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);
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:
wasm_bindgen.start("the_canvas_id");
console.debug("egui app started.");
console.debug("app started.");
document.getElementById("center_text").remove();
}
function on_wasm_error(error) {
console.error("Failed to start egui: " + error);
console.error("Failed to start: " + error);
document.getElementById("center_text").innerHTML = `
<p>
An error occurred loading egui
An error occurred during loading:
</p>
<p style="font-family:Courier New">
${error}

View file

@ -22,19 +22,17 @@ impl epi::App for MyApp {
}
fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) {
let Self { name, age } = self;
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
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() {
*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:

View file

@ -2,7 +2,7 @@
name = "egui"
version = "0.16.1"
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"
rust-version = "1.56"
homepage = "https://github.com/emilk/egui"

View file

@ -9,6 +9,8 @@
//! 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.
//!
//! You must never open one top-level panel from within another panel. Add one panel, then the next.
//!
//! Always add any [`CentralPanel`] last.
//!
//! 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`]).
Text(String),
/// A key was pressed or released.
Key {
key: Key,
/// Was it pressed or released?
pressed: bool,
/// The state of the modifier keys at the time of the event.
modifiers: Modifiers,
},
/// The mouse or touch moved to a new place.
PointerMoved(Pos2),
/// A mouse button was pressed or released (or a touch started or stopped).
PointerButton {
/// Where is the pointer?
pos: Pos2,
/// What mouse button? For touches, use [`PointerButton::Primary`].
button: PointerButton,
/// Was it the button/touch pressed this frame, or released?
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,
},
/// 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
/// to lift-up
id: TouchId,
/// One of: start move end cancel.
phase: TouchPhase,
/// Position of the touch (or where the touch was last detected)
pos: Pos2,

View file

@ -146,7 +146,7 @@ impl Widget for &mut epaint::TessellationOptions {
epsilon: _,
} = self;
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(
crate::widgets::Slider::new(bezier_tolerance, 0.0001..=10.0)
.logarithmic(true)

View file

@ -23,6 +23,7 @@ pub enum Order {
/// Debug layer, always painted last / on top
Debug,
}
impl Order {
const COUNT: usize = 6;
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.
///
/// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to`].
/// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to_rect`].
///
/// ```
/// # 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.
///
/// See also: [`Response::scroll_to_me`], [`Ui::scroll_to`].
/// See also: [`Response::scroll_to_me`], [`Ui::scroll_to_rect`].
///
/// ```
/// # use egui::Align;
@ -933,7 +933,7 @@ impl Ui {
///
/// 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;
@ -1014,7 +1014,7 @@ impl Ui {
.inner
}
/// Add a single[`Widget`] that is possibly disabled, i.e. greyed out and non-interactive.
/// Add a single [`Widget`] that is possibly disabled, i.e. greyed out and non-interactive.
///
/// If you call `add_enabled` from within an already disabled `Ui`,
/// the widget will always be disabled, even if the `enabled` argument is true.
@ -1069,7 +1069,7 @@ impl Ui {
})
}
/// Add a single[`Widget`] that is possibly invisible.
/// Add a single [`Widget`] that is possibly invisible.
///
/// An invisible widget still takes up the same space as if it were visible.
///

View file

@ -613,6 +613,7 @@ impl PlotItem for Polygon {
}
/// Text inside the plot.
#[derive(Clone)]
pub struct Text {
pub(super) text: WidgetText,
pub(super) position: Value,
@ -1079,6 +1080,7 @@ impl PlotItem for Arrows {
}
/// An image in the plot.
#[derive(Clone)]
pub struct PlotImage {
pub(super) position: Value,
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.
* 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).
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),
}
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
impl Shape {
/// A line between two points.
@ -59,25 +73,25 @@ impl Shape {
/// Turn a line into equally spaced dots.
pub fn dotted_line(
points: &[Pos2],
path: &[Pos2],
color: impl Into<Color32>,
spacing: f32,
radius: f32,
) -> Vec<Self> {
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
}
/// Turn a line into dashes.
pub fn dashed_line(
points: &[Pos2],
path: &[Pos2],
stroke: impl Into<Stroke>,
dash_length: f32,
gap_length: f32,
) -> Vec<Self> {
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
}
@ -495,16 +509,15 @@ impl From<TextShape> for Shape {
/// Creates equally spaced filled circles from a line.
fn points_from_line(
line: &[Pos2],
path: &[Pos2],
spacing: f32,
radius: f32,
color: Color32,
shapes: &mut Vec<Shape>,
) {
let mut position_on_segment = 0.0;
line.windows(2).for_each(|window| {
let start = window[0];
let end = window[1];
path.windows(2).for_each(|window| {
let (start, end) = (window[0], window[1]);
let vector = end - start;
let segment_length = vector.length();
while position_on_segment < segment_length {
@ -518,7 +531,7 @@ fn points_from_line(
/// Creates dashes from a line.
fn dashes_from_line(
line: &[Pos2],
path: &[Pos2],
stroke: Stroke,
dash_length: f32,
gap_length: f32,
@ -526,9 +539,8 @@ fn dashes_from_line(
) {
let mut position_on_segment = 0.0;
let mut drawing_dash = false;
line.windows(2).for_each(|window| {
let start = window[0];
let end = window[1];
path.windows(2).for_each(|window| {
let (start, end) = (window[0], window[1]);
let vector = end - start;
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).
///
/// A positive value shifts the text upwards.
/// A negative value shifts it downwards.
/// A positive value shifts the text downwards.
/// A negative value shifts it upwards.
///
/// Example value: `-0.2`.
pub y_offset_factor: f32,
/// Shift font downwards by this amount of logical points.
///
/// Example value: `-1.0`.
/// Example value: `2.0`.
pub y_offset: f32,
}

View file

@ -372,27 +372,27 @@ pub struct WebInfo {
pub struct Location {
/// 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,
/// `location.protocol`
///
/// Example: "http:".
/// Example: `"http:"`.
pub protocol: String,
/// `location.host`
///
/// Example: "example.com:80".
/// Example: `"example.com:80"`.
pub host: String,
/// `location.hostname`
///
/// Example: "example.com".
/// Example: `"example.com"`.
pub hostname: String,
/// `location.port`
///
/// Example: "80".
/// Example: `"80"`.
pub port: String,
/// The "#fragment" part of "www.example.com/index.html?query#fragment".
@ -415,7 +415,7 @@ pub struct Location {
/// `location.origin`
///
/// Example: "http://www.example.com:80"
/// Example: `"http://www.example.com:80"`.
pub origin: String,
}

View file

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