Do secondly refresh via a calloop Timer instead of measuring elapsed time
This commit is contained in:
parent
418ab212ea
commit
916451c4cc
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -844,6 +844,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|||||||
name = "wl-seasonal-hours-clock"
|
name = "wl-seasonal-hours-clock"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"calloop",
|
||||||
"chrono",
|
"chrono",
|
||||||
"resvg",
|
"resvg",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -14,3 +14,4 @@ suncalc = "0.4"
|
|||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
xdg = "2.4"
|
xdg = "2.4"
|
||||||
|
calloop = "0.9"
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -2,8 +2,8 @@ extern crate smithay_client_toolkit as sctk;
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::time::SystemTime;
|
|
||||||
|
|
||||||
|
use calloop::{timer::Timer, EventLoop};
|
||||||
use chrono::prelude::{Local, Utc};
|
use chrono::prelude::{Local, Utc};
|
||||||
use chrono::TimeZone;
|
use chrono::TimeZone;
|
||||||
use chrono::Timelike;
|
use chrono::Timelike;
|
||||||
@ -633,7 +633,7 @@ fn main() {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let (env, display, mut queue) = sctk::new_default_environment!(SeasonalClock, desktop)
|
let (env, display, queue) = sctk::new_default_environment!(SeasonalClock, desktop)
|
||||||
.expect("Unable to connect to a Wayland compositor");
|
.expect("Unable to connect to a Wayland compositor");
|
||||||
|
|
||||||
let surface = env
|
let surface = env
|
||||||
@ -679,28 +679,31 @@ fn main() {
|
|||||||
window.refresh()
|
window.refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
let now = SystemTime::now();
|
let mut event_loop = EventLoop::<Option<WEvent>>::try_new().unwrap();
|
||||||
let mut last_elapsed = 0;
|
let handle = event_loop.handle();
|
||||||
|
let source = Timer::new().expect("Failed to create timer event source!");
|
||||||
|
let timer_handle = source.handle();
|
||||||
|
timer_handle.add_timeout(std::time::Duration::from_secs(1), "");
|
||||||
|
|
||||||
|
handle
|
||||||
|
.insert_source(source, |_, timer_handle, event| {
|
||||||
|
timer_handle.add_timeout(std::time::Duration::from_secs(1), "");
|
||||||
|
if event.is_none() {
|
||||||
|
*event = Some(sctk::window::Event::Refresh);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
sctk::WaylandSource::new(queue)
|
||||||
|
.quick_insert(handle)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Update every second
|
// Update every second
|
||||||
// TODO: There’s probably a better way to do this…
|
|
||||||
match now.elapsed() {
|
|
||||||
Ok(elapsed) => {
|
|
||||||
let new_elapsed = elapsed.as_secs();
|
|
||||||
|
|
||||||
if new_elapsed != last_elapsed {
|
|
||||||
need_redraw = true;
|
|
||||||
last_elapsed = new_elapsed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Error: {:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match next_action.take() {
|
match next_action.take() {
|
||||||
Some(WEvent::Close) => break,
|
Some(WEvent::Close) => break,
|
||||||
Some(WEvent::Refresh) => {
|
Some(WEvent::Refresh) => {
|
||||||
|
redraw(&mut pool, window.surface(), dimensions, &config).expect("Failed to draw");
|
||||||
window.refresh();
|
window.refresh();
|
||||||
window.surface().commit();
|
window.surface().commit();
|
||||||
}
|
}
|
||||||
@ -732,20 +735,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(guard) = queue.prepare_read() {
|
event_loop.dispatch(None, &mut next_action).unwrap();
|
||||||
if let Err(e) = guard.read_events() {
|
|
||||||
if e.kind() != ::std::io::ErrorKind::WouldBlock {
|
|
||||||
eprintln!(
|
|
||||||
"Error while trying to read from the wayland socked: {:?}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
queue
|
|
||||||
.dispatch_pending(&mut next_action, |_, _, _| {})
|
|
||||||
.expect("Failed to dispatch all messages.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user