mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:42:43 +00:00 
			
		
		
		
	 1a97382305
			
		
	
	
		1a97382305
		
	
	
	
	
		
			
			The pattern to construct `Application` was to use the `try_create` method from the `C_OBJECT` macro. While being safe from an OOM perspective, this method doesn't propagate errors from the constructor. This patch make `Application` use the `C_OBJECT_ABSTRACT` and manually define a `create` method that can bubble up errors from the construction stage. This commit also removes the ability to use `argc` and `argv` to create an `Application`, only `Main`'s `Arguments` can be used. From a user point of view, the patch renames `try_create` => `create`, hence the huge number of modified files.
		
			
				
	
	
		
			106 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			106 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/QuickSort.h>
 | |
| #include <LibCore/System.h>
 | |
| #include <LibDesktop/AppFile.h>
 | |
| #include <LibGUI/Application.h>
 | |
| #include <LibGUI/BoxLayout.h>
 | |
| #include <LibGUI/Icon.h>
 | |
| #include <LibGUI/IconView.h>
 | |
| #include <LibGUI/Menu.h>
 | |
| #include <LibGUI/Model.h>
 | |
| #include <LibGUI/Process.h>
 | |
| #include <LibGUI/Window.h>
 | |
| #include <LibMain/Main.h>
 | |
| 
 | |
| class ScreensaverAppsModel final : public GUI::Model {
 | |
| public:
 | |
|     ScreensaverAppsModel()
 | |
|     {
 | |
|         Desktop::AppFile::for_each([&](Desktop::AppFile& app_file) {
 | |
|             if (app_file.category() != "Demos/Screensaver"sv)
 | |
|                 return;
 | |
|             m_apps.append(app_file);
 | |
|         });
 | |
| 
 | |
|         quick_sort(m_apps, [](auto& a, auto& b) { return a->name() < b->name(); });
 | |
|     }
 | |
| 
 | |
|     virtual int row_count(GUI::ModelIndex const&) const override { return static_cast<int>(m_apps.size()); }
 | |
|     virtual int column_count(GUI::ModelIndex const&) const override { return 1; }
 | |
| 
 | |
|     virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const&) const override
 | |
|     {
 | |
|         if (row < 0 || row >= static_cast<int>(m_apps.size()))
 | |
|             return {};
 | |
|         return create_index(row, column, &m_apps[row]);
 | |
|     }
 | |
| 
 | |
|     virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
 | |
|     {
 | |
|         auto const& app = m_apps[index.row()];
 | |
|         if (role == GUI::ModelRole::Icon)
 | |
|             return app->icon();
 | |
| 
 | |
|         if (role == GUI::ModelRole::Display) {
 | |
|             if (app->name().ends_with(" Screensaver"sv))
 | |
|                 return app->name().substring(0, app->name().length() - " Screensaver"sv.length());
 | |
|             return app->name();
 | |
|         }
 | |
| 
 | |
|         if (role == GUI::ModelRole::Custom)
 | |
|             return app->executable();
 | |
| 
 | |
|         return {};
 | |
|     }
 | |
| 
 | |
| private:
 | |
|     Vector<NonnullRefPtr<Desktop::AppFile>> m_apps;
 | |
| };
 | |
| 
 | |
| ErrorOr<int> serenity_main(Main::Arguments arguments)
 | |
| {
 | |
|     TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath unix proc exec"));
 | |
| 
 | |
|     auto app = TRY(GUI::Application::create(arguments));
 | |
| 
 | |
|     TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath proc exec"));
 | |
| 
 | |
|     auto app_icon = GUI::Icon::default_icon("app-screensaver"sv);
 | |
| 
 | |
|     auto window = TRY(GUI::Window::try_create());
 | |
|     window->set_title("Screensaver");
 | |
|     window->resize(360, 240);
 | |
| 
 | |
|     auto file_menu = TRY(window->try_add_menu("&File"_short_string));
 | |
|     file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
 | |
|         app->quit();
 | |
|     }));
 | |
| 
 | |
|     auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
 | |
|     TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
 | |
|     TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Screensaver", app_icon, window)));
 | |
| 
 | |
|     auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
 | |
|     main_widget->set_fill_with_background_color(true);
 | |
|     main_widget->set_layout<GUI::VerticalBoxLayout>();
 | |
| 
 | |
|     auto icon_view = TRY(main_widget->try_add<GUI::IconView>());
 | |
|     icon_view->set_should_hide_unnecessary_scrollbars(true);
 | |
|     auto model = adopt_ref(*new ScreensaverAppsModel);
 | |
|     icon_view->set_model(*model);
 | |
| 
 | |
|     icon_view->on_activation = [&](GUI::ModelIndex const& index) {
 | |
|         auto executable = model->data(index, GUI::ModelRole::Custom).as_string();
 | |
|         GUI::Process::spawn_or_show_error(window, executable);
 | |
|     };
 | |
| 
 | |
|     window->set_icon(app_icon.bitmap_for_size(16));
 | |
|     window->show();
 | |
| 
 | |
|     return app->exec();
 | |
| }
 |