[feat] use tab bar to manage stream windows

This commit is contained in:
dijunkun
2025-04-29 22:16:10 +08:00
parent 532ad0eb51
commit dfbd4317b7
5 changed files with 94 additions and 59 deletions

View File

@@ -163,15 +163,6 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
props->reset_control_bar_pos_ = true; props->reset_control_bar_pos_ = true;
} }
ImGui::SameLine();
// close button
std::string close_button = ICON_FA_XMARK;
if (ImGui::Button(close_button.c_str(), ImVec2(25, 25))) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
ImGui::SameLine(); ImGui::SameLine();
if (!props->is_control_bar_in_left_) { if (!props->is_control_bar_in_left_) {

View File

@@ -36,7 +36,8 @@ SDL_HitTestResult Render::HitTestCallback(SDL_Window* window,
SDL_GetWindowSize(window, &window_width, &window_height); SDL_GetWindowSize(window, &window_width, &window_height);
if (area->y < 30 && area->y > MOUSE_GRAB_PADDING && if (area->y < 30 && area->y > MOUSE_GRAB_PADDING &&
area->x < window_width - 120 && area->x > MOUSE_GRAB_PADDING) { area->x < window_width - 120 && area->x > MOUSE_GRAB_PADDING &&
!render->is_tab_bar_hovered_) {
return SDL_HITTEST_DRAGGABLE; return SDL_HITTEST_DRAGGABLE;
} }
@@ -706,6 +707,8 @@ int Render::DrawStreamWindow() {
ImGui_ImplSDL2_NewFrame(); ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
StreamWindow();
if (!fullscreen_button_pressed_) { if (!fullscreen_button_pressed_) {
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always); ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
@@ -715,23 +718,25 @@ int Render::DrawStreamWindow() {
ImGuiCond_Always); ImGuiCond_Always);
ImGui::Begin("StreamWindowTitleBar", nullptr, ImGui::Begin("StreamWindowTitleBar", nullptr,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBringToFrontOnFocus); ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoDocking);
ImGui::PopStyleColor(); ImGui::PopStyleColor();
TitleBar(false); TitleBar(false);
ImGui::End(); ImGui::End();
} }
StreamWindow();
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
SDL_RenderClear(stream_renderer_); SDL_RenderClear(stream_renderer_);
for (auto& it : client_properties_) { for (auto& it : client_properties_) {
auto props = it.second; auto props = it.second;
SDL_RenderCopy(stream_renderer_, props->stream_texture_, NULL, if (props->tab_selected_) {
&(props->stream_render_rect_)); SDL_RenderCopy(stream_renderer_, props->stream_texture_, NULL,
&(props->stream_render_rect_));
}
} }
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), stream_renderer_); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), stream_renderer_);
SDL_RenderPresent(stream_renderer_); SDL_RenderPresent(stream_renderer_);
@@ -955,14 +960,6 @@ void Render::CleanupPeer(std::string host_name,
LOG_INFO("Destroy peer [{}]", client_id); LOG_INFO("Destroy peer [{}]", client_id);
DestroyPeer(&peer_client); DestroyPeer(&peer_client);
} }
client_properties_.erase(host_name);
if (client_properties_.empty()) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
} }
void Render::CleanupPeers() { void Render::CleanupPeers() {

View File

@@ -12,6 +12,7 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
#include <optional>
#include <string> #include <string>
#include "../../thirdparty/projectx/src/interface/x.h" #include "../../thirdparty/projectx/src/interface/x.h"
@@ -21,6 +22,7 @@
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_sdl2.h" #include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h" #include "imgui_impl_sdlrenderer2.h"
#include "imgui_internal.h"
#include "screen_capturer_factory.h" #include "screen_capturer_factory.h"
#include "speaker_capturer_factory.h" #include "speaker_capturer_factory.h"
#include "thumbnail.h" #include "thumbnail.h"
@@ -55,10 +57,10 @@ class Render {
float sub_stream_window_width_ = 1280; float sub_stream_window_width_ = 1280;
float sub_stream_window_height_ = 720; float sub_stream_window_height_ = 720;
float control_window_min_width_ = 20; float control_window_min_width_ = 20;
float control_window_max_width_ = 200; float control_window_max_width_ = 170;
float control_window_min_height_ = 40; float control_window_min_height_ = 40;
float control_window_max_height_ = 150; float control_window_max_height_ = 150;
float control_window_width_ = 200; float control_window_width_ = 170;
float control_window_height_ = 40; float control_window_height_ = 40;
float control_bar_pos_x_ = 0; float control_bar_pos_x_ = 0;
float control_bar_pos_y_ = 30; float control_bar_pos_y_ = 30;
@@ -79,6 +81,8 @@ class Render {
size_t video_size_ = 0; size_t video_size_ = 0;
bool tab_selected_ = false; bool tab_selected_ = false;
bool tab_opened_ = true; bool tab_opened_ = true;
std::optional<float> pos_x_before_docked_;
std::optional<float> pos_y_before_docked_;
float render_window_x_ = 0; float render_window_x_ = 0;
float render_window_y_ = 0; float render_window_y_ = 0;
float render_window_width_ = 0; float render_window_width_ = 0;
@@ -357,6 +361,7 @@ class Render {
bool reload_recent_connections_ = true; bool reload_recent_connections_ = true;
bool show_confirm_delete_connection_ = false; bool show_confirm_delete_connection_ = false;
bool delete_connection_ = false; bool delete_connection_ = false;
bool is_tab_bar_hovered_ = false;
std::string delete_connection_name_ = ""; std::string delete_connection_name_ = "";
bool re_enter_remote_id_ = false; bool re_enter_remote_id_ = false;
double copy_start_time_ = 0; double copy_start_time_ = 0;

View File

@@ -11,49 +11,90 @@ int Render::StreamWindow() {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0)); ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 0, 0, 0)); ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_DockingEmptyBg, ImVec4(0, 0, 0, 0));
ImGui::Begin("VideoBg", nullptr, ImGui::Begin("VideoBg", nullptr,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBringToFrontOnFocus); ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoDocking);
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); ImGui::PopStyleColor(2);
ImGui::DockSpace(
dockspace_id, ImVec2(stream_window_width_, stream_window_height_),
ImGuiDockNodeFlags_AutoHideTabBar | ImGuiDockNodeFlags_KeepAliveOnly);
ImGui::PopStyleColor(3);
ImGui::PopStyleVar(); ImGui::PopStyleVar();
for (auto& it : client_properties_) { ImGuiTabBarFlags tab_bar_flags =
auto& props = it.second; ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs;
ImGuiWindowFlags stream_window_flag =
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoMove;
// ImGui::SetNextWindowPos( ImGui::SetNextWindowPos(ImVec2(20, 0), ImGuiCond_Always);
// ImVec2(0, fullscreen_button_pressed_ ? 0 : title_bar_height_), ImGui::SetNextWindowSize(ImVec2(0, 20), ImGuiCond_Always);
// ImGuiCond_Once); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 8.0f));
ImGui::SetNextWindowSize( ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImVec2(stream_window_width_, stream_window_height_), ImGuiCond_Once); ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0.0f));
ImGui::SetWindowFontScale(0.5f); ImGui::Begin("TabBar", nullptr,
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration |
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0)); ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0, 0, 0, 0)); ImGuiWindowFlags_NoDocking);
ImGui::Begin(props->remote_id_.c_str(), nullptr, ImGui::PopStyleColor();
ImGuiWindowFlags_NoTitleBar); ImGui::PopStyleVar(2);
ImGui::PopStyleColor(2); if (ImGui::BeginTabBar("StreamTabBar", tab_bar_flags)) {
ImGui::PopStyleVar(); if (ImGui::IsWindowHovered()) {
ImGui::SetWindowFontScale(1.0f); is_tab_bar_hovered_ = true;
} else {
is_tab_bar_hovered_ = false;
}
ImVec2 stream_window_pos = ImGui::GetWindowPos(); for (auto it = client_properties_.begin(); it != client_properties_.end();
ImGuiViewport* viewport = ImGui::GetWindowViewport(); ++it) {
ImVec2 stream_window_size = ImGui::GetWindowSize(); auto& props = it->second;
props->render_window_x_ = stream_window_pos.x;
props->render_window_y_ = stream_window_pos.y;
props->render_window_width_ = stream_window_size.x;
props->render_window_height_ = stream_window_size.y;
ControlWindow(props); ImGui::SetWindowFontScale(0.6f);
if (props->tab_opened_) {
if (ImGui::BeginTabItem(props->remote_id_.c_str(), &props->tab_opened_,
ImGuiTabItemFlags_None)) {
props->tab_selected_ = true;
ImGui::SetWindowFontScale(1.0f);
ImGui::SetNextWindowSize(
ImVec2(stream_window_width_, stream_window_height_),
ImGuiCond_Once);
ImGui::SetNextWindowPos(
ImVec2(0, fullscreen_button_pressed_ ? 0 : title_bar_height_),
ImGuiCond_Appearing);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0.0f));
ImGui::Begin(props->remote_id_.c_str(), nullptr, stream_window_flag);
ImGui::PopStyleColor();
ImGui::PopStyleVar(2);
ImGui::End(); ImVec2 stream_window_pos = ImGui::GetWindowPos();
ImGuiViewport* viewport = ImGui::GetWindowViewport();
ImVec2 stream_window_size = ImGui::GetWindowSize();
props->render_window_x_ = stream_window_pos.x;
props->render_window_y_ = stream_window_pos.y;
props->render_window_width_ = stream_window_size.x;
props->render_window_height_ = stream_window_size.y;
ControlWindow(props);
ImGui::End();
ImGui::EndTabItem();
} else {
props->tab_selected_ = false;
}
} else {
CleanupPeer(it->first, props);
it = client_properties_.erase(it);
if (client_properties_.empty()) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
}
}
ImGui::EndTabBar();
} }
ImGui::End();
UpdateRenderRect(); UpdateRenderRect();

View File

@@ -1,4 +1,5 @@
#include "localization.h" #include "localization.h"
#include "rd_log.h"
#include "render.h" #include "render.h"
#define BUTTON_PADDING 36.0f #define BUTTON_PADDING 36.0f