MonitorWidget.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/Font.h>
  12. #include <LibThreading/BackgroundAction.h>
  13. REGISTER_WIDGET(DisplaySettings, MonitorWidget)
  14. namespace DisplaySettings {
  15. MonitorWidget::MonitorWidget()
  16. {
  17. m_desktop_resolution = GUI::Desktop::the().rect().size();
  18. m_monitor_bitmap = Gfx::Bitmap::load_from_file("/res/graphics/monitor.png"sv).release_value_but_fixme_should_propagate_errors();
  19. m_desktop_bitmap = Gfx::Bitmap::create(m_monitor_bitmap->format(), { 280, 158 }).release_value_but_fixme_should_propagate_errors();
  20. m_monitor_rect = { { 12, 13 }, m_desktop_bitmap->size() };
  21. set_fixed_size(304, 201);
  22. }
  23. bool MonitorWidget::set_wallpaper(DeprecatedString path)
  24. {
  25. if (!is_different_to_current_wallpaper_path(path))
  26. return false;
  27. (void)Threading::BackgroundAction<NonnullRefPtr<Gfx::Bitmap>>::construct(
  28. [path](auto&) -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
  29. if (path.is_empty())
  30. return Error::from_errno(ENOENT);
  31. return Gfx::Bitmap::load_from_file(path);
  32. },
  33. [this, path](NonnullRefPtr<Gfx::Bitmap> bitmap) -> ErrorOr<void> {
  34. // If we've been requested to change while we were loading the bitmap, don't bother spending the cost to
  35. // move and render the now stale bitmap.
  36. if (is_different_to_current_wallpaper_path(path))
  37. return {};
  38. m_wallpaper_bitmap = move(bitmap);
  39. m_desktop_dirty = true;
  40. update();
  41. return {};
  42. },
  43. [this, path](Error) -> void {
  44. m_wallpaper_bitmap = nullptr;
  45. });
  46. if (path.is_empty())
  47. m_desktop_wallpaper_path = nullptr;
  48. else
  49. m_desktop_wallpaper_path = move(path);
  50. return true;
  51. }
  52. StringView MonitorWidget::wallpaper() const
  53. {
  54. return m_desktop_wallpaper_path;
  55. }
  56. void MonitorWidget::set_wallpaper_mode(DeprecatedString mode)
  57. {
  58. if (m_desktop_wallpaper_mode == mode)
  59. return;
  60. m_desktop_wallpaper_mode = move(mode);
  61. m_desktop_dirty = true;
  62. update();
  63. }
  64. StringView MonitorWidget::wallpaper_mode() const
  65. {
  66. return m_desktop_wallpaper_mode;
  67. }
  68. void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
  69. {
  70. if (m_desktop_resolution == resolution)
  71. return;
  72. m_desktop_resolution = resolution;
  73. m_desktop_dirty = true;
  74. update();
  75. }
  76. Gfx::IntSize MonitorWidget::desktop_resolution()
  77. {
  78. return m_desktop_resolution;
  79. }
  80. void MonitorWidget::set_background_color(Gfx::Color color)
  81. {
  82. if (m_desktop_color == color)
  83. return;
  84. m_desktop_color = color;
  85. m_desktop_dirty = true;
  86. update();
  87. }
  88. Gfx::Color MonitorWidget::background_color()
  89. {
  90. return m_desktop_color;
  91. }
  92. void MonitorWidget::redraw_desktop_if_needed()
  93. {
  94. if (!m_desktop_dirty)
  95. return;
  96. m_desktop_dirty = false;
  97. GUI::Painter painter(*m_desktop_bitmap);
  98. painter.fill_rect(m_desktop_bitmap->rect(), m_desktop_color);
  99. if (!m_wallpaper_bitmap)
  100. return;
  101. float sw = (float)m_desktop_bitmap->width() / (float)m_desktop_resolution.width();
  102. float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();
  103. auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled_by(sw, sh).to_type<int>();
  104. auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh).release_value_but_fixme_should_propagate_errors();
  105. if (m_desktop_wallpaper_mode == "Center") {
  106. auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());
  107. painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect());
  108. } else if (m_desktop_wallpaper_mode == "Tile") {
  109. painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
  110. } else if (m_desktop_wallpaper_mode == "Stretch") {
  111. painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect());
  112. } else {
  113. VERIFY_NOT_REACHED();
  114. }
  115. }
  116. void MonitorWidget::paint_event(GUI::PaintEvent& event)
  117. {
  118. redraw_desktop_if_needed();
  119. GUI::Painter painter(*this);
  120. painter.add_clip_rect(event.rect());
  121. painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
  122. painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
  123. #if 0
  124. if (!m_desktop_resolution.is_null()) {
  125. auto displayed_resolution_string = Gfx::IntSize { m_desktop_resolution.width(), m_desktop_resolution.height() }.to_deprecated_string();
  126. // Render text label scaled with scale factor to hint at its effect.
  127. // FIXME: Once bitmaps have intrinsic scale factors, we could create a bitmap with an intrinsic scale factor of m_desktop_scale_factor
  128. // and that should give us the same effect with less code.
  129. auto text_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize { painter.font().width(displayed_resolution_string) + 1, painter.font().pixel_size_rounded_up() + 1 });
  130. GUI::Painter text_painter(*text_bitmap);
  131. text_painter.set_font(painter.font());
  132. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::BottomRight, Color::Black);
  133. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::TopLeft, Color::White);
  134. Gfx::IntRect text_rect = text_bitmap->rect();
  135. text_rect.set_width(text_rect.width() * m_desktop_scale_factor);
  136. text_rect.set_height(text_rect.height() * m_desktop_scale_factor);
  137. text_rect.center_within(m_monitor_rect);
  138. painter.draw_scaled_bitmap(text_rect, *text_bitmap, text_bitmap->rect());
  139. }
  140. #endif
  141. }
  142. }