Compare commits

..

6 Commits

Author SHA1 Message Date
dijunkun
fd242d50c1 [feat] show server window in the bottom-right corner of the screen 2026-01-19 17:42:22 +08:00
dijunkun
d6d8ecd6c5 [feat] add server window 2026-01-19 00:47:34 +08:00
dijunkun
669fac7f50 [feat] support drag-and-drop file sending, refs #63 2026-01-14 18:13:22 +08:00
dijunkun
92d670916e [fix] fix incorrect data send function used for control data 2026-01-13 18:13:54 +08:00
dijunkun
0155413c12 [feat] use reliable transmission to send control info 2026-01-12 17:28:19 +08:00
dijunkun
8468be6532 [fix] update MiniRTC 2026-01-12 17:27:49 +08:00
8 changed files with 588 additions and 81 deletions

View File

@@ -207,6 +207,7 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
}
AddAudioStream(props->peer_, props->audio_label_.c_str());
AddDataStream(props->peer_, props->data_label_.c_str(), false);
AddDataStream(props->peer_, props->control_data_label_.c_str(), true);
AddDataStream(props->peer_, props->file_label_.c_str(), true);
AddDataStream(props->peer_, props->file_feedback_label_.c_str(), true);
AddDataStream(props->peer_, props->clipboard_label_.c_str(), true);

View File

@@ -716,6 +716,7 @@ int Render::CreateConnectionPeer() {
AddAudioStream(peer_, audio_label_.c_str());
AddDataStream(peer_, data_label_.c_str(), false);
AddDataStream(peer_, control_data_label_.c_str(), true);
AddDataStream(peer_, file_label_.c_str(), true);
AddDataStream(peer_, file_feedback_label_.c_str(), true);
AddDataStream(peer_, clipboard_label_.c_str(), true);
@@ -938,6 +939,74 @@ int Render::DestroyStreamWindow() {
return 0;
}
int Render::CreateServerWindow() {
if (server_window_created_) {
return 0;
}
server_ctx_ = ImGui::CreateContext();
if (!server_ctx_) {
LOG_ERROR("Server context is null");
return -1;
}
ImGui::SetCurrentContext(server_ctx_);
if (!SDL_CreateWindowAndRenderer("Server window", (int)server_window_width_,
(int)server_window_height_,
SDL_WINDOW_HIGH_PIXEL_DENSITY |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_TRANSPARENT,
&server_window_, &server_renderer_)) {
LOG_ERROR("Error creating server_window_ and server_renderer_: {}",
SDL_GetError());
return -1;
}
// Set window position to bottom-right corner
SDL_Rect display_bounds;
if (SDL_GetDisplayUsableBounds(SDL_GetDisplayForWindow(server_window_),
&display_bounds)) {
int window_x =
display_bounds.x + display_bounds.w - (int)server_window_width_;
int window_y =
display_bounds.y + display_bounds.h - (int)server_window_height_;
SDL_SetWindowPosition(server_window_, window_x, window_y);
}
SDL_SetWindowResizable(server_window_, false);
SDL_SetRenderDrawBlendMode(server_renderer_, SDL_BLENDMODE_BLEND);
// for window region action
SDL_SetWindowHitTest(server_window_, HitTestCallback, this);
SetupFontAndStyle(false);
ImGui_ImplSDL3_InitForSDLRenderer(server_window_, server_renderer_);
ImGui_ImplSDLRenderer3_Init(server_renderer_);
server_window_created_ = true;
server_window_inited_ = true;
LOG_INFO("Server window inited");
return 0;
}
int Render::DestroyServerWindow() {
if (server_ctx_) {
ImGui::SetCurrentContext(server_ctx_);
}
if (server_renderer_) {
SDL_DestroyRenderer(server_renderer_);
}
if (server_window_) {
SDL_DestroyWindow(server_window_);
}
server_window_created_ = false;
return 0;
}
int Render::SetupFontAndStyle(bool main_window) {
float font_size = 32.0f;
@@ -995,7 +1064,13 @@ int Render::SetupFontAndStyle(bool main_window) {
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
io.Fonts->GetGlyphRangesChineseFull());
if (main_windows_system_chinese_font_ != nullptr) {
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
// Merge FontAwesome icons into the Chinese font
config.MergeMode = true;
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
font_size, &config, icon_ranges);
config.MergeMode = false;
LOG_INFO("Loaded system Chinese font with icons: {}", font_paths[i]);
break;
}
} else {
@@ -1003,7 +1078,13 @@ int Render::SetupFontAndStyle(bool main_window) {
io.Fonts->AddFontFromFileTTF(font_paths[i], font_size, &config,
io.Fonts->GetGlyphRangesChineseFull());
if (stream_windows_system_chinese_font_ != nullptr) {
LOG_INFO("Loaded system Chinese font: {}", font_paths[i]);
// Merge FontAwesome icons into the Chinese font
config.MergeMode = true;
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
font_size, &config, icon_ranges);
config.MergeMode = false;
LOG_INFO("Loaded system Chinese font with icons: {}", font_paths[i]);
break;
}
}
@@ -1014,12 +1095,24 @@ int Render::SetupFontAndStyle(bool main_window) {
if (main_window) {
if (main_windows_system_chinese_font_ == nullptr) {
main_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
LOG_WARN("System Chinese font not found, using default font");
// Merge FontAwesome icons into the default font
config.MergeMode = true;
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
font_size, &config, icon_ranges);
config.MergeMode = false;
LOG_WARN("System Chinese font not found, using default font with icons");
}
} else {
if (stream_windows_system_chinese_font_ == nullptr) {
stream_windows_system_chinese_font_ = io.Fonts->AddFontDefault(&config);
LOG_WARN("System Chinese font not found, using default font");
// Merge FontAwesome icons into the default font
config.MergeMode = true;
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
font_size, &config, icon_ranges);
config.MergeMode = false;
LOG_WARN("System Chinese font not found, using default font with icons");
}
}
@@ -1158,6 +1251,39 @@ int Render::DrawStreamWindow() {
return 0;
}
int Render::DrawServerWindow() {
if (!server_ctx_) {
LOG_ERROR("Server context is null");
return -1;
}
if (server_window_) {
int w = 0, h = 0;
SDL_GetWindowSize(server_window_, &w, &h);
if (w > 0 && h > 0) {
server_window_width_ = (float)w;
server_window_height_ = (float)h;
}
}
ImGui::SetCurrentContext(server_ctx_);
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ServerWindow();
ImGui::Render();
// Transparent clear for compact (shaped) mode; opaque clear for normal mode.
if (server_window_compact_) {
SDL_SetRenderDrawColor(server_renderer_, 0, 0, 0, 0);
} else {
SDL_SetRenderDrawColor(server_renderer_, 255, 255, 255, 255);
}
SDL_RenderClear(server_renderer_);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), server_renderer_);
SDL_RenderPresent(server_renderer_);
return 0;
}
int Render::Run() {
latest_version_info_ = CheckUpdate();
if (!latest_version_info_.empty() &&
@@ -1332,12 +1458,17 @@ void Render::MainLoop() {
UpdateLabels();
HandleRecentConnections();
HandleStreamWindow();
HandleServerWindow();
DrawMainWindow();
if (stream_window_inited_) {
DrawStreamWindow();
}
if (is_server_mode_) {
DrawServerWindow();
}
UpdateInteractions();
if (need_to_send_host_info_) {
@@ -1375,8 +1506,8 @@ void Render::MainLoop() {
remote_action.i.host_name_size = host_name.size();
std::string msg = remote_action.to_json();
int ret =
SendDataFrame(peer_, msg.data(), msg.size(), data_label_.c_str());
int ret = SendReliableDataFrame(peer_, msg.data(), msg.size(),
control_data_label_.c_str());
FreeRemoteAction(remote_action);
if (0 == ret) {
need_to_send_host_info_ = false;
@@ -1429,6 +1560,18 @@ void Render::HandleStreamWindow() {
}
}
void Render::HandleServerWindow() {
if (need_to_create_server_window_) {
CreateServerWindow();
need_to_create_server_window_ = false;
}
if (need_to_destroy_server_window_) {
DestroyServerWindow();
need_to_destroy_server_window_ = false;
}
}
void Render::Cleanup() {
Clipboard::StopMonitoring();
@@ -1807,6 +1950,16 @@ void Render::ProcessSdlEvent(const SDL_Event& event) {
}
}
if (server_window_inited_) {
if (server_ctx_) {
ImGui::SetCurrentContext(server_ctx_);
ImGui_ImplSDL3_ProcessEvent(&event);
} else {
LOG_ERROR("Server context is null");
return;
}
}
switch (event.type) {
case SDL_EVENT_QUIT:
if (stream_window_inited_) {
@@ -1896,6 +2049,12 @@ void Render::ProcessSdlEvent(const SDL_Event& event) {
foucs_on_main_window_ = false;
}
break;
case SDL_EVENT_DROP_FILE:
if (stream_window_ &&
SDL_GetWindowID(stream_window_) == event.window.windowID) {
ProcessFileDropEvent(event);
}
break;
case SDL_EVENT_MOUSE_MOTION:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
@@ -1976,4 +2135,63 @@ void Render::ProcessSdlEvent(const SDL_Event& event) {
break;
}
}
void Render::ProcessFileDropEvent(const SDL_Event& event) {
if (event.type != SDL_EVENT_DROP_FILE) {
return;
}
if (!stream_window_inited_) {
return;
}
std::shared_lock lock(client_properties_mutex_);
for (auto& [_, props] : client_properties_) {
if (props->tab_selected_) {
if (event.drop.data == nullptr) {
LOG_ERROR("ProcessFileDropEvent: drop event data is null");
break;
}
if (!props || !props->peer_) {
LOG_ERROR("ProcessFileDropEvent: invalid props or peer");
break;
}
std::string utf8_path = static_cast<const char*>(event.drop.data);
std::filesystem::path file_path = std::filesystem::u8path(utf8_path);
// Check if file exists
std::error_code ec;
if (!std::filesystem::exists(file_path, ec)) {
LOG_ERROR("ProcessFileDropEvent: file does not exist: {}",
file_path.string().c_str());
break;
}
// Check if it's a regular file
if (!std::filesystem::is_regular_file(file_path, ec)) {
LOG_ERROR("ProcessFileDropEvent: path is not a regular file: {}",
file_path.string().c_str());
break;
}
// Get file size
uint64_t file_size = std::filesystem::file_size(file_path, ec);
if (ec) {
LOG_ERROR("ProcessFileDropEvent: failed to get file size: {}",
ec.message().c_str());
break;
}
LOG_INFO("Drop file [{}] to send (size: {} bytes)", event.drop.data,
file_size);
// Use ProcessSelectedFile to handle the file processing
ProcessSelectedFile(utf8_path, props, props->file_label_);
break;
}
}
}
} // namespace crossdesk

View File

@@ -46,8 +46,9 @@ class Render {
Params params_;
PeerPtr* peer_ = nullptr;
std::string audio_label_ = "control_audio";
std::string data_label_ = "control_data";
std::string data_label_ = "data";
std::string file_label_ = "file";
std::string control_data_label_ = "control_data";
std::string file_feedback_label_ = "file_feedback";
std::string clipboard_label_ = "clipboard";
std::string local_id_ = "";
@@ -180,6 +181,7 @@ class Render {
void UpdateInteractions();
void HandleRecentConnections();
void HandleStreamWindow();
void HandleServerWindow();
void Cleanup();
void CleanupFactories();
void CleanupPeer(std::shared_ptr<SubStreamWindowProperties> props);
@@ -189,12 +191,19 @@ class Render {
void UpdateRenderRect();
void ProcessSdlEvent(const SDL_Event& event);
void ProcessFileDropEvent(const SDL_Event& event);
void ProcessSelectedFile(const std::string& path,
std::shared_ptr<SubStreamWindowProperties>& props,
const std::string& file_label);
private:
int CreateStreamRenderWindow();
int TitleBar(bool main_window);
int MainWindow();
int UpdateNotificationWindow();
int StreamWindow();
int ServerWindow();
int LocalWindow();
int RemoteWindow();
int RecentConnectionsWindow();
@@ -219,11 +228,15 @@ class Render {
int DestroyMainWindow();
int CreateStreamWindow();
int DestroyStreamWindow();
int CreateServerWindow();
int DestroyServerWindow();
int SetupFontAndStyle(bool main_window);
int DestroyMainWindowContext();
int DestroyStreamWindowContext();
int DestroyServerWindowContext();
int DrawMainWindow();
int DrawStreamWindow();
int DrawServerWindow();
int ConfirmDeleteConnection();
int NetTrafficStats(std::shared_ptr<SubStreamWindowProperties>& props);
void DrawConnectionStatusText(
@@ -480,6 +493,35 @@ class Render {
float stream_window_dpi_scaling_w_ = 1.0f;
float stream_window_dpi_scaling_h_ = 1.0f;
// server window render
SDL_Window* server_window_ = nullptr;
SDL_Renderer* server_renderer_ = nullptr;
ImGuiContext* server_ctx_ = nullptr;
// server window properties
bool need_to_create_server_window_ = false;
bool need_to_destroy_server_window_ = false;
bool server_window_created_ = false;
bool server_window_inited_ = false;
int server_window_width_default_ = 300;
int server_window_height_default_ = 450;
float server_window_width_ = 300;
float server_window_height_ = 450;
float server_window_title_bar_height_ = 30.0f;
SDL_PixelFormat server_pixformat_ = SDL_PIXELFORMAT_NV12;
int server_window_width_real_ = 400;
int server_window_height_real_ = 450;
float server_window_dpi_scaling_w_ = 1.0f;
float server_window_dpi_scaling_h_ = 1.0f;
// server window compact mode (50x50) toggle
bool server_window_compact_ = false;
int server_window_width_before_compact_ = 0;
int server_window_height_before_compact_ = 0;
int server_window_x_before_compact_ = 0;
int server_window_y_before_compact_ = 0;
bool server_window_bounds_saved_ = false;
bool label_inited_ = false;
bool connect_button_pressed_ = false;
bool password_validating_ = false;
@@ -496,6 +538,7 @@ class Render {
bool fullscreen_button_pressed_ = false;
bool focus_on_input_widget_ = true;
bool is_client_mode_ = false;
bool is_server_mode_ = false;
bool reload_recent_connections_ = true;
bool show_confirm_delete_connection_ = false;
bool delete_connection_ = false;

View File

@@ -622,7 +622,8 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
switch (status) {
case ConnectionStatus::Connected: {
render->need_to_send_host_info_ = true;
render->need_to_create_server_window_ = true;
render->is_server_mode_ = true;
render->start_screen_capturer_ = true;
render->start_speaker_capturer_ = true;
#ifdef CROSSDESK_DEBUG
@@ -647,6 +648,8 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
kv.second == ConnectionStatus::Failed ||
kv.second == ConnectionStatus::Disconnected;
})) {
render->need_to_destroy_server_window_ = true;
render->is_server_mode_ = false;
render->start_screen_capturer_ = false;
render->start_speaker_capturer_ = false;
render->start_mouse_controller_ = false;

View File

@@ -53,6 +53,77 @@ int LossRateDisplay(float loss_rate) {
return 0;
}
void Render::ProcessSelectedFile(
const std::string& path, std::shared_ptr<SubStreamWindowProperties>& props,
const std::string& file_label) {
if (path.empty()) {
return;
}
LOG_INFO("Selected file: {}", path.c_str());
std::filesystem::path file_path = std::filesystem::u8path(path);
// Get file size
std::error_code ec;
uint64_t file_size = std::filesystem::file_size(file_path, ec);
if (ec) {
LOG_ERROR("Failed to get file size: {}", ec.message().c_str());
file_size = 0;
}
// Add file to transfer list
{
std::lock_guard<std::mutex> lock(props->file_transfer_list_mutex_);
SubStreamWindowProperties::FileTransferInfo info;
info.file_name = file_path.filename().u8string();
info.file_path = file_path; // Store full path for precise matching
info.file_size = file_size;
info.status = SubStreamWindowProperties::FileTransferStatus::Queued;
info.sent_bytes = 0;
info.file_id = 0;
info.rate_bps = 0;
props->file_transfer_list_.push_back(info);
}
props->file_transfer_window_visible_ = true;
if (props->file_sending_.load()) {
// Add to queue
size_t queue_size = 0;
{
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
SubStreamWindowProperties::QueuedFile queued_file;
queued_file.file_path = file_path;
queued_file.file_label = file_label;
props->file_send_queue_.push(queued_file);
queue_size = props->file_send_queue_.size();
}
LOG_INFO("File added to queue: {} ({} files in queue)",
file_path.filename().string().c_str(), queue_size);
} else {
StartFileTransfer(props, file_path, file_label);
if (props->file_sending_.load()) {
} else {
// Failed to start (race condition: another file started between
// check and call) Add to queue
size_t queue_size = 0;
{
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
SubStreamWindowProperties::QueuedFile queued_file;
queued_file.file_path = file_path;
queued_file.file_label = file_label;
props->file_send_queue_.push(queued_file);
queue_size = props->file_send_queue_.size();
}
LOG_INFO(
"File added to queue after race condition: {} ({} files in "
"queue)",
file_path.filename().string().c_str(), queue_size);
}
}
}
int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
float button_width = title_bar_height_ * 0.8f;
float button_height = title_bar_height_ * 0.8f;
@@ -95,8 +166,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
remote_action.d = i;
if (props->connection_status_ == ConnectionStatus::Connected) {
std::string msg = remote_action.to_json();
SendDataFrame(props->peer_, msg.c_str(), msg.size(),
props->data_label_.c_str());
SendReliableDataFrame(props->peer_, msg.c_str(), msg.size(),
props->control_data_label_.c_str());
}
}
props->display_selectable_hovered_ = ImGui::IsWindowHovered();
@@ -176,8 +247,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
remote_action.type = ControlType::audio_capture;
remote_action.a = props->audio_capture_button_pressed_;
std::string msg = remote_action.to_json();
SendDataFrame(props->peer_, msg.c_str(), msg.size(),
props->data_label_.c_str());
SendReliableDataFrame(props->peer_, msg.c_str(), msg.size(),
props->control_data_label_.c_str());
}
}
@@ -204,71 +275,7 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
std::string title =
localization::select_file[localization_language_index_];
std::string path = OpenFileDialog(title);
if (!path.empty()) {
LOG_INFO("Selected file: {}", path.c_str());
std::filesystem::path file_path = std::filesystem::path(path);
std::string file_label = file_label_;
// Get file size
std::error_code ec;
uint64_t file_size = std::filesystem::file_size(file_path, ec);
if (ec) {
LOG_ERROR("Failed to get file size: {}", ec.message().c_str());
file_size = 0;
}
// Add file to transfer list
{
std::lock_guard<std::mutex> lock(props->file_transfer_list_mutex_);
SubStreamWindowProperties::FileTransferInfo info;
info.file_name = file_path.filename().string();
info.file_path = file_path; // Store full path for precise matching
info.file_size = file_size;
info.status = SubStreamWindowProperties::FileTransferStatus::Queued;
info.sent_bytes = 0;
info.file_id = 0;
info.rate_bps = 0;
props->file_transfer_list_.push_back(info);
}
props->file_transfer_window_visible_ = true;
if (props->file_sending_.load()) {
// Add to queue
size_t queue_size = 0;
{
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
SubStreamWindowProperties::QueuedFile queued_file;
queued_file.file_path = file_path;
queued_file.file_label = file_label;
props->file_send_queue_.push(queued_file);
queue_size = props->file_send_queue_.size();
}
LOG_INFO("File added to queue: {} ({} files in queue)",
file_path.filename().string().c_str(), queue_size);
} else {
StartFileTransfer(props, file_path, file_label);
if (props->file_sending_.load()) {
} else {
// Failed to start (race condition: another file started between
// check and call) Add to queue
size_t queue_size = 0;
{
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
SubStreamWindowProperties::QueuedFile queued_file;
queued_file.file_path = file_path;
queued_file.file_label = file_label;
props->file_send_queue_.push(queued_file);
queue_size = props->file_send_queue_.size();
}
LOG_INFO(
"File added to queue after race condition: {} ({} files in "
"queue)",
file_path.filename().string().c_str(), queue_size);
}
}
}
this->ProcessSelectedFile(path, props, file_label_);
}
ImGui::SameLine();

View File

@@ -87,6 +87,11 @@ int Render::FileTransferWindow(
ImVec2(file_transfer_window_width, file_transfer_window_height),
ImGuiCond_Always);
// Set Chinese font for proper display
if (stream_windows_system_chinese_font_) {
ImGui::PushFont(stream_windows_system_chinese_font_);
}
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 3.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 0.9f));
@@ -121,9 +126,9 @@ int Render::FileTransferWindow(
} else {
// Use a scrollable child window for the file list
ImGui::SetWindowFontScale(0.5f);
ImGui::BeginChild("FileList",
ImVec2(0, file_transfer_window_height * 0.75f),
ImGuiChildFlags_Border);
ImGui::BeginChild(
"FileList", ImVec2(0, file_transfer_window_height * 0.75f),
ImGuiChildFlags_Border, ImGuiWindowFlags_HorizontalScrollbar);
ImGui::SetWindowFontScale(1.0f);
ImGui::SetWindowFontScale(0.5f);
@@ -220,6 +225,11 @@ int Render::FileTransferWindow(
ImGui::SetWindowFontScale(0.5f);
ImGui::End();
ImGui::SetWindowFontScale(1.0f);
// Pop Chinese font if it was pushed
if (stream_windows_system_chinese_font_) {
ImGui::PopFont();
}
} else {
ImGui::PopStyleColor(4);
ImGui::PopStyleVar(2);

View File

@@ -0,0 +1,225 @@
#include <cmath>
#include <vector>
#include "rd_log.h"
#include "render.h"
namespace crossdesk {
static void SetServerWindowCircleShape(SDL_Window* window, int diameter) {
if (!window || diameter <= 0) {
return;
}
const int pitch = diameter * 4;
std::vector<unsigned char> pixels((size_t)diameter * (size_t)diameter * 4, 0);
// Sub-pixel centered circle helps symmetry on even diameters.
const float r = (float)diameter * 0.5f;
const float cx = r - 0.5f;
const float cy = r - 0.5f;
for (int y = 0; y < diameter; ++y) {
for (int x = 0; x < diameter; ++x) {
const float dx = (float)x - cx;
const float dy = (float)y - cy;
const float dist = std::sqrt(dx * dx + dy * dy);
// 1px soft edge to reduce jaggies on small circles.
float a = r + 0.5f - dist;
if (a < 0.0f) a = 0.0f;
if (a > 1.0f) a = 1.0f;
const unsigned char alpha = (unsigned char)(a * 255.0f);
const size_t idx = ((size_t)y * (size_t)diameter + (size_t)x) * 4;
pixels[idx + 0] = 255; // R
pixels[idx + 1] = 255; // G
pixels[idx + 2] = 255; // B
pixels[idx + 3] = alpha; // A
}
}
SDL_Surface* shape = SDL_CreateSurfaceFrom(
diameter, diameter, SDL_PIXELFORMAT_RGBA32, pixels.data(), pitch);
if (!shape) {
LOG_ERROR("SDL_CreateSurfaceFrom failed: {}", SDL_GetError());
return;
}
if (!SDL_SetWindowShape(window, shape)) {
LOG_ERROR("SDL_SetWindowShape failed: {}", SDL_GetError());
}
SDL_DestroySurface(shape);
}
int Render::ServerWindow() {
ImGui::SetNextWindowSize(ImVec2(server_window_width_, server_window_height_),
ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
if (server_window_compact_) {
ImGui::SetNextWindowBgAlpha(0.0f);
}
ImGui::Begin("##server_window", nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse);
// Compact mode: show a 50x50 clickable square that restores the window.
if (server_window_compact_) {
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImGui::SetCursorPos(ImVec2(0.0f, 0.0f));
if (ImGui::InvisibleButton(
"##server_compact_restore",
ImVec2(server_window_width_, server_window_height_))) {
if (server_window_) {
SDL_SetWindowShape(server_window_, nullptr);
}
if (server_window_ && server_window_bounds_saved_ &&
server_window_width_before_compact_ > 0 &&
server_window_height_before_compact_ > 0) {
SDL_SetWindowSize(server_window_, server_window_width_before_compact_,
server_window_height_before_compact_);
SDL_SetWindowPosition(server_window_, server_window_x_before_compact_,
server_window_y_before_compact_);
server_window_width_ = (float)server_window_width_before_compact_;
server_window_height_ = (float)server_window_height_before_compact_;
}
server_window_compact_ = false;
server_window_bounds_saved_ = false;
}
// Draw a visible circular affordance.
const float w = server_window_width_;
const float h = server_window_height_;
const float radius = (w < h ? w : h) * 0.5f - 1.0f;
const ImVec2 center(w * 0.5f, h * 0.5f);
draw_list->AddCircleFilled(center, radius, IM_COL32(255, 255, 255, 220),
32);
draw_list->AddCircle(center, radius, IM_COL32(0, 0, 0, 255), 32, 2.0f);
// A simple "restore" hint.
draw_list->AddRect(
ImVec2(center.x - radius * 0.35f, center.y - radius * 0.35f),
ImVec2(center.x + radius * 0.35f, center.y + radius * 0.35f),
IM_COL32(0, 0, 0, 255), 2.0f, 0, 2.0f);
ImGui::End();
return 0;
}
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
ImGui::BeginChild(
"ServerTitleBar",
ImVec2(server_window_width_, server_window_title_bar_height_),
ImGuiChildFlags_Border,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
float server_title_bar_button_width = server_window_title_bar_height_;
float server_title_bar_button_height = server_window_title_bar_height_;
float minimize_button_pos_x =
server_window_width_ - server_title_bar_button_width * 2;
ImGui::SetCursorPos(ImVec2(minimize_button_pos_x, 0.0f));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
float minimize_pos_x =
minimize_button_pos_x + server_title_bar_button_width * 0.33f;
float minimize_pos_y = server_title_bar_button_height * 0.5f;
std::string server_minimize_button = "##minimize"; // ICON_FA_MINUS;
if (ImGui::Button(server_minimize_button.c_str(),
ImVec2(server_title_bar_button_width,
server_title_bar_button_height))) {
if (server_window_) {
int w = 0, h = 0;
int x = 0, y = 0;
SDL_GetWindowSize(server_window_, &w, &h);
SDL_GetWindowPosition(server_window_, &x, &y);
server_window_width_before_compact_ = w;
server_window_height_before_compact_ = h;
server_window_x_before_compact_ = x;
server_window_y_before_compact_ = y;
server_window_bounds_saved_ = true;
constexpr int kCompactSize = 50;
SDL_SetWindowSize(server_window_, kCompactSize, kCompactSize);
// Move to bottom-right of the current display's usable bounds.
SDL_Rect display_bounds;
if (SDL_GetDisplayUsableBounds(SDL_GetDisplayForWindow(server_window_),
&display_bounds)) {
int compact_x = display_bounds.x + display_bounds.w - kCompactSize;
int compact_y = display_bounds.y + display_bounds.h - kCompactSize;
SDL_SetWindowPosition(server_window_, compact_x, compact_y);
}
// Use pixel size to match transparency buffer on HiDPI.
int w_px = kCompactSize;
int h_px = kCompactSize;
SDL_GetWindowSizeInPixels(server_window_, &w_px, &h_px);
const int diameter = (w_px < h_px ? w_px : h_px);
SetServerWindowCircleShape(server_window_, diameter);
server_window_compact_ = true;
}
}
draw_list->AddLine(
ImVec2(minimize_pos_x, minimize_pos_y),
ImVec2(minimize_pos_x + server_title_bar_button_width * 0.33f,
minimize_pos_y),
IM_COL32(0, 0, 0, 255));
ImGui::PopStyleColor(3);
float xmark_button_pos_x =
server_window_width_ - server_title_bar_button_width;
ImGui::SetCursorPos(ImVec2(xmark_button_pos_x, 0.0f));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.0f, 0, 0, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0, 0, 0.5f));
float xmark_pos_x = xmark_button_pos_x + server_title_bar_button_width * 0.5f;
float xmark_pos_y = server_title_bar_button_height * 0.5f;
float xmark_size = server_title_bar_button_width * 0.33f;
std::string server_close_button = "##xmark"; // ICON_FA_XMARK;
if (ImGui::Button(server_close_button.c_str(),
ImVec2(server_title_bar_button_width,
server_title_bar_button_height))) {
LOG_ERROR("Close button clicked");
LeaveConnection(peer_, self_hosted_id_);
}
draw_list->AddLine(ImVec2(xmark_pos_x - xmark_size / 2 - 0.25f,
xmark_pos_y - xmark_size / 2 + 0.75f),
ImVec2(xmark_pos_x + xmark_size / 2 - 1.5f,
xmark_pos_y + xmark_size / 2 - 0.5f),
IM_COL32(0, 0, 0, 255));
draw_list->AddLine(
ImVec2(xmark_pos_x + xmark_size / 2 - 1.75f,
xmark_pos_y - xmark_size / 2 + 0.75f),
ImVec2(xmark_pos_x - xmark_size / 2, xmark_pos_y + xmark_size / 2 - 1.0f),
IM_COL32(0, 0, 0, 255));
ImGui::PopStyleColor(3);
ImGui::PopStyleVar();
ImGui::PopStyleColor();
ImGui::EndChild();
ImGui::PopStyleVar();
ImGui::PopStyleColor();
ImGui::End();
return 0;
}
} // namespace crossdesk