main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2020-2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Application.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. #include <stdio.h>
  33. class UserNameWidget final : public GUI::Widget {
  34. C_OBJECT(UserNameWidget)
  35. public:
  36. UserNameWidget()
  37. : GUI::Widget(nullptr)
  38. {
  39. m_username = getlogin();
  40. m_username_width = Gfx::Font::default_bold_font().width(m_username);
  41. }
  42. virtual ~UserNameWidget() override {}
  43. int get_width()
  44. {
  45. return m_username_width + menubar_menu_margin();
  46. }
  47. private:
  48. static int menubar_menu_margin() { return 4; }
  49. virtual void paint_event(GUI::PaintEvent& event) override
  50. {
  51. GUI::Painter painter(*this);
  52. painter.fill_rect(event.rect(), palette().window());
  53. painter.draw_text(event.rect(), m_username, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().window_text());
  54. }
  55. String m_username;
  56. int m_username_width;
  57. };
  58. int main(int argc, char** argv)
  59. {
  60. if (pledge("stdio shared_buffer rpath cpath unix fattr", nullptr) < 0) {
  61. perror("pledge");
  62. return 1;
  63. }
  64. if (unveil("/res", "r") < 0) {
  65. perror("unveil");
  66. return 1;
  67. }
  68. if (unveil("/tmp", "rwc") < 0) {
  69. perror("unveil");
  70. return 1;
  71. }
  72. if (unveil("/etc/passwd", "r") < 0) {
  73. perror("unveil");
  74. return 1;
  75. }
  76. unveil(nullptr, nullptr);
  77. GUI::Application app(argc, argv);
  78. auto window = GUI::Window::construct();
  79. window->set_title("UserName");
  80. window->set_window_type(GUI::WindowType::MenuApplet);
  81. auto widget = UserNameWidget::construct();
  82. window->resize(widget->get_width(), 16);
  83. window->set_main_widget(widget);
  84. window->show();
  85. if (pledge("stdio shared_buffer rpath", nullptr) < 0) {
  86. perror("pledge");
  87. return 1;
  88. }
  89. return app.exec();
  90. }