Skip to content

Commit

Permalink
Misc code cleanup, docs fixes, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 19, 2022
1 parent e49245f commit 89d1986
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 41 deletions.
8 changes: 4 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
10 changes: 4 additions & 6 deletions eframe/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "egui"
version = "0.16.1"
authors = ["Emil Ernerfeldt <[email protected]>"]
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"
Expand Down
2 changes: 2 additions & 0 deletions egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion egui/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions egui/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down
2 changes: 1 addition & 1 deletion egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
8 changes: 4 additions & 4 deletions egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down
2 changes: 2 additions & 0 deletions egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions egui_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
36 changes: 24 additions & 12 deletions epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -518,17 +531,16 @@ 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,
shapes: &mut Vec<Shape>,
) {
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();

Expand Down
6 changes: 3 additions & 3 deletions epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
12 changes: 6 additions & 6 deletions epi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -415,7 +415,7 @@ pub struct Location {

/// `location.origin`
///
/// Example: "http://www.example.com:80"
/// Example: `"http://www.example.com:80"`.
pub origin: String,
}

Expand Down
1 change: 1 addition & 0 deletions sh/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 89d1986

Please sign in to comment.