/
style.cpp
288 lines (241 loc) · 7.28 KB
/
style.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/** @file style.h User interface style.
*
* @authors Copyright (c) 2013-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Style"
#include "de/BaseGuiApp"
#include "de/GuiRootWidget"
#include <de/CommandLine>
#include <de/Config>
#include <de/Folder>
#include <de/Package>
#include <de/Record>
#include <de/RecordValue>
#include <de/ScriptSystem>
#include <de/Variable>
namespace de {
DE_PIMPL(Style)
, DE_OBSERVES(Variable, Change)
{
Record module;
RuleBank rules;
FontBank fonts;
ColorBank colors;
ImageBank images;
const Package *loadedPack;
const Variable &pixelRatio = ScriptSystem::get()["DisplayMode"]["PIXEL_RATIO"];
const Variable *uiTranslucency = nullptr;
Variable defaultTranslucency;
Impl(Public *i)
: Base(i)
, rules(DE_BASE_GUI_APP->pixelRatio())
{
defaultTranslucency = new NumberValue(true);
// The Style is available as a native module.
App::scriptSystem().addNativeModule("Style", module);
pixelRatio.audienceForChange() += this;
}
void clear()
{
rules.clear();
fonts.clear();
colors.clear();
images.clear();
module.clear();
loadedPack = nullptr;
}
void updateFontSizeFactor()
{
float fontSize = 1.0f;
if (CommandLine::ArgWithParams arg = App::commandLine().check("-fontsize", 1))
{
fontSize = arg.params.at(0).toFloat();
}
fonts.setFontSizeFactor(fontSize);
}
void load(Package const &pack)
{
loadedPack = &pack;
updateFontSizeFactor();
rules .addFromInfo(pack.root().locate<File>("rules.dei"));
fonts .addFromInfo(pack.root().locate<File>("fonts.dei"));
colors.addFromInfo(pack.root().locate<File>("colors.dei"));
images.addFromInfo(pack.root().locate<File>("images.dei"));
// Update the subrecords of the native module.
module.add(new Variable("rules", new RecordValue(rules), Variable::AllowRecord));
module.add(new Variable("fonts", new RecordValue(fonts), Variable::AllowRecord));
module.add(new Variable("colors", new RecordValue(colors), Variable::AllowRecord));
module.add(new Variable("images", new RecordValue(images), Variable::AllowRecord));
}
void variableValueChanged(Variable &, const Value &) override
{
if (loadedPack)
{
LOG_MSG("UI style being updated due to pixel ratio change");
#if 0
#if defined (WIN32)
/*
* KLUDGE: The operating system provides fonts scaled according to the desktop
* scaling factor. The user's scaling factor affects the sizing of the fonts
* on Windows via this value; on other platforms, the font definitions use
* DisplayMode.PIXEL_RATIO directly. (Should do that on Windows, too?)
*/
updateFontSizeFactor();
#endif
#endif
self().performUpdate();
}
}
DE_PIMPL_AUDIENCE(Change)
};
DE_AUDIENCE_METHOD(Style, Change)
Style::Style() : d(new Impl(this))
{}
Style::~Style()
{}
void Style::load(Package const &pack)
{
d->clear();
d->load(pack);
}
RuleBank const &Style::rules() const
{
return d->rules;
}
FontBank const &Style::fonts() const
{
return d->fonts;
}
ColorBank const &Style::colors() const
{
return d->colors;
}
ImageBank const &Style::images() const
{
return d->images;
}
RuleBank &Style::rules()
{
return d->rules;
}
FontBank &Style::fonts()
{
return d->fonts;
}
ColorBank &Style::colors()
{
return d->colors;
}
ImageBank &Style::images()
{
return d->images;
}
void Style::richStyleFormat(int contentStyle,
float &sizeFactor,
Font::RichFormat::Weight &fontWeight,
Font::RichFormat::Style &fontStyle,
int &colorIndex) const
{
switch (contentStyle)
{
default:
case Font::RichFormat::NormalStyle:
sizeFactor = 1.f;
fontWeight = Font::RichFormat::OriginalWeight;
fontStyle = Font::RichFormat::OriginalStyle;
colorIndex = Font::RichFormat::OriginalColor;
break;
case Font::RichFormat::MajorStyle:
sizeFactor = 1.f;
fontWeight = Font::RichFormat::Bold;
fontStyle = Font::RichFormat::Regular;
colorIndex = Font::RichFormat::HighlightColor;
break;
case Font::RichFormat::MinorStyle:
sizeFactor = 1.f;
fontWeight = Font::RichFormat::Normal;
fontStyle = Font::RichFormat::Regular;
colorIndex = Font::RichFormat::DimmedColor;
break;
case Font::RichFormat::MetaStyle:
sizeFactor = .8f;
fontWeight = Font::RichFormat::Light;
fontStyle = Font::RichFormat::Regular;
colorIndex = Font::RichFormat::AccentColor;
break;
case Font::RichFormat::MajorMetaStyle:
sizeFactor = .8f;
fontWeight = Font::RichFormat::Bold;
fontStyle = Font::RichFormat::Regular;
colorIndex = Font::RichFormat::AccentColor;
break;
case Font::RichFormat::MinorMetaStyle:
sizeFactor = .8f;
fontWeight = Font::RichFormat::Light;
fontStyle = Font::RichFormat::Regular;
colorIndex = Font::RichFormat::DimAccentColor;
break;
case Font::RichFormat::AuxMetaStyle:
sizeFactor = .8f;
fontWeight = Font::RichFormat::Light;
fontStyle = Font::RichFormat::OriginalStyle;
colorIndex = Font::RichFormat::AltAccentColor;
break;
}
}
Font const *Style::richStyleFont(Font::RichFormat::Style fontStyle) const
{
switch (fontStyle)
{
case Font::RichFormat::Monospace:
return &fonts().font(DE_STR("monospace"));
default:
return nullptr;
}
}
bool Style::isBlurringAllowed() const
{
if (!d->uiTranslucency)
{
d->uiTranslucency = Config::get().has("ui.translucency") ? &Config::get("ui.translucency")
: &d->defaultTranslucency;
}
return d->uiTranslucency->value().isTrue();
}
GuiWidget *Style::sharedBlurWidget() const
{
return nullptr;
}
void Style::performUpdate()
{
d->fonts.reload();
DE_FOR_AUDIENCE2(Change, i)
{
i->styleChanged(*this);
}
}
static Style *theAppStyle = nullptr;
Style &Style::get()
{
DE_ASSERT(theAppStyle != nullptr);
return *theAppStyle;
}
void Style::setAppStyle(Style &newStyle)
{
theAppStyle = &newStyle;
/// @todo Notify everybody about the style change. -jk
}
} // namespace de