Serenity Operating System
at master 184 lines 5.7 kB view raw
1/* 2 * Copyright (c) 2021, Valtteri Koskivuori <vkoskiv@gmail.com> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include "MagnifierWidget.h" 9#include <LibGUI/ConnectionToWindowServer.h> 10#include <LibGUI/DisplayLink.h> 11#include <LibGUI/Painter.h> 12#include <LibGUI/Window.h> 13#include <LibGfx/Rect.h> 14 15MagnifierWidget::MagnifierWidget() 16{ 17 GUI::DisplayLink::register_callback([this](auto) { sync(); }); 18} 19 20void MagnifierWidget::set_scale_factor(int scale_factor) 21{ 22 VERIFY(scale_factor == 2 || scale_factor == 4 || scale_factor == 8); 23 if (m_scale_factor == scale_factor) 24 return; 25 m_scale_factor = scale_factor; 26 layout_relevant_change_occurred(); 27 update(); 28} 29 30void MagnifierWidget::lock_location(bool lock) 31{ 32 if (lock) 33 m_locked_location = GUI::ConnectionToWindowServer::the().get_global_cursor_position(); 34 else 35 m_locked_location = {}; 36} 37 38void MagnifierWidget::show_grid(bool new_value) 39{ 40 if (m_show_grid == new_value) 41 return; 42 m_show_grid = new_value; 43 update(); 44} 45 46void MagnifierWidget::set_grid_color(Gfx::Color new_color) 47{ 48 if (m_grid_color == new_color) 49 return; 50 m_grid_color = new_color; 51 update(); 52} 53 54void MagnifierWidget::set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter> color_filter) 55{ 56 m_color_filter = move(color_filter); 57 sync(); 58} 59 60void MagnifierWidget::display_previous_frame() 61{ 62 --m_frame_offset_from_head; 63 auto index = m_grabbed_bitmaps.head_index() + m_frame_offset_from_head; 64 m_grabbed_bitmap = m_grabbed_bitmaps.at(index); 65 update(); 66} 67 68void MagnifierWidget::display_next_frame() 69{ 70 ++m_frame_offset_from_head; 71 auto index = m_grabbed_bitmaps.head_index() + m_frame_offset_from_head; 72 m_grabbed_bitmap = m_grabbed_bitmaps.at(index); 73 update(); 74} 75 76void MagnifierWidget::sync() 77{ 78 if (m_pause_capture) 79 return; 80 81 auto size = frame_inner_rect().size(); 82 Gfx::IntSize grab_size { (size.width() + m_scale_factor - 1) / m_scale_factor, (size.height() + m_scale_factor - 1) / m_scale_factor }; 83 VERIFY(grab_size.width() != 0 && grab_size.height() != 0); 84 85 if (m_locked_location.has_value()) { 86 m_grabbed_bitmap = GUI::ConnectionToWindowServer::the().get_screen_bitmap_around_location(grab_size, m_locked_location.value()).bitmap(); 87 } else { 88 m_grabbed_bitmap = GUI::ConnectionToWindowServer::the().get_screen_bitmap_around_cursor(grab_size).bitmap(); 89 } 90 91 m_grabbed_bitmaps.enqueue(m_grabbed_bitmap); 92 update(); 93} 94 95void MagnifierWidget::paint_event(GUI::PaintEvent& event) 96{ 97 GUI::Frame::paint_event(event); 98 99 if (!m_grabbed_bitmap) 100 return; 101 102 GUI::Painter painter(*this); 103 104 if (m_pause_capture) 105 painter.fill_rect(frame_inner_rect(), Gfx::Color::Black); 106 107 // We have to clip the source rect for cases when the currently captured image is larger than the inner_rect 108 int horizontal_clip = (frame_inner_rect().width() + m_scale_factor - 1) / m_scale_factor; 109 int vertical_clip = (frame_inner_rect().height() + m_scale_factor - 1) / m_scale_factor; 110 111 if (m_grabbed_bitmap) 112 painter.draw_scaled_bitmap( 113 frame_inner_rect().intersected(Gfx::IntRect { { 0, 0 }, m_grabbed_bitmap->rect().size() } * m_scale_factor), 114 *m_grabbed_bitmap, 115 m_grabbed_bitmap->rect().intersected({ 0, 0, horizontal_clip, vertical_clip }), 116 1.0, 117 Gfx::Painter::ScalingMode::NearestFractional); 118 119 if (m_show_grid) { 120 121 auto grid_rect = frame_inner_rect(); 122 if (m_grabbed_bitmap) 123 grid_rect = frame_inner_rect().intersected({ 0, 124 0, 125 m_grabbed_bitmap->rect().width() * m_scale_factor, 126 m_grabbed_bitmap->rect().height() * m_scale_factor }); 127 128 int start_y = grid_rect.top(), 129 start_x = grid_rect.left(); 130 131 int end_y = grid_rect.bottom(), 132 end_x = grid_rect.right(); 133 134 for (int current_y = start_y; current_y <= end_y; current_y += m_scale_factor) 135 painter.draw_line({ start_x, current_y }, { end_x, current_y }, m_grid_color); 136 137 for (int current_x = start_y; current_x <= end_x; current_x += m_scale_factor) 138 painter.draw_line({ current_x, start_y }, { current_x, end_y }, m_grid_color); 139 } 140} 141 142void MagnifierWidget::second_paint_event(GUI::PaintEvent&) 143{ 144 if (!m_color_filter) 145 return; 146 147 GUI::Painter painter(*this); 148 149 auto target = painter.target(); 150 auto bitmap_clone_or_error = target->clone(); 151 if (bitmap_clone_or_error.is_error()) 152 return; 153 154 auto clone = bitmap_clone_or_error.release_value(); 155 auto rect = target->rect(); 156 157 m_color_filter->apply(*target, rect, *clone, rect); 158} 159 160void MagnifierWidget::mousemove_event(GUI::MouseEvent& event) 161{ 162 if (m_locked_location.has_value() && m_currently_dragging && !m_pause_capture) { 163 auto current_position = event.position(); 164 auto difference = current_position - m_last_drag_position; 165 Gfx::IntPoint remainder = { difference.x() % m_scale_factor, difference.y() % m_scale_factor }; 166 auto moved_by = difference / m_scale_factor; 167 m_locked_location = m_locked_location.value() - moved_by; 168 m_last_drag_position = current_position - remainder; 169 } 170} 171 172void MagnifierWidget::mousedown_event(GUI::MouseEvent& event) 173{ 174 if (event.button() == GUI::MouseButton::Primary && !m_pause_capture) { 175 m_currently_dragging = true; 176 m_last_drag_position = event.position(); 177 } 178} 179 180void MagnifierWidget::mouseup_event(GUI::MouseEvent& event) 181{ 182 if (event.button() == GUI::MouseButton::Primary) 183 m_currently_dragging = false; 184}