From 647bc8760064d192803c4ade86dc47003811c9ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A1mb=C3=B3=2C=20Levente?= Date: Thu, 7 Sep 2023 13:25:02 +0200 Subject: [PATCH] [v2, Linux] fix menu background color (#2873) * fix menu background color * remove commented line * handle transparent window and background color --------- Co-authored-by: Lea Anthony --- v2/internal/frontend/desktop/linux/window.c | 28 +++++++++++++++++--- v2/internal/frontend/desktop/linux/window.go | 27 ++++++++++++++----- v2/internal/frontend/desktop/linux/window.h | 3 ++- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/v2/internal/frontend/desktop/linux/window.c b/v2/internal/frontend/desktop/linux/window.c index 82ab3b856..a70a3be83 100644 --- a/v2/internal/frontend/desktop/linux/window.c +++ b/v2/internal/frontend/desktop/linux/window.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "window.h" // These are the x,y,time & button of the last mouse down event @@ -145,20 +146,39 @@ void SetBackgroundColour(void *data) { // set webview's background color RGBAOptions *options = (RGBAOptions *)data; + GdkRGBA colour = {options->r / 255.0, options->g / 255.0, options->b / 255.0, options->a / 255.0}; + if (options->windowIsTranslucent != NULL && options->windowIsTranslucent == TRUE) + { + colour.alpha = 0.0; + } webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(options->webview), &colour); // set window's background color - GtkWidget *window = GTK_WIDGET(options->window); - gchar *str = g_strdup_printf("window {background-color: rgba(%d, %d, %d, %d);}", options->r, options->g, options->b, options->a); + // Get the name of the current locale + char *old_locale, *saved_locale; + old_locale = setlocale(LC_ALL, NULL); + + // Copy the name so it won’t be clobbered by setlocale. + saved_locale = strdup(old_locale); + if (saved_locale == NULL) + return; + + //Now change the locale to english for so printf always converts floats with a dot decimal separator + setlocale(LC_ALL, "en_US.UTF-8"); + gchar *str = g_strdup_printf("#webview-box {background-color: rgba(%d, %d, %d, %1.1f);}", options->r, options->g, options->b, options->a / 255.0); + + //Restore the original locale. + setlocale(LC_ALL, saved_locale); + free(saved_locale); if (windowCssProvider == NULL) { windowCssProvider = gtk_css_provider_new(); gtk_style_context_add_provider( - gtk_widget_get_style_context(window), + gtk_widget_get_style_context(GTK_WIDGET(options->webviewBox)), GTK_STYLE_PROVIDER(windowCssProvider), - GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + GTK_STYLE_PROVIDER_PRIORITY_USER); g_object_unref(windowCssProvider); } diff --git a/v2/internal/frontend/desktop/linux/window.go b/v2/internal/frontend/desktop/linux/window.go index d43295134..6d9b256f6 100644 --- a/v2/internal/frontend/desktop/linux/window.go +++ b/v2/internal/frontend/desktop/linux/window.go @@ -43,6 +43,7 @@ type Window struct { webview unsafe.Pointer applicationMenu *menu.Menu menubar *C.GtkWidget + webviewBox *C.GtkWidget vbox *C.GtkWidget accels *C.GtkAccelGroup minWidth, minHeight, maxWidth, maxHeight int @@ -72,6 +73,11 @@ func NewWindow(appoptions *options.App, debug bool, devtools bool) *Window { C.g_object_ref_sink(C.gpointer(gtkWindow)) result.gtkWindow = unsafe.Pointer(gtkWindow) + webviewName := C.CString("webview-box") + defer C.free(unsafe.Pointer(webviewName)) + result.webviewBox = C.gtk_box_new(C.GTK_ORIENTATION_VERTICAL, 0) + C.gtk_widget_set_name(result.webviewBox, webviewName) + result.vbox = C.gtk_box_new(C.GTK_ORIENTATION_VERTICAL, 0) C.gtk_container_add(result.asGTKContainer(), result.vbox) @@ -265,13 +271,18 @@ func (w *Window) IsNormal() bool { } func (w *Window) SetBackgroundColour(r uint8, g uint8, b uint8, a uint8) { + windowIsTranslucent := false + if w.appoptions.Linux != nil && w.appoptions.Linux.WindowIsTranslucent { + windowIsTranslucent = true + } data := C.RGBAOptions{ - r: C.uchar(r), - g: C.uchar(g), - b: C.uchar(b), - a: C.uchar(a), - webview: w.webview, - window: w.gtkWindow, + r: C.uchar(r), + g: C.uchar(g), + b: C.uchar(b), + a: C.uchar(a), + webview: w.webview, + webviewBox: unsafe.Pointer(w.webviewBox), + windowIsTranslucent: gtkBool(windowIsTranslucent), } invokeOnMainThread(func() { C.SetBackgroundColour(unsafe.Pointer(&data)) }) @@ -288,7 +299,9 @@ func (w *Window) Run(url string) { if w.menubar != nil { C.gtk_box_pack_start(C.GTKBOX(unsafe.Pointer(w.vbox)), w.menubar, 0, 0, 0) } - C.gtk_box_pack_start(C.GTKBOX(unsafe.Pointer(w.vbox)), C.GTKWIDGET(w.webview), 1, 1, 0) + + C.gtk_box_pack_start(C.GTKBOX(unsafe.Pointer(w.webviewBox)), C.GTKWIDGET(w.webview), 1, 1, 0) + C.gtk_box_pack_start(C.GTKBOX(unsafe.Pointer(w.vbox)), w.webviewBox, 1, 1, 0) _url := C.CString(url) C.LoadIndex(w.webview, _url) defer C.free(unsafe.Pointer(_url)) diff --git a/v2/internal/frontend/desktop/linux/window.h b/v2/internal/frontend/desktop/linux/window.h index d600b6984..eabcdd1e6 100644 --- a/v2/internal/frontend/desktop/linux/window.h +++ b/v2/internal/frontend/desktop/linux/window.h @@ -55,7 +55,8 @@ typedef struct RGBAOptions uint8_t b; uint8_t a; void *webview; - void *window; + void *webviewBox; + gboolean windowIsTranslucent; } RGBAOptions; typedef struct SetTitleArgs