5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 06:19:43 +08:00

[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 <lea.anthony@gmail.com>
This commit is contained in:
Zámbó, Levente 2023-09-07 13:25:02 +02:00 committed by GitHub
parent ebf56f6585
commit 647bc87600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 12 deletions

View File

@ -4,6 +4,7 @@
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <locale.h>
#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 wont 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);
}

View File

@ -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))

View File

@ -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