From efc335c45770770647e67f9a477a553840cf12ea Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 4 Jul 2020 21:19:01 +0200 Subject: [PATCH] TextEditor: Allow turning off the preview mode This patch adds a PreviewMode enum with the following values: - None - Markdown - HTML This makes it a bit more logical to implement exclusive behavior. --- Applications/TextEditor/TextEditorWidget.cpp | 67 +++++++++++++------- Applications/TextEditor/TextEditorWidget.h | 15 +++-- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index b47d2a9e58..4cb55c2814 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -71,11 +71,7 @@ TextEditorWidget::TextEditorWidget() m_editor->set_line_wrapping_enabled(true); m_editor->on_change = [this] { - if (m_markdown_preview_enabled) - update_markdown_preview(); - - if (m_html_preview_enabled) - update_html_preview(); + update_preview(); // Do not mark as dirty on the first change (When document is first opened.) if (m_document_opening) { @@ -387,18 +383,24 @@ TextEditorWidget::TextEditorWidget() edit_menu.add_action(*m_replace_previous_action); edit_menu.add_action(*m_replace_all_action); + m_no_preview_action = GUI::Action::create_checkable( + "No preview", [this](auto&) { + set_preview_mode(PreviewMode::None); + }); + m_markdown_preview_action = GUI::Action::create_checkable( - "Markdown preview", [this](auto& action) { - set_markdown_preview_enabled(action.is_checked()); + "Markdown preview", [this](auto&) { + set_preview_mode(PreviewMode::Markdown); }, this); m_html_preview_action = GUI::Action::create_checkable( - "HTML preview", [this](auto& action) { - set_html_preview_enabled(action.is_checked()); + "HTML preview", [this](auto&) { + set_preview_mode(PreviewMode::HTML); }, this); + m_preview_actions.add_action(*m_no_preview_action); m_preview_actions.add_action(*m_markdown_preview_action); m_preview_actions.add_action(*m_html_preview_action); m_preview_actions.set_exclusive(true); @@ -406,6 +408,7 @@ TextEditorWidget::TextEditorWidget() auto& view_menu = menubar->add_menu("View"); view_menu.add_action(*m_line_wrapping_setting_action); view_menu.add_separator(); + view_menu.add_action(*m_no_preview_action); view_menu.add_action(*m_markdown_preview_action); view_menu.add_action(*m_html_preview_action); view_menu.add_separator(); @@ -494,8 +497,12 @@ void TextEditorWidget::set_path(const LexicalPath& lexical_path) m_plain_text_highlight->activate(); } - set_markdown_preview_enabled(m_extension == "md"); - set_html_preview_enabled(m_extension == "html"); + if (m_extension == "md") + set_preview_mode(PreviewMode::Markdown); + else if (m_extension == "html") + set_preview_mode(PreviewMode::HTML); + else + set_preview_mode(PreviewMode::None); update_title(); } @@ -561,26 +568,38 @@ void TextEditorWidget::drop_event(GUI::DropEvent& event) } } -void TextEditorWidget::set_html_preview_enabled(bool enabled) +void TextEditorWidget::set_preview_mode(PreviewMode mode) { - if (m_html_preview_enabled == enabled) + if (m_preview_mode == mode) return; - m_html_preview_enabled = enabled; - m_html_preview_action->set_checked(enabled); - m_page_view->set_visible(enabled); - if (enabled) + m_preview_mode = mode; + + if (m_preview_mode == PreviewMode::HTML) { + m_html_preview_action->set_checked(true); + m_page_view->set_visible(true); update_html_preview(); + } else if (m_preview_mode == PreviewMode::Markdown) { + m_markdown_preview_action->set_checked(true); + m_page_view->set_visible(true); + update_markdown_preview(); + } else { + m_no_preview_action->set_checked(true); + m_page_view->set_visible(false); + } } -void TextEditorWidget::set_markdown_preview_enabled(bool enabled) +void TextEditorWidget::update_preview() { - if (m_markdown_preview_enabled == enabled) - return; - m_markdown_preview_enabled = enabled; - m_markdown_preview_action->set_checked(enabled); - m_page_view->set_visible(enabled); - if (enabled) + switch (m_preview_mode) { + case PreviewMode::Markdown: update_markdown_preview(); + break; + case PreviewMode::HTML: + update_html_preview(); + break; + default: + break; + } } void TextEditorWidget::update_markdown_preview() diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 23802f531a..110b72be93 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -44,13 +44,19 @@ public: GUI::TextEditor& editor() { return *m_editor; } - void set_markdown_preview_enabled(bool); - void set_html_preview_enabled(bool); + enum class PreviewMode { + None, + Markdown, + HTML, + }; + + void set_preview_mode(PreviewMode); private: TextEditorWidget(); void set_path(const LexicalPath& file); void update_title(); + void update_preview(); void update_markdown_preview(); void update_html_preview(); @@ -74,6 +80,7 @@ private: RefPtr m_replace_all_action; GUI::ActionGroup m_preview_actions; + RefPtr m_no_preview_action; RefPtr m_markdown_preview_action; RefPtr m_html_preview_action; @@ -100,6 +107,6 @@ private: bool m_document_dirty { false }; bool m_document_opening { false }; - bool m_markdown_preview_enabled { false }; - bool m_html_preview_enabled { false }; + + PreviewMode m_preview_mode { PreviewMode::None }; };