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

use color::{Color, Colorable};
use graphics::{self, Graphics};
use graphics::character::CharacterCache;
use ui::Ui;

/// A type for drawing a colored window background.
#[derive(Copy, Clone)]
pub struct Background {
    maybe_color: Option<Color>,
}

impl Background {

    /// Construct a background.
    pub fn new() -> Background {
        Background {
            maybe_color: None,
        }
    }

    /// Draw the background.
    pub fn draw<B, C>(&mut self, ui: &mut Ui<C>, graphics: &mut B)
        where
            B: Graphics<Texture = <C as CharacterCache>::Texture>,
            C: CharacterCache
    {
        let color = self.maybe_color.unwrap_or(ui.theme.background_color);
        graphics::clear(color.to_fsa(), graphics);
    }

}

impl Colorable for Background {
    fn color(mut self, color: Color) -> Self {
        self.maybe_color = Some(color);
        self
    }
}