diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2668b1b..fd39a57 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,10 +25,19 @@ jobs: sudo apt-get update sudo apt-get install -y build-essential pkg-config \ libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \ - libwayland-dev libwayland-bin libxkbcommon-dev + libwayland-dev libwayland-bin libxkbcommon-dev xvfb xauth fonts-noto-cjk - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@v2 - run: cargo test --locked + - name: GTK appearance and settings smoke tests + run: xvfb-run -a -s '-screen 0 1600x1200x24' sh tests/gtk-ui-smoke.sh "$RUNNER_TEMP/gtk-ui-previews" + - name: Synthetic GTK previews + if: always() + uses: actions/upload-artifact@v4 + with: + name: gtk-ui-previews + path: ${{ runner.temp }}/gtk-ui-previews + if-no-files-found: ignore # Same as the release job: the wayland plugin is built on 22.04; libei is unavailable here, so # the portal plugin is skipped with a warning (release builds it on 24.04). - name: Release feature set diff --git a/README.en.md b/README.en.md index e45348c..ef8f269 100644 --- a/README.en.md +++ b/README.en.md @@ -23,6 +23,13 @@

Host main window: remote assistance and account

Linux host main window: remote assistance and account (device ID, code and account redacted). The UI is available in Chinese and English (see [Language](#language--语言)).

+## Appearance / 外观 + +Choose **System / Light / Dark** (default: System) in the viewer's **Settings → General → Appearance**, or via the gear settings button in host / tool windows. +The choice is saved to `$XDG_CONFIG_HOME/uuremote/theme` (default `~/.config/uuremote/theme`). + +

Light appearance (synthetic devices) Dark appearance (synthetic devices)

+ ## Language / 语言 The language is picked once at start, in this order: diff --git a/README.md b/README.md index 5f0f406..31e051f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,13 @@

Linux 主机主窗口:远程协助与账号

Linux 被控端主窗口:远程协助与账号(设备 ID、验证码和账号已打码)

+## 外观 / Appearance + +在控制端的 **设置 → 常规 → 外观**,或主机 / 工具窗口的齿轮设置按钮中选择 **跟随系统 / 浅色 / 深色**(默认跟随系统)。 +选择保存在 `$XDG_CONFIG_HOME/uuremote/theme`(默认 `~/.config/uuremote/theme`)。 + +

浅色界面(虚构设备) 深色界面(虚构设备)

+ ## 语言 / Language 启动时按以下顺序确定界面语言: diff --git a/build.rs b/build.rs index 807effb..1896b7c 100644 --- a/build.rs +++ b/build.rs @@ -10,6 +10,8 @@ fn main() { println!("cargo:rerun-if-changed=c_engine/assist_window.c"); println!("cargo:rerun-if-changed=c_engine/viewer_window.c"); println!("cargo:rerun-if-changed=c_engine/viewer_tools.c"); + println!("cargo:rerun-if-changed=c_engine/ui_theme.c"); + println!("cargo:rerun-if-changed=c_engine/ui_theme.h"); println!("cargo:rerun-if-changed=c_engine/pam_check.c"); println!("cargo:rerun-if-env-changed=UU_NO_GTK"); println!("cargo:rustc-check-cfg=cfg(uu_gtk)"); @@ -59,7 +61,7 @@ fn main() { match (std::env::var_os("UU_NO_GTK").is_none()).then(|| gtk("--cflags").zip(gtk("--libs"))).flatten() { Some((cflags, libs)) => { let mut build = cc::Build::new(); - build.file("c_engine/assist_window.c").file("c_engine/viewer_window.c").file("c_engine/viewer_tools.c").flag_if_supported("-Wno-deprecated-declarations"); + build.file("c_engine/assist_window.c").file("c_engine/viewer_window.c").file("c_engine/viewer_tools.c").file("c_engine/ui_theme.c").flag_if_supported("-Wno-deprecated-declarations"); for flag in cflags.split_whitespace() { build.flag(flag); } build.compile("uu_login_window"); for flag in libs.split_whitespace() { diff --git a/c_engine/assist_window.c b/c_engine/assist_window.c index e1c675b..5ef0b3d 100644 --- a/c_engine/assist_window.c +++ b/c_engine/assist_window.c @@ -9,27 +9,9 @@ #include #include #include "i18n.h" +#include "ui_theme.h" static void on_language(GtkComboBox *c, gpointer note); -static const char *CSS = - "window { background-color: #f5f6f8; }\n" - "* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n" - ".card { background-color: #ffffff; border-radius: 10px; border: 1px solid #e5e7eb; }\n" - ".heading { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n" - ".key { font-size: 14px; color: #4b5563; }\n" - ".big { font-size: 32px; font-weight: 800; color: #111111; }\n" - ".note { font-size: 13px; color: #9ca3af; }\n" - ".hint { font-size: 13px; color: #6b7280; }\n" - ".footer { font-size: 12px; color: #9ca3af; }\n" - "button.flat { padding: 4px 8px; }\n" - "button.share { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db;" - " border-radius: 6px; padding: 6px 14px; font-size: 14px; }\n" - "separator { background-color: #e5e7eb; min-height: 1px; }\n" - ".user { font-size: 22px; font-weight: 700; color: #111111; }\n" - ".status { font-size: 13px; color: #2f6bff; }\n" - ".status.ok { color: #16a34a; }\n" - ".status.error { color: #dc2626; }\n"; - enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 }; enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 }; @@ -387,11 +369,7 @@ static void build_tray(void) { } static void build_window(void) { - GtkCssProvider *css = gtk_css_provider_new(); - gtk_css_provider_load_from_data(css, CSS, -1, NULL); - gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), - GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - g_object_unref(css); + uu_theme_init(); gtk_window_set_default_icon_name("uu-remote-linux"); // contrib/desktop icon, when installed window = gtk_window_new(GTK_WINDOW_TOPLEVEL); @@ -522,6 +500,7 @@ static void build_window(void) { gtk_box_pack_start(GTK_BOX(lang_row), lang, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(lang_row), lang_note, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(root), lang_row, FALSE, FALSE, 6); + gtk_box_pack_end(GTK_BOX(lang_row), uu_theme_settings_button(GTK_WINDOW(window)), FALSE, FALSE, 0); if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), L("开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行", "Open-source Linux host · not an official NetEase client · remote assistance keeps running after the window closes")); render_code(); if (close_hides) build_tray(); @@ -544,8 +523,6 @@ static void on_language(GtkComboBox *c, gpointer note) { static void *ui_main(void *arg) { (void)arg; int ok = gtk_init_check(NULL, NULL); - // The CSS hard-codes a light palette; a dark system theme would turn unstyled text white on white (#43). - if (ok) g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL); if (ok) build_window(); pthread_mutex_lock(&init_lock); init_state = ok ? 1 : -1; diff --git a/c_engine/ui_theme.c b/c_engine/ui_theme.c new file mode 100644 index 0000000..5226e65 --- /dev/null +++ b/c_engine/ui_theme.c @@ -0,0 +1,141 @@ +// One palette for all GTK windows. Named GTK colors also follow live system theme changes. +#include "ui_theme.h" +#include "i18n.h" +#include +#include + +static int initialized, mode; +static const char *modes[] = {"system", "light", "dark"}; +static const char *CSS = + "* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n" + "@define-color uu_bg mix(@theme_bg_color, @theme_base_color, 0.5);\n" + "@define-color uu_surface @theme_base_color;\n" + "@define-color uu_fg @theme_fg_color;\n" + "@define-color uu_muted mix(@theme_fg_color, @theme_bg_color, 0.25);\n" + "@define-color uu_border mix(@theme_bg_color, @theme_fg_color, 0.18);\n" + "@define-color uu_accent @theme_selected_bg_color;\n" + "@define-color uu_accent_fg @theme_selected_fg_color;\n" + "@define-color uu_tint mix(@theme_base_color, @theme_selected_bg_color, 0.12);\n" + "@define-color uu_success mix(@theme_fg_color, #16a34a, 0.55);\n" + "@define-color uu_error mix(@theme_fg_color, #dc2626, 0.65);\n" + "window { background-color: @uu_bg; color: @uu_fg; }\n" + "headerbar { background-image: none; background-color: @uu_bg; box-shadow: none; border-bottom: 1px solid @uu_border; }\n" + "button { background-image: none; background-color: @uu_surface; color: @uu_fg; border: 1px solid @uu_border; border-radius: 8px; box-shadow: none; text-shadow: none; padding: 7px 12px; }\n" + "button:hover { background-color: @uu_tint; border-color: @uu_accent; }\n" + "button:active, button:checked { background-color: @uu_tint; border-color: @uu_accent; }\n" + "button:disabled { color: mix(@uu_fg, @uu_surface, 0.55); background-color: @uu_bg; border-color: @uu_border; }\n" + "button:focus, entry:focus { border-color: @uu_accent; outline-color: @uu_accent; outline-style: solid; outline-width: 2px; outline-offset: 2px; }\n" + "entry { background-image: none; background-color: @uu_surface; color: @uu_fg; border: 1px solid @uu_border; border-radius: 8px; box-shadow: none; padding: 7px 10px; }\n" + "entry:disabled { color: @uu_muted; background-color: @uu_bg; }\n" + "separator { background-color: @uu_border; min-height: 1px; min-width: 1px; }\n" + ".card, .cardbtn, .banner { background-color: @uu_surface; border: 1px solid @uu_border; border-radius: 12px; }\n" + ".heading, .title, .h2, .user { font-size: 22px; font-weight: 700; color: @uu_fg; }\n" + ".section, .group { font-size: 16px; font-weight: 600; color: @uu_fg; }\n" + ".big { font-size: 32px; font-weight: 700; color: @uu_fg; }\n" + ".key, .hint, .empty { font-size: 14px; color: @uu_muted; }\n" + ".note, .footer { font-size: 13px; color: @uu_muted; }\n" + ".status { font-size: 13px; color: @uu_muted; }\n" + ".status.ok, .online, .dot { color: @uu_success; }\n" + ".status.error { color: @uu_error; }\n" + ".tag { background-color: @uu_tint; color: @uu_fg; border-radius: 6px; padding: 3px 8px; font-size: 12px; }\n" + ".tag.remote { color: @uu_success; }\n" + "button.primary, button.enter { background-color: @uu_accent; color: @uu_accent_fg; border-color: transparent; font-weight: 600; padding: 9px 24px; }\n" + "button.primary:hover, button.enter:hover { background-color: shade(@uu_accent, 0.9); }\n" + "button.primary:active, button.enter:active { background-color: shade(@uu_accent, 0.8); }\n" + "button.primary:disabled, button.enter:disabled { background-color: @uu_bg; color: @uu_muted; border-color: @uu_border; }\n" + "button.flat, button.hbtn, button.tool { background-color: transparent; border-color: transparent; }\n" + "button.flat:hover, button.hbtn:hover, button.tool:hover, button.cardbtn:hover { background-color: @uu_tint; border-color: @uu_border; }\n" + ".toast { background-color: @uu_fg; color: @uu_surface; border-radius: 10px; padding: 12px 18px; font-size: 14px; }\n" + ".qrbox { background-color: white; border: 1px solid @uu_border; border-radius: 12px; padding: 14px; }\n"; + +void uu_theme_load_css(const char *css) { + GtkCssProvider *provider = gtk_css_provider_new(); + GError *error = NULL; + if (gtk_css_provider_load_from_data(provider, css, -1, &error)) + gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + else { g_warning("Could not load UI stylesheet"); g_clear_error(&error); } + g_object_unref(provider); +} + +static char *theme_path(void) { + return g_build_filename(g_get_user_config_dir(), "uuremote", "theme", NULL); +} + +static void apply_mode(void) { + GtkSettings *settings = gtk_settings_get_default(); + if (!mode) { + // Reset the application override, so subsequent desktop changes still propagate. + gtk_settings_reset_property(settings, "gtk-theme-name"); + gtk_settings_reset_property(settings, "gtk-application-prefer-dark-theme"); + } else { + g_object_set(settings, "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", mode == 2, NULL); + } +} + +void uu_theme_init(void) { + if (initialized) return; + initialized = 1; + char *path = theme_path(), *value = NULL; + if (g_file_get_contents(path, &value, NULL, NULL)) { + g_strstrip(value); + for (int i = 0; i < 3; i++) if (!strcmp(value, modes[i])) mode = i; + } + g_free(value); g_free(path); + apply_mode(); + uu_theme_load_css(CSS); +} + +static void on_theme(GtkComboBox *combo, gpointer note) { + int selected = gtk_combo_box_get_active(combo); + if (selected < 0 || selected > 2) return; + mode = selected; + apply_mode(); + char *path = theme_path(), *dir = g_path_get_dirname(path); + gboolean saved = g_mkdir_with_parents(dir, 0700) == 0 && g_file_set_contents(path, modes[mode], -1, NULL); + gtk_label_set_text(GTK_LABEL(note), saved ? "" : L("无法保存外观设置", "Could not save appearance")); + gtk_widget_set_visible(GTK_WIDGET(note), !saved); + g_free(dir); g_free(path); +} + +GtkWidget *uu_theme_selector(void) { + GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6), *row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); + GtkWidget *label = gtk_label_new(L("外观", "Appearance")), *combo = gtk_combo_box_text_new(), *note = gtk_label_new(""); + gtk_style_context_add_class(gtk_widget_get_style_context(label), "key"); + gtk_style_context_add_class(gtk_widget_get_style_context(note), "status"); + gtk_style_context_add_class(gtk_widget_get_style_context(note), "error"); + gtk_label_set_line_wrap(GTK_LABEL(note), TRUE); + gtk_widget_set_no_show_all(note, TRUE); + gtk_widget_set_halign(note, GTK_ALIGN_START); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), L("跟随系统", "System")); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), L("浅色", "Light")); + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), L("深色", "Dark")); + gtk_combo_box_set_active(GTK_COMBO_BOX(combo), mode); + atk_object_set_name(gtk_widget_get_accessible(combo), L("外观", "Appearance")); + g_signal_connect(combo, "changed", G_CALLBACK(on_theme), note); + gtk_box_pack_start(GTK_BOX(row), label, FALSE, FALSE, 0); + gtk_box_pack_end(GTK_BOX(row), combo, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(box), row, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(box), note, FALSE, FALSE, 0); + return box; +} + +static void open_settings(GtkButton *button, gpointer parent) { + (void)button; + GtkWidget *dialog = gtk_dialog_new_with_buttons(L("设置", "Settings"), GTK_WINDOW(parent), + GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, L("关闭", "Close"), GTK_RESPONSE_CLOSE, NULL); + GtkWidget *area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + gtk_window_set_default_size(GTK_WINDOW(dialog), 360, -1); + gtk_container_set_border_width(GTK_CONTAINER(area), 24); + gtk_box_pack_start(GTK_BOX(area), uu_theme_selector(), FALSE, FALSE, 0); + g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog); + gtk_widget_show_all(dialog); +} + +GtkWidget *uu_theme_settings_button(GtkWindow *parent) { + GtkWidget *button = gtk_button_new_from_icon_name("preferences-system-symbolic", GTK_ICON_SIZE_BUTTON); + gtk_style_context_add_class(gtk_widget_get_style_context(button), "flat"); + gtk_widget_set_tooltip_text(button, L("设置", "Settings")); + atk_object_set_name(gtk_widget_get_accessible(button), L("设置", "Settings")); + g_signal_connect(button, "clicked", G_CALLBACK(open_settings), parent); + return button; +} diff --git a/c_engine/ui_theme.h b/c_engine/ui_theme.h new file mode 100644 index 0000000..46a1227 --- /dev/null +++ b/c_engine/ui_theme.h @@ -0,0 +1,10 @@ +#ifndef UU_UI_THEME_H +#define UU_UI_THEME_H +#include + +// Call on the GTK thread, after gtk_init_check. Shared by the host and viewer tools. +void uu_theme_init(void); +GtkWidget *uu_theme_selector(void); +GtkWidget *uu_theme_settings_button(GtkWindow *parent); +void uu_theme_load_css(const char *css); +#endif diff --git a/c_engine/viewer_tools.c b/c_engine/viewer_tools.c index 9c3703d..3b28dab 100644 --- a/c_engine/viewer_tools.c +++ b/c_engine/viewer_tools.c @@ -10,6 +10,7 @@ #include #include #include "i18n.h" +#include "ui_theme.h" #define QSIZE 64 static char queue[QSIZE][4096]; @@ -46,26 +47,17 @@ static gboolean toast_idle(gpointer d) { show_toast(d); g_free(d); return G_SOUR void uu_tools_ui_toast(const char *text) { g_idle_add(toast_idle, g_strdup(text)); } static gboolean on_delete(GtkWidget *w, GdkEvent *e, gpointer d) { (void)e; (void)d; gtk_widget_hide(w); atomic_store(&closed, 1); return TRUE; } -static const char *css = - "window { background: #f5f6f8; }\n" - ".title { font-size: 22px; font-weight: bold; color: #111827; }\n" - ".tag { background: #e5e7eb; color: #4b5563; border-radius: 6px; padding: 2px 8px; }\n" - ".tag.remote { background: #d1fae5; color: #047857; }\n" - ".card { background: white; border: 1px solid #e5e7eb; border-radius: 10px; padding: 8px; }\n" - ".section { font-size: 17px; font-weight: bold; }\n" - ".toast { background: rgba(17,24,39,0.88); color: white; border-radius: 8px; padding: 10px 18px; }\n" - ".primary { background-image: none; background-color: #10b981; color: white; border: none; }\n" - ".empty { color: #6b7280; font-size: 15px; }\n" - ".online { color: #10b981; }\n"; - static GtkWidget *frame(const char *title, GtkWidget **content) { - GtkCssProvider *p = gtk_css_provider_new(); gtk_css_provider_load_from_data(p, css, -1, NULL); - gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(p), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + uu_theme_init(); + uu_theme_load_css(".card { padding: 12px; } treeview.view { padding: 6px; }"); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), title); g_signal_connect(window, "delete-event", G_CALLBACK(on_delete), NULL); GtkWidget *overlay = gtk_overlay_new(); *content = vbox(14); gtk_container_set_border_width(GTK_CONTAINER(*content), 22); + GtkWidget *appearance = uu_theme_settings_button(GTK_WINDOW(window)); + gtk_widget_set_halign(appearance, GTK_ALIGN_END); + gtk_box_pack_start(GTK_BOX(*content), appearance, FALSE, FALSE, 0); gtk_container_add(GTK_CONTAINER(overlay), *content); toast = label("", "toast"); gtk_widget_set_halign(toast, GTK_ALIGN_CENTER); gtk_widget_set_valign(toast, GTK_ALIGN_END); gtk_widget_set_margin_bottom(toast, 30); gtk_widget_set_no_show_all(toast, TRUE); @@ -476,8 +468,6 @@ static void *ui_thread(void *d) { (void)d; if (!gtk_init_check(NULL, NULL)) { pthread_mutex_lock(&init_lock); init_state = -1; pthread_cond_signal(&init_cond); pthread_mutex_unlock(&init_lock); return NULL; } gtk_window_set_default_icon_name("uu-remote-linux"); // same taskbar icon as the host (#13) - // Light palette in CSS; keep a dark system theme from making unstyled text white on white (#43). - g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL); show_window(NULL); gtk_main(); return NULL; } /* Hidden windows retain their widgets so queued idle callbacks stay valid. */ diff --git a/c_engine/viewer_window.c b/c_engine/viewer_window.c index 95125a5..e1a7592 100644 --- a/c_engine/viewer_window.c +++ b/c_engine/viewer_window.c @@ -11,45 +11,32 @@ #include #include #include "i18n.h" +#include "ui_theme.h" static const char *CSS = - "window { background-color: #ffffff; }\n" - "* { font-family: 'Noto Sans CJK SC', 'Noto Sans SC', sans-serif; }\n" - ".sidebar { background-color: #e9e9ec; }\n" - ".section { font-size: 16px; font-weight: 600; color: #1d1d1f; }\n" + ".sidebar { background-color: @theme_bg_color; border-right: 1px solid @uu_border; }\n" ".sidebar list, .sidebar row { background-color: transparent; }\n" - ".sidebar row { border-radius: 8px; margin: 1px 10px; padding: 8px 10px; }\n" - ".sidebar row:selected { background-color: #d4d4d9; color: #1d1d1f; }\n" - ".devname { font-size: 15px; color: #1d1d1f; }\n" - ".offline { color: #9ca3af; }\n" - ".dot { color: #22c55e; font-size: 10px; }\n" + ".sidebar row { border-radius: 8px; margin: 3px 12px; padding: 10px 12px; }\n" + ".sidebar row:hover { background-color: @uu_tint; }\n" + ".sidebar row:selected { background-color: @uu_tint; color: @uu_fg; }\n" + ".devname { font-size: 15px; color: @uu_fg; }\n" + ".offline { color: @uu_muted; }\n" + ".dot { font-size: 10px; }\n" ".dot.off { color: transparent; }\n" - ".icon { background-color: #2f7bff; color: #ffffff; border-radius: 6px; font-size: 11px; font-weight: 700; padding: 2px 5px; }\n" - ".icon.off { background-color: #c4c7cf; }\n" - ".tag { background-color: #e8f0ff; color: #2f6bff; border-radius: 4px; font-size: 12px; padding: 1px 6px; }\n" - ".title { font-size: 20px; font-weight: 700; color: #1d1d1f; }\n" - ".header { border-bottom: 1px solid #e5e7eb; padding: 12px 22px; background-color: #f7f7f8; }\n" - ".badge { background-color: #1d1d1f; color: #ffffff; border-radius: 12px; padding: 3px 12px; font-size: 13px; }\n" - ".badge.off { background-color: #9ca3af; }\n" - ".hbtn { background-image: none; background-color: transparent; border: none; font-size: 17px; color: #4b5563; padding: 4px 10px; }\n" - ".preview { background-image: linear-gradient(135deg, #a8c0ff 0%, #e2c2f2 50%, #f6c8a8 100%); border-radius: 12px 12px 0 0; }\n" - ".enter { background-image: none; background-color: rgba(0,0,0,0.18); color: #ffffff; border: none; font-size: 19px; padding: 8px 20px; border-radius: 8px; }\n" - ".toolbar { border: 1px solid #e5e7eb; border-top: none; border-radius: 0 0 12px 12px; padding: 4px; }\n" - ".tool { background-image: none; background-color: transparent; border: none; font-size: 15px; color: #374151; padding: 10px; }\n" - ".dashed { border: 1px dashed #d1d5db; border-radius: 12px; padding: 36px; }\n" - ".round { background-image: none; background-color: #f3f4f6; border: none; border-radius: 40px; font-size: 26px; min-width: 64px; min-height: 64px; }\n" - ".card { background-color: #fafafa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px 18px; }\n" - ".cardbtn { background-image: none; background-color: #fafafa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 12px 16px; }\n" - ".banner { background-color: #f7f8fa; border: 1px solid #e5e7eb; border-radius: 10px; padding: 14px 18px; }\n" - ".group { font-size: 16px; font-weight: 600; color: #1d1d1f; }\n" - ".h2 { font-size: 19px; font-weight: 700; color: #1d1d1f; }\n" - ".big { font-size: 30px; font-weight: 800; color: #111111; }\n" - ".hint { font-size: 14px; color: #6b7280; }\n" - ".primary { background-image: none; background-color: #3b82f6; color: #ffffff; border: none; border-radius: 8px; font-size: 16px; padding: 8px 26px; }\n" - ".secondary { background-image: none; background-color: #ffffff; border: 1px solid #d1d5db; border-radius: 8px; padding: 6px 16px; }\n" - ".qrbox { background-color: #ffffff; border-radius: 12px; border: 1px solid #e5e7eb; padding: 14px; }\n" - ".status { font-size: 13px; color: #2f6bff; }\n" - ".toast { background-color: #ffffff; border: 1px solid #e5e7eb; border-radius: 10px; padding: 10px 18px; font-size: 15px; }\n"; + ".icon { background-color: @uu_accent; color: @uu_accent_fg; border-radius: 6px; font-size: 11px; font-weight: 700; padding: 3px 6px; }\n" + ".icon.off { background-color: @uu_border; color: @uu_muted; }\n" + ".header { border-bottom: 1px solid @uu_border; padding: 16px 24px; }\n" + ".badge { background-color: @uu_tint; color: @uu_success; border-radius: 8px; padding: 4px 10px; font-size: 13px; }\n" + ".badge.off { background-color: @theme_bg_color; color: @uu_muted; }\n" + ".hbtn { font-size: 17px; padding: 6px 10px; }\n" + ".preview { background-image: none; background-color: @uu_tint; border-radius: 12px 12px 0 0; }\n" + ".enter { font-size: 18px; }\n" + ".toolbar { background-color: @uu_surface; border: 1px solid @uu_border; border-top: none; border-radius: 0 0 12px 12px; padding: 6px; }\n" + ".tool { font-size: 15px; padding: 10px; }\n" + ".dashed { border: 1px dashed @uu_border; border-radius: 12px; padding: 36px; }\n" + ".round { background-color: @uu_tint; border: none; border-radius: 16px; font-size: 26px; min-width: 64px; min-height: 64px; }\n" + ".card, .banner { padding: 18px 20px; }\n" + ".cardbtn { padding: 14px 18px; }\n"; typedef struct { char id[64]; char name[128]; int platform, online, current; char thumb[512]; } Device; static Device devices[64]; @@ -217,7 +204,7 @@ static void show_device(int index) { pthread_mutex_unlock(&d_lock); load_thumb(thumb); set_header(name, 1); - gtk_label_set_markup(GTK_LABEL(badge), online ? L("● 在线", "● Online") : L("离线", "Offline")); + gtk_label_set_text(GTK_LABEL(badge), online ? L("● 在线", "● Online") : L("离线", "Offline")); GtkStyleContext *ctx = gtk_widget_get_style_context(badge); if (online) gtk_style_context_remove_class(ctx, "off"); else gtk_style_context_add_class(ctx, "off"); gtk_stack_set_visible_child_name(GTK_STACK(stack), "device"); @@ -574,6 +561,10 @@ static void open_settings(void) { GtkWidget *g0[] = { acct }; gtk_box_pack_start(GTK_BOX(general), settings_card(g0, 1), FALSE, FALSE, 0); refresh_account(); + const char *languages[] = {"中文", "English"}; GtkWidget *language = combo(languages, 2); + gtk_combo_box_set_active(GTK_COMBO_BOX(language), uu_i18n_english()); g_signal_connect(language, "changed", G_CALLBACK(on_language), NULL); + GtkWidget *g4[] = { setting_row("语言 / Language", NULL, language), uu_theme_selector() }; + gtk_box_pack_start(GTK_BOX(general), settings_card(g4, 2), FALSE, FALSE, 0); const char *wake[] = {L("永不", "Never"), L("接通电源时", "When plugged in"), L("始终", "Always")}, *close_opts[] = {L("最小化", "Minimize"), L("退出程序", "Quit")}; GtkWidget *g1[] = { setting_row(L("开机自启动", "Start at login"), NULL, gtk_switch_new()), setting_row(L("保持唤醒状态", "Stay awake"), L("防止电脑进入睡眠状态,导致无法远程访问。", "Keep the computer from sleeping so it stays reachable."), combo(wake, 3)), @@ -587,11 +578,10 @@ static void open_settings(void) { GtkWidget *real = button(L("在手机 App 中查看", "View in the mobile app"), "secondary"); gtk_widget_set_sensitive(real, FALSE); GtkWidget *g3[] = { setting_row(L("实名认证", "Identity verification"), NULL, real) }; gtk_box_pack_start(GTK_BOX(general), settings_card(g3, 1), FALSE, FALSE, 0); - const char *languages[] = {"中文", "English"}; GtkWidget *language = combo(languages, 2); - gtk_combo_box_set_active(GTK_COMBO_BOX(language), uu_i18n_english()); g_signal_connect(language, "changed", G_CALLBACK(on_language), NULL); - GtkWidget *g4[] = { setting_row("语言 / Language", NULL, language) }; - gtk_box_pack_start(GTK_BOX(general), settings_card(g4, 1), FALSE, FALSE, 0); - gtk_stack_add_titled(GTK_STACK(tabs), general, "general", L("⚙ 常规", "⚙ General")); + GtkWidget *gscroll = gtk_scrolled_window_new(NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(gscroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); + gtk_container_add(GTK_CONTAINER(gscroll), general); + gtk_stack_add_titled(GTK_STACK(tabs), gscroll, "general", L("⚙ 常规", "⚙ General")); // 安全 GtkWidget *security = settings_page(); const char *verify[] = {L("仅使用临时验证码", "Temporary code only"), L("仅使用自定义验证码", "Custom code only"), L("临时与自定义验证码", "Temporary and custom codes")}; @@ -666,12 +656,9 @@ static void *run(void *arg) { (void)arg; int ok = gtk_init_check(NULL, NULL); if (ok) { gtk_window_set_default_icon_name("uu-remote-linux"); } // same taskbar icon as the host (#13) - // The CSS hard-codes a light palette; a dark system theme would turn unstyled text white on white (#43). - if (ok) g_object_set(gtk_settings_get_default(), "gtk-theme-name", "Adwaita", "gtk-application-prefer-dark-theme", FALSE, NULL); if (ok) { - GtkCssProvider *css = gtk_css_provider_new(); - gtk_css_provider_load_from_data(css, CSS, -1, NULL); - gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + uu_theme_init(); + uu_theme_load_css(CSS); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), L("UU 远程 Linux 控制端(非官方)", "UU Remote Linux viewer (unofficial)")); gtk_window_set_default_size(GTK_WINDOW(window), 1180, 780); diff --git a/docs/images/appearance-dark.png b/docs/images/appearance-dark.png new file mode 100644 index 0000000..7056a44 Binary files /dev/null and b/docs/images/appearance-dark.png differ diff --git a/docs/images/appearance-light.png b/docs/images/appearance-light.png new file mode 100644 index 0000000..5f44109 Binary files /dev/null and b/docs/images/appearance-light.png differ diff --git a/tests/gtk-ui-smoke.sh b/tests/gtk-ui-smoke.sh new file mode 100644 index 0000000..6bf7c13 --- /dev/null +++ b/tests/gtk-ui-smoke.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Requires a C compiler, GTK3/X11 development headers, Xvfb and xauth. +# Run: xvfb-run -a -s '-screen 0 1600x1200x24' sh tests/gtk-ui-smoke.sh /tmp/gtk-ui-previews +# Optional output directory retains synthetic main/settings screenshots; no account or QR is used. +# Checks 5 window types in Chinese/English with System/Light/Dark and settings persistence. +set -eu +cd "$(dirname "$0")/.." +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT HUP INT TERM +out=${1:-"$work/previews"} +mkdir -p "$out" "$work/config/gtk-3.0" "$work/config/uuremote" "$work/home/Documents" +export HOME="$work/home" +export XDG_CONFIG_HOME="$work/config" +export GSETTINGS_BACKEND=memory +export G_DEBUG=fatal-warnings +export GDK_BACKEND=x11 +export GDK_SCALE=1 +export GDK_DPI_SCALE=1 +export NO_AT_BRIDGE=1 +unset GTK_THEME +printf '[Settings]\ngtk-theme-name=Adwaita\ngtk-application-prefer-dark-theme=false\n' > "$XDG_CONFIG_HOME/gtk-3.0/settings.ini" +for ui in HOST VIEWER TOOLS; do + # pkg-config output is intentionally split into compiler arguments. + cc -std=gnu11 -Wall -Wextra -Werror -Wno-deprecated-declarations \ + -D"UI_$ui" tests/gtk_ui_smoke.c c_engine/ui_theme.c \ + $(pkg-config --cflags --libs gtk+-3.0) -ldl -lpthread -o "$work/$ui" + for lang in en zh; do + for theme in light dark system; do + case $theme in light) mode=1;; dark) mode=2;; system) mode=0;; esac + for kind in $(if [ "$ui" = TOOLS ]; then printf 'files portmap terminal'; else printf 'main'; fi); do + printf '%s' "$theme" > "$XDG_CONFIG_HOME/uuremote/theme" + UU_LANG=$lang "$work/$ui" "$out/$ui-$kind-$lang-$theme.png" "$mode" "$kind" + done + done + done +done +printf 'GTK UI smoke checks passed (host, viewer, files, port mapping, terminal; English/Chinese; light/dark/system).\n' diff --git a/tests/gtk_ui_smoke.c b/tests/gtk_ui_smoke.c new file mode 100644 index 0000000..ef22d88 --- /dev/null +++ b/tests/gtk_ui_smoke.c @@ -0,0 +1,177 @@ +// Offline fixtures only: no account, QR, cloud, or desktop capture is used. +#include +#include +#include +#include +#include +int uu_i18n_english(void) { return g_strcmp0(g_getenv("UU_LANG"), "zh") != 0; } +int uu_i18n_save(int english) { (void)english; return 1; } +#if defined(UI_HOST) +#include "../c_engine/assist_window.c" +#elif defined(UI_VIEWER) +#include "../c_engine/viewer_window.c" +#else +#include +static int fixture_hostname(char *name, size_t len) { g_strlcpy(name, "Local workstation", len); return 0; } +#define gethostname fixture_hostname +#include "../c_engine/viewer_tools.c" +#endif + +static GtkWidget *appearance_combo(GtkWidget *w) { + if (GTK_IS_COMBO_BOX(w) && !g_strcmp0(atk_object_get_name(gtk_widget_get_accessible(w)), L("外观", "Appearance"))) return w; + if (!GTK_IS_CONTAINER(w)) return NULL; + GList *children = gtk_container_get_children(GTK_CONTAINER(w)); + GtkWidget *found = NULL; + for (GList *c = children; c && !found; c = c->next) found = appearance_combo(c->data); + g_list_free(children); + return found; +} + +#if !defined(UI_VIEWER) +static GtkWidget *settings_button(GtkWidget *w) { + if (GTK_IS_BUTTON(w) && !g_strcmp0(atk_object_get_name(gtk_widget_get_accessible(w)), L("设置", "Settings"))) return w; + if (!GTK_IS_CONTAINER(w)) return NULL; + GList *children = gtk_container_get_children(GTK_CONTAINER(w)); + GtkWidget *found = NULL; + for (GList *c = children; c && !found; c = c->next) found = settings_button(c->data); + g_list_free(children); + return found; +} + +#endif + +static gboolean snapshot(GtkWidget *w, const char *path) { + static int attempts; + int width = gtk_widget_get_allocated_width(w), height = gtk_widget_get_allocated_height(w); + GdkWindow *native = gtk_widget_get_window(w); + int x, y; + gdk_window_get_origin(native, &x, &y); + g_assert_cmpint(x + width, <=, gdk_screen_width()); + g_assert_cmpint(y + height, <=, gdk_screen_height()); + gdk_window_process_updates(native, TRUE); + gdk_display_sync(gtk_widget_get_display(w)); + // GTK 3.24.33's get_from_window marks its cached surface dirty without flushing; + // Cairo 1.16 can abort on attached MIME data. Read through a fresh X11 surface. + cairo_surface_t *surface = cairo_xlib_surface_create(GDK_WINDOW_XDISPLAY(native), + GDK_WINDOW_XID(native), GDK_VISUAL_XVISUAL(gdk_window_get_visual(native)), width, height); + g_assert_cmpint(cairo_surface_status(surface), ==, CAIRO_STATUS_SUCCESS); + GdkPixbuf *shot = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height); + cairo_surface_destroy(surface); + g_assert_nonnull(shot); + const guchar *pixels = gdk_pixbuf_read_pixels(shot); + int low = 255, high = 0; + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + int v = pixels[row * gdk_pixbuf_get_rowstride(shot) + col * gdk_pixbuf_get_n_channels(shot)]; + low = MIN(low, v); high = MAX(high, v); + } + } + if (high - low <= 80) { + // A loaded CI runner may not have painted yet; retry through the main loop, with a bound. + g_object_unref(shot); + g_assert_cmpint(++attempts, <, 10); + return FALSE; + } + attempts = 0; + g_assert_true(gdk_pixbuf_save(shot, path, "png", NULL, NULL)); + g_object_unref(shot); + return TRUE; +} + +static const char *output; +static int expected_mode, stage; +#if defined(UI_VIEWER) +static void verify_badge(void) { + for (int i = 0; i < 2; i++) { + show_device(i); // Fixture 0 is online, fixture 1 is offline. + GtkStyleContext *ctx = gtk_widget_get_style_context(badge); + GdkRGBA actual, expected; + g_assert_cmpint(gtk_style_context_has_class(ctx, "off"), ==, i == 1); + g_assert_true(gtk_style_context_lookup_color(ctx, i ? "uu_muted" : "uu_success", &expected)); + gtk_style_context_get_color(ctx, GTK_STATE_FLAG_NORMAL, &actual); + g_assert_cmpfloat_with_epsilon(actual.red, expected.red, 0.005); + g_assert_cmpfloat_with_epsilon(actual.green, expected.green, 0.005); + g_assert_cmpfloat_with_epsilon(actual.blue, expected.blue, 0.005); + } +} +#endif +static gboolean verify(gpointer unused) { + (void)unused; + if (!stage) { + g_assert_null(appearance_combo(window)); // Appearance belongs in Settings. + if (!snapshot(window, output)) return G_SOURCE_CONTINUE; +#if defined(UI_VIEWER) + open_settings(); +#else + GtkWidget *button = settings_button(window); + g_assert_nonnull(button); + gtk_button_clicked(GTK_BUTTON(button)); +#endif + stage++; + return G_SOURCE_CONTINUE; + } + GList *windows = gtk_window_list_toplevels(); + GtkWidget *combo = NULL, *settings = NULL; + for (GList *w = windows; w && !combo; w = w->next) { + combo = appearance_combo(w->data); + if (combo) settings = w->data; + } + g_list_free(windows); + g_assert_nonnull(combo); + g_assert_cmpint(gtk_combo_box_get_active(GTK_COMBO_BOX(combo)), ==, expected_mode); + if (stage == 1) { + char *path = g_strconcat(output, ".settings.png", NULL); + gboolean painted = snapshot(settings, path); g_free(path); + if (!painted) return G_SOURCE_CONTINUE; +#if defined(UI_VIEWER) + verify_badge(); +#endif + expected_mode = expected_mode == 2 ? 1 : 2; + gtk_combo_box_set_active(GTK_COMBO_BOX(combo), expected_mode); + stage++; + return G_SOURCE_CONTINUE; + } + gboolean dark = FALSE; + g_object_get(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", &dark, NULL); + g_assert_cmpint(dark, ==, expected_mode == 2); +#if defined(UI_VIEWER) + verify_badge(); +#endif + char *path = g_build_filename(g_get_user_config_dir(), "uuremote", "theme", NULL), *saved = NULL; + g_assert_true(g_file_get_contents(path, &saved, NULL, NULL)); + g_assert_cmpstr(saved, ==, expected_mode == 2 ? "dark" : "light"); + g_free(path); g_free(saved); + // System must release the application override and restore the test desktop's light default. + gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0); + g_object_get(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", &dark, NULL); + g_assert_false(dark); + gtk_main_quit(); + return G_SOURCE_REMOVE; +} + +int main(int argc, char **argv) { + g_assert_cmpint(argc, >=, 3); + output = argv[1]; expected_mode = atoi(argv[2]); + gtk_init(NULL, NULL); + g_object_set(gtk_settings_get_default(), "gtk-enable-animations", FALSE, NULL); + g_timeout_add(400, verify, NULL); +#if defined(UI_HOST) + show_account = 1; + build_window(); + gtk_main(); +#elif defined(UI_VIEWER) + uu_viewer_ui_set_devices("demo-workstation\tWorkstation\t1\t1\t0\ndemo-laptop\tLaptop\t4\t0\t0\n"); + logged_in = 1; + g_idle_add(apply_devices, NULL); + g_idle_add(apply_login, NULL); + run(NULL); +#else + g_strlcpy(remote_name, "Workstation", sizeof remote_name); + if (argc > 3 && !strcmp(argv[3], "terminal")) build_terminal(); + else if (argc > 3 && !strcmp(argv[3], "portmap")) build_portmap(); + else build_files(); + gtk_widget_show_all(window); + gtk_main(); +#endif + return 0; +}