Browse Source

Remove "seconds_since_midnight" from epi/eframe. Use chrono instead

chrono works both natively and on web.

Related: https://github.com/emilk/egui/issues/212
pull/821/head
Emil Ernerfeldt 3 years ago
parent
commit
844dd9d7a4
  1. 5
      Cargo.lock
  2. 3
      eframe/Cargo.toml
  3. 3
      eframe/README.md
  4. 2
      egui_demo_app/Cargo.toml
  5. 3
      egui_demo_lib/Cargo.toml
  6. 4
      egui_demo_lib/src/apps/fractal_clock.rs
  7. 16
      egui_demo_lib/src/lib.rs
  8. 2
      egui_demo_lib/src/wrap_app.rs
  9. 6
      egui_glium/Cargo.toml
  10. 1
      egui_glium/src/backend.rs
  11. 14
      egui_glium/src/lib.rs
  12. 6
      egui_glow/Cargo.toml
  13. 1
      egui_glow/src/backend.rs
  14. 14
      egui_glow/src/lib.rs
  15. 1
      egui_web/src/backend.rs
  16. 6
      egui_web/src/lib.rs
  17. 4
      epi/src/lib.rs

5
Cargo.lock

@ -357,10 +357,12 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"js-sys",
"libc",
"num-integer",
"num-traits",
"time",
"wasm-bindgen",
"winapi",
]
@ -880,6 +882,7 @@ dependencies = [
name = "egui_demo_lib"
version = "0.14.0"
dependencies = [
"chrono",
"criterion",
"egui",
"ehttp",
@ -895,7 +898,6 @@ dependencies = [
name = "egui_glium"
version = "0.14.0"
dependencies = [
"chrono",
"directories-next",
"egui",
"egui-winit",
@ -910,7 +912,6 @@ dependencies = [
name = "egui_glow"
version = "0.14.0"
dependencies = [
"chrono",
"directories-next",
"egui",
"egui-winit",

3
eframe/Cargo.toml

@ -51,6 +51,3 @@ persistence = ["epi/persistence", "egui_glium/persistence", "egui_web/persistenc
# experimental support for a screen reader
screen_reader = ["egui_glium/screen_reader", "egui_web/screen_reader"]
# for seconds_since_midnight (used in egui_demo_lib)
time = ["egui_glium/time"]

3
eframe/README.md

@ -36,8 +36,9 @@ eframe = { version = "*", default-features = false, features = ["default_fonts",
## Companion crates
Not all rust crates work when compiles to WASM, but here are some useful crates have been designed to work well both natively and as WASM:
* Audio: [`cpal`](https://github.com/RustAudio/cpal)
* Audio: [`cpal`](https://github.com/RustAudio/cpal).
* HTTP client: [`ehttp`](https://github.com/emilk/ehttp).
* Time: [`chrono`](https://github.com/chronotope/chrono).
## Name

2
egui_demo_app/Cargo.toml

@ -10,7 +10,7 @@ publish = false
crate-type = ["cdylib", "rlib"]
[dependencies]
eframe = { version = "0.14.0", path = "../eframe", features = ["time"] }
eframe = { version = "0.14.0", path = "../eframe" }
egui_demo_lib = { version = "0.14.0", path = "../egui_demo_lib", features = ["extra_debug_asserts"] }
[features]

3
egui_demo_lib/Cargo.toml

@ -26,6 +26,7 @@ all-features = true
egui = { version = "0.14.0", path = "../egui", default-features = false }
epi = { version = "0.14.0", path = "../epi" }
chrono = { version = "0.4", features = ["js-sys", "wasmbind"], optional = true }
enum-map = { version = "1", features = ["serde"] }
unicode_names2 = { version = "0.4.0", default-features = false }
@ -43,7 +44,7 @@ serde = { version = "1", features = ["derive"], optional = true }
criterion = { version = "0.3", default-features = false }
[features]
default = []
default = ["chrono"]
# Enable additional checks if debug assertions are enabled (debug builds).
extra_debug_asserts = ["egui/extra_debug_asserts"]

4
egui_demo_lib/src/apps/fractal_clock.rs

@ -37,10 +37,10 @@ impl epi::App for FractalClock {
"🕑 Fractal Clock"
}
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
fn update(&mut self, ctx: &egui::CtxRef, _frame: &mut epi::Frame<'_>) {
egui::CentralPanel::default()
.frame(Frame::dark_canvas(&ctx.style()))
.show(ctx, |ui| self.ui(ui, frame.info().seconds_since_midnight));
.show(ctx, |ui| self.ui(ui, crate::seconds_since_midnight()));
}
}

16
egui_demo_lib/src/lib.rs

@ -154,3 +154,19 @@ fn test_egui_zero_window_size() {
assert!(clipped_meshes.is_empty(), "There should be nothing to show");
}
}
// ----------------------------------------------------------------------------
/// Time of day as seconds since midnight. Used for clock in demo app.
pub(crate) fn seconds_since_midnight() -> Option<f64> {
#[cfg(feature = "chrono")]
{
use chrono::Timelike;
let time = chrono::Local::now().time();
let seconds_since_midnight =
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64);
Some(seconds_since_midnight)
}
#[cfg(not(feature = "chrono"))]
None
}

2
egui_demo_lib/src/wrap_app.rs

@ -133,7 +133,7 @@ impl WrapApp {
ui.with_layout(egui::Layout::right_to_left(), |ui| {
if false {
// TODO: fix the overlap on small screens
if let Some(seconds_since_midnight) = frame.info().seconds_since_midnight {
if let Some(seconds_since_midnight) = crate::seconds_since_midnight() {
if clock_button(ui, seconds_since_midnight).clicked() {
self.selected_anchor = "clock".to_owned();
if frame.is_web() {

6
egui_glium/Cargo.toml

@ -32,9 +32,6 @@ directories-next = { version = "2", optional = true }
ron = { version = "0.6", optional = true }
serde = { version = "1", optional = true }
# feature "time"
chrono = { version = "0.4", optional = true }
[dev-dependencies]
image = { version = "0.23", default-features = false, features = ["png"] }
@ -63,6 +60,3 @@ persistence = [
# experimental support for a screen reader
screen_reader = ["egui-winit/screen_reader"]
# for seconds_since_midnight (used in egui_demo_lib)
time = ["chrono"]

1
egui_glium/src/backend.rs

@ -149,7 +149,6 @@ fn integration_info(
web_info: None,
prefer_dark_mode: None, // TODO: figure out system default
cpu_usage: previous_frame_time,
seconds_since_midnight: seconds_since_midnight(),
native_pixels_per_point: Some(native_pixels_per_point(display)),
}
}

14
egui_glium/src/lib.rs

@ -91,20 +91,6 @@ use glium::glutin;
// ----------------------------------------------------------------------------
/// Time of day as seconds since midnight. Used for clock in demo app.
pub fn seconds_since_midnight() -> Option<f64> {
#[cfg(feature = "time")]
{
use chrono::Timelike;
let time = chrono::Local::now().time();
let seconds_since_midnight =
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64);
Some(seconds_since_midnight)
}
#[cfg(not(feature = "time"))]
None
}
pub fn screen_size_in_pixels(display: &glium::Display) -> egui::Vec2 {
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
egui::vec2(width_in_pixels as f32, height_in_pixels as f32)

6
egui_glow/Cargo.toml

@ -34,9 +34,6 @@ directories-next = { version = "2", optional = true }
ron = { version = "0.6", optional = true }
serde = { version = "1", optional = true }
# feature "time"
chrono = { version = "0.4", optional = true }
[dev-dependencies]
image = { version = "0.23", default-features = false, features = ["png"] }
@ -65,6 +62,3 @@ persistence = [
# experimental support for a screen reader
screen_reader = ["egui-winit/screen_reader"]
# for seconds_since_midnight (used in egui_demo_lib)
time = ["chrono"]

1
egui_glow/src/backend.rs

@ -164,7 +164,6 @@ fn integration_info(
web_info: None,
prefer_dark_mode: None, // TODO: figure out system default
cpu_usage: previous_frame_time,
seconds_since_midnight: seconds_since_midnight(),
native_pixels_per_point: Some(native_pixels_per_point(window)),
}
}

14
egui_glow/src/lib.rs

@ -89,20 +89,6 @@ pub use epi::NativeOptions;
// ----------------------------------------------------------------------------
/// Time of day as seconds since midnight. Used for clock in demo app.
pub fn seconds_since_midnight() -> Option<f64> {
#[cfg(feature = "time")]
{
use chrono::Timelike;
let time = chrono::Local::now().time();
let seconds_since_midnight =
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64);
Some(seconds_since_midnight)
}
#[cfg(not(feature = "time"))]
None
}
pub fn screen_size_in_pixels(window: &glutin::window::Window) -> egui::Vec2 {
let glutin::dpi::PhysicalSize { width, height } = window.inner_size();
egui::vec2(width as f32, height as f32)

1
egui_web/src/backend.rs

@ -227,7 +227,6 @@ impl AppRunner {
}),
prefer_dark_mode: self.prefer_dark_mode,
cpu_usage: self.web_backend.previous_frame_time,
seconds_since_midnight: Some(seconds_since_midnight()),
native_pixels_per_point: Some(native_pixels_per_point()),
}
}

6
egui_web/src/lib.rs

@ -62,12 +62,6 @@ pub fn now_sec() -> f64 {
/ 1000.0
}
pub fn seconds_since_midnight() -> f64 {
let d = js_sys::Date::new_0();
let seconds = (d.get_hours() * 60 + d.get_minutes()) * 60 + d.get_seconds();
seconds as f64 + 1e-3 * (d.get_milliseconds() as f64)
}
pub fn screen_size_in_native_points() -> Option<egui::Vec2> {
let window = web_sys::window()?;
Some(egui::Vec2::new(

4
epi/src/lib.rs

@ -307,10 +307,6 @@ pub struct IntegrationInfo {
/// `None` if this is the first frame.
pub cpu_usage: Option<f32>,
/// Local time. Used for the clock in the demo app.
/// Set to `None` if you don't know.
pub seconds_since_midnight: Option<f64>,
/// The OS native pixels-per-point
pub native_pixels_per_point: Option<f32>,
}

Loading…
Cancel
Save