diff --git a/pdf_viewer/config.cpp b/pdf_viewer/config.cpp index 6c08d208c..1393ff90c 100644 --- a/pdf_viewer/config.cpp +++ b/pdf_viewer/config.cpp @@ -27,6 +27,7 @@ extern bool SHOULD_DRAW_UNRENDERED_PAGES; extern bool HOVER_OVERVIEW; //extern bool AUTO_EMBED_ANNOTATIONS; extern bool DEFAULT_DARK_MODE; +extern bool USE_SYSTEM_THEME; extern float HIGHLIGHT_COLORS[26 * 3]; extern std::wstring SEARCH_URLS[26]; extern std::wstring EXECUTE_COMMANDS[26]; @@ -707,6 +708,14 @@ ConfigManager::ConfigManager(const Path& default_path, const Path& auto_path, co bool_deserializer, bool_validator }); + configs.push_back({ + L"use_system_theme", + ConfigType::Bool, + &USE_SYSTEM_THEME, + bool_serializer, + bool_deserializer, + bool_validator + }); configs.push_back({ L"render_freetext_borders", ConfigType::Bool, diff --git a/pdf_viewer/main.cpp b/pdf_viewer/main.cpp index b8fe35352..173d372c6 100644 --- a/pdf_viewer/main.cpp +++ b/pdf_viewer/main.cpp @@ -199,6 +199,7 @@ bool FLAT_TABLE_OF_CONTENTS = false; 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 SORT_BOOKMARKS_BY_LOCATION = true; std::wstring LIBGEN_ADDRESS = L""; std::wstring GOOGLE_SCHOLAR_ADDRESS = L""; @@ -1144,7 +1145,7 @@ int main(int argc, char* args[]) { }); #endif - if (DEFAULT_DARK_MODE) { + if (DEFAULT_DARK_MODE && !USE_SYSTEM_THEME) { main_widget->toggle_dark_mode(); } diff --git a/pdf_viewer/main_widget.cpp b/pdf_viewer/main_widget.cpp index 588a2a297..3cfe2fef0 100644 --- a/pdf_viewer/main_widget.cpp +++ b/pdf_viewer/main_widget.cpp @@ -35,6 +35,9 @@ #ifndef SIOYEK_QT6 #include #endif +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) +#include +#endif #include #include @@ -199,6 +202,7 @@ extern ScratchPad global_scratchpad; 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 SHOW_RIGHT_CLICK_CONTEXT_MENU; extern std::wstring CONTEXT_MENU_ITEMS; @@ -1234,6 +1238,10 @@ MainWidget::MainWidget(fz_context* mupdf_context, menu_bar->stackUnder(text_command_line_edit_container); #endif +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + set_system_mode(); +#endif + setFocus(); } @@ -4442,6 +4450,13 @@ void MainWidget::changeEvent(QEvent* event) { //main_window_height = get_current_monitor_height(); } } + +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + if (event->type() == QEvent::ThemeChange) { + set_system_mode(); + } +#endif + QWidget::changeEvent(event); } @@ -6756,17 +6771,56 @@ int MainWidget::get_current_colorscheme_index() { } void MainWidget::set_dark_mode() { - opengl_widget->set_dark_mode(true); + if (opengl_widget->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Dark) { + opengl_widget->set_dark_mode(true); + } + if (helper_opengl_widget_) { + if (helper_opengl_widget_->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Dark) { + helper_opengl_widget_->set_dark_mode(true); + } + } } void MainWidget::set_light_mode() { - opengl_widget->set_dark_mode(false); + if (opengl_widget->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Normal) { + opengl_widget->set_dark_mode(false); + } + if (helper_opengl_widget_) { + if (helper_opengl_widget_->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Normal) { + helper_opengl_widget_->set_dark_mode(false); + } + } } void MainWidget::set_custom_color_mode() { - opengl_widget->set_custom_color_mode(true); + if (opengl_widget->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Custom) { + opengl_widget->set_custom_color_mode(true); + } + if (helper_opengl_widget_) { + if (helper_opengl_widget_->get_current_color_mode() != PdfViewOpenGLWidget::ColorPalette::Custom) { + helper_opengl_widget_->set_custom_color_mode(true); + } + } } + +#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: + set_dark_mode(); + break; + } + } +} +#endif + void MainWidget::update_highlight_buttons_position() { if (selected_highlight_index != -1) { Highlight hl = main_document_view->get_highlight_with_index(selected_highlight_index); @@ -7378,6 +7432,9 @@ void MainWidget::on_configs_changed(std::vector* config_names) { changeTitlebarColor(winId(), MACOS_TITLEBAR_COLOR[0], MACOS_TITLEBAR_COLOR[1], MACOS_TITLEBAR_COLOR[2], 1.0f); } #endif + if (confname == "use_system_theme") { + set_system_mode(); + } if (confname == "tts_rate") { if (is_reading) { @@ -9770,6 +9827,11 @@ void MainWidget::initialize_helper(){ helper_opengl_widget_->show(); helper_opengl_widget_->hide(); #endif + +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + set_system_mode(); +#endif + helper_opengl_widget_->register_on_link_edit_listener([this](OpenedBookState state) { this->update_closest_link_with_opened_book_state(state); }); diff --git a/pdf_viewer/main_widget.h b/pdf_viewer/main_widget.h index b72309a5f..83b35ce1a 100644 --- a/pdf_viewer/main_widget.h +++ b/pdf_viewer/main_widget.h @@ -389,6 +389,7 @@ class MainWidget : public QMainWindow { void set_dark_mode(); void set_light_mode(); void set_custom_color_mode(); + void set_system_mode(); void toggle_statusbar(); void toggle_titlebar();