aboutsummaryrefslogtreecommitdiff
path: root/src/ui/mapper/style/BorderStyleMapper.cpp
blob: 0dc8a05f65509b0b3d34483723aaf0e9108edfe7 (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
#include "cru/ui/mapper/style/BorderStyleMapper.hpp"
#include "../../Helper.hpp"
#include "cru/common/Logger.hpp"
#include "cru/platform/graphics/Factory.hpp"
#include "cru/ui/mapper/MapperRegistry.hpp"
#include "cru/ui/style/ApplyBorderStyleInfo.hpp"
#include "cru/xml/XmlNode.hpp"

namespace cru::ui::mapper::style {
using namespace xml;
using ui::style::ApplyBorderStyleInfo;

bool BorderStyleMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) {
  return node->GetTag() == u"BorderStyle";
}

std::unique_ptr<ApplyBorderStyleInfo> BorderStyleMapper::DoMapFromXml(
    xml::XmlElementNode* node) {
  auto result = std::make_unique<ApplyBorderStyleInfo>();

  for (auto child : node->GetChildren()) {
    if (child->GetType() == XmlNode::Type::Text) {
      log::Debug(u"Ignore text node.");
    } else {
      auto c = child->AsElement();
      auto thickness_mapper =
          MapperRegistry::GetInstance()->GetMapper<Thickness>();
      auto corner_radius_mapper =
          MapperRegistry::GetInstance()->GetMapper<CornerRadius>();
      auto color_mapper = MapperRegistry::GetInstance()->GetMapper<Color>();
      if (thickness_mapper->XmlElementIsOfThisType(c)) {
        result->border_thickness = *thickness_mapper->MapFromXml(c);
      } else if (corner_radius_mapper->XmlElementIsOfThisType(c)) {
        result->border_radius = *corner_radius_mapper->MapFromXml(c);
      } else if (color_mapper->XmlElementIsOfThisType(c)) {
        auto brush = GetGraphicsFactory()->CreateSolidColorBrush(
            *color_mapper->MapFromXml(c));
        auto name = c->GetOptionalAttribute(u"name");
        if (name) {
          if (name == u"foreground") {
            result->foreground_brush = std::move(brush);
          } else if (name == u"background") {
            result->background_brush = std::move(brush);
          } else {
            log::Debug(u"Unknown brush name: {}", *name);
          }
        } else {
          result->border_brush = std::move(brush);
        }
      }
    }
  }

  return result;
}
}  // namespace cru::ui::mapper::style