aboutsummaryrefslogtreecommitdiff
path: root/src/theme_builder/components/StyleRuleSetEditor.cpp
blob: 8c64f9cf6c116f3f2f2210322d6c3203509404e9 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "StyleRuleSetEditor.h"
#include "cru/ui/ThemeManager.h"
#include "cru/ui/controls/FlexLayout.h"
#include "cru/ui/render/FlexLayoutRenderObject.h"
#include "cru/ui/style/Condition.h"
#include "cru/ui/style/Styler.h"

namespace cru::theme_builder::components {
using namespace cru::ui::controls;
StyleRuleSetEditor::StyleRuleSetEditor() {
  scroll_view_.SetChild(&container_);

  container_.SetFlexDirection(ui::render::FlexDirection::Vertical);
  container_.AddChild(&rules_layout_);
  container_.AddChild(&add_button_);

  rules_layout_.SetFlexDirection(ui::controls::FlexDirection::Vertical);

  add_button_.GetStyleRuleSet()->SetParent(
      ui::ThemeManager::GetInstance()->GetResourceStyleRuleSet(
          u"cru.theme_builder.icon-button.style"));
  add_button_.SetIconWithSvgPathDataStringResourceKey(u"icon.plus",
                                                      {0, 0, 16, 16});
  add_button_.SetPreferredSize({24, 24});
  add_button_.SetPadding(ui::Thickness(2));
  add_button_.SetIconFillColor(ui::colors::green);

  add_button_.ClickEvent()->AddSpyOnlyHandler([this] {
    auto rule_set = ui::style::StyleRule(ui::style::NoCondition::Create(),
                                         ui::style::CompoundStyler::Create({}));
    style_rule_set_->AddStyleRule(rule_set);
  });
}

StyleRuleSetEditor::~StyleRuleSetEditor() {}

void StyleRuleSetEditor::BindStyleRuleSet(
    std::shared_ptr<ui::style::StyleRuleSet> rule_set) {
  Expects(style_rule_set_ == nullptr && rule_set);
  style_rule_set_ = std::move(rule_set);
  UpdateView(style_rule_set_.get());
  style_rule_set_->ChangeEvent()->AddSpyOnlyHandler(
      [this] { UpdateView(style_rule_set_.get()); });
}

void StyleRuleSetEditor::UpdateView(ui::style::StyleRuleSet* style_rule_set) {
  style_rule_editors_.clear();

  for (Index i = 0; i < style_rule_set->GetSize(); ++i) {
    const auto& rule = style_rule_set->GetStyleRule(i);
    auto style_rule_editor = std::make_unique<StyleRuleEditor>();
    style_rule_editor->SetValue(rule, false);
    style_rule_editor->RemoveEvent()->AddSpyOnlyHandler(
        [this, i] { style_rule_set_->RemoveStyleRule(i); });
    style_rule_editor->ChangeEvent()->AddSpyOnlyHandler(
        [this, i, editor = style_rule_editor.get()]() {
          style_rule_set_->SetStyleRule(i, editor->GetValue());
        });
    style_rule_editors_.push_back(std::move(style_rule_editor));
    rules_layout_.AddChild(style_rule_editors_.back()->GetRootControl());
  }
}
}  // namespace cru::theme_builder::components