#include "GUI.h" #include "imgui.h" #include "backends/imgui_impl_glfw.h" #include "backends/imgui_impl_opengl3.h" #include "misc/freetype/imgui_freetype.h" #include #include "imgui_internal.h" #include "../config.h" #include "../youtube/api/AuthedBackend.h" #include "widgets/GUIWidgets.h" GUI::GUI(Innertube* tube, Config::Config* config, AuthedBackend* backend) { this->tube = tube; this->config = config; this->chat_backend = backend; this->initialized = false; this->window = nullptr; this->registry = new ImageRegistry(); } void GUI::Init() { assert(this->initialized == false); this->initialized = true; glfwInit(); const char* glsl_version = "#version 130"; glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); this->window = glfwCreateWindow(400, 720, "youtuberino", nullptr, nullptr); glfwMakeContextCurrent(window); glfwSwapInterval(1); IMGUI_CHECKVERSION(); ImGui::CreateContext(); // set style colors ImGui::StyleColorsDark(); ImVec4* colors = ImGui::GetStyle().Colors; colors[ImGuiCol_PlotHistogram] = ImVec4(0.26f, 0.59f, 0.97f, 1.00f); ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init(glsl_version); // load fonts auto io = ImGui::GetIO(); io.Fonts->AddFontFromFileTTF("comic.ttf"); ImFontConfig config; config.MergeMode = true; io.Fonts->AddFontFromFileTTF("arial.ttf", 0.0f, &config); config.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_LoadColor; io.Fonts->AddFontFromFileTTF("seguiemj.ttf", 0.0f, &config); io.Fonts->AddFontFromFileTTF("msgothic.ttc", 0.0f, &config); io.Fonts->Build(); // also, load the last video if (!this->config->lastVideoId.empty()) { this->state.videoId = this->config->lastVideoId; tube->Connect(this->config->lastVideoId); } } void GUI::Shutdown() { assert(this->initialized == true); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(this->window); glfwTerminate(); } void GUI::Run() { while (!glfwWindowShouldClose(this->window)) { registry->ProcessBindingQueue(); glfwPollEvents(); if (glfwGetWindowAttrib(this->window, GLFW_ICONIFIED) != 0) { ImGui_ImplGlfw_Sleep(10); continue; } ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); this->Draw(); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(this->window); } } void DrawMenuBar(State* state, Innertube* tube, AuthedBackend* chat_backend, Config::Config* config) { if (ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("Settings")) { if (ImGui::InputText("##video", &state->videoId, ImGuiInputTextFlags_EnterReturnsTrue)) { tube->Disconnect(); tube->Connect(state->videoId); config->lastVideoId = state->videoId; Config::Save(*config); if (chat_backend->authenticated) { chat_backend->SetVideo(state->videoId); } } // this being in here is a bit questionable ImGui::Text("%d viewers", tube->metadata.viewers); if (ImGui::MenuItem("Show pinned message", nullptr, !state->hidePinnedMessage)) { state->hidePinnedMessage = !state->hidePinnedMessage; } if (ImGui::Selectable("Chat backend settings")) { ImGui::PushOverrideID(ImHashStr("backendConfig")); ImGui::OpenPopup("backendConfig"); ImGui::PopID(); } if (ImGui::Selectable("About")) { // ..why does this need a workaround? https://github.com/ocornut/imgui/issues/331#issuecomment-1542969157 ImGui::PushOverrideID(ImHashStr("about")); ImGui::OpenPopup("about"); ImGui::PopID(); } ImGui::EndMenu(); } if (ImGui::BeginMenu("ImGui")) { if (ImGui::MenuItem("Demo window", nullptr, state->showDemoWindow)) { state->showDemoWindow = !state->showDemoWindow; } if (ImGui::MenuItem("Style editor", nullptr, state->showStyleEditor)) { state->showStyleEditor = !state->showStyleEditor; } if (ImGui::MenuItem("Metrics", nullptr, state->showMetricsWindow)) { state->showMetricsWindow = !state->showMetricsWindow; } if (ImGui::MenuItem("Debug", nullptr, state->showDebugLogWindow)) { state->showDebugLogWindow = !state->showDebugLogWindow; } ImGui::EndMenu(); } } ImGui::EndMenuBar(); } void FilterMessages(std::vector messages, std::string searchTerm, std::vector* output) { // lower-case search term std::ranges::transform(searchTerm, searchTerm.begin(), [](unsigned char c) { return std::tolower(c); }); // i'm well aware that this should probably be debounced *output = {}; for (const auto& message : messages) { auto author_match = std::search( message->author.begin(), message->author.end(), searchTerm.begin(), searchTerm.end(), [](unsigned char ch1, unsigned char ch2) { return std::tolower(ch1) == std::tolower(ch2); } ) != message->author.end(); auto content_match = author_match || std::search( message->content.begin(), message->content.end(), searchTerm.begin(), searchTerm.end(), [](unsigned char ch1, unsigned char ch2) { return std::tolower(ch1) == std::tolower(ch2); } ) != message->content.end(); if (author_match || content_match) { output->push_back(message); } } } void GUI::Draw() { GUIWidgets::AboutModal(); ImGui::PushOverrideID(ImHashStr("backendConfig")); if (ImGui::BeginPopupModal("backendConfig", nullptr)) { this->chat_backend->RenderSettingsUI(); if (ImGui::Button("close")) { ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); } ImGui::PopID(); auto viewport = ImGui::GetMainViewport(); ImGui::SetNextWindowSize(viewport->Size); ImGui::SetNextWindowPos(viewport->Pos); glfwSetWindowTitle(window, this->tube->metadata.title.c_str()); if (ImGui::Begin("chat", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoResize)) { DrawMenuBar(&this->state, tube, this->chat_backend, this->config); // handle keyboard shortcuts if (ImGui::IsKeyChordPressed(ImGuiMod_Ctrl | ImGuiKey_F)) { this->state.shouldShowSearch = true; this->state.filteredMessages = std::vector(tube->messages); // clone messages this->state.currentSearch = ""; ImGui::SetKeyboardFocusHere(); } else if (ImGui::IsKeyPressed(ImGuiKey_Escape)) { this->state.shouldShowSearch = false; } // add a search box if (this->state.shouldShowSearch) { auto availableRegion = ImGui::GetContentRegionAvail(); ImGui::PushItemWidth(availableRegion.x); if (ImGui::InputText("##search", &this->state.currentSearch)) { FilterMessages(tube->messages, this->state.currentSearch, &this->state.filteredMessages); } } // poll else if (tube->poll != nullptr && tube->poll->active) { ImGui::BeginGroup(); ImGui::TextWrapped("%s", tube->poll->title.c_str()); ImGui::PushFont(NULL, 16.f); ImGui::Text("%d votes", tube->poll->votes); ImGui::PopFont(); ImGui::BeginTable("#pollOptions", 2); for (int i = 0; i < tube->poll->optionAmount; i++) { ImGui::TableNextRow(); ImGui::TableNextColumn(); auto option = tube->poll->options[i]; ImGui::Text("%s", option.text.c_str()); ImGui::TableNextColumn(); ImGui::ProgressBar(option.ratio); } ImGui::EndTable(); ImGui::EndGroup(); ImGui::Separator(); } // pinned else if (tube->pinned_message && !this->state.hidePinnedMessage) { ImGui::BeginChild("pinned", ImVec2(0, this->state.wasPinnedHovered ? 0 : 20), ImGuiChildFlags_AutoResizeY, ImGuiWindowFlags_NoScrollbar); GUIWidgets::RenderMessage(tube, tube->pinned_message, registry); ImGui::EndChild(); // this is kinda a hack this->state.wasPinnedHovered = ImGui::IsItemHovered(); ImGui::Separator(); } // message list { std::unique_lock lock{ tube->message_mutex }; ImGui::BeginChild("messages", ImVec2(0, this->chat_backend->authenticated ? -(ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing()) : 0)); float currentRenderingHeight = 0.0f; float lastRenderedMessageAt = 0.0f; auto scrollY = ImGui::GetScrollY(); auto containerHeight = ImGui::GetWindowHeight(); bool shouldRecalculate = state.lastWindowWidth != ImGui::GetWindowSize().x; if (shouldRecalculate) { state.lastWindowWidth = ImGui::GetWindowSize().x; } for (const auto &message : (this->state.shouldShowSearch ? this->state.filteredMessages : tube->messages)) { if (!shouldRecalculate && message->cache_height != 0.0f && (currentRenderingHeight < scrollY - message->cache_height || currentRenderingHeight > scrollY + containerHeight)) { currentRenderingHeight += message->cache_height; continue; } ImGui::SetCursorPosY(currentRenderingHeight); if (message->is_internal) { GUIWidgets::RenderSystemMessage(tube, message); } else { GUIWidgets::RenderMessage(tube, message, registry); } auto end = ImGui::GetCursorPosY(); message->cache_height = end - currentRenderingHeight; lastRenderedMessageAt = end; currentRenderingHeight += message->cache_height; } if (currentRenderingHeight > lastRenderedMessageAt) { ImGui::Dummy(ImVec2(0, currentRenderingHeight - lastRenderedMessageAt)); } if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) { ImGui::SetScrollHereY(1.0f); } ImGui::EndChild(); } if (this->chat_backend->authenticated) { ImGui::PushItemWidth(-1.f); if (ImGui::InputText("##chatMessage", &this->state.currentChatMessage, ImGuiInputTextFlags_EnterReturnsTrue)) { this->chat_backend->SendMessage(this->state.currentChatMessage); this->state.currentChatMessage.clear(); } } } ImGui::End(); if (this->state.showDemoWindow) ImGui::ShowDemoWindow(); if (this->state.showStyleEditor) ImGui::ShowStyleEditor(); if (this->state.showMetricsWindow) ImGui::ShowMetricsWindow(); if (this->state.showDebugLogWindow) ImGui::ShowDebugLogWindow(); }