diff options
author | MacDue <macdue@dueutil.tech> | 2022-06-04 20:35:14 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-05 22:42:35 +0100 |
commit | 29d8ec1b18a49c518d5dd0c1cdef4d10078521b8 (patch) | |
tree | 0c4beccf30ad47e84f58cd7788f9fd30effd0c2c /Userland/Services | |
parent | 2a49b582699e4ad6e9c9a4797ff19af36c9463d1 (diff) | |
download | serenity-29d8ec1b18a49c518d5dd0c1cdef4d10078521b8.zip |
WindowServer: Implement cursor highlighting
This allows drawing a nice cursor highlight with an arbitrary color
and radius in the compositor. The highlight circle is, of course,
antialiased :^).
Diffstat (limited to 'Userland/Services')
-rw-r--r-- | Userland/Services/WindowServer/Compositor.cpp | 23 | ||||
-rw-r--r-- | Userland/Services/WindowServer/WindowManager.cpp | 26 | ||||
-rw-r--r-- | Userland/Services/WindowServer/WindowManager.h | 10 |
3 files changed, 55 insertions, 4 deletions
diff --git a/Userland/Services/WindowServer/Compositor.cpp b/Userland/Services/WindowServer/Compositor.cpp index 7a5243b45b..645b98f176 100644 --- a/Userland/Services/WindowServer/Compositor.cpp +++ b/Userland/Services/WindowServer/Compositor.cpp @@ -17,6 +17,7 @@ #include <AK/Memory.h> #include <AK/ScopeGuard.h> #include <LibCore/Timer.h> +#include <LibGfx/AntiAliasingPainter.h> #include <LibGfx/Font/Font.h> #include <LibGfx/Painter.h> #include <LibGfx/StylePainter.h> @@ -849,7 +850,14 @@ Gfx::IntRect Compositor::current_cursor_rect() const { auto& wm = WindowManager::the(); auto& current_cursor = m_current_cursor ? *m_current_cursor : wm.active_cursor(); - return { ScreenInput::the().cursor_location().translated(-current_cursor.params().hotspot()), current_cursor.size() }; + Gfx::IntRect cursor_rect { ScreenInput::the().cursor_location().translated(-current_cursor.params().hotspot()), current_cursor.size() }; + if (wm.is_cursor_highlight_enabled()) { + auto highlight_diameter = wm.cursor_highlight_radius() * 2; + cursor_rect.inflate( + highlight_diameter - cursor_rect.width(), + highlight_diameter - cursor_rect.height()); + } + return cursor_rect; } void Compositor::invalidate_cursor(bool compose_immediately) @@ -956,10 +964,17 @@ void CompositorScreenData::draw_cursor(Screen& screen, Gfx::IntRect const& curso auto& compositor = Compositor::the(); auto& current_cursor = compositor.m_current_cursor ? *compositor.m_current_cursor : wm.active_cursor(); auto screen_rect = screen.rect(); - m_cursor_back_painter->blit({ 0, 0 }, *m_back_bitmap, current_cursor.rect().translated(cursor_rect.location()).intersected(screen_rect).translated(-screen_rect.location())); + m_cursor_back_painter->blit({ 0, 0 }, *m_back_bitmap, cursor_rect.intersected(screen_rect).translated(-screen_rect.location())); auto cursor_src_rect = current_cursor.source_rect(compositor.m_current_cursor_frame); - m_back_painter->blit(cursor_rect.location(), current_cursor.bitmap(screen.scale_factor()), cursor_src_rect); - m_flush_special_rects.add(Gfx::IntRect(cursor_rect.location(), cursor_src_rect.size()).intersected(screen.rect())); + auto cursor_blit_pos = current_cursor.rect().centered_within(cursor_rect).location(); + + if (wm.is_cursor_highlight_enabled()) { + Gfx::AntiAliasingPainter aa_back_painter { *m_back_painter }; + aa_back_painter.fill_ellipse(cursor_rect, wm.cursor_highlight_color()); + } + m_back_painter->blit(cursor_blit_pos, current_cursor.bitmap(screen.scale_factor()), cursor_src_rect); + + m_flush_special_rects.add(Gfx::IntRect(cursor_rect.location(), cursor_rect.size()).intersected(screen.rect())); m_have_flush_rects = true; m_last_cursor_rect = cursor_rect; VERIFY(compositor.m_current_cursor_screen == &screen); diff --git a/Userland/Services/WindowServer/WindowManager.cpp b/Userland/Services/WindowServer/WindowManager.cpp index a6dfafd15f..463fdfc4e6 100644 --- a/Userland/Services/WindowServer/WindowManager.cpp +++ b/Userland/Services/WindowServer/WindowManager.cpp @@ -82,6 +82,10 @@ void WindowManager::reload_config() m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250); m_buttons_switched = m_config->read_bool_entry("Mouse", "ButtonsSwitched", false); + m_cursor_highlight_radius = m_config->read_num_entry("Mouse", "CursorHighlightRadius", 0); + Color default_highlight_color = Color::NamedColor::Yellow; + default_highlight_color.set_alpha(80); + m_cursor_highlight_color = Color::from_string(m_config->read_entry("Mouse", "CursorHighlightColor")).value_or(default_highlight_color); apply_cursor_theme(m_config->read_entry("Mouse", "CursorTheme", "Default")); auto reload_graphic = [&](RefPtr<MultiScaleBitmaps>& bitmap, String const& name) { @@ -2273,6 +2277,28 @@ void WindowManager::apply_cursor_theme(String const& theme_name) sync_config_to_disk(); } +void WindowManager::set_cursor_highlight_radius(int radius) +{ + // TODO: Validate radius + m_cursor_highlight_radius = radius; + Compositor::the().invalidate_cursor(); + m_config->write_num_entry("Mouse", "CursorHighlightRadius", radius); + sync_config_to_disk(); +} + +void WindowManager::set_cursor_highlight_color(Gfx::Color const& color) +{ + m_cursor_highlight_color = color; + Compositor::the().invalidate_cursor(); + m_config->write_entry("Mouse", "CursorHighlightColor", color.to_string()); + sync_config_to_disk(); +} + +bool WindowManager::is_cursor_highlight_enabled() const +{ + return m_cursor_highlight_radius > 0 && m_cursor_highlight_color.alpha() > 0; +} + bool WindowManager::sync_config_to_disk() { if (auto result = m_config->sync(); result.is_error()) { diff --git a/Userland/Services/WindowServer/WindowManager.h b/Userland/Services/WindowServer/WindowManager.h index c5632b0d4c..035348f819 100644 --- a/Userland/Services/WindowServer/WindowManager.h +++ b/Userland/Services/WindowServer/WindowManager.h @@ -153,6 +153,9 @@ public: Cursor const& eyedropper_cursor() const { return *m_eyedropper_cursor; } Cursor const& zoom_cursor() const { return *m_zoom_cursor; } + int cursor_highlight_radius() const { return m_cursor_highlight_radius; } + Gfx::Color cursor_highlight_color() const { return m_cursor_highlight_color; } + Gfx::Font const& font() const; Gfx::Font const& window_title_font() const; @@ -323,6 +326,11 @@ public: void apply_cursor_theme(String const& name); + void set_cursor_highlight_radius(int radius); + void set_cursor_highlight_color(Gfx::Color const& color); + + bool is_cursor_highlight_enabled() const; + private: explicit WindowManager(Gfx::PaletteImpl const&); @@ -376,6 +384,8 @@ private: RefPtr<Cursor> m_crosshair_cursor; RefPtr<Cursor> m_eyedropper_cursor; RefPtr<Cursor> m_zoom_cursor; + int m_cursor_highlight_radius { 0 }; + Gfx::Color m_cursor_highlight_color; RefPtr<MultiScaleBitmaps> m_overlay_rect_shadow; |