Browse Source

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. :^)
Andreas Kling 4 năm trước cách đây
mục cha
commit
ace1b34798

+ 21 - 8
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;
         }
     }

+ 8 - 5
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

+ 4 - 3
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;