aboutsummaryrefslogtreecommitdiff
path: root/src/ThemeBuilder/components/conditions/CompoundConditionEditor.cpp
blob: 8be3aa0f30dc1fcbcb432e19a77e73da5f4f44fc (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
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
#include "CompoundConditionEditor.h"
#include "CheckedConditionEditor.h"
#include "ClickStateConditionEditor.h"
#include "ConditionEditor.h"
#include "FocusConditionEditor.h"
#include "NoConditionEditor.h"
#include "cru/base/ClonablePtr.h"
#include "cru/platform/Color.h"
#include "cru/ui/Base.h"
#include "cru/ui/ThemeManager.h"
#include "cru/ui/controls/FlexLayout.h"
#include "cru/ui/style/Condition.h"

namespace cru::theme_builder::components::conditions {

CompoundConditionEditor::CompoundConditionEditor() {
  SetLabel(u"Compound Condition");

  GetContainer()->AddChild(&children_container_);
  children_container_.SetMargin({10, 0, 0, 0});
  children_container_.SetFlexDirection(ui::controls::FlexDirection::Vertical);
  children_container_.SetItemCrossAlign(
      ui::controls::FlexCrossAlignment::Start);

  GetHeadContainer()->AddChild(add_child_button_.GetRootControl());

  add_child_button_.GetButton()->GetStyleRuleSet()->SetParent(
      ui::ThemeManager::GetInstance()->GetResourceStyleRuleSet(
          u"cru.theme_builder.icon-button.style"));
  add_child_button_.GetButton()->SetIconWithSvgPathDataStringResourceKey(
      u"icon.plus", {0, 0, 16, 16});
  add_child_button_.GetButton()->SetPreferredSize({24, 24});
  add_child_button_.GetButton()->SetPadding(ui::Thickness(2));
  add_child_button_.GetButton()->SetIconFillColor(ui::colors::green);
  add_child_button_.SetMenuItems({u"And Condition", u"Or Condition",
                                  u"Click State Condition", u"Focus Condition",
                                  u"Checked Condition", u"No Condition"});
  add_child_button_.MenuItemSelectedEvent()->AddHandler([this](Index index) {
    std::unique_ptr<ConditionEditor> editor;
    switch (index) {
      case 0:
        editor = std::make_unique<AndConditionEditor>();
        break;
      case 1:
        editor = std::make_unique<OrConditionEditor>();
        break;
      case 2:
        editor = std::make_unique<ClickStateConditionEditor>();
        break;
      case 3:
        editor = std::make_unique<FocusConditionEditor>();
        break;
      case 4:
        editor = std::make_unique<CheckedConditionEditor>();
        break;
      case 5:
        editor = std::make_unique<NoConditionEditor>();
        break;
      default:
        break;
    }
    if (editor) {
      ConnectChangeEvent(editor.get());
      editor->RemoveEvent()->AddSpyOnlyHandler([this, c = editor.get()] {
        auto index = this->children_container_.IndexOf(c->GetRootControl());
        this->children_.erase(this->children_.begin() + index);
        this->children_container_.RemoveChildAt(index);
        RaiseChangeEvent();
      });
      children_.push_back(std::move(editor));
      children_container_.AddChild(children_.back()->GetRootControl());
      RaiseChangeEvent();
    }
  });
}

CompoundConditionEditor::~CompoundConditionEditor() {}

std::vector<ClonablePtr<ui::style::Condition>>
CompoundConditionEditor::GetChildren() {
  std::vector<ClonablePtr<ui::style::Condition>> children;
  for (auto& child : children_) {
    children.push_back(child->GetCondition());
  }
  return children;
}

void CompoundConditionEditor::SetChildren(
    std::vector<ClonablePtr<ui::style::Condition>> children,
    bool trigger_change) {
  children_container_.ClearChildren();
  children_.clear();
  for (const auto& condition : children) {
    auto editor = CreateConditionEditor(condition.get());
    ConnectChangeEvent(editor.get());
    editor->RemoveEvent()->AddSpyOnlyHandler([this, c = editor.get()] {
      auto index = this->children_container_.IndexOf(c->GetRootControl());
      this->children_.erase(this->children_.begin() + index);
      this->children_container_.RemoveChildAt(index);
      RaiseChangeEvent();
    });
    children_.push_back(std::move(editor));
    children_container_.AddChild(children_.back()->GetRootControl());
  }
  if (trigger_change) {
    RaiseChangeEvent();
  }
}
}  // namespace cru::theme_builder::components::conditions