Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
style.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019-2020 natinusala
3 Copyright 2019 WerWolv
4 Copyright 2019 p-sam
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17*/
18
19#pragma once
20
21#include <initializer_list>
22#include <string>
23#include <unordered_map>
24#include <utility>
25
26namespace aurore
27{
28
32enum class MetricKind
33{
34 OTHER,
35 LENGTH,
36};
37
40{
41 StyleMetric(std::string name, float value, MetricKind kind)
42 : name(std::move(name))
43 , value(value)
44 , kind(kind)
45 {
46 }
47
48 std::string name;
49 float value;
51};
52
54inline constexpr float DENSITY_COMFORTABLE = 1.0f;
55inline constexpr float DENSITY_COMPACT = 0.85f;
56inline constexpr float DENSITY_DENSE = 0.7f;
57inline constexpr float DENSITY_MIN = 0.5f;
58inline constexpr float DENSITY_MAX = 1.5f;
59
67void setDensity(float density);
69float getDensity();
71float dp(float length);
76bool parseDensity(const std::string& text, float* density);
77
81{
82 public:
84 StyleValues(std::initializer_list<std::pair<std::string, float>> list);
86 StyleValues(std::initializer_list<StyleMetric> list);
87
90 void addMetric(const std::string&, float value);
92 void addLength(const std::string& name, float value);
95 float getMetric(const std::string& name);
97 float getRawMetric(const std::string& name);
99 MetricKind getKind(const std::string& name) const;
101 bool hasMetric(const std::string& name) const;
102
104 void setOverlay(const std::unordered_map<std::string, float>& overlay);
105
106 private:
107 std::unordered_map<std::string, float> base;
108 std::unordered_map<std::string, float> overlay;
109 std::unordered_map<std::string, float> values;
110 std::unordered_map<std::string, MetricKind> kinds;
111};
112
114class Style
115{
116 public:
120 float operator[](const std::string& name);
121
123 void addMetric(const std::string& name, float value);
125 void addLength(const std::string& name, float value);
127 float getMetric(const std::string& name);
129 bool hasMetric(const std::string& name);
131 StyleValues* getValues() { return this->values; }
132
133 private:
134 StyleValues* values;
135};
136
139
140} // namespace aurore
Definition style.hpp:81
bool hasMetric(const std::string &name) const
Whether the metric exists.
void addMetric(const std::string &, float value)
void setOverlay(const std::unordered_map< std::string, float > &overlay)
The theme file's metrics, over the base (empty: the base alone).
float getMetric(const std::string &name)
StyleValues(std::initializer_list< std::pair< std::string, float > > list)
The base metrics, of kind OTHER.
float getRawMetric(const std::string &name)
The metric as written, before the density.
void addLength(const std::string &name, float value)
A length of the base (kind LENGTH): it follows the density.
StyleValues(std::initializer_list< StyleMetric > list)
The base metrics, each with its kind.
MetricKind getKind(const std::string &name) const
The kind of a metric (OTHER when unknown).
Simple wrapper around StyleValues for the array operator.
Definition style.hpp:115
void addLength(const std::string &name, float value)
Adds or changes a length of the base (StyleValues::addLength).
float operator[](const std::string &name)
The metric name (getMetric()).
void addMetric(const std::string &name, float value)
Adds or changes a metric of the base (StyleValues::addMetric).
Style(StyleValues *values)
A handle on values (not owned).
bool hasMetric(const std::string &name)
Whether the metric exists.
StyleValues * getValues()
The metrics behind the handle.
Definition style.hpp:131
float getMetric(const std::string &name)
A metric; an unknown name is logged as an error and gives 0.
Definition actions.hpp:25
constexpr float DENSITY_MIN
Definition style.hpp:57
constexpr float DENSITY_DENSE
small screens
Definition style.hpp:56
MetricKind
Definition style.hpp:33
void setDensity(float density)
bool parseDensity(const std::string &text, float *density)
constexpr float DENSITY_COMFORTABLE
The density: lengths drawn that many times their size (aurore::dp()).
Definition style.hpp:54
float dp(float length)
length at the density in use: what 16dp is in XML.
constexpr float DENSITY_MAX
Definition style.hpp:58
Style getStyle()
The app's metrics: what XML reads with @style/….
float getDensity()
The density in use (DENSITY_COMFORTABLE by default).
constexpr float DENSITY_COMPACT
Definition style.hpp:55
Definition text.hpp:13
A metric of the base, with its kind.
Definition style.hpp:40
StyleMetric(std::string name, float value, MetricKind kind)
Definition style.hpp:41
std::string name
Definition style.hpp:48
MetricKind kind
Definition style.hpp:50
float value
Definition style.hpp:49