summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-14 12:51:12 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-14 16:16:36 +0200
commit2e547ce7a354df41c7a4b9e36479804d12e835e3 (patch)
tree7b1657671c0891363414c5f1a2d3ea7e8c70e50e
parent40892ac72e5f03af4dba15b2bf22b095f00250c2 (diff)
downloadserenity-2e547ce7a354df41c7a4b9e36479804d12e835e3.zip
TextEditor: Move the main window UI to JSON
This is our first client of the new JSON GUI declaration thingy. The skeleton of the TextEditor app GUI is now declared separately from the C++ logic, and we use the Core::Object::name() of widgets to locate them once they have been instantiated by the GUI builder.
-rw-r--r--Applications/TextEditor/CMakeLists.txt7
-rw-r--r--Applications/TextEditor/MainWindow.json50
-rw-r--r--Applications/TextEditor/TextEditorWidget.cpp25
-rw-r--r--CMakeLists.txt1
4 files changed, 67 insertions, 16 deletions
diff --git a/Applications/TextEditor/CMakeLists.txt b/Applications/TextEditor/CMakeLists.txt
index 88bfe83171..1feb0ae183 100644
--- a/Applications/TextEditor/CMakeLists.txt
+++ b/Applications/TextEditor/CMakeLists.txt
@@ -1,6 +1,13 @@
set(SOURCES
main.cpp
TextEditorWidget.cpp
+ MainWindowUI.h
+)
+
+add_custom_command(
+ OUTPUT MainWindowUI.h
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/MainWindow.json
+ COMMAND ${write_if_different} MainWindowUI.h ${CMAKE_SOURCE_DIR}/Meta/text-to-cpp-string.sh main_window_ui_json ${CMAKE_CURRENT_SOURCE_DIR}/MainWindow.json
)
serenity_bin(TextEditor)
diff --git a/Applications/TextEditor/MainWindow.json b/Applications/TextEditor/MainWindow.json
new file mode 100644
index 0000000000..7b9c9e9cb0
--- /dev/null
+++ b/Applications/TextEditor/MainWindow.json
@@ -0,0 +1,50 @@
+{
+ "name": "main",
+ "fill_with_background_color": true,
+
+ "layout": {
+ "class": "GUI::VerticalBoxLayout",
+ "spacing": 2
+ },
+
+ "children": [
+ {
+ "class": "GUI::ToolBarContainer",
+ "children": [
+ {
+ "class": "GUI::ToolBar",
+ "name": "toolbar"
+ }
+ ]
+ },
+ {
+ "class": "GUI::HorizontalSplitter",
+ "children": [
+ {
+ "class": "GUI::TextEditor",
+ "name": "editor"
+ },
+ {
+ "class": "Web::InProcessWebView",
+ "name": "webview"
+ }
+ ]
+ },
+ {
+ "class": "GUI::Widget",
+ "name": "find_replace_widget",
+ "fill_with_background_color": true,
+ "horizontal_size_policy": "Fill",
+ "vertical_size_policy": "Fixed",
+ "preferred_height": 48,
+ "layout": {
+ "class": "GUI::VerticalBoxLayout",
+ "margins": [ 2, 2, 2, 4 ]
+ }
+ },
+ {
+ "class": "GUI::StatusBar",
+ "name": "statusbar"
+ }
+ ]
+}
diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp
index 75396a36d5..688fa3eb9f 100644
--- a/Applications/TextEditor/TextEditorWidget.cpp
+++ b/Applications/TextEditor/TextEditorWidget.cpp
@@ -25,9 +25,12 @@
*/
#include "TextEditorWidget.h"
+#include <AK/JsonObject.h>
+#include <AK/JsonValue.h>
#include <AK/Optional.h>
#include <AK/StringBuilder.h>
#include <AK/URL.h>
+#include <Applications/TextEditor/MainWindowUI.h>
#include <LibCore/File.h>
#include <LibCore/MimeData.h>
#include <LibDesktop/Launcher.h>
@@ -57,16 +60,11 @@
TextEditorWidget::TextEditorWidget()
{
- set_fill_with_background_color(true);
- set_layout<GUI::VerticalBoxLayout>();
- layout()->set_spacing(2);
+ load_from_json(main_window_ui_json);
- auto& toolbar_container = add<GUI::ToolBarContainer>();
- auto& toolbar = toolbar_container.add<GUI::ToolBar>();
+ auto& toolbar = static_cast<GUI::ToolBar&>(*find_descendant_by_name("toolbar"));
- auto& splitter = add<GUI::HorizontalSplitter>();
-
- m_editor = splitter.add<GUI::TextEditor>();
+ m_editor = static_cast<GUI::TextEditor&>(*find_descendant_by_name("editor"));
m_editor->set_ruler_visible(true);
m_editor->set_automatic_indentation_enabled(true);
m_editor->set_line_wrapping_enabled(true);
@@ -86,7 +84,7 @@ TextEditorWidget::TextEditorWidget()
update_title();
};
- m_page_view = splitter.add<Web::InProcessWebView>();
+ m_page_view = static_cast<Web::InProcessWebView&>(*find_descendant_by_name("webview"));
m_page_view->set_visible(false);
m_page_view->on_link_hover = [this](auto& url) {
if (url.is_valid())
@@ -104,12 +102,7 @@ TextEditorWidget::TextEditorWidget()
}
};
- m_find_replace_widget = add<GUI::Widget>();
- m_find_replace_widget->set_fill_with_background_color(true);
- m_find_replace_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
- m_find_replace_widget->set_preferred_size(0, 48);
- m_find_replace_widget->set_layout<GUI::VerticalBoxLayout>();
- m_find_replace_widget->layout()->set_margins({ 2, 2, 2, 4 });
+ m_find_replace_widget = *find_descendant_by_name("find_replace_widget");
m_find_replace_widget->set_visible(false);
m_find_widget = m_find_replace_widget->add<GUI::Widget>();
@@ -292,7 +285,7 @@ TextEditorWidget::TextEditorWidget()
m_editor->add_custom_context_menu_action(*m_find_next_action);
m_editor->add_custom_context_menu_action(*m_find_previous_action);
- m_statusbar = add<GUI::StatusBar>();
+ m_statusbar = static_cast<GUI::StatusBar&>(*find_descendant_by_name("statusbar"));
m_editor->on_cursor_change = [this] { update_statusbar_cursor_position(); };
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 427457019b..bcd2efffee 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -197,6 +197,7 @@ add_link_options(--sysroot ${CMAKE_BINARY_DIR}/Root)
include_directories(Libraries/LibC)
include_directories(Libraries/LibM)
include_directories(Services)
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/Services)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/Libraries)