2021-05-09 21:15:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Valtteri Koskivuori <vkoskiv@gmail.com>
|
2022-02-10 19:28:48 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-05-09 21:15:35 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MagnifierWidget.h"
|
2022-02-25 10:39:33 +00:00
|
|
|
#include <LibGUI/ConnectionToWindowServer.h>
|
2021-06-07 07:35:07 +00:00
|
|
|
#include <LibGUI/DisplayLink.h>
|
2021-05-09 21:15:35 +00:00
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2021-05-11 12:02:46 +00:00
|
|
|
#include <LibGfx/Rect.h>
|
2021-05-09 21:15:35 +00:00
|
|
|
|
|
|
|
MagnifierWidget::MagnifierWidget()
|
|
|
|
{
|
2021-06-07 07:35:07 +00:00
|
|
|
GUI::DisplayLink::register_callback([this](auto) { sync(); });
|
2021-05-09 21:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MagnifierWidget::set_scale_factor(int scale_factor)
|
|
|
|
{
|
2021-09-17 20:29:35 +00:00
|
|
|
VERIFY(scale_factor == 2 || scale_factor == 4 || scale_factor == 8);
|
2021-05-09 21:15:35 +00:00
|
|
|
m_scale_factor = scale_factor;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:42:01 +00:00
|
|
|
void MagnifierWidget::set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter> color_filter)
|
|
|
|
{
|
|
|
|
m_color_filter = move(color_filter);
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
2021-12-15 13:26:47 +00:00
|
|
|
void MagnifierWidget::display_previous_frame()
|
|
|
|
{
|
|
|
|
--m_frame_offset_from_head;
|
|
|
|
auto index = m_grabbed_bitmaps.head_index() + m_frame_offset_from_head;
|
|
|
|
m_grabbed_bitmap = m_grabbed_bitmaps.at(index);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MagnifierWidget::display_next_frame()
|
|
|
|
{
|
|
|
|
++m_frame_offset_from_head;
|
|
|
|
auto index = m_grabbed_bitmaps.head_index() + m_frame_offset_from_head;
|
|
|
|
m_grabbed_bitmap = m_grabbed_bitmaps.at(index);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:35:07 +00:00
|
|
|
void MagnifierWidget::sync()
|
2021-05-09 21:15:35 +00:00
|
|
|
{
|
2021-09-17 20:29:35 +00:00
|
|
|
if (m_pause_capture)
|
|
|
|
return;
|
|
|
|
|
2021-07-04 20:33:28 +00:00
|
|
|
auto size = frame_inner_rect().size();
|
|
|
|
Gfx::IntSize grab_size { size.width() / m_scale_factor, size.height() / m_scale_factor };
|
2022-02-25 10:39:33 +00:00
|
|
|
m_grabbed_bitmap = GUI::ConnectionToWindowServer::the().get_screen_bitmap_around_cursor(grab_size).bitmap();
|
2021-12-15 13:26:47 +00:00
|
|
|
m_grabbed_bitmaps.enqueue(m_grabbed_bitmap);
|
2021-05-09 21:15:35 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:33:28 +00:00
|
|
|
void MagnifierWidget::paint_event(GUI::PaintEvent& event)
|
2021-05-09 21:15:35 +00:00
|
|
|
{
|
2021-07-04 20:33:28 +00:00
|
|
|
GUI::Frame::paint_event(event);
|
|
|
|
|
2021-05-09 21:15:35 +00:00
|
|
|
GUI::Painter painter(*this);
|
|
|
|
|
2021-06-07 07:35:07 +00:00
|
|
|
if (m_grabbed_bitmap)
|
2021-07-04 20:33:28 +00:00
|
|
|
painter.draw_scaled_bitmap(frame_inner_rect(), *m_grabbed_bitmap, m_grabbed_bitmap->rect());
|
2021-05-09 21:15:35 +00:00
|
|
|
}
|
2021-12-28 20:42:01 +00:00
|
|
|
|
|
|
|
void MagnifierWidget::second_paint_event(GUI::PaintEvent&)
|
|
|
|
{
|
|
|
|
if (!m_color_filter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
GUI::Painter painter(*this);
|
|
|
|
|
|
|
|
auto target = painter.target();
|
|
|
|
auto bitmap_clone_or_error = target->clone();
|
|
|
|
if (bitmap_clone_or_error.is_error())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto clone = bitmap_clone_or_error.release_value();
|
|
|
|
auto rect = target->rect();
|
|
|
|
|
|
|
|
m_color_filter->apply(*target, rect, *clone, rect);
|
|
|
|
}
|