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.
This commit is contained in:
parent
d47e431d54
commit
92fffc3abc
Notes:
sideshowbarker
2024-07-18 03:33:54 +09:00
Author: https://github.com/thankyouverycool Commit: https://github.com/SerenityOS/serenity/commit/92fffc3abc5 Pull-request: https://github.com/SerenityOS/serenity/pull/10175
15 changed files with 40 additions and 39 deletions
|
@ -41,7 +41,7 @@ public:
|
|||
|
||||
m_audio_client->on_main_mix_volume_change = [this](double volume) {
|
||||
m_audio_volume = static_cast<int>(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();
|
||||
};
|
||||
|
|
|
@ -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 { }
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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); }
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<void(int value)> on_change;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <LibGUI/Forward.h>
|
||||
#include <LibGUI/TextRange.h>
|
||||
#include <LibGUI/UndoStack.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibGfx/Color.h>
|
||||
#include <LibGfx/TextAttributes.h>
|
||||
#include <LibRegex/Regex.h>
|
||||
|
@ -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<TextDocumentSpan> spans) { m_spans = move(spans); }
|
||||
|
||||
bool set_text(const StringView&);
|
||||
bool set_text(const StringView&, AllowCallback = AllowCallback::Yes);
|
||||
|
||||
const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return m_lines; }
|
||||
NonnullOwnPtrVector<TextDocumentLine>& lines() { return m_lines; }
|
||||
|
|
|
@ -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<LineVisualData>());
|
||||
document_did_change();
|
||||
document_did_change(allow_callback);
|
||||
}
|
||||
|
||||
void TextEditor::document_did_set_cursor(TextPosition const& position)
|
||||
|
|
|
@ -108,7 +108,7 @@ public:
|
|||
Function<void()> on_focusin;
|
||||
Function<void()> 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;
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -54,7 +54,7 @@ enum class FocusPolicy {
|
|||
|
||||
AK_ENUM_BITWISE_OPERATORS(FocusPolicy)
|
||||
|
||||
enum class CallOnChange {
|
||||
enum class AllowCallback {
|
||||
No,
|
||||
Yes
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue