Do secondly refresh via a calloop Timer instead of measuring elapsed time
This commit is contained in:
		
							
								
								
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -844,6 +844,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | ||||
| name = "wl-seasonal-hours-clock" | ||||
| version = "0.1.0" | ||||
| dependencies = [ | ||||
|  "calloop", | ||||
|  "chrono", | ||||
|  "resvg", | ||||
|  "serde", | ||||
|   | ||||
| @@ -13,4 +13,5 @@ resvg = "0.22" | ||||
| suncalc = "0.4" | ||||
| toml = "0.5" | ||||
| 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::fs; | ||||
| use std::time::SystemTime; | ||||
|  | ||||
| use calloop::{timer::Timer, EventLoop}; | ||||
| use chrono::prelude::{Local, Utc}; | ||||
| use chrono::TimeZone; | ||||
| use chrono::Timelike; | ||||
| @@ -633,7 +633,7 @@ fn main() { | ||||
|         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"); | ||||
|  | ||||
|     let surface = env | ||||
| @@ -679,28 +679,31 @@ fn main() { | ||||
|         window.refresh() | ||||
|     } | ||||
|  | ||||
|     let now = SystemTime::now(); | ||||
|     let mut last_elapsed = 0; | ||||
|     let mut event_loop = EventLoop::<Option<WEvent>>::try_new().unwrap(); | ||||
|     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 { | ||||
|         // 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() { | ||||
|             Some(WEvent::Close) => break, | ||||
|             Some(WEvent::Refresh) => { | ||||
|                 redraw(&mut pool, window.surface(), dimensions, &config).expect("Failed to draw"); | ||||
|                 window.refresh(); | ||||
|                 window.surface().commit(); | ||||
|             } | ||||
| @@ -732,20 +735,7 @@ fn main() { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if let Some(guard) = queue.prepare_read() { | ||||
|             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."); | ||||
|         event_loop.dispatch(None, &mut next_action).unwrap(); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user