diff --git a/Userland/DevTools/HackStudio/CMakeLists.txt b/Userland/DevTools/HackStudio/CMakeLists.txt index 062f37c36b..61e0274007 100644 --- a/Userland/DevTools/HackStudio/CMakeLists.txt +++ b/Userland/DevTools/HackStudio/CMakeLists.txt @@ -34,6 +34,7 @@ set(SOURCES Git/GitFilesView.cpp Git/GitRepo.cpp Git/GitWidget.cpp + GMLPreviewWidget.cpp HackStudioWidget.cpp Language.cpp LanguageClient.cpp diff --git a/Userland/DevTools/HackStudio/GMLPreviewWidget.cpp b/Userland/DevTools/HackStudio/GMLPreviewWidget.cpp new file mode 100644 index 0000000000..e2f07af251 --- /dev/null +++ b/Userland/DevTools/HackStudio/GMLPreviewWidget.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021, Conor Byrne + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "GMLPreviewWidget.h" +#include +#include + +namespace HackStudio { + +GMLPreviewWidget::GMLPreviewWidget(String const& gml_content) +{ + set_layout(); + load_gml(gml_content); +} + +void GMLPreviewWidget::load_gml(String const& gml) +{ + remove_all_children(); + + if (gml.is_empty()) { + auto& label = add(); + label.set_text("Open a .gml file to show the preview"); + + return; + } + + load_from_gml(gml, [](const String& name) -> RefPtr { + return GUI::Label::construct(String::formatted("{} is not registered as a GML element!", name)); + }); + + if (children().is_empty()) { + auto& label = add(); + label.set_text("Failed to load GML!"); + } +} + +} diff --git a/Userland/DevTools/HackStudio/GMLPreviewWidget.h b/Userland/DevTools/HackStudio/GMLPreviewWidget.h new file mode 100644 index 0000000000..ef1f1529be --- /dev/null +++ b/Userland/DevTools/HackStudio/GMLPreviewWidget.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021, Conor Byrne + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace HackStudio { + +class GMLPreviewWidget final : public GUI::Widget { + C_OBJECT(GMLPreviewWidget) +public: + void load_gml(String const&); + +private: + explicit GMLPreviewWidget(String const&); +}; + +} diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index 52edb457cc..7720f5b953 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -288,6 +288,9 @@ bool HackStudioWidget::open_file(const String& full_filename) current_editor().set_focus(true); current_editor().on_cursor_change = [this] { update_statusbar(); }; + current_editor().on_change = [this] { update_gml_preview(); }; + update_gml_preview(); + return true; } @@ -504,6 +507,7 @@ void HackStudioWidget::add_new_editor(GUI::Widget& parent) wrapper->editor().set_focus(true); wrapper->set_project_root(LexicalPath(m_project->root_path())); wrapper->editor().on_cursor_change = [this] { update_statusbar(); }; + wrapper->editor().on_change = [this] { update_gml_preview(); }; } NonnullRefPtr HackStudioWidget::create_switch_to_next_editor_action() @@ -908,6 +912,7 @@ void HackStudioWidget::create_action_tab(GUI::Widget& parent) m_diff_viewer->set_content(original_content, diff); set_edit_mode(EditMode::Diff); }); + m_gml_preview_widget = m_action_tab_widget->add_tab("GML Preview", ""); ToDoEntries::the().on_update = [this]() { m_todo_entries_widget->refresh(); @@ -1150,4 +1155,10 @@ bool HackStudioWidget::any_document_is_dirty() const return false; } +void HackStudioWidget::update_gml_preview() +{ + auto gml_content = current_editor_wrapper().filename().ends_with(".gml") ? current_editor_wrapper().editor().text() : ""; + m_gml_preview_widget->load_gml(gml_content); +} + } diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.h b/Userland/DevTools/HackStudio/HackStudioWidget.h index cc0fb6e9e8..92f07ea7df 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.h +++ b/Userland/DevTools/HackStudio/HackStudioWidget.h @@ -13,6 +13,7 @@ #include "Debugger/DisassemblyWidget.h" #include "EditorWrapper.h" #include "FindInFilesWidget.h" +#include "GMLPreviewWidget.h" #include "Git/DiffViewer.h" #include "Git/GitWidget.h" #include "Locator.h" @@ -119,6 +120,8 @@ private: void hide_action_tabs(); bool any_document_is_dirty() const; + void update_gml_preview(); + NonnullRefPtrVector m_all_editor_wrappers; RefPtr m_current_editor_wrapper; @@ -135,6 +138,7 @@ private: RefPtr m_editors_splitter; RefPtr m_diff_viewer; RefPtr m_git_widget; + RefPtr m_gml_preview_widget; RefPtr m_class_view; RefPtr m_project_tree_view_context_menu; RefPtr m_statusbar;