MonitorWidget.cpp 4.1 KB

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