2020-03-29 13:01:55 -03:00
|
|
|
|
/*
|
2020-12-25 08:15:16 -03:00
|
|
|
|
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
|
2021-05-19 16:19:09 -03:00
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-03-29 13:01:55 -03:00
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-29 13:01:55 -03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "MonitorWidget.h"
|
2021-05-19 16:19:09 -03:00
|
|
|
|
#include <LibGUI/Desktop.h>
|
2020-03-29 13:01:55 -03:00
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-29 22:44:03 -03:00
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2022-04-09 04:28:38 -03:00
|
|
|
|
#include <LibGfx/Font/Font.h>
|
2021-08-17 09:02:29 -03:00
|
|
|
|
#include <LibThreading/BackgroundAction.h>
|
2020-12-29 22:44:03 -03:00
|
|
|
|
|
2021-05-19 16:19:09 -03:00
|
|
|
|
REGISTER_WIDGET(DisplaySettings, MonitorWidget)
|
|
|
|
|
|
|
2020-12-29 22:44:03 -03:00
|
|
|
|
namespace DisplaySettings {
|
2020-03-29 13:01:55 -03:00
|
|
|
|
|
|
|
|
|
|
MonitorWidget::MonitorWidget()
|
|
|
|
|
|
{
|
2021-05-19 16:19:09 -03:00
|
|
|
|
m_desktop_resolution = GUI::Desktop::the().rect().size();
|
2022-07-11 14:32:29 -03:00
|
|
|
|
m_monitor_bitmap = Gfx::Bitmap::try_load_from_file("/res/graphics/monitor.png"sv).release_value_but_fixme_should_propagate_errors();
|
2021-11-06 15:30:59 -03:00
|
|
|
|
m_desktop_bitmap = Gfx::Bitmap::try_create(m_monitor_bitmap->format(), { 280, 158 }).release_value_but_fixme_should_propagate_errors();
|
2021-05-20 12:43:33 -03:00
|
|
|
|
m_monitor_rect = { { 12, 13 }, m_desktop_bitmap->size() };
|
2021-05-18 17:06:32 -03:00
|
|
|
|
set_fixed_size(304, 201);
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-27 06:04:43 -03:00
|
|
|
|
bool MonitorWidget::set_wallpaper(String path)
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
2021-08-17 09:02:29 -03:00
|
|
|
|
if (!is_different_to_current_wallpaper_path(path))
|
2021-05-20 12:43:33 -03:00
|
|
|
|
return false;
|
2021-05-21 13:27:12 -03:00
|
|
|
|
|
2021-12-02 08:47:21 -03:00
|
|
|
|
(void)Threading::BackgroundAction<ErrorOr<NonnullRefPtr<Gfx::Bitmap>>>::construct(
|
2021-11-14 17:25:59 -03:00
|
|
|
|
[path](auto&) -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
|
|
|
|
|
|
if (path.is_empty())
|
|
|
|
|
|
return Error::from_errno(ENOENT);
|
|
|
|
|
|
return Gfx::Bitmap::try_load_from_file(path);
|
2021-08-17 09:02:29 -03:00
|
|
|
|
},
|
|
|
|
|
|
|
2021-11-14 17:25:59 -03:00
|
|
|
|
[this, path](ErrorOr<NonnullRefPtr<Gfx::Bitmap>> bitmap_or_error) {
|
2021-08-17 09:02:29 -03:00
|
|
|
|
// If we've been requested to change while we were loading the bitmap, don't bother spending the cost to
|
|
|
|
|
|
// move and render the now stale bitmap.
|
|
|
|
|
|
if (is_different_to_current_wallpaper_path(path))
|
|
|
|
|
|
return;
|
2021-11-14 17:25:59 -03:00
|
|
|
|
if (bitmap_or_error.is_error())
|
2021-08-17 09:02:29 -03:00
|
|
|
|
m_wallpaper_bitmap = nullptr;
|
2021-11-14 17:25:59 -03:00
|
|
|
|
else
|
|
|
|
|
|
m_wallpaper_bitmap = bitmap_or_error.release_value();
|
2021-08-17 09:02:29 -03:00
|
|
|
|
m_desktop_dirty = true;
|
|
|
|
|
|
update();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (path.is_empty())
|
2021-05-29 08:34:36 -03:00
|
|
|
|
m_desktop_wallpaper_path = nullptr;
|
2021-08-17 09:02:29 -03:00
|
|
|
|
else
|
|
|
|
|
|
m_desktop_wallpaper_path = move(path);
|
2021-05-29 08:34:36 -03:00
|
|
|
|
|
2020-06-27 06:04:43 -03:00
|
|
|
|
return true;
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-13 15:00:57 -03:00
|
|
|
|
StringView MonitorWidget::wallpaper() const
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
|
|
|
|
|
return m_desktop_wallpaper_path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorWidget::set_wallpaper_mode(String mode)
|
|
|
|
|
|
{
|
2021-05-19 16:50:51 -03:00
|
|
|
|
if (m_desktop_wallpaper_mode == mode)
|
|
|
|
|
|
return;
|
|
|
|
|
|
m_desktop_wallpaper_mode = move(mode);
|
2021-05-20 12:43:33 -03:00
|
|
|
|
m_desktop_dirty = true;
|
2021-05-19 16:50:51 -03:00
|
|
|
|
update();
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-13 15:00:57 -03:00
|
|
|
|
StringView MonitorWidget::wallpaper_mode() const
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
|
|
|
|
|
return m_desktop_wallpaper_mode;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
|
void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
2021-05-20 12:43:33 -03:00
|
|
|
|
if (m_desktop_resolution == resolution)
|
|
|
|
|
|
return;
|
2020-03-29 13:01:55 -03:00
|
|
|
|
m_desktop_resolution = resolution;
|
2021-05-20 12:43:33 -03:00
|
|
|
|
m_desktop_dirty = true;
|
|
|
|
|
|
update();
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
|
Gfx::IntSize MonitorWidget::desktop_resolution()
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
|
|
|
|
|
return m_desktop_resolution;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorWidget::set_background_color(Gfx::Color color)
|
|
|
|
|
|
{
|
2021-05-19 16:50:51 -03:00
|
|
|
|
if (m_desktop_color == color)
|
|
|
|
|
|
return;
|
2020-03-29 13:01:55 -03:00
|
|
|
|
m_desktop_color = color;
|
2021-05-20 12:43:33 -03:00
|
|
|
|
m_desktop_dirty = true;
|
2021-05-19 16:50:51 -03:00
|
|
|
|
update();
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Gfx::Color MonitorWidget::background_color()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_desktop_color;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-20 12:43:33 -03:00
|
|
|
|
void MonitorWidget::redraw_desktop_if_needed()
|
2020-03-29 13:01:55 -03:00
|
|
|
|
{
|
2021-05-20 12:43:33 -03:00
|
|
|
|
if (!m_desktop_dirty)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_desktop_dirty = false;
|
|
|
|
|
|
|
|
|
|
|
|
GUI::Painter painter(*m_desktop_bitmap);
|
|
|
|
|
|
painter.fill_rect(m_desktop_bitmap->rect(), m_desktop_color);
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_wallpaper_bitmap)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
float sw = (float)m_desktop_bitmap->width() / (float)m_desktop_resolution.width();
|
|
|
|
|
|
float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();
|
|
|
|
|
|
|
|
|
|
|
|
auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled_by(sw, sh).to_type<int>();
|
2021-11-06 08:26:04 -03:00
|
|
|
|
auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh).release_value_but_fixme_should_propagate_errors();
|
2021-05-20 12:43:33 -03:00
|
|
|
|
|
2022-08-21 16:39:54 -03:00
|
|
|
|
if (m_desktop_wallpaper_mode == "Center") {
|
2021-08-30 17:18:20 -03:00
|
|
|
|
auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());
|
2021-05-20 12:43:33 -03:00
|
|
|
|
painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect());
|
2022-08-21 16:39:54 -03:00
|
|
|
|
} else if (m_desktop_wallpaper_mode == "Tile") {
|
2021-05-20 12:43:33 -03:00
|
|
|
|
painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
|
2022-08-21 16:39:54 -03:00
|
|
|
|
} else if (m_desktop_wallpaper_mode == "Stretch") {
|
2021-05-20 12:43:33 -03:00
|
|
|
|
painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
2021-05-20 12:43:33 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MonitorWidget::paint_event(GUI::PaintEvent& event)
|
|
|
|
|
|
{
|
|
|
|
|
|
redraw_desktop_if_needed();
|
2020-03-29 13:01:55 -03:00
|
|
|
|
|
2020-04-15 11:40:46 -03:00
|
|
|
|
GUI::Painter painter(*this);
|
|
|
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
|
|
|
|
|
|
|
painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
|
2021-05-20 12:43:33 -03:00
|
|
|
|
painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
|
2020-04-15 11:40:46 -03:00
|
|
|
|
|
2021-05-19 16:19:09 -03:00
|
|
|
|
#if 0
|
2020-12-12 20:51:35 -03:00
|
|
|
|
if (!m_desktop_resolution.is_null()) {
|
2021-01-15 21:02:17 -03:00
|
|
|
|
auto displayed_resolution_string = Gfx::IntSize { m_desktop_resolution.width(), m_desktop_resolution.height() }.to_string();
|
|
|
|
|
|
|
|
|
|
|
|
// Render text label scaled with scale factor to hint at its effect.
|
|
|
|
|
|
// FIXME: Once bitmaps have intrinsic scale factors, we could create a bitmap with an intrinsic scale factor of m_desktop_scale_factor
|
|
|
|
|
|
// and that should give us the same effect with less code.
|
2021-07-21 13:02:15 -03:00
|
|
|
|
auto text_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize { painter.font().width(displayed_resolution_string) + 1, painter.font().glyph_height() + 1 });
|
2021-01-15 21:02:17 -03:00
|
|
|
|
GUI::Painter text_painter(*text_bitmap);
|
|
|
|
|
|
text_painter.set_font(painter.font());
|
|
|
|
|
|
|
|
|
|
|
|
text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::BottomRight, Color::Black);
|
|
|
|
|
|
text_painter.draw_text({}, displayed_resolution_string, Gfx::TextAlignment::TopLeft, Color::White);
|
|
|
|
|
|
|
|
|
|
|
|
Gfx::IntRect text_rect = text_bitmap->rect();
|
|
|
|
|
|
text_rect.set_width(text_rect.width() * m_desktop_scale_factor);
|
|
|
|
|
|
text_rect.set_height(text_rect.height() * m_desktop_scale_factor);
|
|
|
|
|
|
text_rect.center_within(m_monitor_rect);
|
|
|
|
|
|
painter.draw_scaled_bitmap(text_rect, *text_bitmap, text_bitmap->rect());
|
2020-12-12 20:51:35 -03:00
|
|
|
|
}
|
2021-05-19 16:19:09 -03:00
|
|
|
|
#endif
|
2020-03-29 13:01:55 -03:00
|
|
|
|
}
|
2020-12-29 22:44:03 -03:00
|
|
|
|
|
|
|
|
|
|
}
|