1 /************************************************************************
2 **
3 **  Copyright (C) 2019-2020 Kevin B. Hendricks, Stratford Ontario Canada
4 **  Copyright (C) 2012      John Schember <john@nachtimwald.com>
5 **  Copyright (C) 2012      Grant Drake
6 **  Copyright (C) 2012      Dave Heiland
7 **
8 **  This file is part of PageEdit.
9 **
10 **  PageEdit is free software: you can redistribute it and/or modify
11 **  it under the terms of the GNU General Public License as published by
12 **  the Free Software Foundation, either version 3 of the License, or
13 **  (at your option) any later version.
14 **
15 **  PageEdit is distributed in the hope that it will be useful,
16 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 **  GNU General Public License for more details.
19 **
20 **  You should have received a copy of the GNU General Public License
21 **  along with PageEdit.  If not, see <http://www.gnu.org/licenses/>.
22 **
23 *************************************************************************/
24 
25 #include <QApplication>
26 #include <QTimer>
27 #include <QStyleFactory>
28 #include <QStyle>
29 #include <QPalette>
30 #include <QDebug>
31 
32 #include "MainApplication.h"
33 
MainApplication(int & argc,char ** argv)34 MainApplication::MainApplication(int &argc, char **argv)
35     : QApplication(argc, argv),
36       m_Style(nullptr),
37       m_isDark(false)
38 {
39 #ifdef Q_OS_MAC
40     // on macOS the application palette actual text colors never seem to change when DarkMode is enabled
41     // so use a mac style standardPalette
42     m_Style = QStyleFactory::create("macintosh");
43     QPalette app_palette = m_Style->standardPalette();
44     m_isDark = app_palette.color(QPalette::Active,QPalette::WindowText).lightness() > 128;
45     fixMacDarkModePalette(app_palette);
46     // set the initial app palette
47     setPalette(app_palette);
48 #endif
49 }
50 
fixMacDarkModePalette(QPalette & pal)51 void MainApplication::fixMacDarkModePalette(QPalette &pal)
52 {
53 # ifdef Q_OS_MAC
54     // See QTBUG-75321 and follow Kovid's workaround for broken ButtonText always being dark
55     pal.setColor(QPalette::ButtonText, pal.color(QPalette::WindowText));
56     if (m_isDark) {
57         // make alternating base color change not so sharp
58         pal.setColor(QPalette::AlternateBase, pal.color(QPalette::Base).lighter(150));
59         // make link color better for dark mode (try to match calibre for consistency)
60         pal.setColor(QPalette::Link, QColor("#6cb4ee"));
61     }
62 #endif
63 }
64 
65 
66 
67 
68 
69 
event(QEvent * pEvent)70 bool MainApplication::event(QEvent *pEvent)
71 {
72     if (pEvent->type() == QEvent::ApplicationActivate) {
73         emit applicationActivated();
74     } else if (pEvent->type() == QEvent::ApplicationDeactivate) {
75         emit applicationDeactivated();
76     }
77 #ifdef Q_OS_MAC
78     if (pEvent->type() == QEvent::ApplicationPaletteChange) {
79         // qDebug() << "Application Palette Changed";
80 	QTimer::singleShot(0, this, SLOT(EmitPaletteChanged()));
81     }
82 #endif
83     return QApplication::event(pEvent);
84 }
85 
EmitPaletteChanged()86 void MainApplication::EmitPaletteChanged()
87 {
88 #ifdef Q_OS_MAC
89     // on macOS the application palette actual colors never seem to change after launch
90     // even when DarkMode is enabled. So we use a mac style standardPalette to determine
91     // if a drak vs light mode transition has been made and then use it to set the
92     // Application palette
93     QPalette app_palette = m_Style->standardPalette();
94     bool isdark = app_palette.color(QPalette::Active,QPalette::WindowText).lightness() > 128;
95     if (m_isDark != isdark) {
96         // qDebug() << "Theme changed " << "was isDark:" << m_isDark << "now isDark:" << isdark;
97         m_isDark = isdark;
98         fixMacDarkModePalette(app_palette);
99         setPalette(app_palette);
100         emit applicationPaletteChanged();
101     }
102 #endif
103 }
104