1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:18:12 +00:00

LibGUI: Add a simple "recently open files" feature

This feature allows any application to easily install an automatically
updating list of recently open files in a GUI::Menu.

There are three main pieces to this mechanism:

- GUI::Application::set_config_domain(domain): This must be called
  before using the recent files feature. It informs the Application
  object about which config domain to find the relevant RecentFiles
  list under.

- GUI::Menu::add_recently_open_files(callback): This inserts the list
  in a menu. A callback must be provided to handle actually opening
  the recent file in some application-specific way.

- GUI::Application::set_most_recently_open_file(path): This updates
  the list of recently open files, both in the configuration files,
  and in the GUI menu.
This commit is contained in:
Andreas Kling 2023-02-06 18:39:59 +01:00
parent 3e2ceef8c3
commit 544366ff2a
4 changed files with 112 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <AK/IDAllocator.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuItem.h>
@ -217,4 +218,28 @@ void Menu::realize_menu_item(MenuItem& item, int item_id)
}
}
ErrorOr<void> Menu::add_recent_files_list(Function<void(Action&)> callback)
{
m_recent_files_callback = move(callback);
Vector<NonnullRefPtr<GUI::Action>> recent_file_actions;
for (size_t i = 0; i < GUI::Application::max_recently_open_files(); ++i) {
recent_file_actions.append(GUI::Action::create("", [&](auto& action) { m_recent_files_callback(action); }));
}
recent_file_actions.append(GUI::Action::create("(No recently open files)", nullptr));
recent_file_actions.last()->set_enabled(false);
auto* app = GUI::Application::the();
app->register_recent_file_actions({}, recent_file_actions);
for (auto& action : recent_file_actions) {
TRY(try_add_action(action));
}
TRY(try_add_separator());
return {};
}
}