TextEditor: Add charcount info about selected text to the statusbar
This commit is contained in:
parent
daf2e5d335
commit
4f34a78337
Notes:
sideshowbarker
2024-07-18 21:20:41 +09:00
Author: https://github.com/ngc6302h 🔰 Commit: https://github.com/SerenityOS/serenity/commit/4f34a78337e Pull-request: https://github.com/SerenityOS/serenity/pull/5772 Issue: https://github.com/SerenityOS/serenity/issues/5760 Issue: https://github.com/SerenityOS/serenity/issues/5768 Reviewed-by: https://github.com/awesomekling
2 changed files with 23 additions and 4 deletions
Userland/Applications/TextEditor
|
@ -97,7 +97,7 @@ TextEditorWidget::TextEditorWidget()
|
|||
if (url.is_valid())
|
||||
m_statusbar->set_text(url.to_string());
|
||||
else
|
||||
update_statusbar_cursor_position();
|
||||
update_statusbar();
|
||||
};
|
||||
m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
|
||||
if (!Desktop::Launcher::open(url)) {
|
||||
|
@ -274,7 +274,8 @@ TextEditorWidget::TextEditorWidget()
|
|||
|
||||
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&) {
|
||||
if (m_document_dirty) {
|
||||
|
@ -685,9 +686,27 @@ void TextEditorWidget::update_html_preview()
|
|||
m_page_view->scroll_into_view(current_scroll_pos, true, true);
|
||||
}
|
||||
|
||||
void TextEditorWidget::update_statusbar_cursor_position()
|
||||
void TextEditorWidget::update_statusbar()
|
||||
{
|
||||
StringBuilder builder;
|
||||
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());
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ private:
|
|||
void update_preview();
|
||||
void update_markdown_preview();
|
||||
void update_html_preview();
|
||||
void update_statusbar_cursor_position();
|
||||
void update_statusbar();
|
||||
|
||||
virtual void drop_event(GUI::DropEvent&) override;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue