[feat] use DrawToggleSwitch to request permission

This commit is contained in:
dijunkun
2025-11-26 23:32:04 +08:00
parent 76b475450b
commit 0ab6686eb8
6 changed files with 1403 additions and 1344 deletions

0
scripts/macosx/pkg_arm64.sh Normal file → Executable file
View File

File diff suppressed because it is too large Load Diff

View File

@@ -75,8 +75,8 @@
#define SELF_HOSTED_SERVER_CONFIG_OK_BUTTON_PADDING_EN 91 #define SELF_HOSTED_SERVER_CONFIG_OK_BUTTON_PADDING_EN 91
#define UPDATE_NOTIFICATION_OK_BUTTON_PADDING_CN 162 #define UPDATE_NOTIFICATION_OK_BUTTON_PADDING_CN 162
#define UPDATE_NOTIFICATION_OK_BUTTON_PADDING_EN 146 #define UPDATE_NOTIFICATION_OK_BUTTON_PADDING_EN 146
#define REQUEST_PERMISSION_WINDOW_WIDTH_CN 180 #define REQUEST_PERMISSION_WINDOW_WIDTH_CN 130
#define REQUEST_PERMISSION_WINDOW_HEIGHT_CN 128 #define REQUEST_PERMISSION_WINDOW_HEIGHT_CN 128
#define REQUEST_PERMISSION_WINDOW_WIDTH_EN 380 #define REQUEST_PERMISSION_WINDOW_WIDTH_EN 260
#define REQUEST_PERMISSION_WINDOW_HEIGHT_EN 122 #define REQUEST_PERMISSION_WINDOW_HEIGHT_EN 128
#endif #endif

View File

@@ -195,19 +195,9 @@ static std::vector<std::string> screen_recording_permission = {
reinterpret_cast<const char*>(u8"录屏权限"), "Screen Recording Permission"}; reinterpret_cast<const char*>(u8"录屏权限"), "Screen Recording Permission"};
static std::vector<std::string> accessibility_permission = { static std::vector<std::string> accessibility_permission = {
reinterpret_cast<const char*>(u8"键鼠权限"), "Keyboard & Mouse Permission"}; reinterpret_cast<const char*>(u8"键鼠权限"), "Keyboard & Mouse Permission"};
static std::vector<std::string> permission_granted = {
reinterpret_cast<const char*>(u8"已授权"), "Granted"};
static std::vector<std::string> permission_denied = {
reinterpret_cast<const char*>(u8"未授权"), "Denied"};
static std::vector<std::string> open_screen_recording_settings = {
reinterpret_cast<const char*>(u8"打开录屏设置"),
"Open Screen Recording Settings"};
static std::vector<std::string> open_keyboard_mouse_settings = {
reinterpret_cast<const char*>(u8"打开键鼠设置"),
"Open Keyboard & Mouse Settings"};
static std::vector<std::string> permission_required_message = { static std::vector<std::string> permission_required_message = {
reinterpret_cast<const char*>(u8"应用需要以下权限才能正常工作:"), reinterpret_cast<const char*>(u8"应用需要授权以下权限:"),
"The application requires the following permissions to work properly:"}; "The application requires the following permissions:"};
#endif #endif
} // namespace localization } // namespace localization
} // namespace crossdesk } // namespace crossdesk

View File

@@ -192,7 +192,6 @@ class Render {
int RequestPermissionWindow(); int RequestPermissionWindow();
bool CheckScreenRecordingPermission(); bool CheckScreenRecordingPermission();
bool CheckAccessibilityPermission(); bool CheckAccessibilityPermission();
void OpenSystemPreferences();
void OpenScreenRecordingPreferences(); void OpenScreenRecordingPreferences();
void OpenAccessibilityPreferences(); void OpenAccessibilityPreferences();
#endif #endif

View File

@@ -3,87 +3,107 @@
#include "rd_log.h" #include "rd_log.h"
#include "render.h" #include "render.h"
#ifdef __APPLE__
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
#include <CoreGraphics/CoreGraphics.h> #include <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include <unistd.h> #include <unistd.h>
#include <cstdlib> #include <cstdlib>
#endif
namespace crossdesk { namespace crossdesk {
#ifdef __APPLE__ static bool DrawToggleSwitch(const char* id, bool active, bool enabled) {
ImGuiIO& io = ImGui::GetIO();
(void)io;
ImDrawList* draw_list = ImGui::GetWindowDrawList();
float height = ImGui::GetFrameHeight();
float width = height * 1.8f;
float radius = height * 0.5f;
ImVec2 p = ImGui::GetCursorScreenPos();
ImGui::InvisibleButton(id, ImVec2(width, height));
bool hovered = ImGui::IsItemHovered();
bool clicked = ImGui::IsItemClicked() && enabled;
ImVec4 col_bg_vec;
if (active) {
col_bg_vec =
hovered && enabled ? ImVec4(0.26f, 0.59f, 0.98f, 1.0f) : ImVec4(0.0f, 0.0f, 1.0f, 1.0f);
} else {
col_bg_vec =
hovered && enabled ? ImVec4(0.70f, 0.70f, 0.70f, 1.0f) : ImVec4(0.60f, 0.60f, 0.60f, 1.0f);
}
if (!enabled) {
col_bg_vec.w *= 0.6f;
}
ImU32 col_bg = ImGui::GetColorU32(col_bg_vec);
draw_list->AddRectFilled(ImVec2(p.x, p.y + 0.5f), ImVec2(p.x + width, p.y + height - 0.5f),
col_bg, height * 0.5f);
float t = active ? 1.0f : 0.0f;
float knob_height = height - 4.0f;
float knob_width = knob_height * 1.2f;
float knob_radius = knob_height * 0.5f;
float knob_min_x = p.x + 2.0f;
float knob_max_x = p.x + width - knob_width - 2.0f;
float knob_x = knob_min_x + t * (knob_max_x - knob_min_x);
float knob_y = p.y + (height - knob_height) * 0.5f;
ImU32 col_knob = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, enabled ? 1.0f : 0.9f));
draw_list->AddRectFilled(ImVec2(knob_x, knob_y),
ImVec2(knob_x + knob_width, knob_y + knob_height), col_knob,
knob_radius);
return clicked;
}
bool Render::CheckScreenRecordingPermission() { bool Render::CheckScreenRecordingPermission() {
// CGPreflightScreenCaptureAccess is available on macOS 10.15+ // CGPreflightScreenCaptureAccess is available on macOS 10.15+
if (@available(macOS 10.15, *)) { if (@available(macOS 10.15, *)) {
bool granted = CGPreflightScreenCaptureAccess(); bool granted = CGPreflightScreenCaptureAccess();
LOG_INFO("CGPreflightScreenCaptureAccess returned: {}", granted);
return granted; return granted;
} }
// For older macOS versions, assume permission is granted // for older macOS versions, assume permission is granted
return true; return true;
} }
bool Render::CheckAccessibilityPermission() { bool Render::CheckAccessibilityPermission() {
// Check if the process is trusted for accessibility
// Note: This may require app restart to reflect permission changes
NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @NO}; NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @NO};
bool trusted = AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options); bool trusted = AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
LOG_INFO("AXIsProcessTrustedWithOptions returned: {}", trusted);
return trusted; return trusted;
} }
void Render::OpenSystemPreferences() {
// Open System Preferences to the Privacy & Security > Screen Recording or
// Accessibility section
system("open "
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
"ScreenCapture\"");
}
void Render::OpenScreenRecordingPreferences() {
// Request screen recording permission first to ensure app appears in System Settings
if (@available(macOS 10.15, *)) {
CGRequestScreenCaptureAccess();
}
// Open System Preferences to the Privacy & Security > Screen Recording section
system("open "
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
"ScreenCapture\"");
}
void Render::OpenAccessibilityPreferences() { void Render::OpenAccessibilityPreferences() {
// Request accessibility permission first to ensure app appears in System Settings
NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @YES}; NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @YES};
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options); AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
// Open System Preferences to the Privacy & Security > Accessibility section
system("open " system("open "
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_" "\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
"Accessibility\""); "Accessibility\"");
} }
#endif
void Render::OpenScreenRecordingPreferences() {
if (@available(macOS 10.15, *)) {
CGRequestScreenCaptureAccess();
}
system("open "
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
"ScreenCapture\"");
}
int Render::RequestPermissionWindow() { int Render::RequestPermissionWindow() {
#ifdef __APPLE__
// Check permissions - recheck every frame to update status immediately after user grants
// permission
bool screen_recording_granted = CheckScreenRecordingPermission(); bool screen_recording_granted = CheckScreenRecordingPermission();
bool accessibility_granted = CheckAccessibilityPermission(); bool accessibility_granted = CheckAccessibilityPermission();
// Update show_request_permission_window_ based on permission status
// Keep window visible if any permission is not granted
show_request_permission_window_ = !screen_recording_granted || !accessibility_granted; show_request_permission_window_ = !screen_recording_granted || !accessibility_granted;
// Log permission status for debugging
LOG_INFO("Screen recording permission: {}, Accessibility permission: {}",
screen_recording_granted, accessibility_granted);
if (!show_request_permission_window_) { if (!show_request_permission_window_) {
LOG_INFO("Request permission window is not shown");
return 0; return 0;
} }
LOG_INFO("Request permission window is shown");
const ImGuiViewport* viewport = ImGui::GetMainViewport(); const ImGuiViewport* viewport = ImGui::GetMainViewport();
float window_width = localization_language_index_ == 0 ? REQUEST_PERMISSION_WINDOW_WIDTH_CN float window_width = localization_language_index_ == 0 ? REQUEST_PERMISSION_WINDOW_WIDTH_CN
@@ -91,14 +111,13 @@ int Render::RequestPermissionWindow() {
float window_height = localization_language_index_ == 0 ? REQUEST_PERMISSION_WINDOW_HEIGHT_CN float window_height = localization_language_index_ == 0 ? REQUEST_PERMISSION_WINDOW_HEIGHT_CN
: REQUEST_PERMISSION_WINDOW_HEIGHT_EN; : REQUEST_PERMISSION_WINDOW_HEIGHT_EN;
// Center the window on screen // center the window on screen
ImVec2 center_pos = ImVec2((viewport->WorkSize.x - window_width) * 0.5f + viewport->WorkPos.x, ImVec2 center_pos = ImVec2((viewport->WorkSize.x - window_width) * 0.5f + viewport->WorkPos.x,
(viewport->WorkSize.y - window_height) * 0.5f + viewport->WorkPos.y); (viewport->WorkSize.y - window_height) * 0.5f + viewport->WorkPos.y);
ImGui::SetNextWindowPos(center_pos, ImGuiCond_Always); ImGui::SetNextWindowPos(center_pos, ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(window_width, window_height), ImGuiCond_Always); ImGui::SetNextWindowSize(ImVec2(window_width, window_height), ImGuiCond_Always);
// Make window always on top and modal-like
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::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
@@ -107,16 +126,15 @@ int Render::RequestPermissionWindow() {
ImGui::Begin(localization::request_permissions[localization_language_index_].c_str(), nullptr, ImGui::Begin(localization::request_permissions[localization_language_index_].c_str(), nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_Modal); ImGuiWindowFlags_NoSavedSettings);
ImGui::SetWindowFontScale(0.3f); ImGui::SetWindowFontScale(0.3f);
// use system Chinese font // use system font
if (system_chinese_font_ != nullptr) { if (system_chinese_font_ != nullptr) {
ImGui::PushFont(system_chinese_font_); ImGui::PushFont(system_chinese_font_);
} }
// Message
ImGui::SetCursorPosX(10.0f); ImGui::SetCursorPosX(10.0f);
ImGui::TextWrapped( ImGui::TextWrapped(
"%s", localization::permission_required_message[localization_language_index_].c_str()); "%s", localization::permission_required_message[localization_language_index_].c_str());
@@ -125,7 +143,7 @@ int Render::RequestPermissionWindow() {
ImGui::Spacing(); ImGui::Spacing();
ImGui::Spacing(); ImGui::Spacing();
// Accessibility Permission // accessibility permission
ImGui::SetCursorPosX(10.0f); ImGui::SetCursorPosX(10.0f);
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
ImGui::Text("1. %s:", ImGui::Text("1. %s:",
@@ -133,19 +151,16 @@ int Render::RequestPermissionWindow() {
ImGui::SameLine(); ImGui::SameLine();
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
if (accessibility_granted) { if (accessibility_granted) {
ImGui::Text("%s", localization::permission_granted[localization_language_index_].c_str()); DrawToggleSwitch("accessibility_toggle_on", true, false);
} else { } else {
ImGui::Text("%s", localization::permission_denied[localization_language_index_].c_str()); if (DrawToggleSwitch("accessibility_toggle", accessibility_granted, !accessibility_granted)) {
ImGui::SameLine();
if (ImGui::Button(
localization::open_keyboard_mouse_settings[localization_language_index_].c_str())) {
OpenAccessibilityPreferences(); OpenAccessibilityPreferences();
} }
} }
ImGui::Spacing(); ImGui::Spacing();
// Screen Recording Permission // screen recording permission
ImGui::SetCursorPosX(10.0f); ImGui::SetCursorPosX(10.0f);
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
ImGui::Text("2. %s:", ImGui::Text("2. %s:",
@@ -153,12 +168,11 @@ int Render::RequestPermissionWindow() {
ImGui::SameLine(); ImGui::SameLine();
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
if (screen_recording_granted) { if (screen_recording_granted) {
ImGui::Text("%s", localization::permission_granted[localization_language_index_].c_str()); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10.0f);
DrawToggleSwitch("screen_recording_toggle_on", true, false);
} else { } else {
ImGui::Text("%s", localization::permission_denied[localization_language_index_].c_str()); if (DrawToggleSwitch("screen_recording_toggle", screen_recording_granted,
ImGui::SameLine(); !screen_recording_granted)) {
if (ImGui::Button(
localization::open_screen_recording_settings[localization_language_index_].c_str())) {
OpenScreenRecordingPreferences(); OpenScreenRecordingPreferences();
} }
} }
@@ -177,8 +191,5 @@ int Render::RequestPermissionWindow() {
ImGui::PopStyleColor(); ImGui::PopStyleColor();
return 0; return 0;
#else
return 0;
#endif
} }
} // namespace crossdesk } // namespace crossdesk