File: clSystemSettings.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (232 lines) | stat: -rw-r--r-- 7,413 bytes parent folder | download | duplicates (2)
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
#include "clSystemSettings.h"

#include "ColoursAndFontsManager.h"
#include "cl_config.h"
#include "codelite_events.h"
#include "drawingutils.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "globals.h"
#include "imanager.h"
#include "wx/app.h"

#include <wx/button.h>
#include <wx/panel.h>
#include <wx/settings.h>

bool clSystemSettings::m_useCustomColours = false;
clColours clSystemSettings::m_customColours;
wxColour clSystemSettings::btn_face;
wxColour clSystemSettings::panel_face;

#ifdef __WXMSW__
#define IS_MSW 1
#define IS_MAC 0
#define IS_GTK 0
#elif defined(__WXOSX__)
#define IS_MSW 0
#define IS_MAC 1
#define IS_GTK 0
#else
#define IS_MSW 0
#define IS_MAC 0
#define IS_GTK 1
#endif

namespace
{
/// keep the initial startup background colour
/// we use this to detect any theme changes done to the system
/// the checks are done in the OnAppAcitvated event
wxColour startupBackgroundColour;

#ifdef __WXGTK__
double GetLuminance(const wxColour& color)
{
    double r = color.Red();
    double g = color.Green();
    double b = color.Blue();

    double luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
    return luma;
}
#endif
} // namespace

wxDEFINE_EVENT(wxEVT_SYS_COLOURS_CHANGED, clCommandEvent);

clSystemSettings::clSystemSettings()
{
    m_useCustomColours = clConfig::Get().Read("UseCustomBaseColour", false);
    if(m_useCustomColours) {
        wxColour baseColour = clConfig::Get().Read("BaseColour", wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
        m_customColours.InitFromColour(baseColour);
    }
    EventNotifier::Get()->Bind(wxEVT_CMD_COLOURS_FONTS_UPDATED, &clSystemSettings::OnColoursChanged, this);
    wxTheApp->Bind(wxEVT_SYS_COLOUR_CHANGED, &clSystemSettings::OnSystemColourChanged, this);
    wxTheApp->Bind(wxEVT_ACTIVATE_APP, &clSystemSettings::OnAppActivated, this);

    /// keep the initial background colour
    startupBackgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
}

clSystemSettings::~clSystemSettings() {}

wxColour clSystemSettings::GetColour(int index)
{
#ifdef __WXOSX__
    if(!panel_face.IsOk()) {
        wxDialog* __p = new wxDialog(nullptr, wxID_ANY, "", wxPoint(-1000, -1000), wxSize(1, 1));
        panel_face = __p->GetBackgroundColour();
        wxDELETE(__p);
    }
#endif

    if(!IS_MAC && !IS_MSW && m_useCustomColours) {
        bool is_dark = DrawingUtils::IsDark(m_customColours.GetBgColour());

        if(index == wxSYS_COLOUR_TOOLBAR) {
            index = wxSYS_COLOUR_3DFACE; // fallback
        } else if(index == wxSYS_COLOUR_TOOLBARTEXT) {
            return m_customColours.GetItemTextColour();
        }
        switch(index) {
        case wxSYS_COLOUR_MENUBAR:
        case wxSYS_COLOUR_3DFACE:
        case wxSYS_COLOUR_WINDOW:
            return m_customColours.GetBgColour();
        case wxSYS_COLOUR_LISTBOX:
            return m_customColours.GetBgColour().ChangeLightness(is_dark ? 105 : 100);
        case wxSYS_COLOUR_3DSHADOW:
            return m_customColours.GetBorderColour();
        case wxSYS_COLOUR_3DDKSHADOW:
            return m_customColours.GetDarkBorderColour();
        case wxSYS_COLOUR_WINDOWTEXT:
        case wxSYS_COLOUR_BTNTEXT:
            return m_customColours.GetItemTextColour();
        case wxSYS_COLOUR_GRAYTEXT:
            return m_customColours.GetGrayText();
        default:
            return wxSystemSettings::GetColour((wxSystemColour)index);
        }
    } else {
#ifdef __WXOSX__
        switch(index) {
        case wxSYS_COLOUR_TOOLBAR:
        case wxSYS_COLOUR_3DFACE:
            return panel_face;
        case wxSYS_COLOUR_TOOLBARTEXT:
            return wxSystemSettings::GetColour(wxSYS_COLOUR_MENUTEXT);
        default:
            return wxSystemSettings::GetColour((wxSystemColour)index);
        }
#else
        bool is_dark = DrawingUtils::IsDark(wxSYS_COLOUR_3DFACE);
        wxColour bg_colur = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
#ifdef __WXGTK__
        if(is_dark) {
            wxColour window_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
            wxColour face_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
            if(GetLuminance(window_colour) < GetLuminance(face_colour)) {
                // window colour is darker than the face colour, use the face colour
                bg_colur = window_colour;
            } else {
                // use button face colour
                bg_colur = face_colour;
            }
        }
#endif
        if(index == wxSYS_COLOUR_TOOLBAR) {
            return GetDefaultPanelColour();
        } else if(index == wxSYS_COLOUR_TOOLBARTEXT) {
            return wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
        } else if(index == wxSYS_COLOUR_WINDOW || index == wxSYS_COLOUR_3DFACE) {
            return bg_colur;
        } else if(index == wxSYS_COLOUR_LISTBOX) {
            return GetDefaultPanelColour().ChangeLightness(is_dark ? 110 : 90);
        } else {
            return wxSystemSettings::GetColour((wxSystemColour)index);
        }
#endif
    }
}

void clSystemSettings::OnColoursChanged(clCommandEvent& event)
{
    event.Skip();
    DoColourChangedEvent();
}

clSystemSettings& clSystemSettings::Get()
{
    static clSystemSettings settings;
    return settings;
}

void clSystemSettings::OnSystemColourChanged(wxSysColourChangedEvent& event)
{
    event.Skip();
    clDEBUG() << "system colour changed!" << endl;
    DoColourChangedEvent();
}

void clSystemSettings::DoColourChangedEvent()
{
    m_useCustomColours = clConfig::Get().Read("UseCustomBaseColour", false);
    if(m_useCustomColours) {
        wxColour baseColour = clConfig::Get().Read("BaseColour", wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
        if(DrawingUtils::IsDark(baseColour)) {
            baseColour = baseColour.ChangeLightness(110);
        } else {
            baseColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
        }
        m_customColours.InitFromColour(baseColour);

    } else {
        m_customColours.InitFromColour(GetDefaultPanelColour());
    }

    // Notify about colours changes
    clCommandEvent evtColoursChanged(wxEVT_SYS_COLOURS_CHANGED);
    EventNotifier::Get()->AddPendingEvent(evtColoursChanged);
}

wxColour clSystemSettings::GetDefaultPanelColour()
{
    wxColour panel_colour;
#ifdef __WXMSW__
    panel_colour = GetColour(wxSYS_COLOUR_3DFACE);
#else
    panel_colour = GetColour(IS_GTK ? wxSYS_COLOUR_WINDOW : wxSYS_COLOUR_3DFACE);
#ifdef __WXGTK__
    if(!m_useCustomColours && !DrawingUtils::IsDark(panel_colour)) {
        panel_colour = panel_colour.ChangeLightness(95);
    }
#endif
#endif
    return panel_colour;
}

void clSystemSettings::OnAppActivated(wxActivateEvent& event)
{
    event.Skip();
    // check for external theme detection
    wxColour currentBgColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
    if(currentBgColour != startupBackgroundColour) {
        /// and update the new background colour
        startupBackgroundColour = currentBgColour;

        /// external theme change detected, fire an event
        DoColourChangedEvent();
    }
}

void clSystemSettings::SampleColoursFromControls() {}

bool clSystemSettings::IsLexerThemeDark()
{
    auto lexer = ColoursAndFontsManager::Get().GetLexer("text");
    return lexer && lexer->IsDark();
}

bool clSystemSettings::IsDark() { return DrawingUtils::IsDark(GetDefaultPanelColour()); }