LibGUI: Introduce GML - a simple GUI Markup Language :^)

This patch replaces the UI-from-JSON mechanism with a more
human-friendly DSL.

The current implementation simply converts the GML into a JSON object
that can be consumed by GUI::Widget::load_from_json(). The parser is
not very helpful if you make a mistake.

The language offers a very simple way to instantiate any registered
Core::Object class by simply saying @ClassName

@GUI::Label {
    text: "Hello friends!"
    tooltip: ":^)"
}

Layouts are Core::Objects and can be assigned to the "layout" property:

@GUI::Widget {
    layout: @GUI::VerticalBoxLayout {
        spacing: 2
        margins: [8, 8, 8, 8]
    }
}

And finally, child objects are simply nested within their parent:

@GUI::Widget {
    layout: @GUI::HorizontalBoxLayout {
    }
    @GUI::Button {
        text: "OK"
    }
    @GUI::Button {
        text: "Cancel"
    }
}

This feels a *lot* more pleasant to write than the JSON we had. The fact
that no new code was being written with the JSON mechanism was pretty
telling, so let's approach this with developer convenience in mind. :^)
This commit is contained in:
Andreas Kling 2020-12-20 11:47:44 +01:00
parent 18f1c49804
commit 822dc56ef3
Notes: sideshowbarker 2024-07-19 00:44:02 +09:00
24 changed files with 453 additions and 343 deletions

View File

@ -0,0 +1,15 @@
@GUI::Widget {
name: "browser"
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
spacing: 2
}
@GUI::TabWidget {
name: "tab_widget"
container_padding: 0
uniform_tabs: true
text_alignment: "CenterLeft"
}
}

View File

@ -1,19 +0,0 @@
{
"name": "browser",
"fill_with_background_color": true,
"layout": {
"class": "GUI::VerticalBoxLayout",
"spacing": 2
},
"children": [
{
"class": "GUI::TabWidget",
"name": "tab_widget",
"container_padding": 0,
"uniform_tabs": true,
"text_alignment": "CenterLeft"
}
]
}

View File

@ -1,5 +1,5 @@
compile_json_gui(BrowserWindow.json BrowserWindowUI.h browser_window_ui_json) compile_gml(BrowserWindow.gml BrowserWindowGML.h browser_window_gml)
compile_json_gui(Tab.json TabUI.h tab_ui_json) compile_gml(Tab.gml TabGML.h tab_gml)
set(SOURCES set(SOURCES
BookmarksBarWidget.cpp BookmarksBarWidget.cpp
@ -11,8 +11,8 @@ set(SOURCES
main.cpp main.cpp
Tab.cpp Tab.cpp
WindowActions.cpp WindowActions.cpp
BrowserWindowUI.h BrowserWindowGML.h
TabUI.h TabGML.h
) )
serenity_bin(Browser) serenity_bin(Browser)

View File

@ -32,7 +32,7 @@
#include "InspectorWidget.h" #include "InspectorWidget.h"
#include "WindowActions.h" #include "WindowActions.h"
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Applications/Browser/TabUI.h> #include <Applications/Browser/TabGML.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
@ -88,7 +88,7 @@ static void start_download(const URL& url)
Tab::Tab(Type type) Tab::Tab(Type type)
: m_type(type) : m_type(type)
{ {
load_from_json(tab_ui_json); load_from_gml(tab_gml);
m_toolbar_container = static_cast<GUI::ToolBarContainer&>(*find_descendant_by_name("toolbar_container")); m_toolbar_container = static_cast<GUI::ToolBarContainer&>(*find_descendant_by_name("toolbar_container"));
auto& toolbar = static_cast<GUI::ToolBar&>(*find_descendant_by_name("toolbar")); auto& toolbar = static_cast<GUI::ToolBar&>(*find_descendant_by_name("toolbar"));

View File

@ -0,0 +1,22 @@
@GUI::Widget {
layout: @GUI::VerticalBoxLayout {
}
@GUI::ToolBarContainer {
name: "toolbar_container"
@GUI::ToolBar {
name: "toolbar"
}
}
@GUI::Widget {
name: "webview_container"
layout: @GUI::VerticalBoxLayout {
}
}
@GUI::StatusBar {
name: "statusbar"
}
}

View File

@ -1,29 +0,0 @@
{
"layout": {
"class": "GUI::VerticalBoxLayout"
},
"children": [
{
"class": "GUI::ToolBarContainer",
"name": "toolbar_container",
"children": [
{
"class": "GUI::ToolBar",
"name": "toolbar"
}
]
},
{
"class": "GUI::Widget",
"name": "webview_container",
"layout": {
"class": "GUI::VerticalBoxLayout"
}
},
{
"class": "GUI::StatusBar",
"name": "statusbar"
}
]
}

View File

@ -30,7 +30,7 @@
#include "Tab.h" #include "Tab.h"
#include "WindowActions.h" #include "WindowActions.h"
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Applications/Browser/BrowserWindowUI.h> #include <Applications/Browser/BrowserWindowGML.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h> #include <LibCore/ConfigFile.h>
#include <LibCore/File.h> #include <LibCore/File.h>
@ -138,7 +138,7 @@ int main(int argc, char** argv)
window->set_title("Browser"); window->set_title("Browser");
auto& widget = window->set_main_widget<GUI::Widget>(); auto& widget = window->set_main_widget<GUI::Widget>();
widget.load_from_json(browser_window_ui_json); widget.load_from_gml(browser_window_gml);
auto& tab_widget = static_cast<GUI::TabWidget&>(*widget.find_descendant_by_name("tab_widget")); auto& tab_widget = static_cast<GUI::TabWidget&>(*widget.find_descendant_by_name("tab_widget"));

View File

@ -1,5 +1,5 @@
compile_json_gui(CondFormatting.json CondFormattingUI.h cond_fmt_ui_json) compile_gml(CondFormatting.gml CondFormattingGML.h cond_fmt_gml)
compile_json_gui(CondView.json CondFormattingViewUI.h cond_fmt_view_ui_json) compile_gml(CondView.gml CondFormattingViewGML.h cond_fmt_view_gml)
set(SOURCES set(SOURCES
Cell.cpp Cell.cpp
@ -11,8 +11,8 @@ set(SOURCES
CellType/String.cpp CellType/String.cpp
CellType/Type.cpp CellType/Type.cpp
CellTypeDialog.cpp CellTypeDialog.cpp
CondFormattingUI.h CondFormattingGML.h
CondFormattingViewUI.h CondFormattingViewGML.h
HelpWindow.cpp HelpWindow.cpp
JSIntegration.cpp JSIntegration.cpp
Readers/XSV.cpp Readers/XSV.cpp

View File

@ -28,8 +28,8 @@
#include "Cell.h" #include "Cell.h"
#include "Spreadsheet.h" #include "Spreadsheet.h"
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Applications/Spreadsheet/CondFormattingUI.h> #include <Applications/Spreadsheet/CondFormattingGML.h>
#include <Applications/Spreadsheet/CondFormattingViewUI.h> #include <Applications/Spreadsheet/CondFormattingViewGML.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h> #include <LibGUI/Button.h>
#include <LibGUI/CheckBox.h> #include <LibGUI/CheckBox.h>
@ -356,7 +356,7 @@ void CellTypeDialog::setup_tabs(GUI::TabWidget& tabs, const Vector<Position>& po
} }
auto& conditional_fmt_tab = tabs.add_tab<GUI::Widget>("Conditional Format"); auto& conditional_fmt_tab = tabs.add_tab<GUI::Widget>("Conditional Format");
conditional_fmt_tab.load_from_json(cond_fmt_ui_json); conditional_fmt_tab.load_from_gml(cond_fmt_gml);
{ {
auto& view = static_cast<Spreadsheet::ConditionsView&>(*conditional_fmt_tab.find_descendant_by_name("conditions_view")); auto& view = static_cast<Spreadsheet::ConditionsView&>(*conditional_fmt_tab.find_descendant_by_name("conditions_view"));
view.set_formats(&m_conditional_formats); view.set_formats(&m_conditional_formats);
@ -429,7 +429,7 @@ CellTypeMetadata CellTypeDialog::metadata() const
ConditionView::ConditionView(ConditionalFormat& fmt) ConditionView::ConditionView(ConditionalFormat& fmt)
: m_format(fmt) : m_format(fmt)
{ {
load_from_json(cond_fmt_view_ui_json); load_from_gml(cond_fmt_view_gml);
auto& fg_input = *static_cast<GUI::ColorInput*>(find_descendant_by_name("foreground_input")); auto& fg_input = *static_cast<GUI::ColorInput*>(find_descendant_by_name("foreground_input"));
auto& bg_input = *static_cast<GUI::ColorInput*>(find_descendant_by_name("background_input")); auto& bg_input = *static_cast<GUI::ColorInput*>(find_descendant_by_name("background_input"));

View File

@ -0,0 +1,41 @@
@GUI::Widget {
name: "main"
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
spacing: 4
}
@Spreadsheet::ConditionsView {
name: "conditions_view"
}
@GUI::Widget {
vertical_size_policy: "Fixed"
horizontal_size_policy: "Fill"
preferred_width: 0
preferred_height: 20
layout: @GUI::HorizontalBoxLayout {
spacing: 10
}
@GUI::Button {
name: "add_button"
text: "Add"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fixed"
preferred_width: 100
preferred_height: 20
}
@GUI::Button {
name: "remove_button"
text: "Remove"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fixed"
preferred_width: 100
preferred_height: 20
}
}
}

View File

@ -1,47 +0,0 @@
{
"name": "main",
"fill_with_background_color": true,
"layout": {
"class": "GUI::VerticalBoxLayout",
"spacing": 4
},
"children": [
{
"class": "Spreadsheet::ConditionsView",
"name": "conditions_view"
},
{
"class": "GUI::Widget",
"vertical_size_policy": "Fixed",
"horizontal_size_policy": "Fill",
"preferred_width": 0,
"preferred_height": 20,
"layout": {
"class": "GUI::HorizontalBoxLayout",
"spacing": 10
},
"children": [
{
"class": "GUI::Button",
"name": "add_button",
"text": "Add",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fixed",
"preferred_width": 100,
"preferred_height": 20
},
{
"class": "GUI::Button",
"name": "remove_button",
"text": "Remove",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fixed",
"preferred_width": 100,
"preferred_height": 20
}
]
}
]
}

View File

@ -0,0 +1,76 @@
@GUI::Widget {
layout: @GUI::VerticalBoxLayout {
spacing: 2
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 10
}
vertical_size_policy: "Fixed"
preferred_height: 25
@GUI::Label {
text: "if..."
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fixed"
preferred_width: 40
preferred_height: 25
}
@GUI::TextEditor {
name: "formula_editor"
horizontal_size_policy: "Fill"
vertical_size_policy: "Fixed"
preferred_height: 25
tooltip: "Use 'value' to refer to the current cell's value"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 10
}
vertical_size_policy: "Fixed"
preferred_height: 25
@GUI::Label {
text: "Foreground..."
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fixed"
preferred_width: 150
preferred_height: 25
}
@GUI::ColorInput {
name: "foreground_input"
vertical_size_policy: "Fixed"
preferred_height: 25
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 10
}
vertical_size_policy: "Fixed"
preferred_height: 25
@GUI::Label {
text: "Background..."
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fixed"
preferred_width: 150
preferred_height: 25
}
@GUI::ColorInput {
name: "background_input"
vertical_size_policy: "Fixed"
preferred_height: 25
}
}
}

View File

@ -1,93 +0,0 @@
{
"class": "GUI::Widget",
"layout": {
"class": "GUI::VerticalBoxLayout",
"spacing": 2
},
"children": [
{
"class": "GUI::Widget",
"layout": {
"class": "GUI::HorizontalBoxLayout",
"spacing": 10
},
"vertical_size_policy": "Fixed",
"preferred_height": 25,
"children": [
{
"class": "GUI::Label",
"name": "if_label",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fixed",
"text": "if...",
"preferred_width": 40,
"preferred_height": 25
},
{
"class": "GUI::TextEditor",
"name": "formula_editor",
"horizontal_size_policy": "Fill",
"vertical_size_policy": "Fixed",
"tooltip": "Use 'value' to refer to the current cell's value",
"preferred_height": 25
}
]
},
{
"class": "GUI::Widget",
"layout": {
"class": "GUI::HorizontalBoxLayout",
"spacing": 10
},
"vertical_size_policy": "Fixed",
"preferred_height": 25,
"children": [
{
"class": "GUI::Label",
"name": "fg_color_label",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fixed",
"text": "Foreground...",
"preferred_width": 150,
"preferred_height": 25
},
{
"class": "GUI::ColorInput",
"name": "foreground_input",
"horizontal_size_policy": "Fill",
"vertical_size_policy": "Fixed",
"preferred_height": 25,
"preferred_width": 25
}
]
},
{
"class": "GUI::Widget",
"layout": {
"class": "GUI::HorizontalBoxLayout",
"spacing": 10
},
"vertical_size_policy": "Fixed",
"preferred_height": 25,
"children": [
{
"class": "GUI::Label",
"name": "bg_color_label",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fixed",
"text": "Background...",
"preferred_width": 150,
"preferred_height": 25
},
{
"class": "GUI::ColorInput",
"name": "background_input",
"horizontal_size_policy": "Fill",
"vertical_size_policy": "Fixed",
"preferred_height": 25,
"preferred_width": 25
}
]
}
]
}

View File

@ -1,9 +1,9 @@
compile_json_gui(MainWindow.json MainWindowUI.h main_window_ui_json) compile_gml(MainWindow.gml MainWindowGML.h main_window_gml)
set(SOURCES set(SOURCES
main.cpp main.cpp
TextEditorWidget.cpp TextEditorWidget.cpp
MainWindowUI.h MainWindowGML.h
) )
serenity_bin(TextEditor) serenity_bin(TextEditor)

View File

@ -0,0 +1,105 @@
@GUI::Widget {
name: "main"
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
spacing: 2
}
@GUI::ToolBarContainer {
@GUI::ToolBar {
name: "toolbar"
}
}
@GUI::HorizontalSplitter {
@GUI::TextEditor {
name: "editor"
}
@Web::OutOfProcessWebView {
name: "webview"
visible: false
}
}
@GUI::Widget {
name: "find_replace_widget"
visible: false
fill_with_background_color: true
horizontal_size_policy: "Fill"
vertical_size_policy: "Fixed"
preferred_height: 48
layout: @GUI::VerticalBoxLayout {
margins: [2, 2, 2, 4]
}
@GUI::Widget {
name: "find_widget"
fill_with_background_color: true
horizontal_size_policy: "Fill"
vertical_size_policy: "Fixed"
preferred_height: 22
layout: @GUI::HorizontalBoxLayout {
}
@GUI::Button {
name: "find_previous_button"
text: "Find previous"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fill"
preferred_width: 150
}
@GUI::Button {
name: "find_next_button"
text: "Find next"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fill"
preferred_width: 150
}
}
@GUI::Widget {
name: "replace_widget"
fill_with_background_color: true
horizontal_size_policy: "Fill"
vertical_size_policy: "Fixed"
preferred_height: 22
layout: @GUI::HorizontalBoxLayout {
}
@GUI::Button {
name: "replace_previous_button"
text: "Replace previous"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fill"
preferred_width: 100
}
@GUI::Button {
name: "replace_next_button"
text: "Replace next"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fill"
preferred_width: 100
}
@GUI::Button {
name: "replace_all_button"
text: "Replace all"
horizontal_size_policy: "Fixed"
vertical_size_policy: "Fill"
preferred_width: 100
}
}
}
@GUI::StatusBar {
name: "statusbar"
}
}

View File

@ -1,120 +0,0 @@
{
"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::OutOfProcessWebView",
"name": "webview",
"visible": false
}
]
},
{
"class": "GUI::Widget",
"name": "find_replace_widget",
"visible": false,
"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 ]
},
"children": [
{
"class": "GUI::Widget",
"name": "find_widget",
"fill_with_background_color": true,
"horizontal_size_policy": "Fill",
"vertical_size_policy": "Fixed",
"preferred_height": 22,
"layout": {
"class": "GUI::HorizontalBoxLayout"
},
"children": [
{
"class": "GUI::Button",
"name": "find_previous_button",
"text": "Find previous",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fill",
"preferred_width": 150
},
{
"class": "GUI::Button",
"name": "find_next_button",
"text": "Find next",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fill",
"preferred_width": 150
}
]
},
{
"class": "GUI::Widget",
"name": "replace_widget",
"fill_with_background_color": true,
"horizontal_size_policy": "Fill",
"vertical_size_policy": "Fixed",
"preferred_height": 22,
"layout": {
"class": "GUI::HorizontalBoxLayout"
},
"children": [
{
"class": "GUI::Button",
"name": "replace_previous_button",
"text": "Replace previous",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fill",
"preferred_width": 100
},
{
"class": "GUI::Button",
"name": "replace_next_button",
"text": "Replace next",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fill",
"preferred_width": 100
},
{
"class": "GUI::Button",
"name": "replace_all_button",
"text": "Replace all",
"horizontal_size_policy": "Fixed",
"vertical_size_policy": "Fill",
"preferred_width": 100
}
]
}
]
},
{
"class": "GUI::StatusBar",
"name": "statusbar"
}
]
}

View File

@ -30,7 +30,7 @@
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/URL.h> #include <AK/URL.h>
#include <Applications/TextEditor/MainWindowUI.h> #include <Applications/TextEditor/MainWindowGML.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
#include <LibDesktop/Launcher.h> #include <LibDesktop/Launcher.h>
@ -61,7 +61,7 @@
TextEditorWidget::TextEditorWidget() TextEditorWidget::TextEditorWidget()
{ {
load_from_json(main_window_ui_json); load_from_gml(main_window_gml);
auto& toolbar = static_cast<GUI::ToolBar&>(*find_descendant_by_name("toolbar")); auto& toolbar = static_cast<GUI::ToolBar&>(*find_descendant_by_name("toolbar"));

View File

@ -164,7 +164,7 @@ function(serenity_bin target_name)
serenity_generated_sources(${target_name}) serenity_generated_sources(${target_name})
endfunction() endfunction()
function(compile_json_gui source output string_name) function(compile_gml source output string_name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${source}) set(source ${CMAKE_CURRENT_SOURCE_DIR}/${source})
add_custom_command( add_custom_command(
OUTPUT ${output} OUTPUT ${output}
@ -177,6 +177,7 @@ function(compile_json_gui source output string_name)
add_custom_target(generate_${output_name} DEPENDS ${output}) add_custom_target(generate_${output_name} DEPENDS ${output})
endfunction() endfunction()
function(compile_ipc source output) function(compile_ipc source output)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${source}) set(source ${CMAKE_CURRENT_SOURCE_DIR}/${source})
add_custom_command( add_custom_command(

View File

@ -30,6 +30,7 @@ set(SOURCES
FileSystemModel.cpp FileSystemModel.cpp
FilteringProxyModel.cpp FilteringProxyModel.cpp
Frame.cpp Frame.cpp
GMLParser.cpp
GroupBox.cpp GroupBox.cpp
HeaderView.cpp HeaderView.cpp
INILexer.cpp INILexer.cpp

View File

@ -0,0 +1,126 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/GenericLexer.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibGUI/GMLParser.h>
#include <ctype.h>
namespace GUI {
static bool is_valid_class_name_character(char ch)
{
return isalpha(ch) || ch == ':';
}
static bool is_valid_property_name_character(char ch)
{
return isalpha(ch) || ch == '_';
}
static void swallow_whitespace(GenericLexer& scanner)
{
scanner.consume_while([](auto ch) { return isspace(ch); });
}
static JsonValue parse_core_object(GenericLexer& scanner)
{
JsonObject object;
JsonArray children;
// '@Foo' means new Core::Object of class Foo
if (!scanner.consume_specific('@'))
ASSERT_NOT_REACHED();
auto class_name = scanner.consume_while([](auto ch) { return is_valid_class_name_character(ch); });
object.set("class", JsonValue(class_name));
swallow_whitespace(scanner);
if (!scanner.consume_specific('{'))
ASSERT_NOT_REACHED();
swallow_whitespace(scanner);
for (;;) {
swallow_whitespace(scanner);
if (scanner.peek() == '}') {
// End of object
break;
}
if (scanner.peek() == '@') {
// It's a child object.
auto value = parse_core_object(scanner);
ASSERT(value.is_object());
children.append(move(value));
} else {
// It's a property.
auto property_name = scanner.consume_while([](auto ch) { return is_valid_property_name_character(ch); });
swallow_whitespace(scanner);
ASSERT(!property_name.is_empty());
if (!scanner.consume_specific(':'))
ASSERT_NOT_REACHED();
swallow_whitespace(scanner);
JsonValue value;
if (scanner.peek() == '@') {
value = parse_core_object(scanner);
ASSERT(value.is_object());
} else {
auto value_string = scanner.consume_line();
value = JsonValue::from_string(value_string).release_value();
}
object.set(property_name, move(value));
}
}
if (!scanner.consume_specific('}'))
ASSERT_NOT_REACHED();
if (!children.is_empty())
object.set("children", move(children));
return object;
}
JsonValue parse_gml(const StringView& string)
{
GenericLexer scanner(string);
auto root = parse_core_object(scanner);
if (root.is_null())
return JsonValue();
return root;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
namespace GUI {
JsonValue parse_gml(const StringView&);
}

View File

@ -33,6 +33,7 @@
#include <LibGUI/CheckBox.h> #include <LibGUI/CheckBox.h>
#include <LibGUI/ColorInput.h> #include <LibGUI/ColorInput.h>
#include <LibGUI/Event.h> #include <LibGUI/Event.h>
#include <LibGUI/GMLParser.h>
#include <LibGUI/GroupBox.h> #include <LibGUI/GroupBox.h>
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
#include <LibGUI/Layout.h> #include <LibGUI/Layout.h>
@ -926,18 +927,11 @@ void Widget::set_override_cursor(Gfx::StandardCursor cursor)
window->update_cursor({}); window->update_cursor({});
} }
bool Widget::load_from_json(const StringView& json_string) bool Widget::load_from_gml(const StringView& gml_string)
{ {
auto json_value = JsonValue::from_string(json_string); auto value = parse_gml(gml_string);
if (!json_value.has_value()) { ASSERT(value.is_object());
dbg() << "load_from_json parse failed: _" << json_string << "_"; return load_from_json(value.as_object());
return false;
}
if (!json_value.value().is_object()) {
dbg() << "load_from_json parse non-object";
return false;
}
return load_from_json(json_value.value().as_object());
} }
bool Widget::load_from_json(const JsonObject& json) bool Widget::load_from_json(const JsonObject& json)

View File

@ -300,8 +300,8 @@ public:
Gfx::StandardCursor override_cursor() const { return m_override_cursor; } Gfx::StandardCursor override_cursor() const { return m_override_cursor; }
void set_override_cursor(Gfx::StandardCursor); void set_override_cursor(Gfx::StandardCursor);
bool load_from_json(const StringView&); bool load_from_gml(const StringView&);
bool load_from_json(const JsonObject&);
Widget* find_child_by_name(const String&); Widget* find_child_by_name(const String&);
Widget* find_descendant_by_name(const String&); Widget* find_descendant_by_name(const String&);
@ -349,6 +349,8 @@ private:
void show_tooltip(); void show_tooltip();
bool load_from_json(const JsonObject&);
Window* m_window { nullptr }; Window* m_window { nullptr };
RefPtr<Layout> m_layout; RefPtr<Layout> m_layout;

View File

@ -11,4 +11,4 @@ fi
cd "$SERENITY_ROOT" cd "$SERENITY_ROOT"
find . \( -name Base -o -name Patches -o -name Ports -o -name Root -o -name Toolchain -o -name Build \) -prune -o \( -name '*.ipc' -or -name '*.cpp' -or -name '*.idl' -or -name '*.c' -or -name '*.h' -or -name '*.S' -or -name '*.css' -or -name '*.json' \) -print > serenity.files find . \( -name Base -o -name Patches -o -name Ports -o -name Root -o -name Toolchain -o -name Build \) -prune -o \( -name '*.ipc' -or -name '*.cpp' -or -name '*.idl' -or -name '*.c' -or -name '*.h' -or -name '*.S' -or -name '*.css' -or -name '*.json' -or -name '*.gml' \) -print > serenity.files