aboutsummaryrefslogtreecommitdiff
path: root/src/ThemeBuilder/components/properties/MeasureLengthPropertyEditor.cpp
blob: e359199fc86943686f3796dfb6a3f673c07fbbc5 (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
#include "MeasureLengthPropertyEditor.h"
#include "cru/ui/mapper/MapperRegistry.h"
#include "cru/ui/render/MeasureRequirement.h"

#include <string>

namespace cru::theme_builder::components::properties {
MeasureLengthPropertyEditor::MeasureLengthPropertyEditor() {
  container_.AddChild(&label_);
  container_.AddChild(&text_);

  text_.TextChangeEvent()->AddHandler([this](std::nullptr_t) {
    auto text = text_.GetTextView();
    auto measure_length_mapper = ui::mapper::MapperRegistry::GetInstance()
                                     ->GetMapper<ui::render::MeasureLength>();
    try {
      auto measure_length =
          measure_length_mapper->MapFromString(text.ToString());
      measure_length_ = measure_length;
      is_text_valid_ = true;
      RaiseChangeEvent();
    } catch (const Exception&) {
      is_text_valid_ = false;
      // TODO: Show error!
    }
  });
}

MeasureLengthPropertyEditor::~MeasureLengthPropertyEditor() {}

void MeasureLengthPropertyEditor::SetValue(
    const ui::render::MeasureLength& value, bool trigger_change) {
  if (!trigger_change) SuppressNextChangeEvent();
  text_.SetText(measure_length_.IsNotSpecified()
                    ? u"unspecified"
                    : String::FromUtf8(std::to_string(
                          measure_length_.GetLengthOrUndefined())));
}
}  // namespace cru::theme_builder::components::properties