summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/SystemTheme.cpp
blob: 268a2848e3ab2d5bc9b4c3eb1822f17c51d96f6c (plain)
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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/ConfigFile.h>
#include <LibGfx/SystemTheme.h>
#include <string.h>

namespace Gfx {

static SystemTheme dummy_theme;
static const SystemTheme* theme_page = &dummy_theme;
static Core::AnonymousBuffer theme_buffer;

Core::AnonymousBuffer& current_system_theme_buffer()
{
    VERIFY(theme_buffer.is_valid());
    return theme_buffer;
}

void set_system_theme(Core::AnonymousBuffer buffer)
{
    theme_buffer = move(buffer);
    theme_page = theme_buffer.data<SystemTheme>();
}

Core::AnonymousBuffer load_system_theme(const String& path)
{
    auto file = Core::ConfigFile::open(path);
    auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme));

    auto* data = buffer.data<SystemTheme>();

    auto get_color = [&](auto& name) {
        auto color_string = file->read_entry("Colors", name);
        auto color = Color::from_string(color_string);
        if (!color.has_value())
            return Color(Color::Black);
        return color.value();
    };

    auto get_metric = [&](auto& name, auto role) {
        int metric = file->read_num_entry("Metrics", name, -1);
        if (metric == -1) {
            switch (role) {
            case (int)MetricRole::TitleHeight:
                return 19;
            case (int)MetricRole::TitleButtonHeight:
                return 15;
            case (int)MetricRole::TitleButtonWidth:
                return 15;
            default:
                dbgln("Metric {} has no fallback value!", name);
                return 16;
            }
        }
        return metric;
    };

    auto get_path = [&](auto& name, auto role, bool allow_empty) {
        auto path = file->read_entry("Paths", name);
        if (path.is_empty()) {
            switch (role) {
            case (int)PathRole::TitleButtonIcons:
                return "/res/icons/16x16/";
            default:
                return allow_empty ? "" : "/res/";
            }
        }
        return &path[0];
    };

#undef __ENUMERATE_COLOR_ROLE
#define __ENUMERATE_COLOR_ROLE(role) \
    data->color[(int)ColorRole::role] = get_color(#role).value();
    ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE

#define DO_METRIC(x) \
    data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)

    DO_METRIC(TitleHeight);
    DO_METRIC(TitleButtonWidth);
    DO_METRIC(TitleButtonHeight);

#define DO_PATH(x, allow_empty)                                                                                  \
    do {                                                                                                         \
        auto path = get_path(#x, (int)PathRole::x, allow_empty);                                                 \
        memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
        data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0';                           \
    } while (0)

    DO_PATH(TitleButtonIcons, false);
    DO_PATH(ActiveWindowShadow, true);
    DO_PATH(InactiveWindowShadow, true);
    DO_PATH(TaskbarShadow, true);
    DO_PATH(MenuShadow, true);
    DO_PATH(TooltipShadow, true);

    return buffer;
}

}