1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use orbclient::{Color, Renderer}; use std::cell::{Cell, RefCell}; use std::sync::Arc; use cell::{CloneCell, CheckSet}; use event::Event; use point::Point; use rect::Rect; use theme::{BUTTON_BACKGROUND, BUTTON_BG_SELECTION, BUTTON_FOREGROUND, BUTTON_FG_SELECTION, BUTTON_BORDER}; use traits::{Border, Click, Place, Text}; use widgets::Widget; pub struct Button { pub rect: Cell<Rect>, pub bg: Color, pub bg_selected: Color, pub fg: Color, pub fg_selected: Color, pub fg_border: Color, pub border: Cell<bool>, pub border_radius: Cell<u32>, pub text: CloneCell<String>, pub text_offset: Cell<Point>, click_callback: RefCell<Option<Arc<Fn(&Button, Point)>>>, pressed: Cell<bool>, } impl Button { pub fn new() -> Arc<Self> { Arc::new(Button { rect: Cell::new(Rect::default()), bg: BUTTON_BACKGROUND, bg_selected: BUTTON_BG_SELECTION, fg: BUTTON_FOREGROUND, fg_selected: BUTTON_FG_SELECTION, fg_border: BUTTON_BORDER, border: Cell::new(true), border_radius: Cell::new(2), text: CloneCell::new(String::new()), text_offset: Cell::new(Point::default()), click_callback: RefCell::new(None), pressed: Cell::new(false), }) } } impl Border for Button { fn border(&self, enabled: bool) -> &Self { self.border.set(enabled); self } fn border_radius(&self, radius: u32) -> &Self { self.border_radius.set(radius); self } } impl Click for Button { fn emit_click(&self, point: Point) { if let Some(ref click_callback) = *self.click_callback.borrow() { click_callback(self, point); } } fn on_click<T: Fn(&Self, Point) + 'static>(&self, func: T) -> &Self { *self.click_callback.borrow_mut() = Some(Arc::new(func)); self } } impl Place for Button {} impl Text for Button { fn text<S: Into<String>>(&self, text: S) -> &Self { self.text.set(text.into()); self } fn text_offset(&self, x: i32, y: i32) -> &Self { self.text_offset.set(Point::new(x, y)); self } } impl Widget for Button { fn rect(&self) -> &Cell<Rect> { &self.rect } fn draw(&self, renderer: &mut Renderer, _focused: bool) { let rect = self.rect.get(); let w = rect.width as i32; let h = rect.height as i32; let (fg, bg) = if self.pressed.get() { (self.fg_selected, self.bg_selected) } else { (self.fg, self.bg) }; let b_r = self.border_radius.get(); renderer.rounded_rect(rect.x, rect.y, rect.width, rect.height, b_r, true, bg); if self.border.get() { renderer.rounded_rect(rect.x, rect.y, rect.width, rect.height, b_r, false, self.fg_border); } let text = self.text.borrow(); let mut point = self.text_offset.get(); for c in text.chars() { if c == '\n' { point.x = self.text_offset.get().x; point.y += 16; } else { if point.x + 8 <= w && point.y + 16 <= h { renderer.char(point.x + rect.x, point.y + rect.y, c, fg); } point.x += 8; } } } fn event(&self, event: Event, focused: bool, redraw: &mut bool) -> bool { match event { Event::Mouse { point, left_button, .. } => { let mut click = false; let rect = self.rect.get(); if rect.contains(point) { if left_button { if self.pressed.check_set(true) { *redraw = true; } } else { if self.pressed.check_set(false) { click = true; *redraw = true; } } } else { if !left_button { if self.pressed.check_set(false) { *redraw = true; } } } if click { let click_point: Point = point - rect.point(); self.emit_click(click_point); } } _ => (), } focused } }