main.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CatDog.h"
  7. #include "SpeechBubble.h"
  8. #include <LibCore/System.h>
  9. #include <LibCore/Timer.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Icon.h>
  14. #include <LibGUI/Menu.h>
  15. #include <LibGUI/Menubar.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibMain/Main.h>
  18. ErrorOr<int> serenity_main(Main::Arguments arguments)
  19. {
  20. TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix"));
  21. auto app = TRY(GUI::Application::create(arguments));
  22. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-catdog"sv));
  23. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  24. TRY(Core::System::unveil("/res", "r"));
  25. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  26. // FIXME: For some reason, this is needed in the /sys/kernel/processes shenanigans.
  27. TRY(Core::System::unveil("/etc/passwd", "r"));
  28. TRY(Core::System::unveil(nullptr, nullptr));
  29. auto window = TRY(GUI::Window::try_create());
  30. window->set_title("CatDog Demo");
  31. window->resize(32, 32);
  32. window->set_frameless(true);
  33. window->set_resizable(false);
  34. window->set_has_alpha_channel(true);
  35. window->set_alpha_hit_threshold(1.0f);
  36. window->set_icon(app_icon.bitmap_for_size(16));
  37. auto catdog_widget = TRY(CatDog::create());
  38. window->set_main_widget(catdog_widget);
  39. catdog_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 0);
  40. auto context_menu = TRY(GUI::Menu::try_create());
  41. context_menu->add_action(GUI::CommonActions::make_about_action("CatDog Demo"_string, app_icon, window));
  42. context_menu->add_separator();
  43. context_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  44. window->show();
  45. window->set_always_on_top();
  46. catdog_widget->start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);
  47. auto advice_window = TRY(GUI::Window::try_create());
  48. advice_window->set_title("CatDog Advice");
  49. advice_window->resize(225, 50);
  50. advice_window->set_frameless(true);
  51. advice_window->set_resizable(false);
  52. advice_window->set_has_alpha_channel(true);
  53. advice_window->set_alpha_hit_threshold(1.0f);
  54. auto advice_widget = TRY(advice_window->set_main_widget<SpeechBubble>(catdog_widget));
  55. advice_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 0);
  56. auto advice_timer = TRY(Core::Timer::create_single_shot(15'000, [&] {
  57. window->move_to_front();
  58. advice_window->move_to_front();
  59. catdog_widget->set_roaming(false);
  60. advice_window->move_to(window->x() - advice_window->width() / 2, window->y() - advice_window->height());
  61. advice_window->show();
  62. advice_window->set_always_on_top();
  63. }));
  64. advice_timer->start();
  65. advice_widget->on_dismiss = [&] {
  66. catdog_widget->set_roaming(true);
  67. advice_window->hide();
  68. advice_timer->start();
  69. };
  70. // Let users toggle the advice functionality by clicking on catdog.
  71. catdog_widget->on_click = [&] {
  72. if (advice_timer->is_active())
  73. advice_timer->stop();
  74. else
  75. advice_timer->start();
  76. };
  77. catdog_widget->on_context_menu_request = [&](GUI::ContextMenuEvent& event) {
  78. if (catdog_widget->rect().contains(event.position()))
  79. context_menu->popup(event.screen_position());
  80. };
  81. return app->exec();
  82. }