From 0fd99dfb6d9f21d0944bbdb03e21baaa3c141726 Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Mon, 28 Feb 2022 02:49:29 +0100 Subject: [PATCH 1/2] First setup (still some resize bugs) --- src/draw.cc | 8 +- src/mainwindow.cc | 135 ++++++++++++++++---- src/mainwindow.h | 26 +++- src/schema/org.libreweb.browser.gschema.xml | 8 ++ 4 files changed, 142 insertions(+), 35 deletions(-) diff --git a/src/draw.cc b/src/draw.cc index b03719f..885012d 100644 --- a/src/draw.cc +++ b/src/draw.cc @@ -33,8 +33,8 @@ Draw::Draw(MiddlewareInterface& middleware) { this->disableEdit(); set_top_margin(12); - set_left_margin(20); // fallback - set_right_margin(20); // fallback + set_left_margin(10); // fallback + set_right_margin(10); // fallback set_bottom_margin(0); set_indent(0); // fallback set_monospace(false); @@ -343,6 +343,10 @@ void Draw::newDocument() this->redoPool.clear(); this->clear(); + // Set margins to defaults in editor mode + set_left_margin(10); + set_right_margin(10); + enableEdit(); grab_focus(); // Claim focus on text view } diff --git a/src/mainwindow.cc b/src/mainwindow.cc index ac1bc97..6826ea1 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -31,6 +31,7 @@ MainWindow::MainWindow(const std::string& timeout) : m_accelGroup(Gtk::AccelGroup::create()), m_settings(), m_brightnessAdjustment(Gtk::Adjustment::create(1.0, 0.0, 1.0, 0.05, 0.1)), + m_maxContentWidthAdjustment(Gtk::Adjustment::create(700, 0, 99999, 10, 20)), m_spacingAdjustment(Gtk::Adjustment::create(0, -10, 10, 1, 2)), m_marginsAdjustment(Gtk::Adjustment::create(20, 0, 1000, 10, 20)), m_indentAdjustment(Gtk::Adjustment::create(0, 0, 1000, 5, 10)), @@ -89,11 +90,13 @@ MainWindow::MainWindow(const std::string& timeout) m_networkOutcomingLabel("Outcoming"), m_networkKiloBytesLabel("Kilobytes/s"), m_fontLabel("Font"), + m_maxContentWidthLabel("Content width"), m_spacingLabel("Spacing"), m_marginsLabel("Margins"), m_indentLabel("Indent"), m_textWrappingLabel("Wrapping"), m_themeLabel("Dark Theme"), + m_readerViewLabel("Reader View"), m_iconThemeLabel("Active Theme"), // Private members middleware_(*this, timeout), @@ -104,9 +107,12 @@ MainWindow::MainWindow(const std::string& timeout) fontFamily_("Sans"), defaultFontSize_(10), currentFontSize_(10), + contentMargin_(20), + contentMaxWidth_(700), fontSpacing_(0), brightnessScale_(1.0), useDarkTheme_(false), + isReaderViewEnabled_(true), currentHistoryIndex_(0) { set_title(appName_); @@ -367,14 +373,14 @@ void MainWindow::loadStoredSettings() currentFontSize_ = defaultFontSize_ = m_settings->get_int("font-size"); m_fontButton.set_font_name(fontFamily_ + " " + std::to_string(currentFontSize_)); + contentMaxWidth_ = m_settings->get_int("max-content-width"); fontSpacing_ = m_settings->get_int("spacing"); - int margins = m_settings->get_int("margins"); + contentMargin_ = m_settings->get_int("margins"); int indent = m_settings->get_int("indent"); + m_maxContentWidthAdjustment->set_value(contentMaxWidth_); m_spacingAdjustment->set_value(fontSpacing_); - m_marginsAdjustment->set_value(margins); + m_marginsAdjustment->set_value(contentMargin_); m_indentAdjustment->set_value(indent); - m_draw_primary.set_left_margin(margins); - m_draw_primary.set_right_margin(margins); m_draw_primary.set_indent(indent); int tocDividerPosition = m_settings->get_int("position-divider-toc"); m_panedRoot.set_position(tocDividerPosition); @@ -382,15 +388,16 @@ void MainWindow::loadStoredSettings() useCurrentGTKIconTheme_ = m_settings->get_boolean("icon-gtk-theme"); brightnessScale_ = m_settings->get_double("brightness"); useDarkTheme_ = m_settings->get_boolean("dark-theme"); + isReaderViewEnabled_ = m_settings->get_boolean("reader-view"); } else { std::cerr << "ERROR: Gsettings schema file could not be found!" << std::endl; // Fallback adjustment controls - int margins = m_draw_primary.get_margin_left(); int indent = m_draw_primary.get_indent(); - m_spacingAdjustment->set_value(0); - m_marginsAdjustment->set_value(margins); + m_maxContentWidthAdjustment->set_value(contentMaxWidth_); + m_spacingAdjustment->set_value(fontSpacing_); + m_marginsAdjustment->set_value(contentMaxWidth_); m_indentAdjustment->set_value(indent); // Fallback ToC paned divider m_panedRoot.set_position(300); @@ -793,11 +800,10 @@ void MainWindow::initSettingsPopover() m_scaleSettingsBrightness.signal_value_changed().connect(sigc::mem_fun(this, &MainWindow::on_brightness_changed)); m_hboxSetingsBrightness.pack_start(m_brightnessImage, false, false); m_hboxSetingsBrightness.pack_end(m_scaleSettingsBrightness); - // Dark theme switch - m_themeSwitch.set_active(useDarkTheme_); // Override with current dark theme preference - // Settings buttons + // Settings labels / buttons m_wrapWordChar.set_active(true); // Default wrapping mode m_fontLabel.set_tooltip_text("Font familiy"); + m_maxContentWidthLabel.set_tooltip_text("Max content width"); m_spacingLabel.set_tooltip_text("Text spacing"); m_marginsLabel.set_tooltip_text("Text margins"); m_indentLabel.set_tooltip_text("Text indentation"); @@ -806,21 +812,31 @@ void MainWindow::initSettingsPopover() m_wrapChar.set_tooltip_text("Character wrapping"); m_wrapWord.set_tooltip_text("Word wrapping"); m_wrapWordChar.set_tooltip_text("Word wrapping (+ character)"); + m_maxContentWidthSpinButton.set_adjustment(m_maxContentWidthAdjustment); m_spacingSpinButton.set_adjustment(m_spacingAdjustment); m_marginsSpinButton.set_adjustment(m_marginsAdjustment); m_indentSpinButton.set_adjustment(m_indentAdjustment); m_fontLabel.set_xalign(1); + m_maxContentWidthLabel.set_xalign(1); m_spacingLabel.set_xalign(1); m_marginsLabel.set_xalign(1); m_indentLabel.set_xalign(1); m_textWrappingLabel.set_xalign(1); m_themeLabel.set_xalign(1); + m_readerViewLabel.set_xalign(1); m_fontLabel.get_style_context()->add_class("dim-label"); + m_maxContentWidthLabel.get_style_context()->add_class("dim-label"); m_spacingLabel.get_style_context()->add_class("dim-label"); m_marginsLabel.get_style_context()->add_class("dim-label"); m_indentLabel.get_style_context()->add_class("dim-label"); m_textWrappingLabel.get_style_context()->add_class("dim-label"); m_themeLabel.get_style_context()->add_class("dim-label"); + m_readerViewLabel.get_style_context()->add_class("dim-label"); + // Dark theme switch + m_themeSwitch.set_active(useDarkTheme_); // Override with current dark theme preference + // Reader view switch + m_readerViewSwitch.set_active(isReaderViewEnabled_); + // Settings grid m_settingsGrid.set_margin_start(6); m_settingsGrid.set_margin_top(6); m_settingsGrid.set_margin_bottom(6); @@ -828,19 +844,23 @@ void MainWindow::initSettingsPopover() m_settingsGrid.set_column_spacing(10); m_settingsGrid.attach(m_fontLabel, 0, 0, 1); m_settingsGrid.attach(m_fontButton, 1, 0, 2); - m_settingsGrid.attach(m_spacingLabel, 0, 1, 1); - m_settingsGrid.attach(m_spacingSpinButton, 1, 1, 2); - m_settingsGrid.attach(m_marginsLabel, 0, 2, 1); - m_settingsGrid.attach(m_marginsSpinButton, 1, 2, 2); - m_settingsGrid.attach(m_indentLabel, 0, 3, 1); - m_settingsGrid.attach(m_indentSpinButton, 1, 3, 2); - m_settingsGrid.attach(m_textWrappingLabel, 0, 4, 1); - m_settingsGrid.attach(m_wrapNone, 1, 4, 1); - m_settingsGrid.attach(m_wrapChar, 2, 4, 1); - m_settingsGrid.attach(m_wrapWord, 1, 5, 1); - m_settingsGrid.attach(m_wrapWordChar, 2, 5, 1); - m_settingsGrid.attach(m_themeLabel, 0, 6, 1); - m_settingsGrid.attach(m_themeSwitch, 1, 6, 2); + m_settingsGrid.attach(m_maxContentWidthLabel, 0, 1, 1); + m_settingsGrid.attach(m_maxContentWidthSpinButton, 1, 1, 2); + m_settingsGrid.attach(m_spacingLabel, 0, 2, 1); + m_settingsGrid.attach(m_spacingSpinButton, 1, 2, 2); + m_settingsGrid.attach(m_marginsLabel, 0, 3, 1); + m_settingsGrid.attach(m_marginsSpinButton, 1, 3, 2); + m_settingsGrid.attach(m_indentLabel, 0, 4, 1); + m_settingsGrid.attach(m_indentSpinButton, 1, 4, 2); + m_settingsGrid.attach(m_textWrappingLabel, 0, 5, 1); + m_settingsGrid.attach(m_wrapNone, 1, 5, 1); + m_settingsGrid.attach(m_wrapChar, 2, 5, 1); + m_settingsGrid.attach(m_wrapWord, 1, 6, 1); + m_settingsGrid.attach(m_wrapWordChar, 2, 6, 1); + m_settingsGrid.attach(m_themeLabel, 0, 7, 1); + m_settingsGrid.attach(m_themeSwitch, 1, 7, 2); + m_settingsGrid.attach(m_readerViewLabel, 0, 8, 1); + m_settingsGrid.attach(m_readerViewSwitch, 1, 8, 2); // Icon theme (+ submenu) m_iconThemeButton.set_label("Icon Theme"); m_iconThemeButton.property_menu_name() = "icon-theme"; @@ -908,6 +928,10 @@ void MainWindow::initSignals() { // Window signals signal_delete_event().connect(sigc::mem_fun(this, &MainWindow::delete_window)); + // TODO: Trigger all GDK resize events on m_draw_primary, otherwise it doesn't trigger always + m_draw_primary.signal_configure_event().connect(sigc::mem_fun(this, &MainWindow::on_configure_event)); + + // m_panedDraw.signal_configure_event().. // Table of contents m_closeTocWindowButton.signal_clicked().connect(sigc::mem_fun(m_vboxToc, &Gtk::Widget::hide)); tocTreeView.signal_row_activated().connect(sigc::mem_fun(this, &MainWindow::on_toc_row_activated)); @@ -978,6 +1002,7 @@ void MainWindow::initSignals() m_zoomRestoreButton.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::on_zoom_restore)); m_zoomInButton.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::on_zoom_in)); m_fontButton.signal_font_set().connect(sigc::mem_fun(this, &MainWindow::on_font_set)); + m_maxContentWidthSpinButton.signal_value_changed().connect(sigc::mem_fun(this, &MainWindow::on_max_content_width_changed)); m_spacingSpinButton.signal_value_changed().connect(sigc::mem_fun(this, &MainWindow::on_spacing_changed)); m_marginsSpinButton.signal_value_changed().connect(sigc::mem_fun(this, &MainWindow::on_margins_changed)); m_indentSpinButton.signal_value_changed().connect(sigc::mem_fun(this, &MainWindow::on_indent_changed)); @@ -986,6 +1011,7 @@ void MainWindow::initSignals() m_wrapWord.signal_toggled().connect(sigc::bind(sigc::mem_fun(this, &MainWindow::on_wrap_toggled), Gtk::WrapMode::WRAP_WORD)); m_wrapWordChar.signal_toggled().connect(sigc::bind(sigc::mem_fun(this, &MainWindow::on_wrap_toggled), Gtk::WrapMode::WRAP_WORD_CHAR)); m_themeSwitch.property_active().signal_changed().connect(sigc::mem_fun(this, &MainWindow::on_theme_changed)); + m_readerViewSwitch.property_active().signal_changed().connect(sigc::mem_fun(this, &MainWindow::on_reader_view_changed)); m_iconThemeListBox.signal_row_activated().connect(sigc::mem_fun(this, &MainWindow::on_icon_theme_activated)); m_aboutButton.signal_clicked().connect(sigc::mem_fun(m_about, &About::show_about)); } @@ -1031,6 +1057,8 @@ bool MainWindow::delete_window(GdkEventAny* any_event __attribute__((unused))) // m_settings->set_boolean("fullscreen", is_fullscreen()); m_settings->set_string("font-family", fontFamily_); m_settings->set_int("font-size", currentFontSize_); + m_settings->set_boolean("reader-view", isReaderViewEnabled_); + m_settings->set_int("max-content-width", m_maxContentWidthSpinButton.get_value_as_int()); m_settings->set_int("spacing", m_spacingSpinButton.get_value_as_int()); m_settings->set_int("margins", m_marginsSpinButton.get_value_as_int()); m_settings->set_int("indent", m_indentSpinButton.get_value_as_int()); @@ -1192,6 +1220,16 @@ void MainWindow::selectAll() } } +/** + * \brief Triggered on windows resize + */ +bool MainWindow::on_configure_event(__attribute__((unused)) GdkEventConfigure* configure_event) +{ + if (!isEditorEnabled()) + updateMargins(); + return false; +} + /** * \brief Triggered when user clicked on the column in ToC */ @@ -2019,6 +2057,36 @@ std::string MainWindow::getIconImageFromTheme(const std::string& iconName, const } } +/** + * \brief Calculate & update drawing margins + */ +void MainWindow::updateMargins() +{ + if (isReaderViewEnabled_) + { + + int width = m_draw_primary.get_width(); + std::cout << "Width: " << width << std::endl; + if (width > (contentMaxWidth_ + (2 * contentMargin_))) + { + // Calculate margins on the fly + int margin = (width - contentMaxWidth_) / 2; + m_draw_primary.set_left_margin(margin); + m_draw_primary.set_right_margin(margin); + } + else + { + m_draw_primary.set_left_margin(contentMargin_); + m_draw_primary.set_right_margin(contentMargin_); + } + } + else + { + m_draw_primary.set_left_margin(contentMargin_); + m_draw_primary.set_right_margin(contentMargin_); + } +} + /** * \brief Update the CSS provider data */ @@ -2164,16 +2232,24 @@ void MainWindow::on_font_set() updateCSS(); } +void MainWindow::on_max_content_width_changed() +{ + contentMaxWidth_ = m_maxContentWidthSpinButton.get_value_as_int(); + if (!isEditorEnabled()) + updateMargins(); +} + void MainWindow::on_spacing_changed() { - fontSpacing_ = m_spacingSpinButton.get_value_as_int(); // Letter spacing + fontSpacing_ = m_spacingSpinButton.get_value_as_int(); // Letter-spacing updateCSS(); } void MainWindow::on_margins_changed() { - m_draw_primary.set_left_margin(m_marginsSpinButton.get_value_as_int()); - m_draw_primary.set_right_margin(m_marginsSpinButton.get_value_as_int()); + contentMargin_ = m_marginsSpinButton.get_value_as_int(); + if (!isEditorEnabled()) + updateMargins(); } void MainWindow::on_indent_changed() @@ -2199,6 +2275,13 @@ void MainWindow::on_theme_changed() setTheme(); } +void MainWindow::on_reader_view_changed() +{ + isReaderViewEnabled_ = m_readerViewSwitch.get_active(); + if (!isEditorEnabled()) + updateMargins(); +} + void MainWindow::on_icon_theme_activated(Gtk::ListBoxRow* row) { std::string themeName = static_cast(row->get_data("value")); diff --git a/src/mainwindow.h b/src/mainwindow.h index b198729..2bf0969 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -75,6 +75,7 @@ protected: void paste(); void del(); void selectAll(); + bool on_configure_event(GdkEventConfigure* configure_event); void on_toc_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void new_doc(); void open(); @@ -107,21 +108,24 @@ protected: void on_zoom_restore(); void on_zoom_in(); void on_font_set(); + void on_max_content_width_changed(); void on_spacing_changed(); void on_margins_changed(); void on_indent_changed(); void on_wrap_toggled(Gtk::WrapMode mode); void on_brightness_changed(); void on_theme_changed(); + void on_reader_view_changed(); void on_icon_theme_activated(Gtk::ListBoxRow* row); - Glib::RefPtr m_accelGroup; /*!< Accelerator group, used for keyboard shortcut bindings */ - Glib::RefPtr m_settings; /*!< Settings to store our preferences, even during restarts */ - Glib::RefPtr m_brightnessAdjustment; /*!< Bridghtness adjustment settings */ - Glib::RefPtr m_spacingAdjustment; /*!< Spacing adjustment settings */ - Glib::RefPtr m_marginsAdjustment; /*!< Margins adjustment settings */ - Glib::RefPtr m_indentAdjustment; /*!< Indent adjustment settings */ - Glib::RefPtr m_drawCSSProvider; /*!< CSS Provider for draw textviews */ + Glib::RefPtr m_accelGroup; /*!< Accelerator group, used for keyboard shortcut bindings */ + Glib::RefPtr m_settings; /*!< Settings to store our preferences, even during restarts */ + Glib::RefPtr m_brightnessAdjustment; /*!< Bridghtness adjustment settings */ + Glib::RefPtr m_maxContentWidthAdjustment; /*!< max content width adjustment settings */ + Glib::RefPtr m_spacingAdjustment; /*!< Spacing adjustment settings */ + Glib::RefPtr m_marginsAdjustment; /*!< Margins adjustment settings */ + Glib::RefPtr m_indentAdjustment; /*!< Indent adjustment settings */ + Glib::RefPtr m_drawCSSProvider; /*!< CSS Provider for draw textviews */ // Child widgets Menu m_menu; @@ -159,6 +163,7 @@ protected: Gtk::Button m_zoomRestoreButton; Gtk::Button m_zoomInButton; Gtk::FontButton m_fontButton; + Gtk::SpinButton m_maxContentWidthSpinButton; Gtk::SpinButton m_spacingSpinButton; Gtk::SpinButton m_marginsSpinButton; Gtk::SpinButton m_indentSpinButton; @@ -243,6 +248,7 @@ protected: Gtk::PopoverMenu m_settingsPopover; Gtk::ModelButton m_copyIDButton; Gtk::ModelButton m_copyPublicKeyButton; + Gtk::Switch m_readerViewSwitch; Gtk::Switch m_themeSwitch; Gtk::Label m_tableOfContentsLabel; Gtk::Label m_networkHeadingLabel; @@ -263,11 +269,13 @@ protected: Gtk::Label m_networkOutcomingStatusLabel; Gtk::Label m_networkKiloBytesLabel; Gtk::Label m_fontLabel; + Gtk::Label m_maxContentWidthLabel; Gtk::Label m_spacingLabel; Gtk::Label m_marginsLabel; Gtk::Label m_indentLabel; Gtk::Label m_textWrappingLabel; Gtk::Label m_themeLabel; + Gtk::Label m_readerViewLabel; Gtk::Label m_iconThemeLabel; std::unique_ptr m_contentPublishedDialog; Gtk::ScrolledWindow m_scrolledToc; @@ -297,9 +305,12 @@ private: std::string fontFamily_; int defaultFontSize_; int currentFontSize_; + int contentMargin_; + int contentMaxWidth_; int fontSpacing_; double brightnessScale_; bool useDarkTheme_; + bool isReaderViewEnabled_; std::string currentFileSavedPath_; std::size_t currentHistoryIndex_; std::vector history_; @@ -322,6 +333,7 @@ private: void disableEdit(); bool isEditorEnabled(); std::string getIconImageFromTheme(const std::string& iconName, const std::string& typeofIcon); + void updateMargins(); void updateCSS(); void showNotification(const Glib::ustring& title, const Glib::ustring& message = ""); }; diff --git a/src/schema/org.libreweb.browser.gschema.xml b/src/schema/org.libreweb.browser.gschema.xml index 8ca3b1b..a015a3a 100644 --- a/src/schema/org.libreweb.browser.gschema.xml +++ b/src/schema/org.libreweb.browser.gschema.xml @@ -33,6 +33,10 @@ 10 Font size + + 700 + Max document content width + 0 Font spacing @@ -61,5 +65,9 @@ false Prefer dark theme + + true + Is reader view enabled + From a59140dd84af8a3f43343473a2d84ed632493b6e Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Mon, 28 Feb 2022 23:54:31 +0100 Subject: [PATCH 2/2] Working auto-margins (reader-view). Plus fixes in editor mode. --- src/draw.cc | 5 +- src/mainwindow.cc | 160 +++++++++++++------- src/mainwindow.h | 5 +- src/schema/org.libreweb.browser.gschema.xml | 12 +- 4 files changed, 122 insertions(+), 60 deletions(-) diff --git a/src/draw.cc b/src/draw.cc index 885012d..aa099e8 100644 --- a/src/draw.cc +++ b/src/draw.cc @@ -346,6 +346,10 @@ void Draw::newDocument() // Set margins to defaults in editor mode set_left_margin(10); set_right_margin(10); + // Set indent to zero + set_indent(0); + // Reset word wrapping to default + set_wrap_mode(Gtk::WRAP_WORD_CHAR); enableEdit(); grab_focus(); // Claim focus on text view @@ -742,7 +746,6 @@ void Draw::insert_image() buffer->end_user_action(); } -// TODO: set_monospace(true) void Draw::make_code() { Gtk::TextBuffer::iterator start, end; diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 6826ea1..0bc74a5 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -33,7 +33,7 @@ MainWindow::MainWindow(const std::string& timeout) m_brightnessAdjustment(Gtk::Adjustment::create(1.0, 0.0, 1.0, 0.05, 0.1)), m_maxContentWidthAdjustment(Gtk::Adjustment::create(700, 0, 99999, 10, 20)), m_spacingAdjustment(Gtk::Adjustment::create(0, -10, 10, 1, 2)), - m_marginsAdjustment(Gtk::Adjustment::create(20, 0, 1000, 10, 20)), + m_marginsAdjustment(Gtk::Adjustment::create(10, 0, 1000, 10, 20)), m_indentAdjustment(Gtk::Adjustment::create(0, 0, 1000, 5, 10)), m_drawCSSProvider(Gtk::CssProvider::create()), m_menu(m_accelGroup), @@ -107,9 +107,12 @@ MainWindow::MainWindow(const std::string& timeout) fontFamily_("Sans"), defaultFontSize_(10), currentFontSize_(10), + positionDividerDraw_(-1), contentMargin_(20), contentMaxWidth_(700), fontSpacing_(0), + indent_(0), + wrapMode_(Gtk::WRAP_WORD_CHAR), brightnessScale_(1.0), useDarkTheme_(false), isReaderViewEnabled_(true), @@ -368,7 +371,8 @@ void MainWindow::loadStoredSettings() set_default_size(m_settings->get_int("width"), m_settings->get_int("height")); if (m_settings->get_boolean("maximized")) maximize(); - + positionDividerDraw_ = m_settings->get_int("position-divider-draw"); + m_panedDraw.set_position(positionDividerDraw_); fontFamily_ = m_settings->get_string("font-family"); currentFontSize_ = defaultFontSize_ = m_settings->get_int("font-size"); m_fontButton.set_font_name(fontFamily_ + " " + std::to_string(currentFontSize_)); @@ -376,12 +380,13 @@ void MainWindow::loadStoredSettings() contentMaxWidth_ = m_settings->get_int("max-content-width"); fontSpacing_ = m_settings->get_int("spacing"); contentMargin_ = m_settings->get_int("margins"); - int indent = m_settings->get_int("indent"); + indent_ = m_settings->get_int("indent"); + wrapMode_ = static_cast(m_settings->get_enum("wrap-mode")); m_maxContentWidthAdjustment->set_value(contentMaxWidth_); m_spacingAdjustment->set_value(fontSpacing_); m_marginsAdjustment->set_value(contentMargin_); - m_indentAdjustment->set_value(indent); - m_draw_primary.set_indent(indent); + m_indentAdjustment->set_value(indent_); + m_draw_primary.set_indent(indent_); int tocDividerPosition = m_settings->get_int("position-divider-toc"); m_panedRoot.set_position(tocDividerPosition); iconTheme_ = m_settings->get_string("icon-theme"); @@ -389,19 +394,42 @@ void MainWindow::loadStoredSettings() brightnessScale_ = m_settings->get_double("brightness"); useDarkTheme_ = m_settings->get_boolean("dark-theme"); isReaderViewEnabled_ = m_settings->get_boolean("reader-view"); + switch (wrapMode_) + { + case Gtk::WRAP_NONE: + m_wrapNone.set_active(true); + break; + case Gtk::WRAP_CHAR: + m_wrapChar.set_active(true); + break; + case Gtk::WRAP_WORD: + m_wrapWord.set_active(true); + break; + case Gtk::WRAP_WORD_CHAR: + m_wrapWordChar.set_active(true); + break; + default: + m_wrapWordChar.set_active(true); + } } else { std::cerr << "ERROR: Gsettings schema file could not be found!" << std::endl; + // Select default fallback wrap mode + m_wrapWordChar.set_active(true); // Fallback adjustment controls - int indent = m_draw_primary.get_indent(); m_maxContentWidthAdjustment->set_value(contentMaxWidth_); m_spacingAdjustment->set_value(fontSpacing_); m_marginsAdjustment->set_value(contentMaxWidth_); - m_indentAdjustment->set_value(indent); + m_indentAdjustment->set_value(indent_); // Fallback ToC paned divider m_panedRoot.set_position(300); } + // Apply settings that needs to be applied now + // Note: margins are getting automatically applied (on resize), + // and some other attributes are part of CSS. + m_draw_primary.set_indent(indent_); + m_draw_primary.set_wrap_mode(wrapMode_); } /** @@ -801,7 +829,6 @@ void MainWindow::initSettingsPopover() m_hboxSetingsBrightness.pack_start(m_brightnessImage, false, false); m_hboxSetingsBrightness.pack_end(m_scaleSettingsBrightness); // Settings labels / buttons - m_wrapWordChar.set_active(true); // Default wrapping mode m_fontLabel.set_tooltip_text("Font familiy"); m_maxContentWidthLabel.set_tooltip_text("Max content width"); m_spacingLabel.set_tooltip_text("Text spacing"); @@ -928,10 +955,8 @@ void MainWindow::initSignals() { // Window signals signal_delete_event().connect(sigc::mem_fun(this, &MainWindow::delete_window)); - // TODO: Trigger all GDK resize events on m_draw_primary, otherwise it doesn't trigger always - m_draw_primary.signal_configure_event().connect(sigc::mem_fun(this, &MainWindow::on_configure_event)); + m_draw_primary.signal_size_allocate().connect(sigc::mem_fun(this, &MainWindow::on_size_alloc)); - // m_panedDraw.signal_configure_event().. // Table of contents m_closeTocWindowButton.signal_clicked().connect(sigc::mem_fun(m_vboxToc, &Gtk::Widget::hide)); tocTreeView.signal_row_activated().connect(sigc::mem_fun(this, &MainWindow::on_toc_row_activated)); @@ -1057,15 +1082,16 @@ bool MainWindow::delete_window(GdkEventAny* any_event __attribute__((unused))) // m_settings->set_boolean("fullscreen", is_fullscreen()); m_settings->set_string("font-family", fontFamily_); m_settings->set_int("font-size", currentFontSize_); - m_settings->set_boolean("reader-view", isReaderViewEnabled_); - m_settings->set_int("max-content-width", m_maxContentWidthSpinButton.get_value_as_int()); - m_settings->set_int("spacing", m_spacingSpinButton.get_value_as_int()); - m_settings->set_int("margins", m_marginsSpinButton.get_value_as_int()); - m_settings->set_int("indent", m_indentSpinButton.get_value_as_int()); + m_settings->set_int("max-content-width", contentMaxWidth_); + m_settings->set_int("spacing", fontSpacing_); + m_settings->set_int("margins", contentMargin_); + m_settings->set_int("indent", indent_); + m_settings->set_enum("wrap-mode", wrapMode_); m_settings->set_string("icon-theme", iconTheme_); m_settings->set_boolean("icon-gtk-theme", useCurrentGTKIconTheme_); m_settings->set_double("brightness", brightnessScale_); m_settings->set_boolean("dark-theme", useDarkTheme_); + m_settings->set_boolean("reader-view", isReaderViewEnabled_); } return false; } @@ -1221,13 +1247,12 @@ void MainWindow::selectAll() } /** - * \brief Triggered on windows resize + * \brief Triggers when the textview widget changes size */ -bool MainWindow::on_configure_event(__attribute__((unused)) GdkEventConfigure* configure_event) +void MainWindow::on_size_alloc(__attribute__((unused)) Gdk::Rectangle& allocation) { if (!isEditorEnabled()) updateMargins(); - return false; } /** @@ -1949,34 +1974,37 @@ bool MainWindow::isInstalled() } /** - * \brief Enable editor mode. Allowing to create or edit existing documents. + * \brief Enable editor mode. Allowing to create or edit existing documents */ void MainWindow::enableEdit() { - // Inform the Draw class that we are creating a new document + // Inform the Draw class that we are creating a new document, + // will apply change some textview setting changes m_draw_primary.newDocument(); // Show editor toolbars m_hboxStandardEditorToolbar.show(); m_hboxFormattingEditorToolbar.show(); + // Enable monospace in editor + m_draw_primary.set_monospace(true); + // Apply some settings from primary to secondary window + m_draw_secondary.set_indent(indent_); + m_draw_secondary.set_wrap_mode(wrapMode_); + m_draw_secondary.set_left_margin(contentMargin_); + m_draw_secondary.set_right_margin(contentMargin_); // Determine position of divider between the primary and secondary windows - int location = 0; - int positionSettings = 42; - if (m_settings) - positionSettings = m_settings->get_int("position-divider-draw"); - int currentWidth, _ = 0; - get_size(currentWidth, _); - // If position from settings is still default (42) or too big, - // let's calculate the paned divider location - if ((positionSettings == 42) || (positionSettings >= (currentWidth - 10))) + int currentWidth = get_width(); + int maxWidth = currentWidth - 40; + // Recalculate the position divider if it's too big, + // or positionDividerDraw_ is still on default value + if ((m_panedDraw.get_position() >= maxWidth) || positionDividerDraw_ == -1) { - location = static_cast(currentWidth / 2.0); + int proposedPosition = positionDividerDraw_; // Try to first use the gsettings + if ((proposedPosition == -1) || (proposedPosition >= maxWidth)) + { + proposedPosition = static_cast(currentWidth / 2.0); + } + m_panedDraw.set_position(proposedPosition); } - else - { - location = positionSettings; - } - m_panedDraw.set_position(location); - // Enabled secondary text view (on the right) m_scrolledWindowSecondary.show(); // Disable "view source" menu item @@ -1992,7 +2020,7 @@ void MainWindow::enableEdit() } /** - * \brief Disable editor mode. + * \brief Disable editor mode */ void MainWindow::disableEdit() { @@ -2003,6 +2031,11 @@ void MainWindow::disableEdit() m_scrolledWindowSecondary.hide(); // Disconnect text changed signal textChangedSignalHandler_.disconnect(); + // Disable monospace + m_draw_primary.set_monospace(false); + // Re-apply settings on primary window + m_draw_primary.set_indent(indent_); + m_draw_primary.set_wrap_mode(wrapMode_); // Show "view source" menu item again m_draw_primary.setViewSourceMenuItem(true); m_draw_secondary.clear(); @@ -2058,21 +2091,32 @@ std::string MainWindow::getIconImageFromTheme(const std::string& iconName, const } /** - * \brief Calculate & update drawing margins + * \brief Calculate & update margins on primary draw */ void MainWindow::updateMargins() { - if (isReaderViewEnabled_) + if (isEditorEnabled()) { - - int width = m_draw_primary.get_width(); - std::cout << "Width: " << width << std::endl; - if (width > (contentMaxWidth_ + (2 * contentMargin_))) + m_draw_secondary.set_left_margin(contentMargin_); + m_draw_secondary.set_right_margin(contentMargin_); + } + else + { + if (isReaderViewEnabled_) { - // Calculate margins on the fly - int margin = (width - contentMaxWidth_) / 2; - m_draw_primary.set_left_margin(margin); - m_draw_primary.set_right_margin(margin); + int width = m_draw_primary.get_width(); + if (width > (contentMaxWidth_ + (2 * contentMargin_))) + { + // Calculate margins on the fly + int margin = (width - contentMaxWidth_) / 2; + m_draw_primary.set_left_margin(margin); + m_draw_primary.set_right_margin(margin); + } + else + { + m_draw_primary.set_left_margin(contentMargin_); + m_draw_primary.set_right_margin(contentMargin_); + } } else { @@ -2080,11 +2124,6 @@ void MainWindow::updateMargins() m_draw_primary.set_right_margin(contentMargin_); } } - else - { - m_draw_primary.set_left_margin(contentMargin_); - m_draw_primary.set_right_margin(contentMargin_); - } } /** @@ -2248,18 +2287,25 @@ void MainWindow::on_spacing_changed() void MainWindow::on_margins_changed() { contentMargin_ = m_marginsSpinButton.get_value_as_int(); - if (!isEditorEnabled()) - updateMargins(); + updateMargins(); } void MainWindow::on_indent_changed() { - m_draw_primary.set_indent(m_indentSpinButton.get_value_as_int()); + indent_ = m_indentSpinButton.get_value_as_int(); + if (isEditorEnabled()) + m_draw_secondary.set_indent(indent_); + else + m_draw_primary.set_indent(indent_); } void MainWindow::on_wrap_toggled(Gtk::WrapMode mode) { - m_draw_primary.set_wrap_mode(mode); + wrapMode_ = mode; + if (isEditorEnabled()) + m_draw_secondary.set_wrap_mode(wrapMode_); + else + m_draw_primary.set_wrap_mode(wrapMode_); } void MainWindow::on_brightness_changed() diff --git a/src/mainwindow.h b/src/mainwindow.h index 2bf0969..91832a6 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -75,7 +75,7 @@ protected: void paste(); void del(); void selectAll(); - bool on_configure_event(GdkEventConfigure* configure_event); + void on_size_alloc(Gdk::Rectangle& allocation); void on_toc_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column); void new_doc(); void open(); @@ -305,9 +305,12 @@ private: std::string fontFamily_; int defaultFontSize_; int currentFontSize_; + int positionDividerDraw_; int contentMargin_; int contentMaxWidth_; int fontSpacing_; + int indent_; + Gtk::WrapMode wrapMode_; double brightnessScale_; bool useDarkTheme_; bool isReaderViewEnabled_; diff --git a/src/schema/org.libreweb.browser.gschema.xml b/src/schema/org.libreweb.browser.gschema.xml index a015a3a..80b2876 100644 --- a/src/schema/org.libreweb.browser.gschema.xml +++ b/src/schema/org.libreweb.browser.gschema.xml @@ -1,5 +1,11 @@ + + + + + + 1000 @@ -42,13 +48,17 @@ Font spacing - 20 + 10 Text margins 0 Text indent + + "WRAP_CHAR" + Word wrapping mode + "flat" Icon theme