summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Layout
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-02 11:52:35 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-02 17:34:50 +0200
commit2c679d0c8b38b89bf67d55dae21daafc9ad4dccb (patch)
tree7c3abc41e75b6ba7ad098ec01135b8b00905b985 /Libraries/LibWeb/Layout
parente496a74bb343bf5e731aedb947e338c1fe617973 (diff)
downloadserenity-2c679d0c8b38b89bf67d55dae21daafc9ad4dccb.zip
LibWeb: Add a blinking text cursor :^)
Each Web::Frame now has a cursor that sits at a DOM::Position. It will blink and look like a nice regular text cursor. It doesn't really do anything yet, but it will eventually.
Diffstat (limited to 'Libraries/LibWeb/Layout')
-rw-r--r--Libraries/LibWeb/Layout/LayoutText.cpp23
-rw-r--r--Libraries/LibWeb/Layout/LayoutText.h1
2 files changed, 24 insertions, 0 deletions
diff --git a/Libraries/LibWeb/Layout/LayoutText.cpp b/Libraries/LibWeb/Layout/LayoutText.cpp
index 7a4cc81885..55696b5531 100644
--- a/Libraries/LibWeb/Layout/LayoutText.cpp
+++ b/Libraries/LibWeb/Layout/LayoutText.cpp
@@ -32,6 +32,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Layout/LayoutBlock.h>
#include <LibWeb/Layout/LayoutText.h>
+#include <LibWeb/Page/Frame.h>
#include <ctype.h>
namespace Web {
@@ -101,6 +102,28 @@ void LayoutText::paint_fragment(PaintContext& context, const LineBoxFragment& fr
painter.add_clip_rect(enclosing_int_rect(selection_rect));
painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::TopLeft, context.palette().selection_text());
}
+
+ paint_cursor_if_needed(context, fragment);
+}
+
+void LayoutText::paint_cursor_if_needed(PaintContext& context, const LineBoxFragment& fragment) const
+{
+ if (!frame().cursor_blink_state())
+ return;
+
+ if (frame().cursor_position().node() != &node())
+ return;
+
+ if (!(frame().cursor_position().offset() >= (unsigned)fragment.start() && frame().cursor_position().offset() < (unsigned)(fragment.start() + fragment.length())))
+ return;
+
+ auto fragment_rect = fragment.absolute_rect();
+
+ float cursor_x = fragment_rect.x() + specified_style().font().width(fragment.text().substring_view(0, frame().cursor_position().offset() - fragment.start()));
+ float cursor_top = fragment_rect.top();
+ float cursor_height = fragment_rect.height();
+ Gfx::IntRect cursor_rect(cursor_x, cursor_top, 1, cursor_height);
+ context.painter().draw_rect(cursor_rect, context.palette().text_cursor());
}
template<typename Callback>
diff --git a/Libraries/LibWeb/Layout/LayoutText.h b/Libraries/LibWeb/Layout/LayoutText.h
index 66b3bf0cad..155694c25a 100644
--- a/Libraries/LibWeb/Layout/LayoutText.h
+++ b/Libraries/LibWeb/Layout/LayoutText.h
@@ -54,6 +54,7 @@ public:
private:
void split_into_lines_by_rules(LayoutBlock& container, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks);
+ void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
template<typename Callback>
void for_each_chunk(Callback, LayoutMode, bool do_wrap_lines, bool do_wrap_breaks) const;