Skip to content

Commit

Permalink
Traktor: Able to specify CSS class names in icon SVG for theme specif…
Browse files Browse the repository at this point in the history
…ic colors.
  • Loading branch information
apistol78 committed May 8, 2024
1 parent af2193b commit 30b38cc
Show file tree
Hide file tree
Showing 193 changed files with 217 additions and 55,053 deletions.
30 changes: 15 additions & 15 deletions code/Svg/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ float parseNumber(const std::wstring& value, float defaultValue = 0.0f)

T_IMPLEMENT_RTTI_CLASS(L"traktor.svg.Parser", Parser, Object)

Parser::Parser()
Parser::Parser(const class_style_fn& classStyleFn)
: m_classStyleFn(classStyleFn)
{
m_defaultStyle = new Style();
}

Ref< Shape > Parser::parse(xml::Document* doc)
Expand Down Expand Up @@ -208,11 +208,18 @@ Ref< Shape > Parser::traverse(xml::Element* elm)
for (const xml::Attribute* attr = elm->getFirstAttribute(); attr != nullptr; attr = attr->getNext())
shape->setAttribute(attr->getName(), Any::fromString(attr->getValue()));

if (!shape->getStyle())
shape->setStyle(parseStyle(elm));
if (!shape->getStyle())
shape->setStyle(m_defaultStyle);
Ref< Style > style = new Style();

if (m_classStyleFn && elm->getAttribute(L"class") != nullptr)
{
Ref< const Style > classStyle = m_classStyleFn(elm->getAttribute(L"class")->getValue());
if (classStyle)
style = new Style(*classStyle);
}

parseStyle(elm, style);

shape->setStyle(style);
shape->setTransform(parseTransform(elm, L"transform"));

for (xml::Node* child = elm->getFirstChild(); child; child = child->getNextSibling())
Expand Down Expand Up @@ -756,18 +763,15 @@ Ref< Gradient > Parser::parseGradientDef(const xml::Element* defs, const xml::El
return gradient;
}

Ref< Style > Parser::parseStyle(xml::Element* elm)
void Parser::parseStyle(const xml::Element* elm, Style* style)
{
if (!elm)
return nullptr;
return;

Ref< Style > style;
Color4f color;

if (elm->hasAttribute(L"fill"))
{
style = new Style();

const std::wstring fillDesc = elm->getAttribute(L"fill")->getValue();
if (parseColor(fillDesc, color))
{
Expand All @@ -779,8 +783,6 @@ Ref< Style > Parser::parseStyle(xml::Element* elm)
}
else if (elm->hasAttribute(L"style"))
{
style = new Style();

std::vector< std::wstring > styles;
Split< std::wstring >::any(elm->getAttribute(L"style")->getValue(), L";", styles);
for (const auto& st : styles)
Expand Down Expand Up @@ -891,8 +893,6 @@ Ref< Style > Parser::parseStyle(xml::Element* elm)
log::debug << L"Unknown CSS style \"" << key << L"\"" << Endl;
}
}

return style;
}

Matrix33 Parser::parseTransform(const xml::Element* elm, const std::wstring& attrName) const
Expand Down
9 changes: 6 additions & 3 deletions code/Svg/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
#pragma once

#include <functional>
#include <string>
#include "Core/Ref.h"
#include "Core/Object.h"
Expand Down Expand Up @@ -46,14 +47,16 @@ class T_DLLCLASS Parser : public Object
T_RTTI_CLASS;

public:
Parser();
typedef std::function< Ref< const Style >(const std::wstring_view&) > class_style_fn;

explicit Parser(const class_style_fn& classStyleFn = class_style_fn());

Ref< Shape > parse(xml::Document* doc);

Ref< Shape > parse(const traktor::Path& fileName);

private:
Ref< Style > m_defaultStyle;
class_style_fn m_classStyleFn;
SmallMap< std::wstring, Ref< Gradient > > m_gradients;
SmallMap< std::wstring, Ref< Shape > > m_shapeDefs;

Expand Down Expand Up @@ -83,7 +86,7 @@ class T_DLLCLASS Parser : public Object

Ref< Gradient > parseGradientDef(const xml::Element* defs, const xml::Element* elm) const;

Ref< Style > parseStyle(xml::Element* elm);
void parseStyle(const xml::Element* elm, Style* style);

Matrix33 parseTransform(const xml::Element* elm, const std::wstring& attrName) const;

Expand Down
31 changes: 26 additions & 5 deletions code/Ui/StyleBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Svg/Document.h"
#include "Svg/Parser.h"
#include "Svg/Rasterizer.h"
#include "Svg/Style.h"
#include "Ui/Application.h"
#include "Ui/StyleBitmap.h"
#include "Ui/StyleSheet.h"
Expand Down Expand Up @@ -66,22 +67,22 @@ void StyleBitmap::destroy()
Size StyleBitmap::getSize(const Widget* reference) const
{
const int32_t dpi = reference != nullptr ? reference->dpi() : 96;
return resolve(dpi) ? m_bitmap->getSize() : Size();
return resolve(reference, dpi) ? m_bitmap->getSize() : Size();
}

Ref< drawing::Image > StyleBitmap::getImage(const Widget* reference) const
{
const int32_t dpi = reference != nullptr ? reference->dpi() : 96;
return resolve(dpi) ? m_bitmap->getImage() : nullptr;
return resolve(reference, dpi) ? m_bitmap->getImage() : nullptr;
}

ISystemBitmap* StyleBitmap::getSystemBitmap(const Widget* reference) const
{
const int32_t dpi = reference != nullptr ? reference->dpi() : 96;
return resolve(dpi) ? m_bitmap : nullptr;
return resolve(reference, dpi) ? m_bitmap : nullptr;
}

bool StyleBitmap::resolve(int32_t dpi) const
bool StyleBitmap::resolve(const Widget* reference, int32_t dpi) const
{
const StyleSheet* ss = Application::getInstance()->getStyleSheet();
if (!ss)
Expand Down Expand Up @@ -119,7 +120,27 @@ bool StyleBitmap::resolve(int32_t dpi) const

if (fileName.getExtension() == L"svg")
{
Ref< svg::Document > sd = dynamic_type_cast< svg::Document* >(svg::Parser().parse(fileName));
const Color4f themeColor = Color4f::fromColor4ub(ss->getColor(reference, L"theme-color"));

Ref< svg::Style > themeFill = new svg::Style();
themeFill->setFillEnable(true);
themeFill->setFill(themeColor);

Ref< svg::Style > themeStroke = new svg::Style();
themeStroke->setStrokeEnable(true);
themeStroke->setStroke(themeColor);

auto classStyle = [&](const std::wstring_view& className) -> Ref< const svg::Style >
{
if (className == L"theme_fill")
return themeFill;
else if (className == L"theme_stroke")
return themeStroke;
else
return nullptr;
};

Ref< svg::Document > sd = dynamic_type_cast< svg::Document* >(svg::Parser(classStyle).parse(fileName));
if (!sd)
return false;

Expand Down
2 changes: 1 addition & 1 deletion code/Ui/StyleBitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class T_DLLCLASS StyleBitmap : public IBitmap
mutable std::wstring m_fileName;
mutable int32_t m_dpi = -1;

bool resolve(int32_t dpi) const;
bool resolve(const Widget* reference, int32_t dpi) const;
};

}
2 changes: 1 addition & 1 deletion code/Ui/StyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Color4ub StyleSheet::getColor(const std::wstring& typeName, const std::wstring_v

Color4ub StyleSheet::getColor(const Object* widget, const std::wstring_view& element) const
{
const TypeInfo* widgetType = &type_of(widget);
const TypeInfo* widgetType = widget ? &type_of(widget) : &type_of< Object >();
while (widgetType != nullptr)
{
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const Entity& entity) {
Expand Down
Loading

0 comments on commit 30b38cc

Please sign in to comment.