From 9da10605d32870445e9226a4c3d76207268f601f Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 19 Jul 2024 16:47:13 -0300 Subject: [PATCH 1/2] Remove duplicate code with new sort_slices_by_name() function --- src/app/match_words.h | 5 +++-- src/app/ui/context_bar.cpp | 17 +++----------- src/app/ui/layer_frame_comboboxes.cpp | 32 ++++++++++++++++++--------- src/app/ui/layer_frame_comboboxes.h | 8 ++++++- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/app/match_words.h b/src/app/match_words.h index 3113b8e408e..abcf5a81039 100644 --- a/src/app/match_words.h +++ b/src/app/match_words.h @@ -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; diff --git a/src/app/ui/context_bar.cpp b/src/app/ui/context_bar.cpp index 3a594e97d96..4137c39b935 100644 --- a/src/app/ui/context_bar.cpp +++ b/src/app/ui/context_bar.cpp @@ -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 @@ class ContextBar::SliceFields : public HBox { void fillSlices() { m_combobox.deleteAllItems(); if (m_doc && m_doc->sprite()) { - MatchWords match(m_filter); - - std::vector 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); } diff --git a/src/app/ui/layer_frame_comboboxes.cpp b/src/app/ui/layer_frame_comboboxes.cpp index d2fd222365b..285f93fa586 100644 --- a/src/app/ui/layer_frame_comboboxes.cpp +++ b/src/app/ui/layer_frame_comboboxes.cpp @@ -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 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 sort_slices_by_name(const doc::Slices& slices, + const MatchWords& match) +{ + std::vector 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 diff --git a/src/app/ui/layer_frame_comboboxes.h b/src/app/ui/layer_frame_comboboxes.h index ea0f16412b4..20bb205078b 100644 --- a/src/app/ui/layer_frame_comboboxes.h +++ b/src/app/ui/layer_frame_comboboxes.h @@ -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 +#include 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 sort_slices_by_name(const doc::Slices& slices, + const MatchWords& match); + } // namespace app #endif From 3c4483183150f4adeb898013ae0a45a33f8058ac Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 19 Jul 2024 17:10:39 -0300 Subject: [PATCH 2/2] We can use = {} for default arg values in function declarations --- docs/CODING_STYLE.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/CODING_STYLE.md b/docs/CODING_STYLE.md index 5f403994ee8..747bc38fb01 100644 --- a/docs/CODING_STYLE.md +++ b/docs/CODING_STYLE.md @@ -156,5 +156,11 @@ targetting macOS 10.9, some notes are added about this: * You can use ``, ``, ``, and `` * Prefer `using T = ...;` instead of `typedef ... T` * Use `[[fallthrough]]` if needed +* Use `= {}` only to specify a default argument value of an + user-defined type in a function declaration, e.g. + `void func(const std::string& s = {}) { ... }`. + In other cases (e.g. a member variable of an user-defined type) + it's not required or we prefer to use the explicit value + for built-in types (`int m_var = 0;`). * We use gcc 9.2 or clang 9.0 on Linux, so check the features available in https://en.cppreference.com/w/cpp/compiler_support