refactor: rebuild desktop client with Slint

This commit is contained in:
dijunkun
2026-07-21 16:38:51 +08:00
parent eeb6a2a1ae
commit 59f77c2820
79 changed files with 7373 additions and 1009 deletions
+2 -39
View File
@@ -1,24 +1,12 @@
#ifndef CROSSDESK_GUI_APPLICATION_STATE_H_
#define CROSSDESK_GUI_APPLICATION_STATE_H_
#include <SDL3/SDL.h>
#include <chrono>
#include <cstdint>
#include <memory>
#include <nlohmann/json.hpp>
#include <string>
#include "imgui.h"
#if _WIN32
#include "platform/tray/win_tray.h"
#elif defined(__APPLE__)
#include "platform/tray/mac_tray.h"
#elif defined(__linux__)
#include "platform/tray/linux_tray.h"
#endif
namespace crossdesk::gui_detail {
struct MainWindowState {
@@ -27,25 +15,10 @@ struct MainWindowState {
float title_bar_button_width_ = 30;
float title_bar_button_height_ = 30;
SDL_Window *main_window_ = nullptr;
SDL_Renderer *main_renderer_ = nullptr;
ImGuiContext *main_ctx_ = nullptr;
ImFont *main_windows_system_chinese_font_ = nullptr;
ImFont *stream_windows_system_chinese_font_ = nullptr;
ImFont *server_windows_system_chinese_font_ = nullptr;
bool exit_ = false;
const int sdl_refresh_ms_ = 16;
#if _WIN32
std::unique_ptr<WinTray> tray_;
#elif defined(__APPLE__)
std::unique_ptr<MacTray> tray_;
#elif defined(__linux__)
std::unique_ptr<LinuxTray> tray_;
#endif
bool main_window_minimized_ = false;
uint32_t last_main_minimize_request_tick_ = 0;
uint32_t last_stream_minimize_request_tick_ = 0;
int main_window_width_real_ = 720;
int main_window_height_real_ = 540;
float main_window_dpi_scaling_w_ = 1.0f;
@@ -77,8 +50,6 @@ struct MainWindowState {
float about_window_height_ = 170;
float update_notification_window_width_ = 400;
float update_notification_window_height_ = 320;
uint32_t STREAM_REFRESH_EVENT = 0;
uint32_t APP_EXIT_EVENT = 0;
};
// Application-global interaction flags that coordinate SDL events with media
@@ -116,14 +87,15 @@ struct InteractionState {
std::string controlled_remote_id_;
std::string focused_remote_id_;
std::string remote_client_id_;
SDL_Event last_mouse_event{};
};
struct UpdateState {
nlohmann::json latest_version_info_ = nlohmann::json{};
bool update_available_ = false;
std::string latest_version_;
std::string release_name_;
std::string release_notes_;
std::string release_date_;
bool show_new_version_icon_ = false;
bool show_new_version_icon_in_menu_ = true;
double new_version_icon_last_trigger_time_ = 0.0;
@@ -131,20 +103,15 @@ struct UpdateState {
};
struct StreamWindowState {
SDL_Window *stream_window_ = nullptr;
SDL_Renderer *stream_renderer_ = nullptr;
ImGuiContext *stream_ctx_ = nullptr;
bool need_to_create_stream_window_ = false;
bool stream_window_created_ = false;
bool stream_window_inited_ = false;
bool window_maximized_ = false;
bool stream_window_grabbed_ = false;
bool control_mouse_ = false;
int stream_window_width_default_ = 1280;
int stream_window_height_default_ = 720;
float stream_window_width_ = 1280;
float stream_window_height_ = 720;
SDL_PixelFormat stream_pixformat_ = SDL_PIXELFORMAT_NV12;
int stream_window_width_real_ = 1280;
int stream_window_height_real_ = 720;
float stream_window_dpi_scaling_w_ = 1.0f;
@@ -152,9 +119,6 @@ struct StreamWindowState {
};
struct ServerWindowState {
SDL_Window *server_window_ = nullptr;
SDL_Renderer *server_renderer_ = nullptr;
ImGuiContext *server_ctx_ = nullptr;
bool need_to_create_server_window_ = false;
bool need_to_destroy_server_window_ = false;
bool server_window_created_ = false;
@@ -164,7 +128,6 @@ struct ServerWindowState {
float server_window_width_ = 250;
float server_window_height_ = 150;
float server_window_title_bar_height_ = 30.0f;
SDL_PixelFormat server_pixformat_ = SDL_PIXELFORMAT_NV12;
int server_window_normal_width_ = 250;
int server_window_normal_height_ = 150;
float server_window_dpi_scaling_w_ = 1.0f;
File diff suppressed because it is too large Load Diff
+33 -79
View File
@@ -1,20 +1,15 @@
#ifndef CROSSDESK_GUI_APPLICATION_H_
#define CROSSDESK_GUI_APPLICATION_H_
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include "IconsFontAwesome6.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h"
#include "imgui_internal.h"
#include "runtime/gui_runtime.h"
namespace crossdesk {
// SDL/ImGui application shell. Feature state and protocol behavior live in
// GuiRuntime and its composed controllers.
// Slint application shell. GuiRuntime remains the owner of transport, media,
// device, clipboard, transfer and settings behavior.
class GuiApplication final : private GuiRuntime {
public:
GuiApplication();
@@ -23,88 +18,47 @@ public:
int Run();
private:
// Native window integration.
static SDL_HitTestResult HitTestCallback(SDL_Window *window,
const SDL_Point *area, void *data);
struct SlintUi;
// Application lifecycle.
void InitializeLogger();
void InitializeSettings();
void InitializeSDL();
bool InitializeSDL();
void InitializeModules();
void InitializeMainWindow();
void MainLoop();
void HandleStreamWindow();
void HandleServerWindow();
void Cleanup();
// Window lifecycle and rendering.
int CreateMainWindow();
int DestroyMainWindow();
int CreateStreamWindow();
int DestroyStreamWindow();
int CreateServerWindow();
int DestroyServerWindow();
int SetupFontAndStyle(ImFont **system_chinese_font_out);
int DestroyMainWindowContext();
int DestroyStreamWindowContext();
int DestroyServerWindowContext();
int DrawMainWindow();
int DrawStreamWindow();
int DrawServerWindow();
// Views and dialogs.
int TitleBar(bool main_window);
int MainWindow();
int UpdateNotificationWindow();
int StreamWindow();
int ServerWindow();
int RemoteClientInfoWindow();
int LocalWindow();
int RemoteWindow();
int RecentConnectionsWindow();
int SettingWindow();
int SelfHostedServerWindow();
int ControlWindow(std::shared_ptr<RemoteSession> &props);
int ControlBar(std::shared_ptr<RemoteSession> &props);
int AboutWindow();
int StatusBar();
bool
ConnectionStatusWindow(std::shared_ptr<RemoteSession> &props);
int ShowRecentConnections();
int ConfirmDeleteConnection();
int EditRecentConnectionAliasWindow();
int OfflineWarningWindow();
int NetTrafficStats(std::shared_ptr<RemoteSession> &props);
int FileTransferWindow(std::shared_ptr<RemoteSession> &props);
void
DrawConnectionStatusText(std::shared_ptr<RemoteSession> &props);
void
DrawReceivingScreenText(std::shared_ptr<RemoteSession> &props);
bool OpenUrl(const std::string &url);
void Hyperlink(const std::string &label, const std::string &url,
float window_width);
std::string OpenFileDialog(std::string title);
void InitializeUi();
void InitializeSystemTray();
bool MinimizeMainWindowToTray();
// SDL event dispatch and input translation.
void UpdateRenderRect();
void ProcessSdlEvent(const SDL_Event &event);
int ProcessKeyboardEvent(const SDL_Event &event);
int ProcessMouseEvent(const SDL_Event &event);
void CloseTab(decltype(remote_sessions_)::iterator &it);
void BindMainCallbacks();
void BindStreamCallbacks();
void BindServerCallbacks();
void Tick();
void SyncMainWindow();
void SyncConnectionDialog();
void SyncPlatformDialogs();
void SyncStreamWindow();
void SyncServerWindow();
void UpdateLocalization();
void ResetSettingsUi();
void SaveSettingsFromUi();
#if _WIN32 && CROSSDESK_PORTABLE
void CheckPortableWindowsService();
int PortableServiceInstallWindow();
void StartPortableWindowsServiceInstall();
void JoinPortableWindowsServiceInstallThread();
#endif
void ConnectFromUi(const std::string &remote_id);
void SelectStreamTab(int index);
void ReorderStreamTab(int from, float drop_x, float tab_width);
void CloseStreamTab(const std::string &remote_id);
void SendPointerInput(int button, int kind, float x, float y);
void SendScrollInput(float delta_x, float delta_y, float x, float y);
void SendKeyInput(const std::string &text, bool pressed, bool control,
bool alt, bool shift, bool meta);
void Cleanup();
#ifdef __APPLE__
int RequestPermissionWindow();
bool DrawToggleSwitch(const char *id, bool active, bool enabled);
#endif
std::shared_ptr<RemoteSession> SelectedSession();
std::string OpenFileDialog(const std::string &title);
bool OpenUrl(const std::string &url);
std::unique_ptr<SlintUi> ui_;
};
} // namespace crossdesk
@@ -0,0 +1,144 @@
#include "application/gui_application.h"
#if _WIN32 && CROSSDESK_PORTABLE
#include <Windows.h>
#include <shellapi.h>
#include <filesystem>
#include <vector>
#include "rd_log.h"
#include "service_host.h"
namespace crossdesk {
namespace {
std::filesystem::path GetCurrentExecutablePath() {
std::vector<wchar_t> buffer(MAX_PATH);
while (true) {
const DWORD length = GetModuleFileNameW(
nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
if (length == 0) {
return {};
}
if (length < buffer.size()) {
return std::filesystem::path(buffer.data(), buffer.data() + length);
}
if (buffer.size() >= 32768) {
return {};
}
buffer.resize(buffer.size() * 2);
}
}
bool InstallServiceWithElevation() {
const std::filesystem::path executable_path = GetCurrentExecutablePath();
if (executable_path.empty()) {
LOG_ERROR("Portable service install failed: current executable not found");
return false;
}
const std::filesystem::path service_path =
executable_path.parent_path() / L"crossdesk_service.exe";
const std::filesystem::path helper_path =
executable_path.parent_path() / L"crossdesk_session_helper.exe";
if (!std::filesystem::exists(service_path) ||
!std::filesystem::exists(helper_path)) {
LOG_ERROR("Portable service install failed: service binaries missing, "
"service={}, helper={}",
service_path.string(), helper_path.string());
return false;
}
const std::wstring executable = executable_path.wstring();
const std::wstring working_dir = executable_path.parent_path().wstring();
constexpr wchar_t parameters[] = L"--service-install";
SHELLEXECUTEINFOW execute_info{};
execute_info.cbSize = sizeof(execute_info);
execute_info.fMask = SEE_MASK_NOCLOSEPROCESS;
execute_info.lpVerb = L"runas";
execute_info.lpFile = executable.c_str();
execute_info.lpParameters = parameters;
execute_info.lpDirectory = working_dir.c_str();
execute_info.nShow = SW_HIDE;
if (!ShellExecuteExW(&execute_info)) {
LOG_ERROR("Portable service install failed: ShellExecuteExW error={}",
GetLastError());
return false;
}
const DWORD wait_result =
WaitForSingleObject(execute_info.hProcess, INFINITE);
DWORD exit_code = 1;
if (wait_result == WAIT_OBJECT_0) {
GetExitCodeProcess(execute_info.hProcess, &exit_code);
} else {
LOG_ERROR("Portable service install wait failed, result={}", wait_result);
}
CloseHandle(execute_info.hProcess);
if (exit_code != 0) {
LOG_ERROR("Portable service install command failed, exit_code={}",
exit_code);
return false;
}
const bool started = StartCrossDeskService();
if (!started) {
LOG_WARN("Portable service installed but start failed");
}
return IsCrossDeskServiceInstalled() && started;
}
} // namespace
void GuiApplication::CheckPortableWindowsService() {
if (portable_service_prompt_checked_) {
return;
}
portable_service_prompt_checked_ = true;
portable_service_installed_ = IsCrossDeskServiceInstalled();
if (portable_service_installed_ || portable_service_prompt_suppressed_) {
return;
}
portable_service_install_state_.store(PortableServiceInstallState::idle,
std::memory_order_relaxed);
show_portable_service_install_window_ = true;
}
void GuiApplication::StartPortableWindowsServiceInstall() {
portable_service_do_not_remind_ = false;
PortableServiceInstallState expected = PortableServiceInstallState::idle;
if (!portable_service_install_state_.compare_exchange_strong(
expected, PortableServiceInstallState::installing,
std::memory_order_acq_rel)) {
if (expected != PortableServiceInstallState::failed) {
return;
}
portable_service_install_state_.store(
PortableServiceInstallState::installing, std::memory_order_release);
}
JoinPortableWindowsServiceInstallThread();
portable_service_install_thread_ = std::thread([this]() {
const bool installed = InstallServiceWithElevation();
portable_service_install_state_.store(
installed ? PortableServiceInstallState::succeeded
: PortableServiceInstallState::failed,
std::memory_order_release);
});
}
void GuiApplication::JoinPortableWindowsServiceInstallThread() {
if (portable_service_install_thread_.joinable()) {
portable_service_install_thread_.join();
}
}
} // namespace crossdesk
#endif