mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:07:34 +00:00
LibGUI: Add optional placeholder to TextEditor
This lets you show some disabled text when no text is entered, and the editor is not focused.
This commit is contained in:
parent
5b6ccbb918
commit
fa96e57c15
4 changed files with 19 additions and 1 deletions
|
@ -675,6 +675,11 @@ void TextDocument::remove(const TextRange& unnormalized_range)
|
||||||
notify_did_change();
|
notify_did_change();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TextDocument::is_empty() const
|
||||||
|
{
|
||||||
|
return line_count() == 1 && line(0).is_empty();
|
||||||
|
}
|
||||||
|
|
||||||
TextRange TextDocument::range_for_entire_line(size_t line_index) const
|
TextRange TextDocument::range_for_entire_line(size_t line_index) const
|
||||||
{
|
{
|
||||||
if (line_index >= line_count())
|
if (line_index >= line_count())
|
||||||
|
|
|
@ -139,6 +139,8 @@ public:
|
||||||
|
|
||||||
virtual bool is_code_document() const { return false; }
|
virtual bool is_code_document() const { return false; }
|
||||||
|
|
||||||
|
bool is_empty() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit TextDocument(Client* client);
|
explicit TextDocument(Client* client);
|
||||||
|
|
||||||
|
@ -179,6 +181,7 @@ public:
|
||||||
size_t first_non_whitespace_column() const;
|
size_t first_non_whitespace_column() const;
|
||||||
Optional<size_t> last_non_whitespace_column() const;
|
Optional<size_t> last_non_whitespace_column() const;
|
||||||
bool ends_in_whitespace() const;
|
bool ends_in_whitespace() const;
|
||||||
|
bool is_empty() const { return length() == 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// NOTE: This vector is null terminated.
|
// NOTE: This vector is null terminated.
|
||||||
|
|
|
@ -461,7 +461,12 @@ void TextEditor::paint_event(PaintEvent& event)
|
||||||
#ifdef DEBUG_TEXTEDITOR
|
#ifdef DEBUG_TEXTEDITOR
|
||||||
painter.draw_rect(visual_line_rect, Color::Cyan);
|
painter.draw_rect(visual_line_rect, Color::Cyan);
|
||||||
#endif
|
#endif
|
||||||
if (!document().has_spans()) {
|
|
||||||
|
if (!placeholder().is_empty() && document().is_empty() && !is_focused() && line_index == 0) {
|
||||||
|
auto line_rect = visual_line_rect;
|
||||||
|
line_rect.set_width(font().width(placeholder()));
|
||||||
|
painter.draw_text(line_rect, placeholder(), m_text_alignment, palette().color(Gfx::ColorRole::DisabledText));
|
||||||
|
} else if (!document().has_spans()) {
|
||||||
// Fast-path for plain text
|
// Fast-path for plain text
|
||||||
auto color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
|
auto color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
|
||||||
if (is_displayonly() && (is_focused() || has_visible_list()))
|
if (is_displayonly() && (is_focused() || has_visible_list()))
|
||||||
|
|
|
@ -60,6 +60,9 @@ public:
|
||||||
|
|
||||||
virtual void set_document(TextDocument&);
|
virtual void set_document(TextDocument&);
|
||||||
|
|
||||||
|
const String& placeholder() const { return m_placeholder; }
|
||||||
|
void set_placeholder(const StringView& placeholder) { m_placeholder = placeholder; }
|
||||||
|
|
||||||
void set_visualize_trailing_whitespace(bool);
|
void set_visualize_trailing_whitespace(bool);
|
||||||
bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
|
bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
|
||||||
|
|
||||||
|
@ -301,6 +304,8 @@ private:
|
||||||
|
|
||||||
RefPtr<TextDocument> m_document;
|
RefPtr<TextDocument> m_document;
|
||||||
|
|
||||||
|
String m_placeholder { "" };
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void for_each_visual_line(size_t line_index, Callback) const;
|
void for_each_visual_line(size_t line_index, Callback) const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue