From 92fffc3abc50dfd6a7cf6788984f712cece0b7d8 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Tue, 21 Sep 2021 17:02:48 -0400 Subject: [PATCH] LibGUI: Rename CallOnChange => AllowCallback and implement elsewhere This is a helpful option to prevent unwanted side effects, distinguish between user and programmatic input, etc. Sliders and SpinBoxes were implementing it idiosyncratically, so let's generalize the API and give Buttons and TextEditors the same ability. --- Userland/Applets/Audio/main.cpp | 2 +- .../HackStudio/LanguageServers/FileDB.cpp | 4 ++-- Userland/Libraries/LibGUI/AbstractButton.cpp | 4 ++-- Userland/Libraries/LibGUI/AbstractButton.h | 2 +- Userland/Libraries/LibGUI/AbstractSlider.cpp | 4 ++-- Userland/Libraries/LibGUI/AbstractSlider.h | 2 +- Userland/Libraries/LibGUI/SpinBox.cpp | 8 ++++---- Userland/Libraries/LibGUI/SpinBox.h | 8 ++++---- Userland/Libraries/LibGUI/TextDocument.cpp | 4 ++-- Userland/Libraries/LibGUI/TextDocument.h | 7 ++++--- Userland/Libraries/LibGUI/TextEditor.cpp | 18 +++++++++--------- Userland/Libraries/LibGUI/TextEditor.h | 8 ++++---- Userland/Libraries/LibGUI/ValueSlider.cpp | 4 ++-- Userland/Libraries/LibGUI/ValueSlider.h | 2 +- Userland/Libraries/LibGUI/Widget.h | 2 +- 15 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Userland/Applets/Audio/main.cpp b/Userland/Applets/Audio/main.cpp index a469b8d5d6d..3b5fdb00910 100644 --- a/Userland/Applets/Audio/main.cpp +++ b/Userland/Applets/Audio/main.cpp @@ -41,7 +41,7 @@ public: m_audio_client->on_main_mix_volume_change = [this](double volume) { m_audio_volume = static_cast(round(volume * 100)); - m_slider->set_value(m_slider->max() - m_audio_volume, GUI::CallOnChange::No); + m_slider->set_value(m_slider->max() - m_audio_volume, GUI::AllowCallback::No); if (!m_audio_muted) update(); }; diff --git a/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp b/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp index 73f74d7d0c4..e3f91b9124a 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp @@ -101,8 +101,8 @@ public: virtual void document_did_insert_line(size_t) override {}; virtual void document_did_remove_line(size_t) override {}; virtual void document_did_remove_all_lines() override {}; - virtual void document_did_change() override {}; - virtual void document_did_set_text() override {}; + virtual void document_did_change(GUI::AllowCallback) override {}; + virtual void document_did_set_text(GUI::AllowCallback) override {}; virtual void document_did_set_cursor(const GUI::TextPosition&) override {}; virtual void document_did_update_undo_stack() override { } diff --git a/Userland/Libraries/LibGUI/AbstractButton.cpp b/Userland/Libraries/LibGUI/AbstractButton.cpp index f23a0aa46a2..cb9c465924d 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.cpp +++ b/Userland/Libraries/LibGUI/AbstractButton.cpp @@ -44,7 +44,7 @@ void AbstractButton::set_text(String text) update(); } -void AbstractButton::set_checked(bool checked) +void AbstractButton::set_checked(bool checked, AllowCallback allow_callback) { if (m_checked == checked) return; @@ -71,7 +71,7 @@ void AbstractButton::set_checked(bool checked) } update(); - if (on_checked) + if (on_checked && allow_callback == AllowCallback::Yes) on_checked(checked); } diff --git a/Userland/Libraries/LibGUI/AbstractButton.h b/Userland/Libraries/LibGUI/AbstractButton.h index 23572bd9a04..0748c46f4ca 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.h +++ b/Userland/Libraries/LibGUI/AbstractButton.h @@ -26,7 +26,7 @@ public: void set_exclusive(bool b) { m_exclusive = b; } bool is_checked() const { return m_checked; } - void set_checked(bool); + void set_checked(bool, AllowCallback = AllowCallback::Yes); bool is_checkable() const { return m_checkable; } void set_checkable(bool); diff --git a/Userland/Libraries/LibGUI/AbstractSlider.cpp b/Userland/Libraries/LibGUI/AbstractSlider.cpp index c6109ad6cf7..97b353b3c4c 100644 --- a/Userland/Libraries/LibGUI/AbstractSlider.cpp +++ b/Userland/Libraries/LibGUI/AbstractSlider.cpp @@ -53,7 +53,7 @@ void AbstractSlider::set_range(int min, int max) update(); } -void AbstractSlider::set_value(int value, CallOnChange call_on_change) +void AbstractSlider::set_value(int value, AllowCallback allow_callback) { value = clamp(value, m_min, m_max); if (m_value == value) @@ -61,7 +61,7 @@ void AbstractSlider::set_value(int value, CallOnChange call_on_change) m_value = value; update(); - if (on_change && call_on_change == CallOnChange::Yes) + if (on_change && allow_callback == AllowCallback::Yes) on_change(m_value); } diff --git a/Userland/Libraries/LibGUI/AbstractSlider.h b/Userland/Libraries/LibGUI/AbstractSlider.h index dc6204e9e59..a254eb48088 100644 --- a/Userland/Libraries/LibGUI/AbstractSlider.h +++ b/Userland/Libraries/LibGUI/AbstractSlider.h @@ -30,7 +30,7 @@ public: bool is_max() const { return m_value == m_max; } void set_range(int min, int max); - virtual void set_value(int, CallOnChange call_on_change = CallOnChange::Yes); + virtual void set_value(int, AllowCallback = AllowCallback::Yes); void set_min(int min) { set_range(min, max()); } void set_max(int max) { set_range(min(), max); } diff --git a/Userland/Libraries/LibGUI/SpinBox.cpp b/Userland/Libraries/LibGUI/SpinBox.cpp index 76a314e5367..fcadebcc7ef 100644 --- a/Userland/Libraries/LibGUI/SpinBox.cpp +++ b/Userland/Libraries/LibGUI/SpinBox.cpp @@ -53,7 +53,7 @@ SpinBox::~SpinBox() { } -void SpinBox::set_value(int value) +void SpinBox::set_value(int value, AllowCallback allow_callback) { value = clamp(value, m_min, m_max); if (m_value == value) @@ -61,11 +61,11 @@ void SpinBox::set_value(int value) m_value = value; m_editor->set_text(String::number(value)); update(); - if (on_change) + if (on_change && allow_callback == AllowCallback::Yes) on_change(value); } -void SpinBox::set_range(int min, int max, bool change) +void SpinBox::set_range(int min, int max, AllowCallback allow_callback) { VERIFY(min <= max); if (m_min == min && m_max == max) @@ -78,7 +78,7 @@ void SpinBox::set_range(int min, int max, bool change) m_value = clamp(m_value, m_min, m_max); if (m_value != old_value) { m_editor->set_text(String::number(m_value)); - if (change && on_change) + if (on_change && allow_callback == AllowCallback::Yes) on_change(m_value); } diff --git a/Userland/Libraries/LibGUI/SpinBox.h b/Userland/Libraries/LibGUI/SpinBox.h index 8fc46f5f5cb..b71b900dde6 100644 --- a/Userland/Libraries/LibGUI/SpinBox.h +++ b/Userland/Libraries/LibGUI/SpinBox.h @@ -16,13 +16,13 @@ public: virtual ~SpinBox() override; int value() const { return m_value; } - void set_value(int); + void set_value(int, AllowCallback = AllowCallback::Yes); int min() const { return m_min; } int max() const { return m_max; } - void set_min(int min) { set_range(min, max()); } - void set_max(int max) { set_range(min(), max); } - void set_range(int min, int max, bool change = true); + void set_min(int min, AllowCallback allow_callback = AllowCallback::Yes) { set_range(min, max(), allow_callback); } + void set_max(int max, AllowCallback allow_callback = AllowCallback::Yes) { set_range(min(), max, allow_callback); } + void set_range(int min, int max, AllowCallback = AllowCallback::Yes); Function on_change; diff --git a/Userland/Libraries/LibGUI/TextDocument.cpp b/Userland/Libraries/LibGUI/TextDocument.cpp index c8874aa8cf6..db5f0d42312 100644 --- a/Userland/Libraries/LibGUI/TextDocument.cpp +++ b/Userland/Libraries/LibGUI/TextDocument.cpp @@ -39,7 +39,7 @@ TextDocument::~TextDocument() { } -bool TextDocument::set_text(const StringView& text) +bool TextDocument::set_text(const StringView& text, AllowCallback allow_callback) { m_client_notifications_enabled = false; m_undo_stack.clear(); @@ -90,7 +90,7 @@ bool TextDocument::set_text(const StringView& text) m_client_notifications_enabled = true; for (auto* client : m_clients) - client->document_did_set_text(); + client->document_did_set_text(allow_callback); clear_text_guard.disarm(); diff --git a/Userland/Libraries/LibGUI/TextDocument.h b/Userland/Libraries/LibGUI/TextDocument.h index e1617694b55..17218725b50 100644 --- a/Userland/Libraries/LibGUI/TextDocument.h +++ b/Userland/Libraries/LibGUI/TextDocument.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -44,8 +45,8 @@ public: virtual void document_did_insert_line(size_t) = 0; virtual void document_did_remove_line(size_t) = 0; virtual void document_did_remove_all_lines() = 0; - virtual void document_did_change() = 0; - virtual void document_did_set_text() = 0; + virtual void document_did_change(AllowCallback = AllowCallback::Yes) = 0; + virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) = 0; virtual void document_did_set_cursor(const TextPosition&) = 0; virtual void document_did_update_undo_stack() = 0; @@ -62,7 +63,7 @@ public: void set_spans(Vector spans) { m_spans = move(spans); } - bool set_text(const StringView&); + bool set_text(const StringView&, AllowCallback = AllowCallback::Yes); const NonnullOwnPtrVector& lines() const { return m_lines; } NonnullOwnPtrVector& lines() { return m_lines; } diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index b5ba7837149..425da4e01b8 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -101,11 +101,11 @@ void TextEditor::create_actions() m_select_all_action = CommonActions::make_select_all_action([this](auto&) { select_all(); }, this); } -void TextEditor::set_text(StringView const& text) +void TextEditor::set_text(StringView const& text, AllowCallback allow_callback) { m_selection.clear(); - document().set_text(text); + document().set_text(text, allow_callback); update_content_size(); recompute_all_visual_lines(); @@ -1480,7 +1480,7 @@ void TextEditor::leave_event(Core::Event&) m_automatic_selection_scroll_timer->start(); } -void TextEditor::did_change() +void TextEditor::did_change(AllowCallback allow_callback) { update_content_size(); recompute_all_visual_lines(); @@ -1492,9 +1492,9 @@ void TextEditor::did_change() m_needs_rehighlight = true; if (!m_has_pending_change_notification) { m_has_pending_change_notification = true; - deferred_invoke([this] { + deferred_invoke([this, allow_callback] { m_has_pending_change_notification = false; - if (on_change) + if (on_change && allow_callback == AllowCallback::Yes) on_change(); }); } @@ -1784,9 +1784,9 @@ void TextEditor::document_did_insert_line(size_t line_index) update(); } -void TextEditor::document_did_change() +void TextEditor::document_did_change(AllowCallback allow_callback) { - did_change(); + did_change(allow_callback); update(); } @@ -1814,12 +1814,12 @@ void TextEditor::document_did_update_undo_stack() on_modified_change(document().is_modified()); } -void TextEditor::document_did_set_text() +void TextEditor::document_did_set_text(AllowCallback allow_callback) { m_line_visual_data.clear(); for (size_t i = 0; i < m_document->line_count(); ++i) m_line_visual_data.append(make()); - document_did_change(); + document_did_change(allow_callback); } void TextEditor::document_did_set_cursor(TextPosition const& position) diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index a0ea51005e5..c291e38c490 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -108,7 +108,7 @@ public: Function on_focusin; Function on_focusout; - void set_text(StringView const&); + void set_text(StringView const&, AllowCallback = AllowCallback::Yes); void scroll_cursor_into_view(); void scroll_position_into_view(TextPosition const&); size_t line_count() const { return document().line_count(); } @@ -195,7 +195,7 @@ public: TextRange* selection() { return &m_selection; }; void did_update_selection(); - void did_change(); + void did_change(AllowCallback = AllowCallback::Yes); void update_cursor(); void add_code_point(u32 code_point); @@ -248,8 +248,8 @@ private: virtual void document_did_insert_line(size_t) override; virtual void document_did_remove_line(size_t) override; virtual void document_did_remove_all_lines() override; - virtual void document_did_change() override; - virtual void document_did_set_text() override; + virtual void document_did_change(AllowCallback = AllowCallback::Yes) override; + virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) override; virtual void document_did_set_cursor(TextPosition const&) override; virtual void document_did_update_undo_stack() override; diff --git a/Userland/Libraries/LibGUI/ValueSlider.cpp b/Userland/Libraries/LibGUI/ValueSlider.cpp index 227c8ba6079..4033a4e31eb 100644 --- a/Userland/Libraries/LibGUI/ValueSlider.cpp +++ b/Userland/Libraries/LibGUI/ValueSlider.cpp @@ -141,9 +141,9 @@ int ValueSlider::value_at(const Gfx::IntPoint& position) const return (int)(relative_offset * (float)max()); } -void ValueSlider::set_value(int value, CallOnChange call_on_change) +void ValueSlider::set_value(int value, AllowCallback allow_callback) { - AbstractSlider::set_value(value, call_on_change); + AbstractSlider::set_value(value, allow_callback); m_textbox->set_text(formatted_value()); } diff --git a/Userland/Libraries/LibGUI/ValueSlider.h b/Userland/Libraries/LibGUI/ValueSlider.h index b9a3c91f3ca..8fa3e8c2cce 100644 --- a/Userland/Libraries/LibGUI/ValueSlider.h +++ b/Userland/Libraries/LibGUI/ValueSlider.h @@ -24,7 +24,7 @@ public: void set_suffix(String suffix) { m_suffix = move(suffix); } void set_knob_style(KnobStyle knobstyle) { m_knob_style = knobstyle; } - virtual void set_value(int value, CallOnChange call_on_change = CallOnChange::Yes) override; + virtual void set_value(int value, AllowCallback = AllowCallback::Yes) override; protected: virtual void paint_event(PaintEvent&) override; diff --git a/Userland/Libraries/LibGUI/Widget.h b/Userland/Libraries/LibGUI/Widget.h index e90f1c6269d..b4d97b515c3 100644 --- a/Userland/Libraries/LibGUI/Widget.h +++ b/Userland/Libraries/LibGUI/Widget.h @@ -54,7 +54,7 @@ enum class FocusPolicy { AK_ENUM_BITWISE_OPERATORS(FocusPolicy) -enum class CallOnChange { +enum class AllowCallback { No, Yes };