From 597e4f1c9bae740de81f9f18dbe31f5b45ea97d8 Mon Sep 17 00:00:00 2001 From: Isak Samsten Date: Sun, 24 Mar 2024 22:42:57 +0100 Subject: [PATCH 1/2] Improve hide MacOS titlebar - remove unused code - correctly indent the code with 4 spaces. --- pdf_viewer/macos_specific.mm | 70 ++++++++++++++---------------------- pdf_viewer/main_widget.cpp | 8 ++--- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/pdf_viewer/macos_specific.mm b/pdf_viewer/macos_specific.mm index f5ddc268e..90eadc721 100644 --- a/pdf_viewer/macos_specific.mm +++ b/pdf_viewer/macos_specific.mm @@ -17,66 +17,48 @@ @implementation DraggableTitleView // Handle mouse click events - (void)mouseDown:(NSEvent *)event { - // double-click to zoom - if ([event clickCount] == 2) { - [self.window zoom:nil]; - } else { - // drag Window - [self.window performWindowDragWithEvent:event]; - } + // double-click to zoom + if ([event clickCount] == 2) { + [self.window zoom:nil]; + } else { + // drag Window + [self.window performWindowDragWithEvent:event]; + } } - (void)updateTrackingAreas { - [self initTrackingArea]; + [self initTrackingArea]; } -(void) initTrackingArea { - NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect | - NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved); + NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect | + NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved); - NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds] - options:options - owner:self - userInfo:nil]; + NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self bounds] + options:options + owner:self + userInfo:nil]; - [self addTrackingArea:area]; + [self addTrackingArea:area]; } -(void)mouseEntered:(NSEvent *)event { - if((self.window.styleMask & NSWindowStyleMaskFullScreen) == 0) { - [[self.window standardWindowButton: NSWindowCloseButton] setHidden:NO]; - [[self.window standardWindowButton: NSWindowMiniaturizeButton] setHidden:NO]; - [[self.window standardWindowButton: NSWindowZoomButton] setHidden:NO]; - } + if((self.window.styleMask & NSWindowStyleMaskFullScreen) == 0) { + [[self.window standardWindowButton: NSWindowCloseButton] setHidden:NO]; + [[self.window standardWindowButton: NSWindowMiniaturizeButton] setHidden:NO]; + [[self.window standardWindowButton: NSWindowZoomButton] setHidden:NO]; + } } -(void)mouseExited:(NSEvent *)event { - if((self.window.styleMask & NSWindowStyleMaskFullScreen) == 0) { - [[self.window standardWindowButton: NSWindowCloseButton] setHidden:YES]; - [[self.window standardWindowButton: NSWindowMiniaturizeButton] setHidden:YES]; - [[self.window standardWindowButton: NSWindowZoomButton] setHidden:YES]; - } + if((self.window.styleMask & NSWindowStyleMaskFullScreen) == 0) { + [[self.window standardWindowButton: NSWindowCloseButton] setHidden:YES]; + [[self.window standardWindowButton: NSWindowMiniaturizeButton] setHidden:YES]; + [[self.window standardWindowButton: NSWindowZoomButton] setHidden:YES]; + } } @end -extern "C" void showWindowTitleBarButtons(WId winId) { - if (winId == 0) return; - NSView* nativeView = reinterpret_cast(winId); - NSWindow* nativeWindow = [nativeView window]; - [[nativeWindow standardWindowButton: NSWindowCloseButton] setHidden:NO]; - [[nativeWindow standardWindowButton: NSWindowMiniaturizeButton] setHidden:NO]; - [[nativeWindow standardWindowButton: NSWindowZoomButton] setHidden:NO]; -} - -extern "C" void hideWindowTitleBarButtons(WId winId) { - if (winId == 0) return; - NSView* nativeView = reinterpret_cast(winId); - NSWindow* nativeWindow = [nativeView window]; - [[nativeWindow standardWindowButton: NSWindowCloseButton] setHidden:YES]; - [[nativeWindow standardWindowButton: NSWindowMiniaturizeButton] setHidden:YES]; - [[nativeWindow standardWindowButton: NSWindowZoomButton] setHidden:YES]; -} - extern "C" void hideWindowTitleBar(WId winId) { if (winId == 0) return; @@ -84,7 +66,7 @@ -(void)mouseExited:(NSEvent *)event { NSWindow* nativeWindow = [nativeView window]; if(nativeWindow.titleVisibility == NSWindowTitleHidden){ - return; + return; } [[nativeWindow standardWindowButton: NSWindowCloseButton] setHidden:YES]; diff --git a/pdf_viewer/main_widget.cpp b/pdf_viewer/main_widget.cpp index ec06e982d..588a2a297 100644 --- a/pdf_viewer/main_widget.cpp +++ b/pdf_viewer/main_widget.cpp @@ -98,8 +98,6 @@ extern "C" { #ifdef Q_OS_MACOS extern "C" void changeTitlebarColor(WId, double, double, double, double); extern "C" void hideWindowTitleBar(WId); -extern "C" void showWindowTitleBarButtons(WId); -extern "C" void hideWindowTitleBarButtons(WId); #endif extern int next_window_id; @@ -1229,7 +1227,7 @@ MainWidget::MainWidget(fz_context* mupdf_context, } if (MACOS_HIDE_TITLEBAR) { - hideWindowTitleBar(winId()); + hideWindowTitleBar(winId()); } menu_bar = create_main_menu_bar(); setMenuBar(menu_bar); @@ -4075,7 +4073,7 @@ void MainWidget::apply_window_params_for_two_window_mode() { #ifdef Q_OS_MACOS if (MACOS_HIDE_TITLEBAR) { - hideWindowTitleBar(helper_window->winId()); + hideWindowTitleBar(helper_window->winId()); } #endif //int main_window_width = QApplication::desktop()->screenGeometry(0).width(); @@ -9767,7 +9765,7 @@ void MainWidget::initialize_helper(){ #ifdef Q_OS_MACOS QWidget* helper_window = get_top_level_widget(helper_opengl_widget_); if (MACOS_HIDE_TITLEBAR) { - hideWindowTitleBar(helper_window->winId()); + hideWindowTitleBar(helper_window->winId()); } helper_opengl_widget_->show(); helper_opengl_widget_->hide(); From 9a9b9613abcefc82fd90e75f15c059d3919dc722 Mon Sep 17 00:00:00 2001 From: Isak Samsten Date: Sun, 24 Mar 2024 22:55:33 +0100 Subject: [PATCH 2/2] Use the system theme (dark/light mode) Automatically toggle between dark and light mode depending on the desired mode from the operating system. Introduces a new config option `use_system_theme` which if set automatically set light or dark mode when starting depending on if the current operating system theme is in light or dark mode. Then, if the operating system mode changes, the change is reflected in Sioyek. --- pdf_viewer/config.cpp | 9 +++++ pdf_viewer/main.cpp | 3 +- pdf_viewer/main_widget.cpp | 68 ++++++++++++++++++++++++++++++++++++-- pdf_viewer/main_widget.h | 1 + 4 files changed, 77 insertions(+), 4 deletions(-) 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();