MonitorWidget.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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::try_load_from_file("/res/graphics/monitor.png").release_value_but_fixme_should_propagate_errors();
  19. m_desktop_bitmap = Gfx::Bitmap::try_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(String path)
  24. {
  25. if (!is_different_to_current_wallpaper_path(path))
  26. return false;
  27. (void)Threading::BackgroundAction<ErrorOr<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::try_load_from_file(path);
  32. },
  33. [this, path](ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap_or_error) {
  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. if (bitmap_or_error.is_error())
  39. m_wallpaper_bitmap = nullptr;
  40. else
  41. m_wallpaper_bitmap = bitmap_or_error.release_value();
  42. m_desktop_dirty = true;
  43. update();
  44. });
  45. if (path.is_empty())
  46. m_desktop_wallpaper_path = nullptr;
  47. else
  48. m_desktop_wallpaper_path = move(path);
  49. return true;
  50. }
  51. StringView MonitorWidget::wallpaper() const
  52. {
  53. return m_desktop_wallpaper_path;
  54. }
  55. void MonitorWidget::set_wallpaper_mode(String mode)
  56. {
  57. if (m_desktop_wallpaper_mode == mode)
  58. return;
  59. m_desktop_wallpaper_mode = move(mode);
  60. m_desktop_dirty = true;
  61. update();
  62. }
  63. StringView MonitorWidget::wallpaper_mode() const
  64. {
  65. return m_desktop_wallpaper_mode;
  66. }
  67. void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
  68. {
  69. if (m_desktop_resolution == resolution)
  70. return;
  71. m_desktop_resolution = resolution;
  72. m_desktop_dirty = true;
  73. update();
  74. }
  75. Gfx::IntSize MonitorWidget::desktop_resolution()
  76. {
  77. return m_desktop_resolution;
  78. }
  79. void MonitorWidget::set_background_color(Gfx::Color color)
  80. {
  81. if (m_desktop_color == color)
  82. return;
  83. m_desktop_color = color;
  84. m_desktop_dirty = true;
  85. update();
  86. }
  87. Gfx::Color MonitorWidget::background_color()
  88. {
  89. return m_desktop_color;
  90. }
  91. void MonitorWidget::redraw_desktop_if_needed()
  92. {
  93. if (!m_desktop_dirty)
  94. return;
  95. m_desktop_dirty = false;
  96. GUI::Painter painter(*m_desktop_bitmap);
  97. painter.fill_rect(m_desktop_bitmap->rect(), m_desktop_color);
  98. if (!m_wallpaper_bitmap)
  99. return;
  100. float sw = (float)m_desktop_bitmap->width() / (float)m_desktop_resolution.width();
  101. float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();
  102. auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled_by(sw, sh).to_type<int>();
  103. auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh).release_value_but_fixme_should_propagate_errors();
  104. if (m_desktop_wallpaper_mode == "center") {
  105. auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());
  106. painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect());
  107. } else if (m_desktop_wallpaper_mode == "tile") {
  108. painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
  109. } else if (m_desktop_wallpaper_mode == "stretch") {
  110. painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect());
  111. } else {
  112. VERIFY_NOT_REACHED();
  113. }
  114. }
  115. void MonitorWidget::paint_event(GUI::PaintEvent& event)
  116. {
  117. redraw_desktop_if_needed();
  118. GUI::Painter painter(*this);
  119. painter.add_clip_rect(event.rect());
  120. painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
  121. painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
  122. #if 0
  123. if (!m_desktop_resolution.is_null()) {
  124. auto displayed_resolution_string = Gfx::IntSize { m_desktop_resolution.width(), m_desktop_resolution.height() }.to_string();
  125. // Render text label scaled with scale factor to hint at its effect.
  126. // FIXME: Once bitmaps have intrinsic scale factors, we could create a bitmap with an intrinsic scale factor of m_desktop_scale_factor
  127. // and that should give us the same effect with less code.
  128. auto text_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize { painter.font().width(displayed_resolution_string) + 1, painter.font().glyph_height() + 1 });
  129. GUI::Painter text_painter(*text_bitmap);
  130. text_painter.set_font(painter.font());
  131. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::BottomRight, Color::Black);
  132. text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::TopLeft, Color::White);
  133. Gfx::IntRect text_rect = text_bitmap->rect();
  134. text_rect.set_width(text_rect.width() * m_desktop_scale_factor);
  135. text_rect.set_height(text_rect.height() * m_desktop_scale_factor);
  136. text_rect.center_within(m_monitor_rect);
  137. painter.draw_scaled_bitmap(text_rect, *text_bitmap, text_bitmap->rect());
  138. }
  139. #endif
  140. }
  141. }