[fix] fix control bar layout and position in stream window

This commit is contained in:
dijunkun
2025-04-30 18:18:55 +08:00
parent 893051f9b3
commit a9084ba98d
5 changed files with 90 additions and 117 deletions

View File

@@ -163,6 +163,13 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
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))) {
CleanupPeer(props);
}
ImGui::SameLine();
if (!props->is_control_bar_in_left_) {

View File

@@ -43,7 +43,7 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties> &props) {
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1, 1, 1, 1));
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 10.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
@@ -51,86 +51,54 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties> &props) {
ImVec2(props->control_window_width_, props->control_window_height_),
ImGuiCond_Always);
if (0 == props->control_winodw_pos_.x && 0 == props->control_winodw_pos_.y) {
ImGui::SetNextWindowPos(ImVec2(0, title_bar_height_ + 1), ImGuiCond_Once);
}
float pos_x = 0;
float pos_y = 0;
float y_boundary = fullscreen_button_pressed_ ? 0 : (title_bar_height_ + 1);
if (props->reset_control_bar_pos_) {
float new_control_window_pos_x, new_control_window_pos_y, new_cursor_pos_x,
new_cursor_pos_y;
float new_cursor_pos_x = 0;
float new_cursor_pos_y = 0;
// set control window pos
new_control_window_pos_x = props->control_winodw_pos_.x;
if (props->control_winodw_pos_.y < props->stream_render_rect_last_.y) {
new_control_window_pos_y =
props->stream_render_rect_.y -
(props->stream_render_rect_last_.y - props->control_winodw_pos_.y);
if (fullscreen_button_pressed_ && new_control_window_pos_y < 0) {
new_control_window_pos_y = 0;
} else if (!fullscreen_button_pressed_ &&
new_control_window_pos_y < (title_bar_height_ + 1)) {
new_control_window_pos_y = title_bar_height_ + 1;
}
} else if (props->control_winodw_pos_.y + props->control_window_height_ >
props->stream_render_rect_last_.y +
props->stream_render_rect_last_.h) {
new_control_window_pos_y =
props->stream_render_rect_.y + props->stream_render_rect_.h +
(props->control_winodw_pos_.y - props->stream_render_rect_last_.y -
props->stream_render_rect_last_.h);
if (new_control_window_pos_y >
props->sub_stream_window_height_ - props->control_window_height_) {
new_control_window_pos_y =
props->sub_stream_window_height_ - props->control_window_height_;
}
} else if (props->control_winodw_pos_.y + props->control_window_height_ ==
props->stream_render_rect_last_.y +
props->stream_render_rect_last_.h) {
new_control_window_pos_y = props->stream_render_rect_.y +
props->stream_render_rect_.h -
props->control_window_height_;
if (props->control_window_pos_.y + props->control_window_height_ >
stream_window_height_) {
pos_y = stream_window_height_ - props->control_window_height_;
} else if (props->control_window_pos_.y < y_boundary) {
pos_y = y_boundary;
} else {
new_control_window_pos_y =
(props->control_winodw_pos_.y - props->stream_render_rect_last_.y) /
(float)(props->stream_render_rect_last_.h) *
props->stream_render_rect_.h +
props->stream_render_rect_.y;
pos_y = props->control_window_pos_.y;
}
ImGui::SetNextWindowPos(
ImVec2(new_control_window_pos_x, new_control_window_pos_y),
ImGuiCond_Always);
if (props->is_control_bar_in_left_) {
pos_x = 0;
} else {
pos_x = stream_window_width_ - props->control_window_width_;
}
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
if (0 != props->mouse_diff_control_bar_pos_x_ &&
0 != props->mouse_diff_control_bar_pos_y_) {
// set cursor pos
new_cursor_pos_x =
new_control_window_pos_x + props->mouse_diff_control_bar_pos_x_;
new_cursor_pos_y =
new_control_window_pos_y + props->mouse_diff_control_bar_pos_y_;
new_cursor_pos_x = pos_x + props->mouse_diff_control_bar_pos_x_;
new_cursor_pos_y = pos_y + props->mouse_diff_control_bar_pos_y_;
SDL_WarpMouseInWindow(stream_window_, (int)new_cursor_pos_x,
(int)new_cursor_pos_y);
}
props->reset_control_bar_pos_ = false;
} else if (!props->reset_control_bar_pos_ &&
ImGui::IsMouseReleased(ImGuiPopupFlags_MouseButtonLeft) ||
ImGui::IsMouseReleased(ImGuiMouseButton_Left) ||
props->control_window_width_is_changing_) {
if (props->control_winodw_pos_.x <= props->sub_stream_window_width_ / 2) {
float pos_x = 0;
float pos_y =
(props->control_winodw_pos_.y >=
(fullscreen_button_pressed_ ? 0 : (title_bar_height_ + 1)) &&
props->control_winodw_pos_.y <=
props->sub_stream_window_height_ - props->control_window_height_)
? props->control_winodw_pos_.y
: (props->control_winodw_pos_.y < (fullscreen_button_pressed_
? 0
: (title_bar_height_ + 1))
? (fullscreen_button_pressed_ ? 0
: (title_bar_height_ + 1))
: (props->sub_stream_window_height_ -
props->control_window_height_));
if (props->control_window_pos_.x <= stream_window_width_ / 2) {
if (props->control_window_pos_.y + props->control_window_height_ >
stream_window_height_) {
pos_y = stream_window_height_ - props->control_window_height_;
} else {
pos_y = props->control_window_pos_.y;
}
if (props->control_bar_expand_) {
if (props->control_window_width_ >= props->control_window_max_width_) {
@@ -147,51 +115,50 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties> &props) {
props->control_window_width_is_changing_ = true;
}
}
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
props->is_control_bar_in_left_ = true;
} else if (props->control_winodw_pos_.x >
props->sub_stream_window_width_ / 2) {
float pos_x = 0;
float pos_y =
(props->control_winodw_pos_.y >=
(fullscreen_button_pressed_ ? 0 : (title_bar_height_ + 1)) &&
props->control_winodw_pos_.y <=
props->sub_stream_window_height_ - props->control_window_height_)
? props->control_winodw_pos_.y
: (props->control_winodw_pos_.y < (fullscreen_button_pressed_
} else if (props->control_window_pos_.x > stream_window_width_ / 2) {
pos_x = 0;
pos_y =
(props->control_window_pos_.y >= y_boundary &&
props->control_window_pos_.y <=
stream_window_height_ - props->control_window_height_)
? props->control_window_pos_.y
: (props->control_window_pos_.y < (fullscreen_button_pressed_
? 0
: (title_bar_height_ + 1))
? (fullscreen_button_pressed_ ? 0
: (title_bar_height_ + 1))
: (props->sub_stream_window_height_ -
props->control_window_height_));
: (stream_window_height_ - props->control_window_height_));
if (props->control_bar_expand_) {
if (props->control_window_width_ >= props->control_window_max_width_) {
props->control_window_width_ = props->control_window_max_width_;
props->control_window_width_is_changing_ = false;
pos_x = props->sub_stream_window_width_ -
props->control_window_max_width_;
pos_x = stream_window_width_ - props->control_window_max_width_;
} else {
props->control_window_width_is_changing_ = true;
pos_x =
props->sub_stream_window_width_ - props->control_window_width_;
pos_x = stream_window_width_ - props->control_window_width_;
}
} else {
if (props->control_window_width_ <= props->control_window_min_width_) {
props->control_window_width_ = props->control_window_min_width_;
props->control_window_width_is_changing_ = false;
pos_x = props->sub_stream_window_width_ -
props->control_window_min_width_;
pos_x = stream_window_width_ - props->control_window_min_width_;
} else {
props->control_window_width_is_changing_ = true;
pos_x =
props->sub_stream_window_width_ - props->control_window_width_;
pos_x = stream_window_width_ - props->control_window_width_;
}
}
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
props->is_control_bar_in_left_ = false;
}
if (props->control_window_pos_.y + props->control_window_height_ >
stream_window_height_) {
pos_y = stream_window_height_ - props->control_window_height_;
} else if (props->control_window_pos_.y < y_boundary) {
pos_y = y_boundary;
}
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
}
if (props->control_bar_expand_ && props->control_window_height_is_changing_) {
@@ -217,20 +184,20 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties> &props) {
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking);
ImGui::PopStyleVar();
props->control_winodw_pos_ = ImGui::GetWindowPos();
props->control_window_pos_ = ImGui::GetWindowPos();
SDL_GetMouseState(&props->mouse_pos_x_, &props->mouse_pos_y_);
props->mouse_diff_control_bar_pos_x_ =
props->mouse_pos_x_ - props->control_winodw_pos_.x;
props->mouse_pos_x_ - props->control_window_pos_.x;
props->mouse_diff_control_bar_pos_y_ =
props->mouse_pos_y_ - props->control_winodw_pos_.y;
props->mouse_pos_y_ - props->control_window_pos_.y;
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
static bool a, b, c, d, e;
ImGui::SetNextWindowPos(
ImVec2(props->is_control_bar_in_left_
? props->control_winodw_pos_.x - props->control_window_width_
: props->control_winodw_pos_.x,
props->control_winodw_pos_.y),
? props->control_window_pos_.x - props->control_window_width_
: props->control_window_pos_.x,
props->control_window_pos_.y),
ImGuiCond_Always);
ImGui::SetWindowFontScale(0.5f);

View File

@@ -938,27 +938,19 @@ void Render::CleanupFactories() {
}
}
void Render::CleanupPeer(std::string host_name,
std::shared_ptr<SubStreamWindowProperties> props) {
void Render::CleanupPeer(std::shared_ptr<SubStreamWindowProperties> props) {
if (props->dst_buffer_) {
thumbnail_->SaveToThumbnail(
(char*)props->dst_buffer_, props->video_width_, props->video_height_,
host_name, props->remote_host_name_,
props->remote_id_, props->remote_host_name_,
props->remember_password_ ? props->remote_password_ : "");
}
PeerPtr* peer_client = props->peer_;
if (peer_client) {
std::string client_id;
if (host_name == client_id_) {
client_id = "C-" + std::string(client_id_);
} else {
client_id = client_id_;
}
LOG_INFO("[{}] Leave connection [{}]", client_id, host_name);
LeaveConnection(peer_client, host_name.c_str());
LOG_INFO("Destroy peer [{}]", client_id);
DestroyPeer(&peer_client);
if (props->peer_) {
LOG_INFO("[{}] Leave connection [{}]", props->local_id_, props->remote_id_);
LeaveConnection(props->peer_, props->remote_id_.c_str());
LOG_INFO("Destroy peer [{}]", props->local_id_);
DestroyPeer(&props->peer_);
}
}
@@ -973,7 +965,7 @@ void Render::CleanupPeers() {
for (auto& it : client_properties_) {
auto props = it.second;
CleanupPeer(it.first, props);
CleanupPeer(props);
}
client_properties_.clear();

View File

@@ -57,10 +57,10 @@ class Render {
float sub_stream_window_width_ = 1280;
float sub_stream_window_height_ = 720;
float control_window_min_width_ = 20;
float control_window_max_width_ = 170;
float control_window_max_width_ = 200;
float control_window_min_height_ = 40;
float control_window_max_height_ = 150;
float control_window_width_ = 170;
float control_window_width_ = 200;
float control_window_height_ = 40;
float control_bar_pos_x_ = 0;
float control_bar_pos_y_ = 30;
@@ -95,7 +95,7 @@ class Render {
SDL_Texture *stream_texture_ = nullptr;
SDL_Rect stream_render_rect_;
SDL_Rect stream_render_rect_last_;
ImVec2 control_winodw_pos_;
ImVec2 control_window_pos_;
ConnectionStatus connection_status_ = ConnectionStatus::Closed;
TraversalMode traversal_mode_ = TraversalMode::UnknownMode;
XNetTrafficStats net_traffic_stats_;
@@ -120,8 +120,7 @@ class Render {
void HandleStreamWindow();
void Cleanup();
void CleanupFactories();
void CleanupPeer(std::string host_name,
std::shared_ptr<SubStreamWindowProperties> props);
void CleanupPeer(std::shared_ptr<SubStreamWindowProperties> props);
void CleanupPeers();
void UpdateRenderRect();
void ProcessSdlEvent();

View File

@@ -46,7 +46,6 @@ int Render::StreamWindow() {
for (auto it = client_properties_.begin();
it != client_properties_.end();) {
auto& props = it->second;
if (props->tab_opened_) {
ImGui::SetWindowFontScale(0.6f);
if (ImGui::BeginTabItem(props->remote_id_.c_str(), &props->tab_opened_,
@@ -58,7 +57,7 @@ int Render::StreamWindow() {
ImGuiCond_Always);
ImGui::SetNextWindowPos(
ImVec2(0, fullscreen_button_pressed_ ? 0 : title_bar_height_),
ImGuiCond_Appearing);
ImGuiCond_Always);
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));
@@ -75,16 +74,26 @@ int Render::StreamWindow() {
props->render_window_height_ = stream_window_size.y;
ControlWindow(props);
if (!props->peer_) {
it = client_properties_.erase(it);
if (client_properties_.empty()) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
}
} else {
++it;
}
ImGui::End();
ImGui::EndTabItem();
} else {
props->tab_selected_ = false;
ImGui::SetWindowFontScale(1.0f);
}
++it;
}
} else {
CleanupPeer(it->first, props);
CleanupPeer(props);
it = client_properties_.erase(it);
if (client_properties_.empty()) {
SDL_Event event;
@@ -93,7 +102,6 @@ int Render::StreamWindow() {
}
}
}
ImGui::EndTabBar();
}
ImGui::End();