1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 16:25:06 +00:00

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.
This commit is contained in:
Andreas Kling 2020-08-02 11:52:35 +02:00
parent e496a74bb3
commit 2c679d0c8b
5 changed files with 66 additions and 0 deletions

View file

@ -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>