mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
320 lines
12 KiB
C++
320 lines
12 KiB
C++
#include <random>
|
|
|
|
#include "IconsFontAwesome6.h"
|
|
#include "layout_style.h"
|
|
#include "localization.h"
|
|
#include "rd_log.h"
|
|
#include "render.h"
|
|
|
|
int Render::LocalWindow() {
|
|
ImGui::SetNextWindowPos(ImVec2(-1.0f, title_bar_height_), ImGuiCond_Always);
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
|
ImGui::BeginChild("LocalDesktopWindow",
|
|
ImVec2(local_window_width_, local_window_height_),
|
|
ImGuiChildFlags_None,
|
|
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
|
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
|
|
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
|
ImGui::PopStyleColor();
|
|
|
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + main_window_text_y_padding_);
|
|
ImGui::Indent(main_child_window_x_padding_);
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
|
|
ImGui::TextColored(
|
|
ImVec4(0.0f, 0.0f, 0.0f, 0.5f), "%s",
|
|
localization::local_desktop[localization_language_index_].c_str());
|
|
|
|
ImGui::Spacing();
|
|
{
|
|
ImGui::SetNextWindowPos(
|
|
ImVec2(main_child_window_x_padding_,
|
|
title_bar_height_ + main_child_window_y_padding_),
|
|
ImGuiCond_Always);
|
|
ImGui::PushStyleColor(ImGuiCol_ChildBg,
|
|
ImVec4(239.0 / 255, 240.0 / 255, 242.0 / 255, 1.0f));
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f);
|
|
ImGui::BeginChild(
|
|
"LocalDesktopWindow_1",
|
|
ImVec2(local_child_window_width_, local_child_window_height_),
|
|
ImGuiChildFlags_Border,
|
|
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
|
ImGuiWindowFlags_NoTitleBar |
|
|
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
|
ImGui::PopStyleVar();
|
|
ImGui::PopStyleColor();
|
|
{
|
|
ImGui::SetWindowFontScale(0.5f);
|
|
ImGui::Text("%s",
|
|
localization::local_id[localization_language_index_].c_str());
|
|
|
|
ImGui::Spacing();
|
|
|
|
ImGui::SetNextItemWidth(IPUT_WINDOW_WIDTH);
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
|
|
|
|
if (strcmp(client_id_display_, client_id_)) {
|
|
for (int i = 0, j = 0; i < sizeof(client_id_); i++, j++) {
|
|
client_id_display_[j] = client_id_[i];
|
|
if (i == 2 || i == 5) {
|
|
client_id_display_[++j] = ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
ImGui::InputText(
|
|
"##local_id", client_id_display_, IM_ARRAYSIZE(client_id_display_),
|
|
ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_ReadOnly);
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::SameLine();
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
|
|
ImGui::SetWindowFontScale(0.5f);
|
|
|
|
if (ImGui::Button(ICON_FA_COPY, ImVec2(35, 38))) {
|
|
local_id_copied_ = true;
|
|
ImGui::SetClipboardText(client_id_);
|
|
copy_start_time_ = ImGui::GetTime();
|
|
}
|
|
|
|
ImGui::PopStyleColor(3);
|
|
|
|
auto time_duration = ImGui::GetTime() - copy_start_time_;
|
|
if (local_id_copied_ && time_duration < 1.0f) {
|
|
const ImGuiViewport *viewport = ImGui::GetMainViewport();
|
|
ImGui::SetNextWindowPos(
|
|
ImVec2((viewport->WorkSize.x - viewport->WorkPos.x -
|
|
notification_window_width_) /
|
|
2,
|
|
(viewport->WorkSize.y - viewport->WorkPos.y -
|
|
notification_window_height_) /
|
|
2));
|
|
|
|
ImGui::SetNextWindowSize(
|
|
ImVec2(notification_window_width_, notification_window_height_));
|
|
ImGui::PushStyleColor(ImGuiCol_WindowBg,
|
|
ImVec4(1.0, 1.0, 1.0, 1.0 - time_duration));
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
|
ImGui::Begin("ConnectionStatusWindow", nullptr,
|
|
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
|
ImGuiWindowFlags_NoSavedSettings);
|
|
ImGui::PopStyleVar(2);
|
|
ImGui::PopStyleColor();
|
|
|
|
auto window_width = ImGui::GetWindowSize().x;
|
|
auto window_height = ImGui::GetWindowSize().y;
|
|
ImGui::SetWindowFontScale(0.8f);
|
|
std::string text = localization::local_id_copied_to_clipboard
|
|
[localization_language_index_];
|
|
auto text_width = ImGui::CalcTextSize(text.c_str()).x;
|
|
ImGui::SetCursorPosX((window_width - text_width) * 0.5f);
|
|
ImGui::SetCursorPosY(window_height * 0.5f);
|
|
ImGui::PushStyleColor(ImGuiCol_Text,
|
|
ImVec4(0, 0, 0, 1.0 - time_duration));
|
|
ImGui::Text("%s", text.c_str());
|
|
ImGui::PopStyleColor();
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
|
|
ImGui::End();
|
|
}
|
|
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
|
|
ImGui::Spacing();
|
|
ImGui::Separator();
|
|
ImGui::Spacing();
|
|
|
|
ImGui::SetWindowFontScale(0.5f);
|
|
|
|
ImGui::Text("%s",
|
|
localization::password[localization_language_index_].c_str());
|
|
|
|
ImGui::SetNextItemWidth(IPUT_WINDOW_WIDTH);
|
|
ImGui::Spacing();
|
|
|
|
if (!password_inited_) {
|
|
char a[] = {
|
|
"123456789QWERTYUPASDFGHJKLZXCVBNMqwertyupasdfghijkzxcvbnm"};
|
|
std::mt19937 generator(
|
|
std::chrono::system_clock::now().time_since_epoch().count());
|
|
std::uniform_int_distribution<int> distribution(0, strlen(a) - 1);
|
|
|
|
random_password_.clear();
|
|
for (int i = 0, len = strlen(a); i < 6; i++) {
|
|
random_password_ += a[distribution(generator)];
|
|
}
|
|
password_inited_ = true;
|
|
if (0 != strcmp(random_password_.c_str(), password_saved_)) {
|
|
strncpy(password_saved_, random_password_.c_str(),
|
|
sizeof(password_saved_));
|
|
LOG_INFO("Generate new password and save into cache file");
|
|
SaveSettingsIntoCacheFile();
|
|
}
|
|
}
|
|
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
|
|
ImGui::InputTextWithHint(
|
|
"##server_pwd",
|
|
localization::max_password_len[localization_language_index_].c_str(),
|
|
password_saved_, IM_ARRAYSIZE(password_saved_),
|
|
show_password_
|
|
? ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_ReadOnly
|
|
: ImGuiInputTextFlags_CharsNoBlank |
|
|
ImGuiInputTextFlags_Password |
|
|
ImGuiInputTextFlags_ReadOnly);
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::SameLine();
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
|
|
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
|
|
|
|
ImGui::SetWindowFontScale(0.5f);
|
|
auto l_x = ImGui::GetCursorScreenPos().x;
|
|
auto l_y = ImGui::GetCursorScreenPos().y;
|
|
if (ImGui::Button(ICON_FA_EYE, ImVec2(22, 38))) {
|
|
show_password_ = !show_password_;
|
|
}
|
|
|
|
if (!show_password_) {
|
|
ImDrawList *draw_list = ImGui::GetWindowDrawList();
|
|
draw_list->AddLine(ImVec2(l_x + 3.0f, l_y + 12.5f),
|
|
ImVec2(l_x + 20.3f, l_y + 26.5f),
|
|
IM_COL32(239, 240, 242, 255), 2.0f);
|
|
draw_list->AddLine(ImVec2(l_x + 3.0f, l_y + 11.0f),
|
|
ImVec2(l_x + 20.3f, l_y + 25.0f),
|
|
IM_COL32(0, 0, 0, 255), 1.5f);
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button(
|
|
regenerate_password_ ? ICON_FA_SPINNER : ICON_FA_ARROWS_ROTATE,
|
|
ImVec2(22, 38))) {
|
|
regenerate_password_ = true;
|
|
password_inited_ = false;
|
|
regenerate_password_start_time_ = ImGui::GetTime();
|
|
LeaveConnection(peer_, client_id_);
|
|
is_create_connection_ = false;
|
|
}
|
|
if (ImGui::GetTime() - regenerate_password_start_time_ > 0.3f) {
|
|
regenerate_password_ = false;
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button(ICON_FA_PEN, ImVec2(22, 38))) {
|
|
show_reset_password_window_ = true;
|
|
}
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
ImGui::PopStyleColor(3);
|
|
|
|
if (show_reset_password_window_) {
|
|
const ImGuiViewport *viewport = ImGui::GetMainViewport();
|
|
|
|
ImGui::SetNextWindowPos(
|
|
ImVec2((viewport->WorkSize.x - viewport->WorkPos.x -
|
|
connection_status_window_width_) /
|
|
2,
|
|
(viewport->WorkSize.y - viewport->WorkPos.y -
|
|
connection_status_window_height_) /
|
|
2));
|
|
|
|
ImGui::SetNextWindowSize(ImVec2(connection_status_window_width_,
|
|
connection_status_window_height_));
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0));
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
|
|
|
ImGui::Begin("ResetPasswordWindow", nullptr,
|
|
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
|
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar |
|
|
ImGuiWindowFlags_NoSavedSettings);
|
|
ImGui::PopStyleVar(2);
|
|
ImGui::PopStyleColor();
|
|
|
|
auto window_width = ImGui::GetWindowSize().x;
|
|
auto window_height = ImGui::GetWindowSize().y;
|
|
std::string text =
|
|
localization::new_password[localization_language_index_];
|
|
auto text_width = ImGui::CalcTextSize(text.c_str()).x;
|
|
ImGui::SetWindowFontScale(0.5f);
|
|
ImGui::SetCursorPosX((window_width - text_width / 2) * 0.5f);
|
|
ImGui::SetCursorPosY(window_height * 0.2f);
|
|
ImGui::Text("%s", text.c_str());
|
|
|
|
ImGui::SetCursorPosX((window_width - IPUT_WINDOW_WIDTH / 2) * 0.5f);
|
|
ImGui::SetCursorPosY(window_height * 0.4f);
|
|
ImGui::SetNextItemWidth(IPUT_WINDOW_WIDTH / 2);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
|
|
|
|
if (focus_on_input_widget_) {
|
|
ImGui::SetKeyboardFocusHere();
|
|
focus_on_input_widget_ = false;
|
|
}
|
|
|
|
bool enter_pressed = ImGui::InputText(
|
|
"##new_password", new_password_, IM_ARRAYSIZE(new_password_),
|
|
ImGuiInputTextFlags_CharsNoBlank |
|
|
ImGuiInputTextFlags_EnterReturnsTrue);
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::SetCursorPosX(window_width * 0.315f);
|
|
ImGui::SetCursorPosY(window_height * 0.75f);
|
|
|
|
// OK
|
|
if (ImGui::Button(
|
|
localization::ok[localization_language_index_].c_str()) ||
|
|
enter_pressed) {
|
|
if (6 != strlen(new_password_)) {
|
|
LOG_ERROR("Invalid password length");
|
|
show_reset_password_window_ = true;
|
|
focus_on_input_widget_ = true;
|
|
} else {
|
|
show_reset_password_window_ = false;
|
|
LOG_INFO("Generate new password and save into cache file");
|
|
strncpy(password_saved_, new_password_, sizeof(password_saved_));
|
|
memset(new_password_, 0, sizeof(new_password_));
|
|
SaveSettingsIntoCacheFile();
|
|
LeaveConnection(peer_, client_id_);
|
|
is_create_connection_ = false;
|
|
focus_on_input_widget_ = true;
|
|
}
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button(
|
|
localization::cancel[localization_language_index_].c_str())) {
|
|
show_reset_password_window_ = false;
|
|
focus_on_input_widget_ = true;
|
|
memset(new_password_, 0, sizeof(new_password_));
|
|
}
|
|
|
|
ImGui::SetWindowFontScale(1.0f);
|
|
|
|
ImGui::End();
|
|
ImGui::PopStyleVar();
|
|
}
|
|
}
|
|
|
|
ImGui::EndChild();
|
|
}
|
|
|
|
ImGui::EndChild();
|
|
ImGui::PopStyleVar();
|
|
|
|
return 0;
|
|
} |