diff --git a/src/util/area.cc b/src/util/area.cc index 7e6beee..72eb71d 100644 --- a/src/util/area.cc +++ b/src/util/area.cc @@ -410,19 +410,11 @@ void Area::Draw() { } void Area::DrawBackground(cairo_t* c) { - Color fill_color; - if (mouse_state_ == MouseState::kMouseNormal) { - fill_color = bg_.fill_color(); - } else if (mouse_state_ == MouseState::kMouseOver) { - fill_color = bg_.fill_color_hover(); - } else { - fill_color = bg_.fill_color_pressed(); - } - const int w = bg_.border().width(); util::Rect extents{0, 0, width_, height_}; extents.ShrinkBy(w); + Color fill_color = bg_.fill_color_for(mouse_state_); if (fill_color.alpha() > 0.0) { DrawRect(c, extents.top_left().first, extents.top_left().second, extents.bottom_right().first, extents.bottom_right().second, @@ -432,27 +424,11 @@ void Area::DrawBackground(cairo_t* c) { cairo_fill(c); } - int gradient_id = -1; - if (mouse_state_ == MouseState::kMouseNormal) { - gradient_id = bg_.gradient_id(); - } else if (mouse_state_ == MouseState::kMouseOver) { - gradient_id = bg_.gradient_id_hover(); - } else { - gradient_id = bg_.gradient_id_pressed(); - } + int gradient_id = bg_.gradient_id_for(mouse_state_); if (gradient_id >= 0 && gradient_id < gradients.size()) { gradients[gradient_id].Draw(c, extents); } - Color border_color; - if (mouse_state_ == MouseState::kMouseNormal) { - border_color = bg_.border(); - } else if (mouse_state_ == MouseState::kMouseOver) { - border_color = bg_.border_color_hover(); - } else { - border_color = bg_.border_color_pressed(); - } - if (w > 0) { cairo_set_line_width(c, w); @@ -460,6 +436,7 @@ void Area::DrawBackground(cairo_t* c) { DrawRect(c, w / 2.0, w / 2.0, width_ - w, height_ - w, bg_.border().rounded()); + Color border_color = bg_.border_color_for(mouse_state_); cairo_set_source_rgba(c, border_color[0], border_color[1], border_color[2], border_color.alpha()); cairo_stroke(c); diff --git a/src/util/color.cc b/src/util/color.cc index f5526e7..7098511 100644 --- a/src/util/color.cc +++ b/src/util/color.cc @@ -3,6 +3,7 @@ #include #include +#include "util/area.hh" #include "util/color.hh" namespace { @@ -123,6 +124,16 @@ bool Border::operator!=(Border const& other) const { return !(*this == other); } Background::Background() : gradient_id_(-1), gradient_id_hover_(-1), gradient_id_pressed_(-1) {} +Color Background::fill_color_for(MouseState mouse_state) const { + if (mouse_state == MouseState::kMouseNormal) { + return fill_color(); + } + if (mouse_state == MouseState::kMouseOver) { + return fill_color_hover(); + } + return fill_color_pressed(); +} + Color Background::fill_color() const { return fill_color_; } void Background::set_fill_color(Color const& color) { fill_color_ = color; } @@ -149,6 +160,16 @@ void Background::set_fill_color_pressed(Color const& color) { fill_color_pressed_ = color; } +Color Background::border_color_for(MouseState mouse_state) const { + if (mouse_state == MouseState::kMouseNormal) { + return border(); + } + if (mouse_state == MouseState::kMouseOver) { + return border_color_hover(); + } + return border_color_pressed(); +} + Border const& Background::border() const { return border_; } Border& Background::border() { return border_; } @@ -177,6 +198,16 @@ void Background::set_border_color_pressed(Color const& color) { border_color_pressed_ = color; } +int Background::gradient_id_for(MouseState mouse_state) const { + if (mouse_state == MouseState::kMouseNormal) { + return gradient_id(); + } + if (mouse_state == MouseState::kMouseOver) { + return gradient_id_hover(); + } + return gradient_id_pressed(); +} + int Background::gradient_id() const { return gradient_id_; } void Background::set_gradient_id(int id) { gradient_id_ = id; } diff --git a/src/util/color.hh b/src/util/color.hh index b195e06..9102430 100644 --- a/src/util/color.hh +++ b/src/util/color.hh @@ -6,6 +6,9 @@ #include "util/nullable.hh" +// forward declaration from area.hh +enum class MouseState; + class Color { public: friend std::ostream& operator<<(std::ostream& os, Color const& color); @@ -63,31 +66,28 @@ class Background { Background(Background&&) = default; Background& operator=(Background const&) = default; + Color fill_color_for(MouseState mouse_state) const; Color fill_color() const; void set_fill_color(Color const& color); - Color fill_color_hover() const; void set_fill_color_hover(Color const& color); - Color fill_color_pressed() const; void set_fill_color_pressed(Color const& color); + Color border_color_for(MouseState mouse_state) const; Border const& border() const; Border& border(); void set_border(Border const& border); - Color border_color_hover() const; void set_border_color_hover(Color const& border); - Color border_color_pressed() const; void set_border_color_pressed(Color const& border); + int gradient_id_for(MouseState mouse_state) const; int gradient_id() const; void set_gradient_id(int id); - int gradient_id_hover() const; void set_gradient_id_hover(int id); - int gradient_id_pressed() const; void set_gradient_id_pressed(int id);