Calendar: Port to LibMain and TRY all the things

This commit is contained in:
Rummskartoffel 2022-01-04 18:14:56 +01:00 committed by Idan Horowitz
parent d17dca5c91
commit 902b8f1583
Notes: sideshowbarker 2024-07-17 21:40:59 +09:00
2 changed files with 40 additions and 48 deletions

View File

@ -12,4 +12,4 @@ set(SOURCES
)
serenity_app(Calendar ICON app-calendar)
target_link_libraries(Calendar LibGUI)
target_link_libraries(Calendar LibGUI LibMain)

View File

@ -6,6 +6,7 @@
#include "AddEventDialog.h"
#include <Applications/Calendar/CalendarWindowGML.h>
#include <LibCore/System.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
@ -17,43 +18,32 @@
#include <LibGUI/Menubar.h>
#include <LibGUI/Toolbar.h>
#include <LibGUI/Window.h>
#include <unistd.h>
#include <LibMain/Main.h>
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix", nullptr));
auto app = GUI::Application::construct(argc, argv);
auto app = TRY(GUI::Application::try_create(arguments));
if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio recvfd sendfd rpath", nullptr));
TRY(Core::System::unveil("/res", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
auto app_icon = GUI::Icon::default_icon("app-calendar");
auto window = GUI::Window::construct();
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-calendar"));
auto window = TRY(GUI::Window::try_create());
window->set_title("Calendar");
window->resize(600, 480);
window->set_minimum_size(171, 141);
window->set_icon(app_icon.bitmap_for_size(16));
auto& main_widget = window->set_main_widget<GUI::Widget>();
main_widget.load_from_gml(calendar_window_gml);
auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
main_widget->load_from_gml(calendar_window_gml);
auto toolbar = main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar");
auto calendar = main_widget.find_descendant_of_type_named<GUI::Calendar>("calendar");
auto toolbar = main_widget->find_descendant_of_type_named<GUI::Toolbar>("toolbar");
auto calendar = main_widget->find_descendant_of_type_named<GUI::Calendar>("calendar");
auto prev_date_action = GUI::Action::create({}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto prev_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png")), [&](const GUI::Action&) {
unsigned view_month = calendar->view_month();
unsigned view_year = calendar->view_year();
if (calendar->mode() == GUI::Calendar::Month) {
@ -68,7 +58,7 @@ int main(int argc, char** argv)
calendar->update_tiles(view_year, view_month);
});
auto next_date_action = GUI::Action::create({}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto next_date_action = GUI::Action::create({}, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png")), [&](const GUI::Action&) {
unsigned view_month = calendar->view_month();
unsigned view_year = calendar->view_year();
if (calendar->mode() == GUI::Calendar::Month) {
@ -83,22 +73,22 @@ int main(int argc, char** argv)
calendar->update_tiles(view_year, view_month);
});
auto add_event_action = GUI::Action::create("&Add Event", {}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/add-event.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto add_event_action = GUI::Action::create("&Add Event", {}, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/add-event.png")), [&](const GUI::Action&) {
AddEventDialog::show(calendar->selected_date(), window);
});
auto jump_to_action = GUI::Action::create("Jump to &Today", {}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-date.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto jump_to_action = GUI::Action::create("Jump to &Today", {}, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-date.png")), [&](const GUI::Action&) {
calendar->set_selected_date(Core::DateTime::now());
calendar->update_tiles(Core::DateTime::now().year(), Core::DateTime::now().month());
});
auto view_month_action = GUI::Action::create_checkable("&Month View", { Mod_Ctrl, KeyCode::Key_1 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-month-view.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto view_month_action = GUI::Action::create_checkable("&Month View", { Mod_Ctrl, KeyCode::Key_1 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/calendar-month-view.png")), [&](const GUI::Action&) {
if (calendar->mode() == GUI::Calendar::Year)
calendar->toggle_mode();
});
view_month_action->set_checked(true);
auto view_year_action = GUI::Action::create_checkable("&Year View", { Mod_Ctrl, KeyCode::Key_2 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/icon-view.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
auto view_year_action = GUI::Action::create_checkable("&Year View", { Mod_Ctrl, KeyCode::Key_2 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/icon-view.png")), [&](const GUI::Action&) {
if (calendar->mode() == GUI::Calendar::Month)
calendar->toggle_mode();
});
@ -108,14 +98,14 @@ int main(int argc, char** argv)
view_type_action_group->add_action(*view_month_action);
view_type_action_group->add_action(*view_year_action);
toolbar->add_action(prev_date_action);
toolbar->add_action(next_date_action);
toolbar->add_separator();
toolbar->add_action(jump_to_action);
toolbar->add_action(add_event_action);
toolbar->add_separator();
toolbar->add_action(view_month_action);
toolbar->add_action(view_year_action);
(void)TRY(toolbar->try_add_action(prev_date_action));
(void)TRY(toolbar->try_add_action(next_date_action));
TRY(toolbar->try_add_separator());
(void)TRY(toolbar->try_add_action(jump_to_action));
(void)TRY(toolbar->try_add_action(add_event_action));
TRY(toolbar->try_add_separator());
(void)TRY(toolbar->try_add_action(view_month_action));
(void)TRY(toolbar->try_add_action(view_year_action));
calendar->on_tile_doubleclick = [&] {
AddEventDialog::show(calendar->selected_date(), window);
@ -126,24 +116,26 @@ int main(int argc, char** argv)
};
auto& file_menu = window->add_menu("&File");
file_menu.add_action(GUI::Action::create("&Add Event", { Mod_Ctrl | Mod_Shift, Key_E }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/add-event.png").release_value_but_fixme_should_propagate_errors(),
file_menu.add_action(GUI::Action::create("&Add Event", { Mod_Ctrl | Mod_Shift, Key_E }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/add-event.png")),
[&](const GUI::Action&) {
AddEventDialog::show(calendar->selected_date(), window);
}));
file_menu.add_separator();
TRY(file_menu.try_add_separator());
file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
TRY(file_menu.try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
}));
})));
auto& view_menu = window->add_menu("&View");
view_menu.add_action(*view_month_action);
view_menu.add_action(*view_year_action);
auto view_menu = TRY(window->try_add_menu("&View"));
TRY(view_menu->try_add_action(*view_month_action));
TRY(view_menu->try_add_action(*view_year_action));
auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Calendar", app_icon, window));
auto help_menu = TRY(window->try_add_menu("&Help"));
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Calendar", app_icon, window)));
window->show();
app->exec();
return 0;
}