From cb39327b1c8772516b359bb98f5bd80a4f52e8d1 Mon Sep 17 00:00:00 2001 From: Oriko Date: Wed, 11 Mar 2020 03:02:37 +0200 Subject: [PATCH] TextEditor: Add syntax toggle to View menu --- Applications/TextEditor/TextEditorWidget.cpp | 29 +++++++++++++++++++- Applications/TextEditor/TextEditorWidget.h | 5 ++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index a51c7c04f6..1ffa8bb2a9 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -378,10 +379,34 @@ TextEditorWidget::TextEditorWidget() })); }); + syntax_actions = GUI::ActionGroup {}; + syntax_actions.set_exclusive(true); + + auto syntax_menu = GUI::Menu::construct("Syntax"); + m_plain_text_highlight = GUI::Action::create("Plain Text", [&](GUI::Action& action) { + action.set_checked(true); + m_editor->set_syntax_highlighter(nullptr); + m_editor->update(); + }); + m_plain_text_highlight->set_checkable(true); + m_plain_text_highlight->set_checked(true); + syntax_actions.add_action(*m_plain_text_highlight); + syntax_menu->add_action(*m_plain_text_highlight); + + m_cpp_highlight = GUI::Action::create("C++", [&](GUI::Action& action) { + action.set_checked(true); + m_editor->set_syntax_highlighter(make()); + m_editor->update(); + }); + m_cpp_highlight->set_checkable(true); + syntax_actions.add_action(*m_cpp_highlight); + syntax_menu->add_action(*m_cpp_highlight); + auto view_menu = GUI::Menu::construct("View"); view_menu->add_action(*m_line_wrapping_setting_action); view_menu->add_separator(); view_menu->add_submenu(move(font_menu)); + view_menu->add_submenu(move(syntax_menu)); menubar->add_menu(move(view_menu)); auto help_menu = GUI::Menu::construct("Help"); @@ -420,7 +445,9 @@ void TextEditorWidget::set_path(const FileSystemPath& file) m_extension = file.extension(); if (m_extension == "cpp" || m_extension == "h") - m_editor->set_syntax_highlighter(make()); + m_cpp_highlight->activate(); + else + m_plain_text_highlight->activate(); update_title(); } diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 35267f8ab9..66a4d4edc0 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -78,6 +79,10 @@ private: RefPtr m_find_widget; RefPtr m_replace_widget; + GUI::ActionGroup syntax_actions; + RefPtr m_plain_text_highlight; + RefPtr m_cpp_highlight; + bool m_document_dirty { false }; bool m_document_opening { false }; };