about summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs47
1 files changed, 44 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 60b6648..a61f93a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,9 +3,11 @@ use crate::modules::*;
 use crate::x::*;
 
 use std::sync::Arc;
+use std::time::Duration;
 
 use xcb::{ Xid };
 use xcb::x::{
+  ClearArea,
   ClientMessageData,
   Gc, ChangeGc,
   Cw, CreateWindow,
@@ -55,7 +57,7 @@ fn main() -> xcb::Result<()> {
   // #5c01be
   let foreground = display.allocate_color(23644, 257, 48830)?;
 
-  let dock = Dock::new(Arc::clone(&display), DockConfiguration {
+  let mut dock = Dock::new(Arc::clone(&display), DockConfiguration {
     thickness: 34,
     baseline_offset: 2,
     edge_inset: 12,
@@ -68,7 +70,15 @@ fn main() -> xcb::Result<()> {
   loop {
     match display.wait_for_event()? {
       xcb::Event::X(xcb::x::Event::Expose(_event)) => {
-        let _ = dock.redraw(); // TODO
+        match dock.redraw() {
+          Ok(Some(duration)) => {
+            dock.connection().flush()?;
+            println!("sleeping");
+            std::thread::sleep(duration);
+          }
+          Ok(None) => { }
+          Err(_) => { } // TODO
+        }
       }
 
       xcb::Event::X(xcb::x::Event::ButtonPress(event)) => {
@@ -227,11 +237,14 @@ impl Dock {
   }
 
 
-  fn redraw(&self) -> xcb::Result<()> {
+  fn redraw(&mut self) -> xcb::Result<Option<Duration>> {
     let drawable = Drawable::Window(self.window_id());
     let context = GraphicContext::new(
                       Arc::clone(self.display()), drawable)?;
 
+    let foo = self.erase_all()?;
+    println!("{:?}", foo);
+
     if self.configuration.use_rounded_inside_corners
        || self.configuration.use_rounded_outside_corners
     {
@@ -243,6 +256,34 @@ impl Dock {
     let time = self.time_module.format();
     self.draw_text(&time, Alignment::Right)?;
 
+    let mut shortest_duration = None;
+
+    let next_duration = self.time_module.sleep_duration();
+    if let Some(old_value) = shortest_duration {
+      if let Some(new_value) = next_duration
+         && new_value < old_value
+      {
+        shortest_duration = Some(new_value);
+      }
+    } else {
+      shortest_duration = next_duration;
+    }
+
+    Ok(shortest_duration)
+  }
+
+
+  fn erase_all(&self) -> xcb::Result<()> {
+    let cookie = self.send_request_checked(&ClearArea {
+      exposures: true,
+      window: self.window().window_id(),
+      x: 0,
+      y: 0,
+      width: self.bounds.width,
+      height: self.bounds.height,
+    });
+    self.check_request(cookie)?;
+
     Ok(())
   }