From 3c2c6790df7dd0ec516b27f1b56512ec0435686e Mon Sep 17 00:00:00 2001 From: MacDue Date: Fri, 10 Jun 2022 15:12:50 +0100 Subject: [PATCH] MouseSettings: Support animated cursors in the highlighting preview --- .../MouseSettings/HighlightPreviewWidget.cpp | 19 +++++++++++++++++-- .../MouseSettings/HighlightPreviewWidget.h | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/MouseSettings/HighlightPreviewWidget.cpp b/Userland/Applications/MouseSettings/HighlightPreviewWidget.cpp index 2ee4f852a4f..1b467000ae5 100644 --- a/Userland/Applications/MouseSettings/HighlightPreviewWidget.cpp +++ b/Userland/Applications/MouseSettings/HighlightPreviewWidget.cpp @@ -34,6 +34,17 @@ ErrorOr 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)); + } } } diff --git a/Userland/Applications/MouseSettings/HighlightPreviewWidget.h b/Userland/Applications/MouseSettings/HighlightPreviewWidget.h index 01d4b3d5ba2..06714f13c82 100644 --- a/Userland/Applications/MouseSettings/HighlightPreviewWidget.h +++ b/Userland/Applications/MouseSettings/HighlightPreviewWidget.h @@ -6,8 +6,10 @@ #pragma once +#include #include #include +#include namespace MouseSettings { @@ -36,7 +38,10 @@ private: ErrorOr reload_cursor(); RefPtr m_cursor_bitmap; + Gfx::CursorParams m_cursor_params; + RefPtr m_frame_timer; + int m_cursor_frame { 0 }; int m_radius { 0 }; Gfx::Color m_color; };