瀏覽代碼

Fix drawing text with clipping

Thomas Dy 7 年之前
父節點
當前提交
6981ac1ce8
共有 3 個文件被更改,包括 16 次插入37 次删除
  1. 14 35
      src/ui/draw_context.rs
  2. 1 1
      src/ui/panel.rs
  3. 1 1
      src/widgets/music.rs

+ 14 - 35
src/ui/draw_context.rs

@@ -1,24 +1,19 @@
 use xcb;
-use ui::ext;
-use ui::ext::ConnectionExt;
 use ui::font;
 
-use std::cmp;
 use std::rc::Rc;
 use std::sync::Arc;
 
 pub struct DrawContext {
     conn: Arc<xcb::Connection>,
-    window: xcb::Window,
     picture: xcb::render::Picture,
     pen: xcb::render::Picture,
     fonts: Rc<font::FontLoader>,
-    pen_color: xcb::render::Color,
     bg_color: xcb::render::Color
 }
 
 impl DrawContext {
-    pub fn new(conn: Arc<xcb::Connection>, window: xcb::Window, picture: xcb::render::Picture, fonts: Rc<font::FontLoader>) -> DrawContext {
+    pub fn new(conn: Arc<xcb::Connection>, picture: xcb::render::Picture, fonts: Rc<font::FontLoader>) -> DrawContext {
         let pen = conn.generate_id();
         let color = xcb::render::Color::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
         xcb::render::create_solid_fill(
@@ -29,11 +24,9 @@ impl DrawContext {
 
         DrawContext {
             conn: conn,
-            window: window,
             picture: picture,
             pen: pen,
             fonts: fonts,
-            pen_color: color,
             bg_color: xcb::render::Color::new(0, 0, 0, 0xFFFF)
         }
     }
@@ -66,35 +59,21 @@ impl DrawContext {
         }
     }
 
-    pub fn draw_text_until(&self, name: &str, x: u16, width: u16) {
-        if !name.is_empty() {
-            let text = self.fonts.create_renderable_text(name);
-            let baseline = 20 - self.fonts.default_offset(20);
+    pub fn draw_text_with_clipping(&self, name: &str, x: u16, width: u16) {
+        xcb::render::set_picture_clip_rectangles(
+            &self.conn,
+            self.picture,
+            0, 0,
+            &[xcb::Rectangle::new(x as i16, 0, width, 20)]
+        );
 
-            // weird things happen if you draw too little
-            let width = cmp::min(width, text.width);
+        self.draw_text(name, x);
 
-            let pixmap = self.conn.generate_id();
-            xcb::create_pixmap(&self.conn, 32, pixmap, self.window, x + width, 1);
-            let pen = self.conn.generate_id();
-            xcb::render::create_picture(
-                &self.conn,
-                pen,
-                pixmap,
-                self.conn.get_pict_format(ext::PictFormat::ARGB32),
-                &[]
-            );
-            xcb::render::fill_rectangles(
-                &self.conn,
-                xcb::render::PICT_OP_SRC as u8,
-                pen,
-                self.pen_color,
-                &[xcb::Rectangle::new(x as i16, 0, width, 1)]
-            );
-            text.render(&self.conn, pen, self.picture, x, baseline as u16);
-            xcb::render::free_picture(&self.conn, pen);
-            xcb::free_pixmap(&self.conn, pixmap);
-        }
+        xcb::render::change_picture(
+            &self.conn,
+            self.picture,
+            &[(xcb::render::CP_CLIP_MASK, xcb::render::PICTURE_NONE)]
+        );
     }
 
     pub fn flush(&self) {

+ 1 - 1
src/ui/panel.rs

@@ -96,7 +96,7 @@ impl Panel {
     }
 
     pub fn make_draw_context(&self) -> DrawContext {
-        DrawContext::new(self.conn.clone_connection(), self.window, self.picture, self.fonts.clone())
+        DrawContext::new(self.conn.clone_connection(), self.picture, self.fonts.clone())
     }
 
     fn widgets_iter(&mut self) -> iter::Chain<slice::IterMut<WidgetState>, slice::IterMut<WidgetState>>{

+ 1 - 1
src/widgets/music.rs

@@ -72,7 +72,7 @@ impl Widget for Music {
         let text_width = WIDTH - MARGIN * 4 - icon_width;
         self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
         self.context.draw_bg(x + icon_width + MARGIN * 3, text_width);
-        self.context.draw_text_until(&text, x + icon_width + MARGIN * 3, text_width);
+        self.context.draw_text_with_clipping(&text, x + icon_width + MARGIN * 3, text_width);
     }
 
     fn width(&mut self) -> u16 {