1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 15:47:35 +00:00

AK+Everywhere: Remove the null state of DeprecatedString

This commit removes DeprecatedString's "null" state, and replaces all
its users with one of the following:
- A normal, empty DeprecatedString
- Optional<DeprecatedString>

Note that null states of DeprecatedFlyString/StringView/etc are *not*
affected by this commit. However, DeprecatedString::empty() is now
considered equal to a null StringView.
This commit is contained in:
Ali Mohammad Pur 2023-10-10 15:00:58 +03:30 committed by Ali Mohammad Pur
parent daf6d8173c
commit aeee98b3a1
189 changed files with 597 additions and 652 deletions

View file

@ -89,9 +89,11 @@ void AbstractView::model_did_update(unsigned int flags)
m_drop_candidate_index = {};
selection().remove_all_matching([this](auto& index) { return !model()->is_within_range(index); });
auto index = find_next_search_match(m_highlighted_search.view());
if (index.is_valid())
highlight_search(index);
if (m_highlighted_search.has_value()) {
auto index = find_next_search_match(m_highlighted_search->view());
if (index.is_valid())
highlight_search(index);
}
}
m_selection_start_index = {};
}
@ -592,11 +594,11 @@ void AbstractView::keydown_event(KeyEvent& event)
if (is_searchable()) {
if (event.key() == KeyCode::Key_Backspace) {
if (!m_highlighted_search.is_null()) {
if (m_highlighted_search.has_value()) {
// if (event.modifiers() == Mod_Ctrl) {
// TODO: delete last word
// }
Utf8View view(m_highlighted_search);
Utf8View view(*m_highlighted_search);
size_t n_code_points = view.length();
if (n_code_points > 1) {
n_code_points--;
@ -621,7 +623,7 @@ void AbstractView::keydown_event(KeyEvent& event)
return;
}
} else if (event.key() == KeyCode::Key_Escape) {
if (!m_highlighted_search.is_null()) {
if (m_highlighted_search.has_value()) {
stop_highlighted_search_timer();
event.accept();
@ -629,7 +631,8 @@ void AbstractView::keydown_event(KeyEvent& event)
}
} else if (event.key() != KeyCode::Key_Tab && !event.ctrl() && !event.alt() && event.code_point() != 0) {
StringBuilder sb;
sb.append(m_highlighted_search);
if (m_highlighted_search.has_value())
sb.append(*m_highlighted_search);
sb.append_code_point(event.code_point());
auto index = find_next_search_match(sb.string_view());
@ -650,7 +653,7 @@ void AbstractView::keydown_event(KeyEvent& event)
void AbstractView::stop_highlighted_search_timer()
{
m_highlighted_search = nullptr;
m_highlighted_search.clear();
if (m_highlighted_search_timer)
m_highlighted_search_timer->stop();
if (m_highlighted_search_index.is_valid()) {
@ -723,8 +726,8 @@ void AbstractView::draw_item_text(Gfx::Painter& painter, ModelIndex const& index
text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
if (index == m_highlighted_search_index) {
auto const byte_offset = search_highlighting_offset < m_highlighted_search.length() ? 0 : item_text.length();
auto const byte_length = min(item_text.length() - byte_offset, m_highlighted_search.length() - search_highlighting_offset);
auto const byte_offset = search_highlighting_offset < m_highlighted_search.value_or("").length() ? 0 : item_text.length();
auto const byte_length = min(item_text.length() - byte_offset, m_highlighted_search.value_or("").length() - search_highlighting_offset);
Utf8View const searching_text(item_text.substring_view(byte_offset, byte_length));
// Highlight the text background first

View file

@ -199,7 +199,7 @@ private:
RefPtr<Model> m_model;
ModelSelection m_selection;
DeprecatedString m_highlighted_search;
Optional<DeprecatedString> m_highlighted_search;
RefPtr<Core::Timer> m_highlighted_search_timer;
SelectionBehavior m_selection_behavior { SelectionBehavior::SelectItems };
SelectionMode m_selection_mode { SelectionMode::SingleSelection };

View file

@ -48,9 +48,10 @@ ErrorOr<Optional<String>> FilePicker::get_filepath(Badge<FileSystemAccessServer:
ConnectionToWindowServer::the().set_window_parent_from_client(window_server_client_id, parent_window_id, picker->window_id());
if (picker->exec() == ExecResult::OK) {
auto file_path = TRY(String::from_deprecated_string(picker->selected_file()));
if (file_path.is_empty())
auto file_path = TRY(picker->selected_file().map([](auto& v) { return String::from_deprecated_string(v); }));
if (file_path.has_value() && file_path->is_empty())
return Optional<String> {};
return file_path;
}
return Optional<String> {};
@ -60,17 +61,12 @@ Optional<DeprecatedString> FilePicker::get_open_filepath(Window* parent_window,
{
auto picker = FilePicker::construct(parent_window, folder ? Mode::OpenFolder : Mode::Open, ""sv, path, screen_position, move(allowed_file_types));
if (!window_title.is_null())
if (!window_title.is_empty())
picker->set_title(window_title);
if (picker->exec() == ExecResult::OK) {
DeprecatedString file_path = picker->selected_file();
if (picker->exec() == ExecResult::OK)
return picker->selected_file();
if (file_path.is_null())
return {};
return file_path;
}
return {};
}
@ -78,14 +74,8 @@ Optional<DeprecatedString> FilePicker::get_save_filepath(Window* parent_window,
{
auto picker = FilePicker::construct(parent_window, Mode::Save, DeprecatedString::formatted("{}.{}", title, extension), path, screen_position);
if (picker->exec() == ExecResult::OK) {
DeprecatedString file_path = picker->selected_file();
if (file_path.is_null())
return {};
return file_path;
}
if (picker->exec() == ExecResult::OK)
return picker->selected_file();
return {};
}

View file

@ -44,7 +44,7 @@ public:
virtual ~FilePicker() override;
DeprecatedString const& selected_file() const { return m_selected_file; }
Optional<DeprecatedString> const& selected_file() const { return m_selected_file; }
private:
void on_file_return();
@ -77,7 +77,7 @@ private:
RefPtr<MultiView> m_view;
NonnullRefPtr<FileSystemModel> m_model;
DeprecatedString m_selected_file;
Optional<DeprecatedString> m_selected_file;
Vector<DeprecatedString> m_allowed_file_types_names;
Optional<Vector<FileTypeFilter>> m_allowed_file_types;

View file

@ -68,7 +68,7 @@ bool FileSystemModel::Node::fetch_data(DeprecatedString const& full_path, bool i
perror("readlink");
else {
symlink_target = sym_link_target_or_error.release_value().to_deprecated_string();
if (symlink_target.is_null())
if (symlink_target.is_empty())
perror("readlink");
}
}
@ -364,7 +364,7 @@ void FileSystemModel::update_node_on_selection(ModelIndex const& index, bool con
void FileSystemModel::set_root_path(DeprecatedString root_path)
{
if (root_path.is_null())
if (root_path.is_empty())
m_root_path = {};
else
m_root_path = LexicalPath::canonicalized_path(move(root_path));
@ -382,7 +382,7 @@ void FileSystemModel::invalidate()
{
m_root = adopt_own(*new Node(*this));
if (m_root_path.is_null())
if (m_root_path.is_empty())
m_root->m_parent_of_root = true;
m_root->reify_if_needed();

View file

@ -96,7 +96,10 @@ public:
virtual void set_value(Variant const& value, SelectionBehavior selection_behavior) override
{
auto& textbox = static_cast<TextBox&>(*widget());
textbox.set_text(value.to_deprecated_string());
if (value.is_valid())
textbox.set_text(value.to_deprecated_string());
else
textbox.clear();
if (selection_behavior == SelectionBehavior::SelectAll)
textbox.select_all();
}

View file

@ -202,10 +202,10 @@ void TableView::keydown_event(KeyEvent& event)
if (selection().size() > 1) {
selection().for_each_index([&](GUI::ModelIndex& index) {
begin_editing(index);
m_editing_delegate->set_value(DeprecatedString {});
m_editing_delegate->set_value(GUI::Variant {});
});
} else {
m_editing_delegate->set_value(DeprecatedString {});
m_editing_delegate->set_value(GUI::Variant {});
}
} else if (is_backspace) {
m_editing_delegate->set_value(DeprecatedString::empty());

View file

@ -98,7 +98,6 @@ public:
return visit(
[](Empty) { return false; },
[](Detail::Boolean v) { return v.value; },
[](DeprecatedString const& v) { return !v.is_null(); },
[](Integral auto v) { return v != 0; },
[](Gfx::IntPoint const& v) { return !v.is_zero(); },
[](OneOf<Gfx::IntRect, Gfx::IntSize> auto const& v) { return !v.is_empty(); },