123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- use xcb;
- use ui::font;
- use std::rc::Rc;
- use std::sync::Arc;
- pub trait Widget {
- fn init(&mut self) {}
- fn render(&mut self, x: u16);
- fn width(&mut self) -> u16;
- fn handle_event(&mut self, _event: &xcb::GenericEvent, _is_finishing: bool) -> bool {
- false
- }
- fn update(&mut self) {}
- fn finish(&mut self) {}
- }
- pub struct DrawContext {
- conn: Arc<xcb::Connection>,
- picture: xcb::render::Picture,
- pen: xcb::render::Picture,
- fonts: Rc<font::FontLoader>,
- bg_color: xcb::render::Color
- }
- impl 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(
- &conn,
- pen,
- color
- );
- DrawContext {
- conn: conn,
- picture: picture,
- pen: pen,
- fonts: fonts,
- bg_color: xcb::render::Color::new(0, 0, 0, 0xFFFF)
- }
- }
- pub fn draw_bg(&self, x: u16, width: u16) {
- xcb::render::fill_rectangles(
- &self.conn,
- xcb::render::PICT_OP_SRC as u8,
- self.picture,
- self.bg_color,
- &[xcb::Rectangle::new(x as i16, 0, width, 20)]
- );
- }
- pub fn measure_text(&self, name: &str) -> u16 {
- let text = self.fonts.create_renderable_text(name);
- text.width
- }
- pub fn set_bg_color(&mut self, red: u16, blue: u16, green: u16, alpha: u16) {
- self.bg_color = xcb::render::Color::new(red, blue, green, alpha);
- }
- pub fn draw_text(&self, name: &str, x: u16) {
- if !name.is_empty() {
- let text = self.fonts.create_renderable_text(name);
- let baseline = 20 - self.fonts.default_offset(20);
- text.render(&self.conn, self.pen, self.picture, x, baseline as u16);
- }
- }
- }
|