MouseSettings: Support animated cursors in the highlighting preview

This commit is contained in:
MacDue 2022-06-10 15:12:50 +01:00 committed by Linus Groh
parent ceab9903cd
commit 3c2c6790df
Notes: sideshowbarker 2024-07-17 10:19:17 +09:00
2 changed files with 22 additions and 2 deletions

View file

@ -34,6 +34,17 @@ ErrorOr<void> HighlightPreviewWidget::reload_cursor()
auto cursor_path = String::formatted("/res/cursor-themes/{}/{}",
cursor_theme, cursor_theme_config->read_entry("Cursor", "Arrow"));
m_cursor_bitmap = TRY(load_bitmap(cursor_path, default_cursor_path));
m_cursor_params = Gfx::CursorParams::parse_from_filename(cursor_path, m_cursor_bitmap->rect().center()).constrained(*m_cursor_bitmap);
// Setup cursor animation:
if (m_cursor_params.frames() > 1 && m_cursor_params.frame_ms() > 0) {
m_frame_timer = Core::Timer::create_repeating(m_cursor_params.frame_ms(), [&] {
m_cursor_frame = (m_cursor_frame + 1) % m_cursor_params.frames();
update();
});
m_frame_timer->start();
} else {
m_frame_timer = nullptr;
}
return {};
}
@ -46,8 +57,12 @@ void HighlightPreviewWidget::paint_preview(GUI::PaintEvent&)
highlight_rect.center_within(frame_inner_rect());
aa_painter.fill_ellipse(highlight_rect, m_color);
}
if (m_cursor_bitmap)
painter.blit(m_cursor_bitmap->rect().centered_within(frame_inner_rect()).location(), *m_cursor_bitmap, m_cursor_bitmap->rect());
if (m_cursor_bitmap) {
auto cursor_rect = m_cursor_bitmap->rect();
if (m_cursor_params.frames() > 1)
cursor_rect.set_width(cursor_rect.width() / m_cursor_params.frames());
painter.blit(cursor_rect.centered_within(frame_inner_rect()).location(), *m_cursor_bitmap, cursor_rect.translated(m_cursor_frame * cursor_rect.width(), 0));
}
}
}

View file

@ -6,8 +6,10 @@
#pragma once
#include <LibCore/Timer.h>
#include <LibGUI/AbstractThemePreview.h>
#include <LibGfx/Color.h>
#include <LibGfx/CursorParams.h>
namespace MouseSettings {
@ -36,7 +38,10 @@ private:
ErrorOr<void> reload_cursor();
RefPtr<Gfx::Bitmap> m_cursor_bitmap;
Gfx::CursorParams m_cursor_params;
RefPtr<Core::Timer> m_frame_timer;
int m_cursor_frame { 0 };
int m_radius { 0 };
Gfx::Color m_color;
};