Browse Source

Implement clipping for title widget

Thomas Dy 7 years ago
parent
commit
f0c29d065c

+ 9 - 1
src/ui/panel.rs

@@ -197,9 +197,17 @@ impl Panel {
             state.position = right_pos;
         }
 
+        let margin = right_pos.saturating_sub(left_pos);
+
         // render
         for state in self.widgets_iter() {
-            state.widget.render(state.position);
+            let width = if state.widget.fit_width() {
+                margin
+            }
+            else {
+                state.width
+            };
+            state.widget.render(state.position, width);
         }
         self.conn.flush();
     }

+ 1 - 1
src/widgets/bspwm.rs

@@ -84,7 +84,7 @@ impl Widget for Bspwm {
         thread::spawn(move || monitor_thread(tx));
     }
 
-    fn render(&mut self, x: u16) {
+    fn render(&mut self, x: u16, _w: u16) {
         for desktop in self.desktops.iter() {
             if desktop.selected {
                 self.context.set_bg_color(0x6666, 0xCCCC, 0x6666, 0xFFFF);

+ 4 - 1
src/widgets/mod.rs

@@ -23,8 +23,11 @@ pub enum Message {
 
 pub trait Widget {
     fn init(&mut self) {}
-    fn render(&mut self, x: u16);
+    fn render(&mut self, x: u16, width: u16);
     fn width(&mut self) -> u16;
+    fn fit_width(&self) -> bool {
+        false
+    }
     fn handle_event(&mut self, _event: &Message) -> bool {
         false
     }

+ 2 - 2
src/widgets/music.rs

@@ -42,7 +42,7 @@ impl Music {
         let x = self.last_pos;
         self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
         self.context.draw_bg(x, WIDTH);
-        self.render(x);
+        self.render(x, WIDTH);
         self.context.flush();
     }
 
@@ -54,7 +54,7 @@ impl Widget for Music {
         thread::spawn(move || monitor_thread(tx));
     }
 
-    fn render(&mut self, x: u16) {
+    fn render(&mut self, x: u16, _w: u16) {
         self.last_pos = x;
         let icon_width = self.context.measure_text(self.icon());
 

+ 1 - 1
src/widgets/sensors.rs

@@ -21,7 +21,7 @@ impl Sensors {
 }
 
 impl Widget for Sensors {
-    fn render(&mut self, x: u16) {
+    fn render(&mut self, x: u16, _w: u16) {
         let mut offset = x;
         for ref sensor in self.sensors.iter() {
             let icon_width = self.context.measure_text(&sensor.icon());

+ 2 - 2
src/widgets/spacer.rs

@@ -15,8 +15,8 @@ pub fn create(mut context: DrawContext, width: u16, red: u16, green: u16, blue:
 }
 
 impl Widget for Spacer {
-    fn render(&mut self, x: u16) {
-        self.context.draw_bg(x, self.width);
+    fn render(&mut self, x: u16, width: u16) {
+        self.context.draw_bg(x, width);
     }
 
     fn width(&mut self) -> u16 {

+ 10 - 5
src/widgets/title.rs

@@ -30,8 +30,9 @@ impl Title {
 
     pub fn redraw(&mut self) {
         self.context.draw_bg(self.last_pos, self.last_width);
-        self.context.draw_text(&self.title, self.last_pos + MARGIN);
-        self.last_width = self.context.measure_text(&self.title) + MARGIN;
+        let x = self.last_pos;
+        let w = self.last_width;
+        self.render(x, w);
         self.conn.flush();
     }
 }
@@ -45,16 +46,20 @@ impl Widget for Title {
         self.conn.watch(self.last_win, true);
     }
 
-    fn render(&mut self, x: u16) {
+    fn render(&mut self, x: u16, w: u16) {
         self.last_pos = x;
-        self.context.draw_text(&self.title, self.last_pos + MARGIN);
-        self.last_width = self.context.measure_text(&self.title) + MARGIN;
+        self.last_width = w;
+        self.context.draw_text_with_clipping(&self.title, self.last_pos + MARGIN, w - MARGIN * 2);
     }
 
     fn width(&mut self) -> u16 {
         0
     }
 
+    fn fit_width(&self) -> bool {
+        true
+    }
+
     fn handle_event(&mut self, event: &Message) -> bool {
         match event {
             &Message::XcbEvent(ref event) =>

+ 1 - 1
src/widgets/tray.rs

@@ -90,7 +90,7 @@ impl Widget for Tray {
         (self.children.len() * 20) as u16
     }
 
-    fn render(&mut self, x: u16) {
+    fn render(&mut self, x: u16, _w: u16) {
         for (index, child) in self.children.iter().enumerate() {
             let window = *child;
             let xpos = x as u32 + index as u32 * 20;