Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2020-2022, Itamar S. <itamar8910@gmail.com>
4 * Copyright (c) 2020-2021, the SerenityOS developers.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#pragma once
10
11#include "ClassViewWidget.h"
12#include "Debugger/DebugInfoWidget.h"
13#include "Debugger/DisassemblyWidget.h"
14#include "EditorWrapper.h"
15#include "FindInFilesWidget.h"
16#include "GMLPreviewWidget.h"
17#include "Git/DiffViewer.h"
18#include "Git/GitWidget.h"
19#include "Locator.h"
20#include "Project.h"
21#include "ProjectBuilder.h"
22#include "ProjectFile.h"
23#include "TerminalWrapper.h"
24#include "ToDoEntriesWidget.h"
25#include <LibCoredump/Inspector.h>
26#include <LibGUI/ActionGroup.h>
27#include <LibGUI/Button.h>
28#include <LibGUI/Scrollbar.h>
29#include <LibGUI/Splitter.h>
30#include <LibGUI/Widget.h>
31#include <LibGfx/Font/Font.h>
32#include <LibThreading/Thread.h>
33
34namespace HackStudio {
35
36class HackStudioWidget : public GUI::Widget {
37 C_OBJECT_ABSTRACT(HackStudioWidget)
38
39public:
40 static ErrorOr<NonnullRefPtr<HackStudioWidget>> create(DeprecatedString path_to_project);
41 virtual ~HackStudioWidget() override;
42
43 bool open_file(DeprecatedString const& filename, size_t line = 0, size_t column = 0);
44 void close_file_in_all_editors(DeprecatedString const& filename);
45
46 void update_actions();
47 Project& project();
48 GUI::TextEditor& current_editor();
49 GUI::TextEditor const& current_editor() const;
50 EditorWrapper& current_editor_wrapper();
51 EditorWrapper const& current_editor_wrapper() const;
52 void set_current_editor_wrapper(RefPtr<EditorWrapper>);
53 void set_current_editor_tab_widget(RefPtr<GUI::TabWidget>);
54
55 GUI::TabWidget& current_editor_tab_widget();
56 GUI::TabWidget const& current_editor_tab_widget() const;
57
58 DeprecatedString const& active_file() const { return m_current_editor_wrapper->filename(); }
59 ErrorOr<void> initialize_menubar(GUI::Window&);
60
61 Locator& locator()
62 {
63 VERIFY(m_locator);
64 return *m_locator;
65 }
66
67 enum class ContinueDecision {
68 No,
69 Yes
70 };
71 ContinueDecision warn_unsaved_changes(DeprecatedString const& prompt);
72
73 enum class Mode {
74 Code,
75 Coredump
76 };
77
78 void open_coredump(DeprecatedString const& coredump_path);
79 void debug_process(pid_t pid);
80 void for_each_open_file(Function<void(ProjectFile const&)>);
81 bool semantic_syntax_highlighting_is_enabled() const;
82
83 static Vector<DeprecatedString> read_recent_projects();
84
85 void update_current_editor_title();
86 void update_window_title();
87
88private:
89 static constexpr size_t recent_projects_history_size = 15;
90
91 static DeprecatedString get_full_path_of_serenity_source(DeprecatedString const& file);
92 DeprecatedString get_absolute_path(DeprecatedString const&) const;
93 Vector<DeprecatedString> selected_file_paths() const;
94
95 void open_project(DeprecatedString const& root_path);
96
97 enum class EditMode {
98 Text,
99 Diff,
100 };
101
102 void set_edit_mode(EditMode);
103
104 ErrorOr<NonnullRefPtr<GUI::Menu>> create_project_tree_view_context_menu();
105 ErrorOr<NonnullRefPtr<GUI::Action>> create_new_file_action(DeprecatedString const& label, DeprecatedString const& icon, DeprecatedString const& extension);
106 ErrorOr<NonnullRefPtr<GUI::Action>> create_new_directory_action();
107 ErrorOr<NonnullRefPtr<GUI::Action>> create_open_selected_action();
108 NonnullRefPtr<GUI::Action> create_delete_action();
109 ErrorOr<NonnullRefPtr<GUI::Action>> create_new_project_action();
110 NonnullRefPtr<GUI::Action> create_switch_to_next_editor_tab_widget_action();
111 NonnullRefPtr<GUI::Action> create_switch_to_next_editor_action();
112 NonnullRefPtr<GUI::Action> create_switch_to_previous_editor_action();
113 NonnullRefPtr<GUI::Action> create_remove_current_editor_tab_widget_action();
114 ErrorOr<NonnullRefPtr<GUI::Action>> create_remove_current_editor_action();
115 ErrorOr<NonnullRefPtr<GUI::Action>> create_open_action();
116 NonnullRefPtr<GUI::Action> create_save_action();
117 NonnullRefPtr<GUI::Action> create_save_as_action();
118 NonnullRefPtr<GUI::Action> create_show_in_file_manager_action();
119 NonnullRefPtr<GUI::Action> create_copy_relative_path_action();
120 NonnullRefPtr<GUI::Action> create_copy_full_path_action();
121 NonnullRefPtr<GUI::Action> create_add_editor_tab_widget_action();
122 ErrorOr<NonnullRefPtr<GUI::Action>> create_add_editor_action();
123 ErrorOr<NonnullRefPtr<GUI::Action>> create_add_terminal_action();
124 ErrorOr<NonnullRefPtr<GUI::Action>> create_remove_current_terminal_action();
125 ErrorOr<NonnullRefPtr<GUI::Action>> create_debug_action();
126 ErrorOr<NonnullRefPtr<GUI::Action>> create_build_action();
127 ErrorOr<NonnullRefPtr<GUI::Action>> create_run_action();
128 ErrorOr<NonnullRefPtr<GUI::Action>> create_stop_action();
129 ErrorOr<NonnullRefPtr<GUI::Action>> create_toggle_syntax_highlighting_mode_action();
130 ErrorOr<NonnullRefPtr<GUI::Action>> create_open_project_configuration_action();
131 ErrorOr<void> create_location_history_actions();
132
133 void add_new_editor_tab_widget(GUI::Widget& parent);
134 void add_new_editor(GUI::TabWidget& parent);
135 RefPtr<EditorWrapper> get_editor_of_file(DeprecatedString const& filename);
136 DeprecatedString get_project_executable_path() const;
137
138 void on_action_tab_change();
139 void reveal_action_tab(GUI::Widget&);
140 void initialize_debugger();
141 void update_statusbar();
142
143 void handle_external_file_deletion(DeprecatedString const& filepath);
144 void stop_debugger_if_running();
145 void close_current_project();
146
147 void create_open_files_view(GUI::Widget& parent);
148 void create_toolbar(GUI::Widget& parent);
149 ErrorOr<void> create_action_tab(GUI::Widget& parent);
150 ErrorOr<void> create_file_menu(GUI::Window&);
151 void update_recent_projects_submenu();
152 ErrorOr<void> create_edit_menu(GUI::Window&);
153 void create_build_menu(GUI::Window&);
154 ErrorOr<void> create_view_menu(GUI::Window&);
155 void create_help_menu(GUI::Window&);
156 void create_project_tab(GUI::Widget& parent);
157 void configure_project_tree_view();
158
159 void run();
160 void build();
161
162 void hide_action_tabs();
163 bool any_document_is_dirty() const;
164
165 void update_gml_preview();
166 void update_tree_view();
167 void update_toolbar_actions();
168 void on_cursor_change();
169 void file_renamed(DeprecatedString const& old_name, DeprecatedString const& new_name);
170
171 struct ProjectLocation {
172 DeprecatedString filename;
173 size_t line { 0 };
174 size_t column { 0 };
175 };
176
177 ProjectLocation current_project_location() const;
178 void update_history_actions();
179
180 Vector<NonnullRefPtr<EditorWrapper>> m_all_editor_wrappers;
181 RefPtr<EditorWrapper> m_current_editor_wrapper;
182 Vector<NonnullRefPtr<GUI::TabWidget>> m_all_editor_tab_widgets;
183 RefPtr<GUI::TabWidget> m_current_editor_tab_widget;
184
185 HashMap<DeprecatedString, NonnullRefPtr<ProjectFile>> m_open_files;
186 RefPtr<Core::FileWatcher> m_file_watcher;
187 Vector<DeprecatedString> m_open_files_vector; // NOTE: This contains the keys from m_open_files and m_file_watchers
188
189 OwnPtr<Project> m_project;
190
191 Vector<ProjectLocation> m_locations_history;
192 // This index is the boundary between the "Go Back" and "Go Forward" locations.
193 // It always points at one past the current location in the list.
194 size_t m_locations_history_end_index { 0 };
195 bool m_locations_history_disabled { false };
196
197 RefPtr<GUI::TreeView> m_project_tree_view;
198 RefPtr<GUI::ListView> m_open_files_view;
199 RefPtr<GUI::VerticalSplitter> m_right_hand_splitter;
200 RefPtr<GUI::StackWidget> m_right_hand_stack;
201 RefPtr<GUI::Splitter> m_editors_splitter;
202 RefPtr<DiffViewer> m_diff_viewer;
203 RefPtr<GitWidget> m_git_widget;
204 RefPtr<GMLPreviewWidget> m_gml_preview_widget;
205 RefPtr<ClassViewWidget> m_class_view;
206 RefPtr<GUI::Menu> m_project_tree_view_context_menu;
207 RefPtr<GUI::Statusbar> m_statusbar;
208 RefPtr<GUI::TabWidget> m_action_tab_widget;
209 RefPtr<GUI::TabWidget> m_project_tab;
210 RefPtr<TerminalWrapper> m_terminal_wrapper;
211 RefPtr<Locator> m_locator;
212 RefPtr<FindInFilesWidget> m_find_in_files_widget;
213 RefPtr<ToDoEntriesWidget> m_todo_entries_widget;
214 RefPtr<DebugInfoWidget> m_debug_info_widget;
215 RefPtr<DisassemblyWidget> m_disassembly_widget;
216 RefPtr<Threading::Thread> m_debugger_thread;
217 RefPtr<EditorWrapper> m_current_editor_in_execution;
218 RefPtr<GUI::Menu> m_recent_projects_submenu { nullptr };
219
220 Vector<NonnullRefPtr<GUI::Action>> m_new_file_actions;
221 RefPtr<GUI::Action> m_new_plain_file_action;
222
223 RefPtr<GUI::Action> m_new_directory_action;
224 RefPtr<GUI::Action> m_open_selected_action;
225 RefPtr<GUI::Action> m_show_in_file_manager_action;
226 RefPtr<GUI::Action> m_copy_relative_path_action;
227 RefPtr<GUI::Action> m_copy_full_path_action;
228 RefPtr<GUI::Action> m_delete_action;
229 RefPtr<GUI::Action> m_tree_view_rename_action;
230 RefPtr<GUI::Action> m_new_project_action;
231 RefPtr<GUI::Action> m_switch_to_next_editor_tab_widget;
232 RefPtr<GUI::Action> m_switch_to_next_editor;
233 RefPtr<GUI::Action> m_switch_to_previous_editor;
234 RefPtr<GUI::Action> m_remove_current_editor_tab_widget_action;
235 RefPtr<GUI::Action> m_remove_current_editor_action;
236 RefPtr<GUI::Action> m_open_action;
237 RefPtr<GUI::Action> m_save_action;
238 RefPtr<GUI::Action> m_save_as_action;
239 RefPtr<GUI::Action> m_add_editor_action;
240 RefPtr<GUI::Action> m_add_editor_tab_widget_action;
241 RefPtr<GUI::Action> m_add_terminal_action;
242 RefPtr<GUI::Action> m_remove_current_terminal_action;
243 RefPtr<GUI::Action> m_stop_action;
244 RefPtr<GUI::Action> m_debug_action;
245 RefPtr<GUI::Action> m_build_action;
246 RefPtr<GUI::Action> m_run_action;
247 RefPtr<GUI::Action> m_locations_history_back_action;
248 RefPtr<GUI::Action> m_locations_history_forward_action;
249 RefPtr<GUI::Action> m_toggle_semantic_highlighting_action;
250 RefPtr<GUI::Action> m_open_project_configuration_action;
251
252 RefPtr<Gfx::Font const> read_editor_font_from_config();
253 void change_editor_font(RefPtr<Gfx::Font const>);
254 RefPtr<Gfx::Font const> m_editor_font;
255 RefPtr<GUI::Action> m_editor_font_action;
256
257 GUI::TextEditor::WrappingMode m_wrapping_mode { GUI::TextEditor::NoWrap };
258 GUI::ActionGroup m_wrapping_mode_actions;
259 RefPtr<GUI::Action> m_no_wrapping_action;
260 RefPtr<GUI::Action> m_wrap_anywhere_action;
261 RefPtr<GUI::Action> m_wrap_at_words_action;
262
263 RefPtr<GUI::Button> m_cut_button;
264 RefPtr<GUI::Button> m_paste_button;
265 RefPtr<GUI::Button> m_copy_button;
266
267 Mode m_mode { Mode::Code };
268 OwnPtr<Coredump::Inspector> m_coredump_inspector;
269 OwnPtr<ProjectBuilder> m_project_builder;
270};
271}