ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Theme.c File Reference
#include <assert.h>
#include <Objectively/JSONContext.h>
#include "ImageAtlas.h"
#include "Log.h"
#include "Theme.h"
#include "View.h"
#include "WindowController.h"

Go to the source code of this file.

Macros

#define _Class   _Theme
 

Functions

Class * _Theme (void)
 
static AtlasImage * addIcon (Theme *self, const char *name, Image *image)
 
static void addStylesheet (Theme *self, Stylesheet *stylesheet)
 
static String * cacheKey (const FontAttributes *attributes, float pixelDensity)
 Builds the cache key for the given attributes and pixel density.
 
static Style * computeStyle (const Theme *self, const View *view)
 
static ident computeStyle_reduce (const ident obj, ident accumulator, ident data)
 Reducer for computeStyle.
 
static void dealloc (Object *self)
 
static Font * font (Theme *self, const FontAttributes *attributes, float pixelDensity)
 
static AtlasImage * icon (Theme *self, const char *name)
 
static ImageAtlas * icons (Theme *self)
 
static Image * image (Theme *self, const char *name, float pixelDensity)
 
static Theme * init (Theme *self)
 
static void initialize (Class *clazz)
 
static void removeStylesheet (Theme *self, Stylesheet *stylesheet)
 
static void renderDeviceDidReset (Theme *self)
 
static void renderDeviceWillReset (Theme *self)
 
static void renderDeviceWillReset_enumerate (const Dictionary *dictionary, ident obj, ident key, ident data)
 DictionaryEnumerator for renderDeviceWillReset.
 
static Theme * theme (SDL_Window *window)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Theme

Definition at line 34 of file Theme.c.

Function Documentation

◆ _Theme()

Class * _Theme ( void  )

Definition at line 334 of file Theme.c.

334 {
335 static Class *clazz;
336 static Once once;
337
338 do_once(&once, {
339 clazz = _initialize(&(const ClassDef) {
340 .name = "Theme",
341 .superclass = _Object(),
342 .instanceSize = sizeof(Theme),
343 .interfaceSize = sizeof(ThemeInterface),
344 .initialize = initialize,
345 });
346 });
347
348 return clazz;
349}
static void initialize(Class *clazz)
Definition Theme.c:312
The Theme type.
Definition Theme.h:54

◆ addIcon()

static AtlasImage * addIcon ( Theme *  self,
const char *  name,
Image *  image 
)
static

Definition at line 158 of file Theme.c.

158 {
159
160 ImageAtlas *atlas = $(self, icons);
161
163
164 if (!$(atlas, compile)) {
165 MVC_LogError("Failed to compile icons after adding %s\n", name);
166 }
167
168 return icon;
169}
static String * name(const Font *self)
Definition Font.c:303
static AtlasImage * addImageWithName(ImageAtlas *self, const char *name, Image *image)
Definition ImageAtlas.c:96
static bool compile(ImageAtlas *self)
Definition ImageAtlas.c:170
#define MVC_LogError(fmt,...)
Definition Log.h:57
static AtlasImage * icon(Theme *self, const char *name)
Definition Theme.c:175
static ImageAtlas * icons(Theme *self)
Definition Theme.c:144
static Image * image(Theme *self, const char *name, float pixelDensity)
Definition Theme.c:82
An Image packed into an ImageAtlas.
Definition AtlasImage.h:41
Packs many Images into one sheet, backed by one Texture.
Definition ImageAtlas.h:49

◆ addStylesheet()

static void addStylesheet ( Theme *  self,
Stylesheet *  stylesheet 
)
static

Definition at line 59 of file Theme.c.

59 {
60 $((Array *) self->stylesheets, addObject, stylesheet);
61}
Array * stylesheets
The Stylesheets, in order of priority.
Definition Theme.h:89

◆ cacheKey()

static String * cacheKey ( const FontAttributes *  attributes,
float  pixelDensity 
)
static

Builds the cache key for the given attributes and pixel density.

Definition at line 66 of file Theme.c.

66 {
67
68 String *name = $$(Font, nameWithAttributes, attributes);
69 assert(name);
70
71 String *key = str("%s@%g", name->chars, pixelDensity);
72 assert(key);
73
74 release(name);
75 return key;
76}
static String * nameWithAttributes(const FontAttributes *attributes)
Definition Font.c:312
TrueType fonts.
Definition Font.h:88

◆ computeStyle()

static Style * computeStyle ( const Theme *  self,
const View *  view 
)
static

Definition at line 223 of file Theme.c.

223 {
224
225 assert(view);
226
227 Style *style = $(alloc(Style), initWithAttributes, NULL);
228 assert(style);
229
230 return $((Array *) self->stylesheets, reduce, computeStyle_reduce, style, (ident) view);
231}
static Style * initWithAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:394
static ident computeStyle_reduce(const ident obj, ident accumulator, ident data)
Reducer for computeStyle.
Definition Theme.c:182
The Style type.
Definition Style.h:43

◆ computeStyle_reduce()

static ident computeStyle_reduce ( const ident  obj,
ident  accumulator,
ident  data 
)
static

Reducer for computeStyle.

Definition at line 182 of file Theme.c.

182 {
183
184 const Stylesheet *stylesheet = obj;
185
186 const View *view = data;
187
188 const Array *selectors = (Array *) stylesheet->selectors;
189 for (size_t i = 0; i < selectors->count; i++) {
190
191 Selector *selector = $(selectors, objectAtIndex, i);
192 if ($(selector, matchesView, view)) {
193
194 assert(selector->style);
195
196 if (MVC_LogEnabled(SDL_LOG_PRIORITY_VERBOSE)) {
197
198 String *this = $((Object *) selector, description);
199 String *that = $((Object *) view, description);
200
201 MVC_LogVerbose("%s -> %s\n", this->chars, that->chars);
202
203 release(this);
204 release(that);
205 }
206
207 if ($(selector->style, attributeValue, "debug")) {
208 SDL_TriggerBreakpoint();
209 }
210
211 $((Style *) accumulator, addSelector, selector);
212 $((Style *) accumulator, addAttributes, selector->style->attributes);
213 }
214 }
215
216 return accumulator;
217}
static String * description(const Object *self)
Definition Label.c:47
#define MVC_LogVerbose(fmt,...)
Definition Log.h:45
#define MVC_LogEnabled(priority)
Definition Log.h:39
static bool matchesView(const Selector *self, const View *view)
Definition Selector.c:277
static ident attributeValue(const Style *self, const char *attr)
Definition Style.c:386
static void addSelector(Style *self, Selector *selector)
Definition Style.c:357
static void addAttributes(Style *self, const Dictionary *attributes)
Definition Style.c:221
Selectors are comprised of one or more SelectorSequences.
Definition Selector.h:49
Style * style
The Style.
Definition Selector.h:82
Dictionary * attributes
Definition Style.h:59
Stylesheets are comprised of Selectors and Styles.
Definition Stylesheet.h:44
Array * selectors
The Selectors, ordered by specificity.
Definition Stylesheet.h:60
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 41 of file Theme.c.

41 {
42
43 Theme *this = (Theme *) self;
44
45 release(this->fontCache);
46 release(this->imageCache);
47 release(this->icons);
48 release(this->stylesheets);
49
50 super(Object, self, dealloc);
51}
static void dealloc(Object *self)
Definition Theme.c:41

◆ font()

static Font * font ( Theme *  self,
const FontAttributes *  attributes,
float  pixelDensity 
)
static

Definition at line 108 of file Theme.c.

108 {
109
110 String *key = cacheKey(attributes, pixelDensity);
111
112 Font *font = $(self->fontCache, objectForKeyPath, key->chars);
113 if (font == NULL) {
114
115 Font *resolved = $$(Font, fontWithAttributes, attributes, pixelDensity);
116 assert(resolved);
117
118 // Keyed by the resolved Font's own name: when the requested family is not registered,
119 // Font::fontWithAttributes falls back to the default Font, and caching that under the
120 // requested key would pin the wrong Font to it permanently. This way the request
121 // simply misses again, and heals once the family is registered via Font::cacheFont.
122 const FontAttributes resolvedAttributes = { resolved->family, resolved->size, resolved->style };
123 String *resolvedKey = cacheKey(&resolvedAttributes, pixelDensity);
124
125 font = $(self->fontCache, objectForKeyPath, resolvedKey->chars);
126 if (font == NULL) {
127 $(self->fontCache, setObjectForKeyPath, resolved, resolvedKey->chars);
128 font = resolved;
129 }
130
131 release(resolvedKey);
132 release(resolved);
133 }
134
135 release(key);
136
137 return font;
138}
static Font * fontWithAttributes(const FontAttributes *attributes, float pixelDensity)
Definition Font.c:199
static String * cacheKey(const FontAttributes *attributes, float pixelDensity)
Builds the cache key for the given attributes and pixel density.
Definition Theme.c:66
static Font * font(Theme *self, const FontAttributes *attributes, float pixelDensity)
Definition Theme.c:108
The family, size and style attributes that identify a Font, independent of the pixel density at which...
Definition Font.h:64
char * family
The family name.
Definition Font.h:117
int size
The point size.
Definition Font.h:144
int style
The style.
Definition Font.h:149

◆ icon()

static AtlasImage * icon ( Theme *  self,
const char *  name 
)
static

Definition at line 175 of file Theme.c.

175 {
176 return $($(self, icons), imageWithName, name);
177}
static AtlasImage * imageWithName(const ImageAtlas *self, const char *name)
Definition ImageAtlas.c:112

◆ icons()

static ImageAtlas * icons ( Theme *  self)
static

Definition at line 144 of file Theme.c.

144 {
145
146 if (self->icons == NULL) {
147 self->icons = $(alloc(ImageAtlas), init);
148 assert(self->icons);
149 }
150
151 return self->icons;
152}
static Theme * init(Theme *self)
Definition Theme.c:237

◆ image()

static Image * image ( Theme *  self,
const char *  name,
float  pixelDensity 
)
static

Definition at line 82 of file Theme.c.

82 {
83
84 assert(name);
85
86 String *key = str("%s@%g", name, pixelDensity);
87 assert(key);
88
89 Image *image = $(self->imageCache, objectForKeyPath, key->chars);
90 if (image == NULL) {
91
92 image = $$(Image, imageWithResourceName, name, pixelDensity);
93 if (image) {
94 $(self->imageCache, setObjectForKeyPath, image, key->chars);
95 release(image);
96 }
97 }
98
99 release(key);
100
101 return image;
102}
static Image * imageWithResourceName(const char *name, float pixelDensity)
Definition Image.c:74
Image loading.
Definition Image.h:41

◆ init()

static Theme * init ( Theme *  self)
static

Definition at line 237 of file Theme.c.

237 {
238
239 self = (Theme *) super(Object, self, init);
240 if (self) {
241
242 self->fontCache = $$(Dictionary, dictionary);
243 assert(self->fontCache);
244
245 self->imageCache = $$(Dictionary, dictionary);
246 assert(self->imageCache);
247
248 self->stylesheets = $$(Array, arrayWithCapacity, 8);
249 assert(self->stylesheets);
250
252 }
253
254 return self;
255}
static Stylesheet * defaultStylesheet(void)
Definition Stylesheet.c:95
static void addStylesheet(Theme *self, Stylesheet *stylesheet)
Definition Theme.c:59

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 312 of file Theme.c.

312 {
313
314 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
315
316 ((ThemeInterface *) clazz->interface)->addIcon = addIcon;
317 ((ThemeInterface *) clazz->interface)->addStylesheet = addStylesheet;
318 ((ThemeInterface *) clazz->interface)->computeStyle = computeStyle;
319 ((ThemeInterface *) clazz->interface)->font = font;
320 ((ThemeInterface *) clazz->interface)->image = image;
321 ((ThemeInterface *) clazz->interface)->icon = icon;
322 ((ThemeInterface *) clazz->interface)->icons = icons;
323 ((ThemeInterface *) clazz->interface)->init = init;
324 ((ThemeInterface *) clazz->interface)->removeStylesheet = removeStylesheet;
325 ((ThemeInterface *) clazz->interface)->renderDeviceDidReset = renderDeviceDidReset;
326 ((ThemeInterface *) clazz->interface)->renderDeviceWillReset = renderDeviceWillReset;
327 ((ThemeInterface *) clazz->interface)->theme = theme;
328}
static Theme * theme(SDL_Window *window)
Definition Theme.c:295
static void renderDeviceWillReset(Theme *self)
Definition Theme.c:276
static Style * computeStyle(const Theme *self, const View *view)
Definition Theme.c:223
static void removeStylesheet(Theme *self, Stylesheet *stylesheet)
Definition Theme.c:261
static AtlasImage * addIcon(Theme *self, const char *name, Image *image)
Definition Theme.c:158
static void renderDeviceDidReset(Theme *self)
Definition Theme.c:287
ImageAtlas * init(ImageAtlas *self)
Initializes this ImageAtlas.
Definition ImageAtlas.c:260
void removeStylesheet(Theme *self, Stylesheet *stylesheet)
Removes the given Stylesheet from this Theme.
Definition Theme.c:261

◆ removeStylesheet()

static void removeStylesheet ( Theme *  self,
Stylesheet *  stylesheet 
)
static

Definition at line 261 of file Theme.c.

261 {
262 $((Array *) self->stylesheets, removeObject, stylesheet);
263}

◆ renderDeviceDidReset()

static void renderDeviceDidReset ( Theme *  self)
static

Definition at line 287 of file Theme.c.

287 {
288 // Bitmap Textures are recreated lazily by ImageAtlas::texture on next use.
289}

◆ renderDeviceWillReset()

static void renderDeviceWillReset ( Theme *  self)
static

Definition at line 276 of file Theme.c.

276 {
277 $(self->fontCache, enumerateObjectsAndKeys, renderDeviceWillReset_enumerate, NULL);
278 if (self->icons) {
279 $(self->icons, renderDeviceWillReset);
280 }
281}
static void renderDeviceWillReset_enumerate(const Dictionary *dictionary, ident obj, ident key, ident data)
DictionaryEnumerator for renderDeviceWillReset.
Definition Theme.c:268

◆ renderDeviceWillReset_enumerate()

static void renderDeviceWillReset_enumerate ( const Dictionary *  dictionary,
ident  obj,
ident  key,
ident  data 
)
static

DictionaryEnumerator for renderDeviceWillReset.

Definition at line 268 of file Theme.c.

268 {
269 $((Font *) obj, renderDeviceWillReset);
270}

◆ theme()

static Theme * theme ( SDL_Window *  window)
static

Definition at line 295 of file Theme.c.

295 {
296
297 assert(window);
298
300 if (windowController) {
301 return windowController->theme;
302 }
303
304 return NULL;
305}
static WindowController * windowController(SDL_Window *window)
A WindowController manages a ViewController and its descendants within an SDL_Window.
Theme * theme
The Theme.