From 1599e9fd04a4f8dd48e4d22070ae1522bc34a693 Mon Sep 17 00:00:00 2001 From: Vankata453 <78196474+Vankata453@users.noreply.github.com> Date: Tue, 16 Jul 2024 12:39:53 +0300 Subject: [PATCH] Add `Menu::add_item(...)` template function Used for initializing and adding a `MenuItem` to the menu. Reduces code duplication with `Menu::add_*` functions. --- src/gui/menu.cpp | 155 ++++++++++++----------------------------------- src/gui/menu.hpp | 8 +++ 2 files changed, 46 insertions(+), 117 deletions(-) diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 86aefee9acf..8c235b05dcf 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -145,110 +145,74 @@ Menu::delete_item(int pos_) ItemHorizontalLine& Menu::add_hl() { - auto item = std::make_unique(); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(); } ItemLabel& Menu::add_label(const std::string& text) { - auto item = std::make_unique(text); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text); } ItemControlField& Menu::add_controlfield(int id, const std::string& text, const std::string& mapping) { - auto item = std::make_unique(text, mapping, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, mapping, id); } ItemTextField& Menu::add_textfield(const std::string& text, std::string* input, int id) { - auto item = std::make_unique(text, input, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, input, id); } ItemScript& Menu::add_script(const std::string& text, std::string* script, int id) { - auto item = std::make_unique(text, script, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, script, id); } ItemScriptLine& Menu::add_script_line(std::string* input, int id) { - auto item = std::make_unique(input, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(input, id); } ItemIntField& Menu::add_intfield(const std::string& text, int* input, int id, bool positive) { - auto item = std::make_unique(text, input, id, positive); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, input, id, positive); } ItemFloatField& Menu::add_floatfield(const std::string& text, float* input, int id, bool positive) { - auto item = std::make_unique(text, input, id, positive); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, input, id, positive); } ItemAction& Menu::add_entry(int id, const std::string& text) { - auto item = std::make_unique(text, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, id); } ItemAction& Menu::add_entry(const std::string& text, const std::function& callback) { - auto item = std::make_unique(text, -1, callback); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, -1, callback); } ItemInactive& Menu::add_inactive(const std::string& text, bool default_color) { - auto item = std::make_unique(text, default_color); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, default_color); } ItemToggle& Menu::add_toggle(int id, const std::string& text, bool* toggled, bool center_text) { - auto item = std::make_unique(text, toggled, id, center_text); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, toggled, id, center_text); } ItemToggle& @@ -257,28 +221,19 @@ Menu::add_toggle(int id, const std::string& text, const std::function& set_func, bool center_text) { - auto item = std::make_unique(text, get_func, set_func, id, center_text); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, get_func, set_func, id, center_text); } ItemStringSelect& Menu::add_string_select(int id, const std::string& text, int* selected, const std::vector& strings) { - auto item = std::make_unique(text, strings, selected, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, strings, selected, id); } ItemStringSelect& Menu::add_string_select(int id, const std::string& text, int default_item, const std::vector& strings) { - auto item = std::make_unique(text, strings, default_item, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, strings, default_item, id); } ItemAction& @@ -286,118 +241,84 @@ Menu::add_file(const std::string& text, std::string* input, const std::vector& item_processor, int id) { - auto item = std::make_unique(text, id, + return add_item(text, id, [input, extensions, basedir, path_relative_to_basedir, item_processor]() { MenuManager::instance().push_menu(std::make_unique(input, extensions, basedir, path_relative_to_basedir, nullptr, item_processor)); }); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; } ItemBack& Menu::add_back(const std::string& text, int id) { - auto item = std::make_unique(text, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, id); } ItemGoTo& Menu::add_submenu(const std::string& text, int submenu, int id) { - auto item = std::make_unique(text, submenu, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, submenu, id); } ItemColorChannelRGBA& -Menu::add_color_channel_rgba(float* input, Color channel, int id, bool is_linear) { - auto item = std::make_unique(input, channel, id, is_linear); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; +Menu::add_color_channel_rgba(float* input, Color channel, int id, bool is_linear) +{ + return add_item(input, channel, id, is_linear); } ItemColorChannelOKLab& -Menu::add_color_channel_oklab(Color* color, int channel) { - auto item = std::make_unique(color, channel, this); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; +Menu::add_color_channel_oklab(Color* color, int channel) +{ + return add_item(color, channel, this); } ItemPaths& -Menu::add_path_settings(const std::string& text, PathObject& target, const std::string& path_ref) { - auto item = std::make_unique(text, target, path_ref); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; +Menu::add_path_settings(const std::string& text, PathObject& target, const std::string& path_ref) +{ + return add_item(text, target, path_ref); } ItemColorDisplay& -Menu::add_color_display(Color* color, int id) { - auto item = std::make_unique(color, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; +Menu::add_color_display(Color* color, int id) +{ + return add_item(color, id); } ItemColor& -Menu::add_color(const std::string& text, Color* color, int id) { - auto item = std::make_unique(text, color, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; +Menu::add_color(const std::string& text, Color* color, int id) +{ + return add_item(text, color, id); } ItemStringArray& Menu::add_string_array(const std::string& text, std::vector& items, int id) { - auto item = std::make_unique(text, items, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, items, id); } ItemImages& Menu::add_images(const std::string& image_path, int max_image_width, int max_image_height, int id) { - auto item = std::make_unique(image_path, max_image_width, max_image_height, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(image_path, max_image_width, max_image_height, id); } ItemImages& Menu::add_images(const std::vector& image_paths, int max_image_width, int max_image_height, int id) { - auto item = std::make_unique(image_paths, max_image_width, max_image_height, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(image_paths, max_image_width, max_image_height, id); } ItemList& Menu::add_list(const std::string& text, const std::vector& items, std::string* value_ptr, int id) { - auto item = std::make_unique(text, items, value_ptr, id); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(text, items, value_ptr, id); } ItemHorizontalMenu& Menu::add_horizontalmenu(int id, float height, float min_item_width) { - auto item = std::make_unique(id, height, min_item_width); - auto item_ptr = item.get(); - add_item(std::move(item)); - return *item_ptr; + return add_item(id, height, min_item_width); } void diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 954d8f188d3..0e0a1d05053 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -133,6 +133,14 @@ class Menu protected: MenuItem& add_item(std::unique_ptr menu_item); MenuItem& add_item(std::unique_ptr menu_item, int pos_); + template + T& add_item(Args&&... args) + { + auto item = std::make_unique(std::forward(args)...); + auto item_ptr = item.get(); + add_item(std::move(item)); + return *item_ptr; + } void delete_item(int pos_); /** Recalculates the width for this menu */