aboutsummaryrefslogtreecommitdiff
path: root/src/ThemeBuilder/components/properties/ColorPropertyEditor.cpp
blob: e9e486ac1da8115564d79a7229fabb5833af4a42 (plain)
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
#include "ColorPropertyEditor.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/ui/Base.h"
#include "cru/ui/ThemeManager.h"

namespace cru::theme_builder::components::properties {
ColorPropertyEditor::ColorPropertyEditor() {
  container_.AddChild(&label_);
  container_.AddChild(&color_cube_);
  container_.AddChild(&color_text_);

  color_cube_.SetBorderEnabled(true);
  color_cube_.GetStyleRuleSet()->SetParent(
      ui::ThemeManager::GetInstance()->GetResourceStyleRuleSet(
          u"cru.theme_builder.color_cube.style"));

  color_cube_brush_ = platform::gui::IUiApplication::GetInstance()
                          ->GetGraphicsFactory()
                          ->CreateSolidColorBrush(color_);

  color_cube_.SetForegroundBrush(color_cube_brush_);

  color_text_.SetText(color_.ToString());
  color_text_.SetMargin(ui::Thickness(10, 0, 0, 0));

  color_text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) {
    auto text = color_text_.GetTextView();
    auto color = ui::Color::Parse(text);
    if (color) {
      color_ = *color;
      color_cube_brush_->SetColor(*color);
      is_color_text_valid_ = true;
      RaiseChangeEvent();
    } else {
      is_color_text_valid_ = false;
      // TODO: Show error!
    }
  });
}

ColorPropertyEditor::~ColorPropertyEditor() {}

void ColorPropertyEditor::SetValue(const ui::Color &color,
                                   bool trigger_change) {
  if (!trigger_change) SuppressNextChangeEvent();
  color_text_.SetText(color.ToString());
}
}  // namespace cru::theme_builder::components::properties