diff options
author | Andreas Kling <kling@serenityos.org> | 2021-02-09 21:23:50 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-09 21:25:38 +0100 |
commit | ace1b347983f119433bb6fdcf49579648dbce8d2 (patch) | |
tree | 91ba14af962653d142b859ae099dbc881c54b607 | |
parent | 2c4829cb147957635af493257089179f48e71537 (diff) | |
download | serenity-ace1b347983f119433bb6fdcf49579648dbce8d2.zip |
LibWeb: Reset the HTML editing cursor blink cycle on arrow key movement
Also stop exposing the DOM cursor as a mutable reference on Frame,
since event handling code was using that to mess with the text offset
directly. Setting the cursor now always goes through the Frame where
we can reset the blink cycle appropriately.
This makes cursor movement look a lot more natural. :^)
-rw-r--r-- | Userland/Libraries/LibWeb/Page/EventHandler.cpp | 29 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Page/Frame.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Page/Frame.h | 7 |
3 files changed, 33 insertions, 16 deletions
diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 21f5d12df3..33ddb8eed2 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -343,11 +343,13 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin m_edit_event_handler->handle_delete(range); return true; } else { - m_edit_event_handler->handle_delete(range); - m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); - m_frame.cursor_position().set_offset(m_frame.cursor_position().offset() + 1); + + auto new_position = m_frame.cursor_position(); + new_position.set_offset(new_position.offset() + 1); + m_frame.set_cursor_position(move(new_position)); + return true; } } @@ -360,7 +362,10 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin if (position.offset() == 0) TODO(); - m_frame.cursor_position().set_offset(position.offset() - 1); + auto new_position = m_frame.cursor_position(); + new_position.set_offset(position.offset() - 1); + m_frame.set_cursor_position(move(new_position)); + m_edit_event_handler->handle_delete(DOM::Range::create(*position.node(), position.offset() - 1, *position.node(), position.offset())); return true; @@ -379,7 +384,9 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin if (position.offset() >= downcast<DOM::Text>(position.node())->data().length()) TODO(); - m_frame.cursor_position().set_offset(position.offset() + 1); + auto new_position = m_frame.cursor_position(); + new_position.set_offset(position.offset() + 1); + m_frame.set_cursor_position(move(new_position)); return true; } else if (key == KeyCode::Key_Left) { @@ -388,12 +395,18 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin if (position.offset() == 0) TODO(); - m_frame.cursor_position().set_offset(position.offset() - 1); + auto new_position = m_frame.cursor_position(); + new_position.set_offset(new_position.offset() - 1); + m_frame.set_cursor_position(move(new_position)); return true; } else { m_edit_event_handler->handle_insert(m_frame.cursor_position(), code_point); - m_frame.cursor_position().set_offset(m_frame.cursor_position().offset() + 1); + + auto new_position = m_frame.cursor_position(); + new_position.set_offset(new_position.offset() + 1); + m_frame.set_cursor_position(move(new_position)); + return true; } } diff --git a/Userland/Libraries/LibWeb/Page/Frame.cpp b/Userland/Libraries/LibWeb/Page/Frame.cpp index bea214f768..29decc7d08 100644 --- a/Userland/Libraries/LibWeb/Page/Frame.cpp +++ b/Userland/Libraries/LibWeb/Page/Frame.cpp @@ -72,8 +72,11 @@ void Frame::setup() void Frame::did_edit(Badge<EditEventHandler>) { - // The user has edited the content, restart the cursor blink cycle so that - // the cursor doesn't disappear during rapid continuous editing. + reset_cursor_blink_cycle(); +} + +void Frame::reset_cursor_blink_cycle() +{ m_cursor_blink_state = true; m_cursor_blink_timer->restart(); } @@ -211,7 +214,7 @@ Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position) return position; } -void Frame::set_cursor_position(const DOM::Position& position) +void Frame::set_cursor_position(DOM::Position position) { if (m_cursor_position == position) return; @@ -219,12 +222,12 @@ void Frame::set_cursor_position(const DOM::Position& position) if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) m_cursor_position.node()->layout_node()->set_needs_display(); - m_cursor_position = position; + m_cursor_position = move(position); if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) m_cursor_position.node()->layout_node()->set_needs_display(); - dbgln("Cursor position: {}", m_cursor_position); + reset_cursor_blink_cycle(); } String Frame::selected_text() const diff --git a/Userland/Libraries/LibWeb/Page/Frame.h b/Userland/Libraries/LibWeb/Page/Frame.h index c98554b4aa..aad2e566b8 100644 --- a/Userland/Libraries/LibWeb/Page/Frame.h +++ b/Userland/Libraries/LibWeb/Page/Frame.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -94,9 +94,8 @@ public: Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&); Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&); - DOM::Position& cursor_position() { return m_cursor_position; } const DOM::Position& cursor_position() const { return m_cursor_position; } - void set_cursor_position(const DOM::Position&); + void set_cursor_position(DOM::Position); bool cursor_blink_state() const { return m_cursor_blink_state; } @@ -108,6 +107,8 @@ private: explicit Frame(DOM::Element& host_element, Frame& main_frame); explicit Frame(Page&); + void reset_cursor_blink_cycle(); + void setup(); WeakPtr<Page> m_page; |