فهرست منبع

HackStudio: Start fleshing out the GUI for a GUI designer :^)

I'll be reconstructing parts of the VisualBuilder application here and
then we can retire VisualBuilder entirely once all the functionality
is available in HackStudio.
Andreas Kling 5 سال پیش
والد
کامیت
d016d5e365

+ 1 - 1
AK/Tests/TestJSON.cpp

@@ -9,7 +9,7 @@
 
 TEST_CASE(load_form)
 {
-    FILE* fp = fopen("../../Base/home/anon/test.frm", "r");
+    FILE* fp = fopen("../../Base/home/anon/little/test.frm", "r");
     ASSERT(fp);
 
     StringBuilder builder;

+ 1 - 0
Base/home/anon/little/little.files

@@ -1,3 +1,4 @@
 main.cpp
 Makefile
 little.files
+test.frm

+ 0 - 0
Base/home/anon/test.frm → Base/home/anon/little/test.frm


+ 28 - 0
DevTools/HackStudio/FormEditorWidget.cpp

@@ -0,0 +1,28 @@
+#include "FormEditorWidget.h"
+#include "FormWidget.h"
+#include <LibGUI/GPainter.h>
+
+FormEditorWidget::FormEditorWidget(GWidget* parent)
+    : GScrollableWidget(parent)
+{
+    set_fill_with_background_color(true);
+    set_background_color(Color::White);
+
+    set_frame_shape(FrameShape::Container);
+    set_frame_shadow(FrameShadow::Sunken);
+    set_frame_thickness(2);
+
+    m_form_widget = FormWidget::construct(*this);
+}
+
+FormEditorWidget::~FormEditorWidget()
+{
+}
+
+void FormEditorWidget::paint_event(GPaintEvent& event)
+{
+    GFrame::paint_event(event);
+
+    GPainter painter(*this);
+    painter.add_clip_rect(event.rect());
+}

+ 18 - 0
DevTools/HackStudio/FormEditorWidget.h

@@ -0,0 +1,18 @@
+#pragma once
+
+#include <LibGUI/GScrollableWidget.h>
+
+class FormWidget;
+
+class FormEditorWidget final : public GScrollableWidget {
+    C_OBJECT(FormEditorWidget)
+public:
+    virtual ~FormEditorWidget() override;
+
+private:
+    virtual void paint_event(GPaintEvent&) override;
+
+    explicit FormEditorWidget(GWidget* parent);
+
+    RefPtr<FormWidget> m_form_widget;
+};

+ 27 - 0
DevTools/HackStudio/FormWidget.cpp

@@ -0,0 +1,27 @@
+#include "FormWidget.h"
+#include "FormEditorWidget.h"
+#include <LibGUI/GPainter.h>
+
+FormWidget::FormWidget(FormEditorWidget& parent)
+    : GWidget(&parent)
+{
+    set_fill_with_background_color(true);
+    set_background_color(Color::WarmGray);
+    set_relative_rect(20, 20, 400, 300);
+}
+
+FormWidget::~FormWidget()
+{
+}
+
+void FormWidget::paint_event(GPaintEvent& event)
+{
+    GPainter painter(*this);
+    painter.add_clip_rect(event.rect());
+
+    for (int y = 0; y < height(); y += m_grid_size) {
+        for (int x = 0; x < width(); x += m_grid_size) {
+            painter.set_pixel({ x, y }, Color::from_rgb(0x404040));
+        }
+    }
+}

+ 22 - 0
DevTools/HackStudio/FormWidget.h

@@ -0,0 +1,22 @@
+#pragma once
+
+#include <LibGUI/GWidget.h>
+
+class FormEditorWidget;
+
+class FormWidget final : public GWidget {
+    C_OBJECT(FormWidget)
+public:
+    virtual ~FormWidget() override;
+
+    FormEditorWidget& editor();
+    const FormEditorWidget& editor() const;
+
+private:
+    virtual void paint_event(GPaintEvent&) override;
+
+    explicit FormWidget(FormEditorWidget& parent);
+
+    // FIXME: This should be an app-wide preference instead.
+    int m_grid_size { 5 };
+};

+ 2 - 0
DevTools/HackStudio/Makefile

@@ -6,6 +6,8 @@ OBJS = \
     TerminalWrapper.o \
     FindInFilesWidget.o \
     ProcessStateWidget.o \
+    FormEditorWidget.o \
+    FormWidget.o \
     CppLexer.o \
     Editor.o \
     EditorWrapper.o \

+ 37 - 8
DevTools/HackStudio/main.cpp

@@ -2,6 +2,7 @@
 #include "Editor.h"
 #include "EditorWrapper.h"
 #include "FindInFilesWidget.h"
+#include "FormEditorWidget.h"
 #include "Locator.h"
 #include "Project.h"
 #include "TerminalWrapper.h"
@@ -19,6 +20,7 @@
 #include <LibGUI/GMenuBar.h>
 #include <LibGUI/GMessageBox.h>
 #include <LibGUI/GSplitter.h>
+#include <LibGUI/GStackWidget.h>
 #include <LibGUI/GTabWidget.h>
 #include <LibGUI/GTextBox.h>
 #include <LibGUI/GTextEditor.h>
@@ -35,6 +37,9 @@ String g_currently_open_file;
 OwnPtr<Project> g_project;
 RefPtr<GWindow> g_window;
 RefPtr<GListView> g_project_list_view;
+RefPtr<GStackWidget> g_right_hand_stack;
+RefPtr<GSplitter> g_inner_splitter;
+RefPtr<FormEditorWidget> g_form_editor_widget;
 
 static RefPtr<GTabWidget> s_action_tab_widget;
 
@@ -51,6 +56,20 @@ void add_new_editor(GWidget& parent)
     wrapper->editor().set_focus(true);
 }
 
+enum class EditMode {
+    Text,
+    Form,
+};
+
+void set_edit_mode(EditMode mode)
+{
+    if (mode == EditMode::Text) {
+        g_right_hand_stack->set_active_widget(g_inner_splitter);
+    } else if (mode == EditMode::Form) {
+        g_right_hand_stack->set_active_widget(g_form_editor_widget);
+    }
+}
+
 EditorWrapper& current_editor_wrapper()
 {
     ASSERT(g_current_editor_wrapper);
@@ -98,9 +117,13 @@ int main(int argc, char** argv)
     g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
     g_project_list_view->set_preferred_size(140, 0);
 
-    auto inner_splitter = GSplitter::construct(Orientation::Vertical, outer_splitter);
-    inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
-    add_new_editor(inner_splitter);
+    g_right_hand_stack = GStackWidget::construct(outer_splitter);
+
+    g_form_editor_widget = FormEditorWidget::construct(g_right_hand_stack);
+
+    g_inner_splitter = GSplitter::construct(Orientation::Vertical, g_right_hand_stack);
+    g_inner_splitter->layout()->set_margins({ 0, 3, 0, 0 });
+    add_new_editor(*g_inner_splitter);
 
     auto new_action = GAction::create("Add new file to project...", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GAction&) {
         auto input_box = GInputBox::construct("Enter name of new file:", "Add new file to project", g_window);
@@ -136,7 +159,7 @@ int main(int argc, char** argv)
         if (g_all_editor_wrappers.size() <= 1)
             return;
         Vector<EditorWrapper*> wrappers;
-        inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
+        g_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
             wrappers.append(&child);
             return IterationDecision::Continue;
         });
@@ -154,7 +177,7 @@ int main(int argc, char** argv)
         if (g_all_editor_wrappers.size() <= 1)
             return;
         Vector<EditorWrapper*> wrappers;
-        inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
+        g_inner_splitter->for_each_child_of_type<EditorWrapper>([&](auto& child) {
             wrappers.append(&child);
             return IterationDecision::Continue;
         });
@@ -173,7 +196,7 @@ int main(int argc, char** argv)
             return;
         auto wrapper = g_current_editor_wrapper;
         switch_to_next_editor->activate();
-        inner_splitter->remove_child(*wrapper);
+        g_inner_splitter->remove_child(*wrapper);
         g_all_editor_wrappers.remove_first_matching([&](auto& entry) { return entry == wrapper.ptr(); });
         update_actions();
     });
@@ -202,7 +225,7 @@ int main(int argc, char** argv)
         open_file(filename);
     };
 
-    s_action_tab_widget = GTabWidget::construct(inner_splitter);
+    s_action_tab_widget = GTabWidget::construct(g_inner_splitter);
 
     s_action_tab_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
     s_action_tab_widget->set_preferred_size(0, 24);
@@ -222,7 +245,7 @@ int main(int argc, char** argv)
     });
 
     auto add_editor_action = GAction::create("Add new editor", { Mod_Ctrl | Mod_Alt, Key_E }, [&](auto&) {
-        add_new_editor(inner_splitter);
+        add_new_editor(*g_inner_splitter);
         update_actions();
     });
 
@@ -379,6 +402,12 @@ void open_file(const String& filename)
         current_editor().on_change = nullptr;
     }
 
+    if (filename.ends_with(".frm")) {
+        set_edit_mode(EditMode::Form);
+    } else {
+        set_edit_mode(EditMode::Text);
+    }
+
     g_currently_open_file = filename;
     g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
     g_project_list_view->update();