aboutsummaryrefslogtreecommitdiff
path: root/src/ui/mapper/BorderStyleMapper.cpp
blob: 691f091d6acbf4089c287270cf1fa020ec8670ad (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/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 {
using namespace xml;
using ui::style::ApplyBorderStyleInfo;

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

ApplyBorderStyleInfo BorderStyleMapper::DoMapFromXml(
    xml::XmlElementNode* node) {
  ApplyBorderStyleInfo result;

  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->GetOptionalAttributeCaseInsensitive(u"name");
        if (name) {
          if (name->CaseInsensitiveCompare(u"foreground") == 0) {
            result.foreground_brush = std::move(brush);
          } else if (name->CaseInsensitiveCompare(u"background") == 0) {
            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