MonitorWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MonitorWidget.h"
  8. #include <LibGUI/Desktop.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/Font.h>
  12. REGISTER_WIDGET(DisplaySettings, MonitorWidget)
  13. namespace DisplaySettings {
  14. MonitorWidget::MonitorWidget()
  15. {
  16. m_desktop_resolution = GUI::Desktop::the().rect().size();
  17. m_monitor_bitmap = Gfx::Bitmap::load_from_file("/res/graphics/monitor.png");
  18. m_monitor_rect = { 12, 13, 280, 158 };
  19. set_fixed_size(304, 201);
  20. }
  21. bool MonitorWidget::set_wallpaper(String path)
  22. {
  23. auto bitmap_ptr = Gfx::Bitmap::load_from_file(path);
  24. if (!bitmap_ptr && !path.is_empty())
  25. return false;
  26. m_desktop_wallpaper_path = move(path);
  27. m_desktop_wallpaper_bitmap = bitmap_ptr;
  28. update();
  29. return true;
  30. }
  31. String MonitorWidget::wallpaper()
  32. {
  33. return m_desktop_wallpaper_path;
  34. }
  35. void MonitorWidget::set_wallpaper_mode(String mode)
  36. {
  37. if (m_desktop_wallpaper_mode == mode)
  38. return;
  39. m_desktop_wallpaper_mode = move(mode);
  40. update();
  41. }
  42. String MonitorWidget::wallpaper_mode()
  43. {
  44. return m_desktop_wallpaper_mode;
  45. }
  46. void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
  47. {
  48. m_desktop_resolution = resolution;
  49. }
  50. Gfx::IntSize MonitorWidget::desktop_resolution()
  51. {
  52. return m_desktop_resolution;
  53. }
  54. void MonitorWidget::set_background_color(Gfx::Color color)
  55. {
  56. if (m_desktop_color == color)
  57. return;
  58. m_desktop_color = color;
  59. update();
  60. }
  61. Gfx::Color MonitorWidget::background_color()
  62. {
  63. return m_desktop_color;
  64. }
  65. void MonitorWidget::paint_event(GUI::PaintEvent& event)
  66. {
  67. Gfx::IntRect screen_rect = { { 0, 0 }, m_desktop_resolution };
  68. auto screen_bitmap = Gfx::Bitmap::create(m_monitor_bitmap->format(), m_desktop_resolution);
  69. GUI::Painter screen_painter(*screen_bitmap);
  70. screen_painter.fill_rect(screen_rect, m_desktop_color);
  71. if (!m_desktop_wallpaper_bitmap.is_null()) {
  72. if (m_desktop_wallpaper_mode == "simple") {
  73. screen_painter.blit({ 0, 0 }, *m_desktop_wallpaper_bitmap, m_desktop_wallpaper_bitmap->rect());
  74. } else if (m_desktop_wallpaper_mode == "center") {
  75. Gfx::IntPoint offset { (screen_rect.width() - m_desktop_wallpaper_bitmap->width()) / 2, (screen_rect.height() - m_desktop_wallpaper_bitmap->height()) / 2 };
  76. screen_painter.blit_offset(screen_rect.location(), *m_desktop_wallpaper_bitmap, screen_rect, offset);
  77. } else if (m_desktop_wallpaper_mode == "tile") {
  78. screen_painter.draw_tiled_bitmap(screen_bitmap->rect(), *m_desktop_wallpaper_bitmap);
  79. } else if (m_desktop_wallpaper_mode == "stretch") {
  80. screen_painter.draw_scaled_bitmap(screen_bitmap->rect(), *m_desktop_wallpaper_bitmap, m_desktop_wallpaper_bitmap->rect());
  81. } else {
  82. VERIFY_NOT_REACHED();
  83. }
  84. }
  85. GUI::Painter painter(*this);
  86. painter.add_clip_rect(event.rect());
  87. painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
  88. painter.draw_scaled_bitmap(m_monitor_rect, *screen_bitmap, screen_bitmap->rect());
  89. #if 0
  90. if (!m_desktop_resolution.is_null()) {
  91. auto displayed_resolution_string = Gfx::IntSize { m_desktop_resolution.width(), m_desktop_resolution.height() }.to_string();
  92. // Render text label scaled with scale factor to hint at its effect.
  93. // FIXME: Once bitmaps have intrinsic scale factors, we could create a bitmap with an intrinsic scale factor of m_desktop_scale_factor
  94. // and that should give us the same effect with less code.
  95. auto text_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize { painter.font().width(displayed_resolution_string) + 1, painter.font().glyph_height() + 1 });
  96. GUI::Painter text_painter(*text_bitmap);
  97. text_painter.set_font(painter.font());
  98. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::BottomRight, Color::Black);
  99. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::TopLeft, Color::White);
  100. Gfx::IntRect text_rect = text_bitmap->rect();
  101. text_rect.set_width(text_rect.width() * m_desktop_scale_factor);
  102. text_rect.set_height(text_rect.height() * m_desktop_scale_factor);
  103. text_rect.center_within(m_monitor_rect);
  104. painter.draw_scaled_bitmap(text_rect, *text_bitmap, text_bitmap->rect());
  105. }
  106. #endif
  107. }
  108. }