mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:57:45 +00:00
Magnifier: Use a GUI::DisplayLink to drive the screen captures
This ensures that we don't try to update more often than the screen is updated. It also avoids doing synchronous IPC in paint_event().
This commit is contained in:
parent
61c56e75f4
commit
0ea1fd2d54
3 changed files with 13 additions and 8 deletions
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "MagnifierWidget.h"
|
#include "MagnifierWidget.h"
|
||||||
|
#include <LibGUI/DisplayLink.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
#include <LibGUI/WindowServerConnection.h>
|
#include <LibGUI/WindowServerConnection.h>
|
||||||
|
@ -12,6 +13,7 @@
|
||||||
|
|
||||||
MagnifierWidget::MagnifierWidget()
|
MagnifierWidget::MagnifierWidget()
|
||||||
{
|
{
|
||||||
|
GUI::DisplayLink::register_callback([this](auto) { sync(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
MagnifierWidget::~MagnifierWidget()
|
MagnifierWidget::~MagnifierWidget()
|
||||||
|
@ -35,10 +37,15 @@ void MagnifierWidget::set_scale_factor(int scale_factor)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MagnifierWidget::timer_event(Core::TimerEvent&)
|
void MagnifierWidget::sync()
|
||||||
{
|
{
|
||||||
m_mouse_position = GUI::WindowServerConnection::the().get_global_cursor_position();
|
m_mouse_position = GUI::WindowServerConnection::the().get_global_cursor_position();
|
||||||
m_desktop_display_scale = GUI::WindowServerConnection::the().get_desktop_display_scale();
|
m_desktop_display_scale = GUI::WindowServerConnection::the().get_desktop_display_scale();
|
||||||
|
|
||||||
|
// Grab and paint our screenshot.
|
||||||
|
Gfx::IntSize region_size { size().width() / m_scale_factor, size().height() / m_scale_factor };
|
||||||
|
Gfx::Rect region { (m_mouse_position.x() * m_desktop_display_scale) - (region_size.width() / 2), (m_mouse_position.y() * m_desktop_display_scale) - (region_size.height() / 2), region_size.width(), region_size.height() };
|
||||||
|
m_grabbed_bitmap = GUI::WindowServerConnection::the().get_screen_bitmap(region).bitmap();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,9 +53,6 @@ void MagnifierWidget::paint_event(GUI::PaintEvent&)
|
||||||
{
|
{
|
||||||
GUI::Painter painter(*this);
|
GUI::Painter painter(*this);
|
||||||
|
|
||||||
// Grab and paint our screenshot.
|
if (m_grabbed_bitmap)
|
||||||
Gfx::IntSize region_size { size().width() / m_scale_factor, size().height() / m_scale_factor };
|
painter.draw_scaled_bitmap(rect(), *m_grabbed_bitmap, m_grabbed_bitmap->rect());
|
||||||
Gfx::Rect region { (m_mouse_position.x() * m_desktop_display_scale) - (region_size.width() / 2), (m_mouse_position.y() * m_desktop_display_scale) - (region_size.height() / 2), region_size.width(), region_size.height() };
|
|
||||||
auto map = GUI::WindowServerConnection::the().get_screen_bitmap(region);
|
|
||||||
painter.draw_scaled_bitmap(rect(), *map.bitmap(), map.bitmap()->rect());
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,12 @@ public:
|
||||||
void track_cursor_globally();
|
void track_cursor_globally();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void timer_event(Core::TimerEvent&) override;
|
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
|
|
||||||
|
void sync();
|
||||||
|
|
||||||
Gfx::IntPoint m_mouse_position;
|
Gfx::IntPoint m_mouse_position;
|
||||||
int m_scale_factor { 2 };
|
int m_scale_factor { 2 };
|
||||||
int m_desktop_display_scale { 1 };
|
int m_desktop_display_scale { 1 };
|
||||||
|
RefPtr<Gfx::Bitmap> m_grabbed_bitmap;
|
||||||
};
|
};
|
||||||
|
|
|
@ -84,7 +84,6 @@ int main(int argc, char** argv)
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
magnifier.track_cursor_globally();
|
magnifier.track_cursor_globally();
|
||||||
magnifier.start_timer(16);
|
|
||||||
|
|
||||||
return app->exec();
|
return app->exec();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue