From 0bcb5d42d24f179b7b9629edf136be342ddbfec1 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Wed, 15 Jan 2025 08:54:52 -0800 Subject: [PATCH] Add subtitle system to imgui backend, and use it. --- backends/ui/imgui/RendererBackend.cpp | 32 ++++++++++++++++++++++----- backends/ui/imgui/RendererBackend.h | 7 ++++++ backends/ui/imgui/main.cpp | 5 +++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/backends/ui/imgui/RendererBackend.cpp b/backends/ui/imgui/RendererBackend.cpp index df03633..881525d 100644 --- a/backends/ui/imgui/RendererBackend.cpp +++ b/backends/ui/imgui/RendererBackend.cpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include using std::vector; using namespace Looper::Options; void RendererBackend::on_resize() { @@ -222,7 +224,8 @@ RendererBackend::~RendererBackend() { renderer_backend = nullptr; } void RendererBackend::SetWindowTitle(const char *title) { - SDL_SetWindowTitle(window, title); + this->title_text = title; + update_real_title(); } void RendererBackend::GuiFunction() { // Do nothing by default. @@ -414,12 +417,11 @@ bool RendererBackend::BeginMainMenuBar() { if (ImGui::BeginMainMenuBar()) { ImVec2 winsize = ImGui::GetWindowSize(); float texsize = winsize.y; - ImGui::SetCursorPosX(0); - ImGui::SetCursorPosY(0); + ImGui::SetCursorPosX(0); + ImGui::SetCursorPosY(0); ImGui::Image((ImTextureID)(icon_texture), ImVec2(texsize, texsize)); - //ImGui::SetCursorPosY((winsize.y - ImGui::GetFrameHeight()) / 2); - ImGui::TextUnformatted(SDL_GetWindowTitle(window)); - menubar_start = ImGui::GetCursorPosX(); + ImGui::TextUnformatted(title_text.c_str()); + menubar_start = ImGui::GetCursorPosX(); return true; } else { return false; @@ -470,10 +472,28 @@ SDL_HitTestResult RendererBackend::HitTest(SDL_Window *window, const SDL_Point * } } +void RendererBackend::SetSubtitle(const char *subtitle) { + this->subtitle = subtitle; + update_real_title(); +} +void RendererBackend::update_real_title() { + if (subtitle == "") { + SDL_SetWindowTitle(window, title_text.c_str()); + } else { + SDL_SetWindowTitle(window, fmt::format("{} - {}", subtitle, title_text).c_str()); + } +} void RendererBackend::EndMainMenuBar() { #ifndef __EMSCRIPTEN__ menubar_end = ImGui::GetCursorPosX(); ImVec2 size = ImGui::GetWindowSize(); + if (subtitle != "") { + ImVec4 text_color = ImGui::GetStyleColorVec4(ImGuiCol_Text); + text_color.w *= 0.5; + ImGui::PushStyleColor(ImGuiCol_Text, text_color); + ImGui::TextUnformatted(subtitle.c_str()); + ImGui::PopStyleColor(); + } float btnw = size.y; int btn_count = 3; ImVec2 btn_size(btnw, btnw); diff --git a/backends/ui/imgui/RendererBackend.h b/backends/ui/imgui/RendererBackend.h index 157cdd3..1f0365b 100644 --- a/backends/ui/imgui/RendererBackend.h +++ b/backends/ui/imgui/RendererBackend.h @@ -30,6 +30,9 @@ class RendererBackend { int menubar_start; int menubar_end; int title_btn_start; + std::string subtitle; + std::string title_text; + void update_real_title(); public: bool is_fullscreen(); bool is_maximized(); @@ -55,6 +58,10 @@ class RendererBackend { const char *prefPath; ImVec4 accent_color = ImVec4(0.75, 1.0, 1.0, 1.0); int Run(); + void SetSubtitle(const char *subtitle); + inline void ClearSubtitle() { + SetSubtitle(""); + } void SetWindowTitle(const char *title); virtual void Init(); virtual void GuiFunction(); diff --git a/backends/ui/imgui/main.cpp b/backends/ui/imgui/main.cpp index 7625013..9680bf9 100644 --- a/backends/ui/imgui/main.cpp +++ b/backends/ui/imgui/main.cpp @@ -154,6 +154,7 @@ void MainLoop::Init() { } } theme->Apply(accent_color, (float)scale); + SetWindowTitle("Looper"); FileLoaded(); } void MainLoop::Drop(std::string file) { @@ -201,9 +202,9 @@ void MainLoop::FileLoaded() { auto file_maybe = playback->get_current_title(); if (file_maybe.has_value()) { auto name = file_maybe.value(); - SetWindowTitle((name + std::string(" - Looper")).c_str()); + SetSubtitle(name.c_str()); } else { - SetWindowTitle("Looper"); + ClearSubtitle(); } streams = playback->get_streams(); properties = playback->get_property_list();