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
use orbclient::Color; use std::cell::{Cell, RefCell}; use std::cmp::{min, max}; use std::sync::Arc; use cell::CheckSet; use event::Event; use point::Point; use rect::Rect; use renderer::Renderer; use theme::{ITEM_BACKGROUND, ITEM_BORDER, ITEM_SELECTION}; use traits::{Border, Click, Place}; use widgets::Widget; pub struct ProgressBar { pub rect: Cell<Rect>, pub bg: Color, pub fg: Color, pub fg_border: Color, pub border: Cell<bool>, pub border_radius: Cell<u32>, pub value: Cell<i32>, pub minimum: i32, pub maximum: i32, click_callback: RefCell<Option<Arc<Fn(&ProgressBar, Point)>>>, pressed: Cell<bool>, } impl ProgressBar { pub fn new() -> Arc<Self> { Arc::new(ProgressBar { rect: Cell::new(Rect::default()), bg: ITEM_BACKGROUND, fg: ITEM_SELECTION, fg_border: ITEM_BORDER, border: Cell::new(true), border_radius: Cell::new(0), value: Cell::new(0), minimum: 0, maximum: 100, click_callback: RefCell::new(None), pressed: Cell::new(false), }) } pub fn value(&self, value: i32) -> &Self { self.value.set(value); self } } impl Border for ProgressBar { 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 ProgressBar { 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 ProgressBar {} impl Widget for ProgressBar { fn rect(&self) -> &Cell<Rect> { &self.rect } fn draw(&self, renderer: &mut Renderer, _focused: bool) { let rect = self.rect.get(); let progress_rect = Rect::new(rect.x, rect.y, ((rect.width as i32 * max(0, min(self.maximum, self.value.get() - self.minimum))) / max(1, self.maximum - self.minimum)) as u32, rect.height); let b_r = self.border_radius.get(); renderer.rounded_rect(rect, b_r, true, self.bg); if progress_rect.width >= b_r * 2 { renderer.rounded_rect(progress_rect, b_r, true, self.fg); } if self.border.get() { renderer.rounded_rect(rect, b_r, false, self.fg_border); } } 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 } }