Remove duplicate code with new sort_slices_by_name() function

This commit is contained in:
David Capello 2024-07-19 16:47:13 -03:00
parent 6047ab69de
commit 9da10605d3
4 changed files with 35 additions and 27 deletions

View File

@ -1,4 +1,5 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
// Copyright (C) 2017 David Capello
//
// This program is distributed under the terms of
@ -18,12 +19,12 @@ namespace app {
class MatchWords {
public:
MatchWords(const std::string& search) {
MatchWords(const std::string& search = {}) {
base::split_string(base::string_to_lower(search),
m_parts, " ");
}
bool operator()(const std::string& item) {
bool operator()(const std::string& item) const {
std::string lowerItem = base::string_to_lower(item);
std::size_t matches = 0;

View File

@ -45,11 +45,11 @@
#include "app/ui/expr_entry.h"
#include "app/ui/icon_button.h"
#include "app/ui/keyboard_shortcuts.h"
#include "app/ui/layer_frame_comboboxes.h"
#include "app/ui/sampling_selector.h"
#include "app/ui/selection_mode_field.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui_context.h"
#include "base/fs.h"
#include "base/pi.h"
#include "base/scoped_value.h"
#include "doc/brush.h"
@ -1738,19 +1738,8 @@ private:
void fillSlices() {
m_combobox.deleteAllItems();
if (m_doc && m_doc->sprite()) {
MatchWords match(m_filter);
std::vector<doc::Slice*> slices;
for (auto slice : m_doc->sprite()->slices()) {
if (match(slice->name()))
slices.push_back(slice);
}
std::sort(slices.begin(), slices.end(),
[](const doc::Slice* a, const doc::Slice* b){
return (base::compare_filenames(a->name(), b->name()) < 0);
});
for (auto slice : slices) {
for (auto* slice : sort_slices_by_name(m_doc->sprite()->slices(),
MatchWords(m_filter))) {
Item* item = new Item(slice);
m_combobox.addItem(item);
}

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2016-2018 David Capello
//
// This program is distributed under the terms of
@ -13,17 +13,19 @@
#include "app/doc.h"
#include "app/i18n/strings.h"
#include "app/match_words.h"
#include "app/restore_visible_layers.h"
#include "app/site.h"
#include "base/fs.h"
#include "doc/anidir.h"
#include "doc/frames_sequence.h"
#include "doc/layer.h"
#include "doc/playback.h"
#include "doc/selected_frames.h"
#include "doc/selected_layers.h"
#include "doc/slice.h"
#include "doc/sprite.h"
#include "doc/tag.h"
#include "doc/playback.h"
#include "ui/combobox.h"
namespace app {
@ -84,17 +86,11 @@ void fill_area_combobox(const doc::Sprite* sprite, ui::ComboBox* area, const std
if (defArea == kSelectedCanvas)
area->setSelectedItemIndex(i);
std::vector<doc::Slice*> sliceList;
for (auto* slice : sprite->slices()) {
for (auto* slice : sort_slices_by_name(sprite->slices(),
MatchWords())) {
if (slice->name().empty())
continue;
sliceList.push_back(slice);
}
std::sort(sliceList.begin(),
sliceList.end(),
[](doc::Slice* a, doc::Slice* b) { return a->name() < b->name(); });
for (auto* slice : sliceList) {
i = area->addItem(new SliceListItem(slice));
if (defArea == slice->name())
area->setSelectedItemIndex(i);
@ -320,4 +316,20 @@ doc::Tag* calculate_selected_frames(const Site& site,
return tag;
}
std::vector<doc::Slice*> sort_slices_by_name(const doc::Slices& slices,
const MatchWords& match)
{
std::vector<doc::Slice*> result;
for (auto* slice : slices) {
if (match(slice->name()))
result.push_back(slice);
}
std::sort(result.begin(),
result.end(),
[](const doc::Slice* a, const doc::Slice* b) {
return (base::compare_filenames(a->name(), b->name()) < 0);
});
return result;
}
} // namespace app

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2016-2018 David Capello
//
// This program is distributed under the terms of
@ -13,6 +13,7 @@
#include "ui/listitem.h"
#include <string>
#include <vector>
namespace doc {
class Layer;
@ -20,6 +21,7 @@ namespace doc {
class FramesSequence;
class SelectedLayers;
class Slice;
class Slices;
class Sprite;
class Tag;
}
@ -29,6 +31,7 @@ namespace ui {
}
namespace app {
class MatchWords;
class RestoreVisibleLayers;
class Site;
@ -85,6 +88,9 @@ namespace app {
const std::string& framesValue,
doc::SelectedFrames& selFrames);
std::vector<doc::Slice*> sort_slices_by_name(const doc::Slices& slices,
const MatchWords& match);
} // namespace app
#endif