1// Aseprite UI Library
2// Copyright (C) 2020-2022 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "ui/manager.h"
13#include "ui/system.h"
14#include "ui/theme.h"
15#include "ui/widget.h"
16#include "ui/window.h"
17
18#include <memory>
19#include <set>
20
21namespace ui {
22namespace details {
23
24static std::unique_ptr<std::set<Widget*>> widgets;
25
26void initWidgets()
27{
28 assert_ui_thread();
29 widgets = std::make_unique<std::set<Widget*>>();
30}
31
32void exitWidgets()
33{
34 assert_ui_thread();
35 widgets.reset();
36}
37
38void addWidget(Widget* widget)
39{
40 assert_ui_thread();
41
42 widgets->insert(widget);
43}
44
45void removeWidget(Widget* widget)
46{
47 assert_ui_thread();
48
49 ASSERT(!Manager::widgetAssociatedToManager(widget));
50
51 widgets->erase(widget);
52}
53
54// TODO we should be able to re-initialize all widgets without using
55// this global "widgets" set, so we don't have to keep track of
56// all widgets globally
57void reinitThemeForAllWidgets()
58{
59 assert_ui_thread();
60
61 // Reinitialize the theme in this order:
62 // 1. From the manager to children (windows and children widgets in
63 // the window etc.)
64 auto theme = get_theme();
65 if (auto man = Manager::getDefault()) {
66 man->setTheme(theme);
67 }
68
69 // 2. If some other widget wasn't updated (e.g. is outside the
70 // hierarchy of widgets), we update the theme for that one too.
71 //
72 // TODO Is this really needed?
73 for (auto widget : *widgets) {
74 if (theme != widget->theme())
75 widget->setTheme(theme);
76 }
77}
78
79} // namespace details
80} // namespace ui
81