Skip to content

Commit 1fba364

Browse files
committed
GMLPlayground: Add list of recent files to the File menu
1 parent d0730c1 commit 1fba364

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

Userland/DevTools/GMLPlayground/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ set(GENERATED_SOURCES
1515
)
1616

1717
serenity_app(GMLPlayground ICON app-gml-playground)
18-
target_link_libraries(GMLPlayground PRIVATE LibCore LibDesktop LibFileSystemAccessClient LibGfx LibGUI LibMain LibSyntax)
18+
target_link_libraries(GMLPlayground PRIVATE LibConfig LibCore LibDesktop LibFileSystemAccessClient LibGfx LibGUI LibMain LibSyntax)

Userland/DevTools/GMLPlayground/main.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include <AK/URL.h>
11+
#include <LibConfig/Client.h>
1112
#include <LibCore/ArgsParser.h>
1213
#include <LibCore/System.h>
1314
#include <LibDesktop/Launcher.h>
@@ -67,6 +68,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
6768
TRY(Core::System::pledge("stdio thread recvfd sendfd cpath rpath wpath unix"));
6869
auto app = TRY(GUI::Application::try_create(arguments));
6970

71+
Config::pledge_domain("GMLPlayground");
72+
app->set_config_domain(TRY("GMLPlayground"_string));
73+
7074
TRY(Core::System::unveil("/res", "r"));
7175
TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
7276
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
@@ -136,6 +140,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
136140
update_title();
137141
};
138142

143+
auto load_file = [&](auto file) {
144+
auto buffer_or_error = file.stream().read_until_eof();
145+
if (buffer_or_error.is_error())
146+
return;
147+
148+
editor->set_text(buffer_or_error.release_value());
149+
editor->set_focus(true);
150+
update_title();
151+
152+
GUI::Application::the()->set_most_recently_open_file(file.filename());
153+
};
154+
139155
auto file_menu = TRY(window->try_add_menu("&File"));
140156

141157
auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
@@ -150,6 +166,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
150166
}
151167
file_path = response.value().filename().to_deprecated_string();
152168
update_title();
169+
170+
GUI::Application::the()->set_most_recently_open_file(response.value().filename());
153171
});
154172

155173
auto save_action = GUI::CommonActions::make_save_action([&](auto&) {
@@ -182,22 +200,30 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
182200
if (response.is_error())
183201
return;
184202

185-
auto file = response.release_value();
186-
file_path = file.filename().to_deprecated_string();
187-
auto buffer_or_error = file.stream().read_until_eof();
188-
if (buffer_or_error.is_error())
189-
return;
190-
191-
editor->set_text(buffer_or_error.release_value());
192-
editor->set_focus(true);
193-
update_title();
203+
load_file(response.release_value());
194204
});
195205

196206
TRY(file_menu->try_add_action(open_action));
197207
TRY(file_menu->try_add_action(save_action));
198208
TRY(file_menu->try_add_action(save_as_action));
199209
TRY(file_menu->try_add_separator());
200210

211+
TRY(file_menu->add_recent_files_list([&](auto& action) {
212+
if (window->is_modified()) {
213+
auto result = GUI::MessageBox::ask_about_unsaved_changes(window, file_path, editor->document().undo_stack().last_unmodified_timestamp());
214+
if (result == GUI::MessageBox::ExecResult::Yes)
215+
save_action->activate();
216+
if (result != GUI::MessageBox::ExecResult::No && window->is_modified())
217+
return;
218+
}
219+
220+
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, action.text());
221+
if (response.is_error())
222+
return;
223+
file_path = response.value().filename().to_deprecated_string();
224+
load_file(response.release_value());
225+
}));
226+
201227
TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
202228
if (window->on_close_request() == GUI::Window::CloseRequestDecision::Close)
203229
app->quit();

0 commit comments

Comments
 (0)