Skip to content

Commit

Permalink
HackStudio: Start fleshing out the GUI for a GUI designer :^)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Nov 8, 2019
1 parent bce510b commit d016d5e
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 9 deletions.
2 changes: 1 addition & 1 deletion AK/Tests/TestJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Base/home/anon/little/little.files
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
main.cpp
Makefile
little.files
test.frm
File renamed without changes.
28 changes: 28 additions & 0 deletions DevTools/HackStudio/FormEditorWidget.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions DevTools/HackStudio/FormEditorWidget.h
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions DevTools/HackStudio/FormWidget.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions DevTools/HackStudio/FormWidget.h
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions DevTools/HackStudio/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ OBJS = \
TerminalWrapper.o \
FindInFilesWidget.o \
ProcessStateWidget.o \
FormEditorWidget.o \
FormWidget.o \
CppLexer.o \
Editor.o \
EditorWrapper.o \
Expand Down
45 changes: 37 additions & 8 deletions DevTools/HackStudio/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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>
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});
Expand All @@ -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;
});
Expand All @@ -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();
});
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d016d5e

Please sign in to comment.