mirror of
https://github.com/RGBCube/serenity
synced 2025-06-15 17:02:06 +00:00
TextEditor: Add charcount info about selected text to the statusbar
This commit is contained in:
parent
daf2e5d335
commit
4f34a78337
2 changed files with 23 additions and 4 deletions
|
@ -97,7 +97,7 @@ TextEditorWidget::TextEditorWidget()
|
||||||
if (url.is_valid())
|
if (url.is_valid())
|
||||||
m_statusbar->set_text(url.to_string());
|
m_statusbar->set_text(url.to_string());
|
||||||
else
|
else
|
||||||
update_statusbar_cursor_position();
|
update_statusbar();
|
||||||
};
|
};
|
||||||
m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
|
m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
|
||||||
if (!Desktop::Launcher::open(url)) {
|
if (!Desktop::Launcher::open(url)) {
|
||||||
|
@ -274,7 +274,8 @@ TextEditorWidget::TextEditorWidget()
|
||||||
|
|
||||||
m_statusbar = *find_descendant_of_type_named<GUI::StatusBar>("statusbar");
|
m_statusbar = *find_descendant_of_type_named<GUI::StatusBar>("statusbar");
|
||||||
|
|
||||||
m_editor->on_cursor_change = [this] { update_statusbar_cursor_position(); };
|
m_editor->on_cursor_change = [this] { update_statusbar(); };
|
||||||
|
m_editor->on_selection_change = [this] { update_statusbar(); };
|
||||||
|
|
||||||
m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
|
m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
|
||||||
if (m_document_dirty) {
|
if (m_document_dirty) {
|
||||||
|
@ -685,9 +686,27 @@ void TextEditorWidget::update_html_preview()
|
||||||
m_page_view->scroll_into_view(current_scroll_pos, true, true);
|
m_page_view->scroll_into_view(current_scroll_pos, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::update_statusbar_cursor_position()
|
void TextEditorWidget::update_statusbar()
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
|
builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
|
||||||
|
|
||||||
|
if (m_editor->has_selection()) {
|
||||||
|
int word_count = 0;
|
||||||
|
bool in_word = false;
|
||||||
|
String selected_text = m_editor->selected_text();
|
||||||
|
for (char c : selected_text) {
|
||||||
|
if (in_word && isspace(c)) {
|
||||||
|
in_word = false;
|
||||||
|
word_count++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!in_word && !isspace(c))
|
||||||
|
in_word = true;
|
||||||
|
}
|
||||||
|
if (in_word)
|
||||||
|
word_count++;
|
||||||
|
builder.appendff(" Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
|
||||||
|
}
|
||||||
m_statusbar->set_text(builder.to_string());
|
m_statusbar->set_text(builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ private:
|
||||||
void update_preview();
|
void update_preview();
|
||||||
void update_markdown_preview();
|
void update_markdown_preview();
|
||||||
void update_html_preview();
|
void update_html_preview();
|
||||||
void update_statusbar_cursor_position();
|
void update_statusbar();
|
||||||
|
|
||||||
virtual void drop_event(GUI::DropEvent&) override;
|
virtual void drop_event(GUI::DropEvent&) override;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue