0xc3 4 weeks ago
parent
commit
b5b7283dc0
4 changed files with 140 additions and 16 deletions
  1. 7 6
      CMakeLists.txt
  2. 87 10
      src/saura/cmd/sandbox/sandbox.cpp
  3. 7 0
      src/saura/cmd/tmp/CMakeLists.txt
  4. 39 0
      src/saura/cmd/tmp/tmp.cpp

+ 7 - 6
CMakeLists.txt

@@ -8,14 +8,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 # === CCACHE АВТОМАТИЧЕСКИЙ ЗАПУСК ===
 find_program(CCACHE_PROGRAM ccache)
 if(CCACHE_PROGRAM)
-    message(STATUS "ccache найден: ${CCACHE_PROGRAM}")
+ message(STATUS "ccache найден: ${CCACHE_PROGRAM}")
 
-    # Это работает и с Ninja, и с Makefiles
-    set(CMAKE_C_COMPILER_LAUNCHER   "${CCACHE_PROGRAM}")
-    set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
+ # Это работает и с Ninja, и с Makefiles
+ set(CMAKE_C_COMPILER_LAUNCHER   "${CCACHE_PROGRAM}")
+ set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
 
-    # Для старых версий CMake (< 3.4) можно использовать альтернативу:
-    # set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
+ # Для старых версий CMake (< 3.4) можно использовать альтернативу:
+ # set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
 endif()
 
 set(OUTPUT_DIR ${CMAKE_BINARY_DIR}/$<CONFIG>)
@@ -67,3 +67,4 @@ target_link_libraries(${PROJECT_NAME}_lib PUBLIC
 
 add_subdirectory(src/saura/cmd/sandbox)
 add_subdirectory(src/saura/cmd/tests)
+add_subdirectory(src/saura/cmd/tmp)

+ 87 - 10
src/saura/cmd/sandbox/sandbox.cpp

@@ -1,12 +1,12 @@
+#include <algorithm>
 #include <cpr/api.h>
+
+#include <cstddef>
 #include <cstring>
 #include <fmt/format.h>
 #include <memory>
-
-#include <spdlog/spdlog.h>
-
-#include <imgui.h>
-#include <imgui_internal.h>
+#include <span>
+#include <sstream>
 #include <string>
 #include <vector>
 
@@ -19,6 +19,10 @@
 #include <saura/core/app_base/app_base.hpp>
 #include <saura/core/os/os.hpp>
 
+#include <imgui.h>
+#include <imgui_internal.h>
+#include <spdlog/spdlog.h>
+
 namespace saura {
 static bool req_open_ppm = false;
 static bool is_add_node = false;
@@ -236,17 +240,90 @@ struct SandboxApp : AppBase {
 
           this->mem_arena_temp->begin(this->def_mem_arena.get());
 
+          // Тип
+          {
+            struct Item {
+              std::string name;
+              bool is_hovered;
+            };
+
+            auto join_items_by_hovered{[](const std::vector<Item> &items,
+                                          size_t limit) -> std::string {
+              std::ostringstream oss;
+              for (size_t i = 0; i < items.size(); ++i) {
+                if (!items[i].is_hovered)
+                  continue;
+
+                if (i == limit) {
+                  oss << "...";
+                  break;
+                }
+                if (i > 0)
+                  oss << ", ";
+
+                oss << items[i].name;
+              }
+              return oss.str();
+            }};
+
+            static std::vector<Item> items = {
+                {"Нет", false},
+                {"Физ", false},
+                {"Юр", false},
+            };
+
+            static int current_item = 0;
+            static std::string title = "Нет";
+
+            if (ImGui::BeginCombo("Тип", title.c_str())) {
+              if (ImGui::IsWindowAppearing()) {
+              }
+
+              // TODO(0xc3): почистить
+              for (int i = 0; i < items.size(); i++) {
+                if (i == 0) {
+                  if (ImGui::Selectable(
+                          items[0].name.c_str(), false,
+                          ImGuiSelectableFlags_NoAutoClosePopups)) {
+                    for (auto &item : items) {
+                      item.is_hovered = false;
+                    }
+                    title = items[0].name;
+                  }
+                } else {
+                  if (!items[i].is_hovered) {
+                    if (ImGui::Selectable(
+                            items[i].name.c_str(), false,
+                            ImGuiSelectableFlags_NoAutoClosePopups)) {
+                      items[i].is_hovered = true;
+                    }
+                  } else {
+                    if (ImGui::Selectable(
+                            items[i].name.c_str(), false,
+                            ImGuiSelectableFlags_NoAutoClosePopups |
+                                ImGuiSelectableFlags_Highlight)) {
+                      items[i].is_hovered = false;
+                    }
+                  }
+                }
+              }
+
+              title = join_items_by_hovered(items, 3);
+
+              ImGui::EndCombo();
+            }
+          }
+
           // Теги
           {
             // TODO: get form server (DB)
-            static std::vector<std::string> items;
-            static int current_item;
-
-            std::string preview_value = items.size() == 0 ? "None" : items.at(current_item);
+            static std::vector<std::string> items = {"Нет"};
+            static int current_item = 0;
 
-            if (ImGui::BeginCombo("Теги", preview_value.c_str())) {
+            if (ImGui::BeginCombo("Теги", items[current_item].c_str())) {
               if (ImGui::IsWindowAppearing()) {
                 items.clear();
+                items.push_back("Нет");
                 auto res = cpr::Get(
                     cpr::Url(
                         "http://api.localhost:8090/v1/example/client/tags"),

+ 7 - 0
src/saura/cmd/tmp/CMakeLists.txt

@@ -0,0 +1,7 @@
+get_filename_component(TARGET_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+
+file(GLOB SOURCES "*.cpp")
+
+add_executable(${TARGET_NAME} ${SOURCES})
+
+target_link_libraries(${TARGET_NAME} PRIVATE ${PROJECT_NAME}_lib)

+ 39 - 0
src/saura/cmd/tmp/tmp.cpp

@@ -0,0 +1,39 @@
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <vector>
+
+std::string join_with_limit(const std::vector<std::string> &items, size_t limit) {
+  if (items.empty()) {
+    return "";
+  }
+
+  std::ostringstream oss;
+  for (size_t i = 0; i < items.size(); ++i) {
+    if (i == limit) {
+      oss << "...";
+      break;
+    }
+
+    if (i > 0) {
+      oss << ", ";
+    }
+
+    oss << items[i];
+  }
+
+  return oss.str();
+}
+
+int main() {
+  // Пример 1: Много элементов
+  std::vector<std::string> fruits = {"банан", "яблоко", "груша", "апельсин",
+                                     "киви"};
+  std::cout << "Много: " << join_with_limit(fruits, 3) << std::endl;
+
+  // Пример 2: Мало элементов (меньше лимита)
+  std::vector<std::string> few_fruits = {"банан", "яблоко"};
+  std::cout << "Мало:  " << join_with_limit(few_fruits, 3) << std::endl;
+
+  return 0;
+}