File: MainApplication.cpp

package info (click to toggle)
pageedit 1.9.20%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,508 kB
  • sloc: ansic: 31,777; cpp: 12,496; python: 1,415; makefile: 106; javascript: 87; sh: 21
file content (107 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download
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
/************************************************************************
**
**  Copyright (C) 2019-2021 Kevin B. Hendricks, Stratford Ontario Canada
**  Copyright (C) 2012      John Schember <john@nachtimwald.com>
**  Copyright (C) 2012      Grant Drake
**  Copyright (C) 2012      Dave Heiland
**
**  This file is part of PageEdit.
**
**  PageEdit is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  PageEdit 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 General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with PageEdit.  If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/

#include <QApplication>
#include <QTimer>
#include <QStyleFactory>
#include <QStyle>
#include <QPalette>
#include <QDebug>

#include "MainApplication.h"

MainApplication::MainApplication(int &argc, char **argv)
    : QApplication(argc, argv),
      m_Style(nullptr),
      m_isDark(false)
{
#ifdef Q_OS_MAC
    // on macOS the application palette actual text colors never seem to change when DarkMode is enabled
    // so use a mac style standardPalette
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    m_Style = QStyleFactory::create("macintosh");
#else
    m_Style = QStyleFactory::create("macos");
#endif
    QPalette app_palette = m_Style->standardPalette();
    m_isDark = app_palette.color(QPalette::Active,QPalette::WindowText).lightness() > 128;
    fixMacDarkModePalette(app_palette);
    // set the initial app palette
    setPalette(app_palette);
#endif
}

void MainApplication::fixMacDarkModePalette(QPalette &pal)
{
# ifdef Q_OS_MAC
    // See QTBUG-75321 and follow Kovid's workaround for broken ButtonText always being dark
    pal.setColor(QPalette::ButtonText, pal.color(QPalette::WindowText));
    if (m_isDark) {
        // make alternating base color change not so sharp
        pal.setColor(QPalette::AlternateBase, pal.color(QPalette::Base).lighter(150));
        // make link color better for dark mode (try to match calibre for consistency)
        pal.setColor(QPalette::Link, QColor("#6cb4ee"));
    }
#endif
}






bool MainApplication::event(QEvent *pEvent)
{
    if (pEvent->type() == QEvent::ApplicationActivate) {
        emit applicationActivated();
    } else if (pEvent->type() == QEvent::ApplicationDeactivate) {
        emit applicationDeactivated();
    }
#ifdef Q_OS_MAC
    if (pEvent->type() == QEvent::ApplicationPaletteChange) {
        // qDebug() << "Application Palette Changed";
	QTimer::singleShot(0, this, SLOT(EmitPaletteChanged()));
    }
#endif
    return QApplication::event(pEvent);
}

void MainApplication::EmitPaletteChanged()
{
#ifdef Q_OS_MAC
    // on macOS the application palette actual colors never seem to change after launch
    // even when DarkMode is enabled. So we use a mac style standardPalette to determine
    // if a drak vs light mode transition has been made and then use it to set the
    // Application palette
    QPalette app_palette = m_Style->standardPalette();
    bool isdark = app_palette.color(QPalette::Active,QPalette::WindowText).lightness() > 128;
    if (m_isDark != isdark) {
        // qDebug() << "Theme changed " << "was isDark:" << m_isDark << "now isDark:" << isdark;
        m_isDark = isdark;
        fixMacDarkModePalette(app_palette);
        setPalette(app_palette);
        emit applicationPaletteChanged();
    }
#endif
}