Skip to content

Commit

Permalink
Add a config option to use custom color as dark system theme
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrm committed Mar 26, 2024
1 parent d091b0a commit 42c8ac9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
9 changes: 9 additions & 0 deletions pdf_viewer/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern bool HOVER_OVERVIEW;
//extern bool AUTO_EMBED_ANNOTATIONS;
extern bool DEFAULT_DARK_MODE;
extern bool USE_SYSTEM_THEME;
extern bool USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME;
extern float HIGHLIGHT_COLORS[26 * 3];
extern std::wstring SEARCH_URLS[26];
extern std::wstring EXECUTE_COMMANDS[26];
Expand Down Expand Up @@ -716,6 +717,14 @@ ConfigManager::ConfigManager(const Path& default_path, const Path& auto_path, co
bool_deserializer,
bool_validator
});
configs.push_back({
L"use_custom_color_as_dark_system_theme",
ConfigType::Bool,
&USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME,
bool_serializer,
bool_deserializer,
bool_validator
});
configs.push_back({
L"render_freetext_borders",
ConfigType::Bool,
Expand Down
1 change: 1 addition & 0 deletions pdf_viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ bool SHOULD_USE_MULTIPLE_MONITORS = false;
bool SHOULD_CHECK_FOR_LATEST_VERSION_ON_STARTUP = false;
bool DEFAULT_DARK_MODE = false;
bool USE_SYSTEM_THEME = false;
bool USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME = false;
bool SORT_BOOKMARKS_BY_LOCATION = true;
std::wstring LIBGEN_ADDRESS = L"";
std::wstring GOOGLE_SCHOLAR_ADDRESS = L"";
Expand Down
37 changes: 18 additions & 19 deletions pdf_viewer/main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
#ifndef SIOYEK_QT6
#include <qdesktopwidget.h>
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
#include <QStyleHints>
#endif

#include <qkeyevent.h>
#include <qlabel.h>
Expand Down Expand Up @@ -68,6 +65,7 @@
#include <qqmlengine.h>
#include <qtextdocumentfragment.h>
#include <qmenubar.h>
#include <qstylehints.h>

#include <mupdf/fitz.h>

Expand Down Expand Up @@ -203,6 +201,7 @@ extern int NUM_CACHED_PAGES;
extern bool IGNORE_SCROLL_EVENTS;
extern bool DONT_FOCUS_IF_SYNCTEX_RECT_IS_VISIBLE;
extern bool USE_SYSTEM_THEME;
extern bool USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME;

extern bool SHOW_RIGHT_CLICK_CONTEXT_MENU;
extern std::wstring CONTEXT_MENU_ITEMS;
Expand Down Expand Up @@ -1239,7 +1238,7 @@ MainWidget::MainWidget(fz_context* mupdf_context,
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
set_system_mode();
set_color_mode_to_system_theme();
#endif

setFocus();
Expand Down Expand Up @@ -4453,7 +4452,7 @@ void MainWidget::changeEvent(QEvent* event) {

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (event->type() == QEvent::ThemeChange) {
set_system_mode();
set_color_mode_to_system_theme();
}
#endif

Expand Down Expand Up @@ -6804,22 +6803,24 @@ void MainWidget::set_custom_color_mode() {
}


void MainWidget::set_color_mode_to_system_theme() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
void MainWidget::set_system_mode() {
if (USE_SYSTEM_THEME) {
QStyleHints *styleHints = QGuiApplication::styleHints();
switch (styleHints->colorScheme()) {
case Qt::ColorScheme::Unknown:
case Qt::ColorScheme::Light:
set_light_mode();
break;
case Qt::ColorScheme::Dark:
QStyleHints *style_hints = QGuiApplication::styleHints();
if (style_hints->colorScheme() == Qt::ColorScheme::Light){
set_light_mode();
}
if (style_hints->colorScheme() == Qt::ColorScheme::Dark){
if (USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME){
set_custom_color_mode();
}
else{
set_dark_mode();
break;
}
}
}
}
#endif
}

void MainWidget::update_highlight_buttons_position() {
if (selected_highlight_index != -1) {
Expand Down Expand Up @@ -7433,7 +7434,7 @@ void MainWidget::on_configs_changed(std::vector<std::string>* config_names) {
}
#endif
if (confname == "use_system_theme") {
set_system_mode();
set_color_mode_to_system_theme();
}

if (confname == "tts_rate") {
Expand Down Expand Up @@ -9828,9 +9829,7 @@ void MainWidget::initialize_helper(){
helper_opengl_widget_->hide();
#endif

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
set_system_mode();
#endif
set_color_mode_to_system_theme();

helper_opengl_widget_->register_on_link_edit_listener([this](OpenedBookState state) {
this->update_closest_link_with_opened_book_state(state);
Expand Down
2 changes: 1 addition & 1 deletion pdf_viewer/main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class MainWidget : public QMainWindow {
void set_dark_mode();
void set_light_mode();
void set_custom_color_mode();
void set_system_mode();
void set_color_mode_to_system_theme();
void toggle_statusbar();
void toggle_titlebar();

Expand Down

0 comments on commit 42c8ac9

Please sign in to comment.