2020-03-29 19:01:55 +03:00
|
|
|
|
/*
|
2020-12-25 14:15:16 +03:00
|
|
|
|
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
|
2021-05-19 22:19:09 +03:00
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-03-29 19:01:55 +03:00
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-29 19:01:55 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MonitorWidget.h"
|
2021-05-19 22:19:09 +03:00
|
|
|
|
#include <LibGUI/Desktop.h>
|
2023-03-17 13:32:12 +03:00
|
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-03-29 19:01:55 +03:00
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-30 04:44:03 +03:00
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-08-17 15:02:29 +03:00
|
|
|
|
#include <LibThreading/BackgroundAction.h>
|
2020-12-30 04:44:03 +03:00
|
|
|
|
|
2021-05-19 22:19:09 +03:00
|
|
|
|
REGISTER_WIDGET(DisplaySettings, MonitorWidget)
|
|
|
|
|
|
2020-12-30 04:44:03 +03:00
|
|
|
|
namespace DisplaySettings {
|
2020-03-29 19:01:55 +03:00
|
|
|
|
|
2023-03-17 13:32:12 +03:00
|
|
|
|
ErrorOr<NonnullRefPtr<MonitorWidget>> MonitorWidget::try_create()
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2023-03-17 13:32:12 +03:00
|
|
|
|
auto monitor_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MonitorWidget()));
|
|
|
|
|
monitor_widget->m_desktop_resolution = GUI::Desktop::the().rect().size();
|
|
|
|
|
monitor_widget->m_monitor_bitmap = TRY(Gfx::Bitmap::load_from_file("/res/graphics/monitor.png"sv));
|
|
|
|
|
monitor_widget->m_desktop_bitmap = TRY(Gfx::Bitmap::create(monitor_widget->m_monitor_bitmap->format(), { 280, 158 }));
|
|
|
|
|
monitor_widget->m_monitor_rect = { { 12, 13 }, monitor_widget->m_desktop_bitmap->size() };
|
|
|
|
|
monitor_widget->set_fixed_size(304, 201);
|
|
|
|
|
return monitor_widget;
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 22:11:53 +03:00
|
|
|
|
bool MonitorWidget::set_wallpaper(String path)
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2021-08-17 15:02:29 +03:00
|
|
|
|
if (!is_different_to_current_wallpaper_path(path))
|
2021-05-20 18:43:33 +03:00
|
|
|
|
return false;
|
2021-05-21 19:27:12 +03:00
|
|
|
|
|
2022-12-29 16:57:00 +03:00
|
|
|
|
(void)Threading::BackgroundAction<NonnullRefPtr<Gfx::Bitmap>>::construct(
|
2021-11-14 23:25:59 +03:00
|
|
|
|
[path](auto&) -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
|
|
|
|
|
if (path.is_empty())
|
|
|
|
|
return Error::from_errno(ENOENT);
|
2023-01-20 22:06:05 +03:00
|
|
|
|
return Gfx::Bitmap::load_from_file(path);
|
2021-08-17 15:02:29 +03:00
|
|
|
|
},
|
|
|
|
|
|
2022-12-29 16:57:00 +03:00
|
|
|
|
[this, path](NonnullRefPtr<Gfx::Bitmap> bitmap) -> ErrorOr<void> {
|
2021-08-17 15: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))
|
2022-12-13 01:34:28 +03:00
|
|
|
|
return {};
|
2022-12-29 16:57:00 +03:00
|
|
|
|
m_wallpaper_bitmap = move(bitmap);
|
2021-08-17 15:02:29 +03:00
|
|
|
|
m_desktop_dirty = true;
|
|
|
|
|
update();
|
2022-12-29 16:57:00 +03:00
|
|
|
|
return {};
|
|
|
|
|
},
|
|
|
|
|
[this, path](Error) -> void {
|
|
|
|
|
m_wallpaper_bitmap = nullptr;
|
2021-08-17 15:02:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (path.is_empty())
|
2023-03-16 22:11:53 +03:00
|
|
|
|
m_desktop_wallpaper_path = OptionalNone();
|
2021-08-17 15:02:29 +03:00
|
|
|
|
else
|
2023-03-16 22:11:53 +03:00
|
|
|
|
m_desktop_wallpaper_path = path;
|
2021-05-29 14:34:36 +03:00
|
|
|
|
|
2020-06-27 12:04:43 +03:00
|
|
|
|
return true;
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 22:11:53 +03:00
|
|
|
|
Optional<StringView> MonitorWidget::wallpaper() const
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2023-03-16 22:11:53 +03:00
|
|
|
|
if (m_desktop_wallpaper_path.has_value())
|
|
|
|
|
return m_desktop_wallpaper_path->bytes_as_string_view();
|
|
|
|
|
return OptionalNone();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 22:11:53 +03:00
|
|
|
|
void MonitorWidget::set_wallpaper_mode(String mode)
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2021-05-19 22:50:51 +03:00
|
|
|
|
if (m_desktop_wallpaper_mode == mode)
|
|
|
|
|
return;
|
|
|
|
|
m_desktop_wallpaper_mode = move(mode);
|
2021-05-20 18:43:33 +03:00
|
|
|
|
m_desktop_dirty = true;
|
2021-05-19 22:50:51 +03:00
|
|
|
|
update();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-13 21:00:57 +03:00
|
|
|
|
StringView MonitorWidget::wallpaper_mode() const
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
|
|
|
|
return m_desktop_wallpaper_mode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
|
void MonitorWidget::set_desktop_resolution(Gfx::IntSize resolution)
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2021-05-20 18:43:33 +03:00
|
|
|
|
if (m_desktop_resolution == resolution)
|
|
|
|
|
return;
|
2020-03-29 19:01:55 +03:00
|
|
|
|
m_desktop_resolution = resolution;
|
2021-05-20 18:43:33 +03:00
|
|
|
|
m_desktop_dirty = true;
|
|
|
|
|
update();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
|
Gfx::IntSize MonitorWidget::desktop_resolution()
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
|
|
|
|
return m_desktop_resolution;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MonitorWidget::set_background_color(Gfx::Color color)
|
|
|
|
|
{
|
2021-05-19 22:50:51 +03:00
|
|
|
|
if (m_desktop_color == color)
|
|
|
|
|
return;
|
2020-03-29 19:01:55 +03:00
|
|
|
|
m_desktop_color = color;
|
2021-05-20 18:43:33 +03:00
|
|
|
|
m_desktop_dirty = true;
|
2021-05-19 22:50:51 +03:00
|
|
|
|
update();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::Color MonitorWidget::background_color()
|
|
|
|
|
{
|
|
|
|
|
return m_desktop_color;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 18:43:33 +03:00
|
|
|
|
void MonitorWidget::redraw_desktop_if_needed()
|
2020-03-29 19:01:55 +03:00
|
|
|
|
{
|
2021-05-20 18: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();
|
|
|
|
|
|
2023-08-17 03:29:31 +03:00
|
|
|
|
auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled(sw, sh).to_type<int>();
|
2023-03-17 13:32:12 +03:00
|
|
|
|
auto scaled_bitmap_or_error = m_wallpaper_bitmap->scaled(sw, sh);
|
|
|
|
|
if (scaled_bitmap_or_error.is_error()) {
|
|
|
|
|
GUI::MessageBox::show_error(window(), "There was an error updating the desktop preview"sv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto scaled_bitmap = scaled_bitmap_or_error.release_value();
|
2021-05-20 18:43:33 +03:00
|
|
|
|
|
2023-03-16 22:11:53 +03:00
|
|
|
|
if (m_desktop_wallpaper_mode == "Center"sv) {
|
2021-08-30 23:18:20 +03:00
|
|
|
|
auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());
|
2021-05-20 18:43:33 +03:00
|
|
|
|
painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect());
|
2023-03-16 22:11:53 +03:00
|
|
|
|
} else if (m_desktop_wallpaper_mode == "Tile"sv) {
|
2021-05-20 18:43:33 +03:00
|
|
|
|
painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
|
2023-03-16 22:11:53 +03:00
|
|
|
|
} else if (m_desktop_wallpaper_mode == "Stretch"sv) {
|
2023-04-27 22:10:31 +03:00
|
|
|
|
painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect(), 1.f, Gfx::Painter::ScalingMode::BilinearBlend);
|
2021-05-20 18:43:33 +03:00
|
|
|
|
} else {
|
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
2021-05-20 18:43:33 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MonitorWidget::paint_event(GUI::PaintEvent& event)
|
|
|
|
|
{
|
|
|
|
|
redraw_desktop_if_needed();
|
2020-03-29 19:01:55 +03:00
|
|
|
|
|
2020-04-15 17: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 18:43:33 +03:00
|
|
|
|
painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
|
2020-03-29 19:01:55 +03:00
|
|
|
|
}
|
2020-12-30 04:44:03 +03:00
|
|
|
|
|
|
|
|
|
}
|