summaryrefslogtreecommitdiff
path: root/Userland/Applications/Magnifier/MagnifierWidget.cpp
blob: de759bbce6b34674a028a865cfe3e95733c1654b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2021, Valtteri Koskivuori <vkoskiv@gmail.com>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "MagnifierWidget.h"
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/DisplayLink.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
#include <LibGfx/Rect.h>

MagnifierWidget::MagnifierWidget()
{
    GUI::DisplayLink::register_callback([this](auto) { sync(); });
}

void MagnifierWidget::set_scale_factor(int scale_factor)
{
    VERIFY(scale_factor == 2 || scale_factor == 4 || scale_factor == 8);
    if (m_scale_factor == scale_factor)
        return;
    m_scale_factor = scale_factor;
    layout_relevant_change_occurred();
    update();
}

void MagnifierWidget::lock_location(bool lock)
{
    if (lock)
        m_locked_location = GUI::ConnectionToWindowServer::the().get_global_cursor_position();
    else
        m_locked_location = {};
}

void MagnifierWidget::show_grid(bool new_value)
{
    if (m_show_grid == new_value)
        return;
    m_show_grid = new_value;
    update();
}

void MagnifierWidget::set_grid_color(Gfx::Color new_color)
{
    if (m_grid_color == new_color)
        return;
    m_grid_color = new_color;
    update();
}

void MagnifierWidget::set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter> color_filter)
{
    m_color_filter = move(color_filter);
    sync();
}

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();
}

void MagnifierWidget::sync()
{
    if (m_pause_capture)
        return;

    auto size = frame_inner_rect().size();
    Gfx::IntSize grab_size { (size.width() + m_scale_factor - 1) / m_scale_factor, (size.height() + m_scale_factor - 1) / m_scale_factor };
    VERIFY(grab_size.width() != 0 && grab_size.height() != 0);

    if (m_locked_location.has_value()) {
        m_grabbed_bitmap = GUI::ConnectionToWindowServer::the().get_screen_bitmap_around_location(grab_size, m_locked_location.value()).bitmap();
    } else {
        m_grabbed_bitmap = GUI::ConnectionToWindowServer::the().get_screen_bitmap_around_cursor(grab_size).bitmap();
    }

    m_grabbed_bitmaps.enqueue(m_grabbed_bitmap);
    update();
}

void MagnifierWidget::paint_event(GUI::PaintEvent& event)
{
    GUI::Frame::paint_event(event);

    if (!m_grabbed_bitmap)
        return;

    GUI::Painter painter(*this);
    auto frame_inner_rect = this->frame_inner_rect();
    auto bitmap_rect = Gfx::IntRect { frame_inner_rect.top_left(), m_grabbed_bitmap->rect().size() * m_scale_factor };
    painter.add_clip_rect(frame_inner_rect);

    if (m_pause_capture)
        painter.fill_rect(frame_inner_rect, Gfx::Color::Black);

    if (m_grabbed_bitmap)
        painter.draw_scaled_bitmap(bitmap_rect, *m_grabbed_bitmap, m_grabbed_bitmap->rect(), 1.0, Gfx::Painter::ScalingMode::NearestNeighbor);

    if (m_show_grid) {
        int start_y = bitmap_rect.top();
        int start_x = bitmap_rect.left();

        int end_y = bitmap_rect.bottom();
        int end_x = bitmap_rect.right();

        for (int current_y = start_y; current_y < end_y; current_y += m_scale_factor)
            painter.draw_line({ start_x, current_y }, { end_x - 1, current_y }, m_grid_color);

        for (int current_x = start_y; current_x < end_x; current_x += m_scale_factor)
            painter.draw_line({ current_x, start_y }, { current_x, end_y - 1 }, m_grid_color);
    }
}

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);
}

void MagnifierWidget::mousemove_event(GUI::MouseEvent& event)
{
    if (m_locked_location.has_value() && m_currently_dragging && !m_pause_capture) {
        auto current_position = event.position();
        auto difference = current_position - m_last_drag_position;
        Gfx::IntPoint remainder = { difference.x() % m_scale_factor, difference.y() % m_scale_factor };
        auto moved_by = difference / m_scale_factor;
        m_locked_location = m_locked_location.value() - moved_by;
        m_last_drag_position = current_position - remainder;
    }
}

void MagnifierWidget::mousedown_event(GUI::MouseEvent& event)
{
    if (event.button() == GUI::MouseButton::Primary && !m_pause_capture) {
        m_currently_dragging = true;
        m_last_drag_position = event.position();
    }
}

void MagnifierWidget::mouseup_event(GUI::MouseEvent& event)
{
    if (event.button() == GUI::MouseButton::Primary)
        m_currently_dragging = false;
}