mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 04:11:05 +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:
parent
ebf56f6585
commit
647bc87600
@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <locale.h>
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
// These are the x,y,time & button of the last mouse down event
|
// 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
|
// set webview's background color
|
||||||
RGBAOptions *options = (RGBAOptions *)data;
|
RGBAOptions *options = (RGBAOptions *)data;
|
||||||
|
|
||||||
GdkRGBA colour = {options->r / 255.0, options->g / 255.0, options->b / 255.0, options->a / 255.0};
|
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);
|
webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(options->webview), &colour);
|
||||||
|
|
||||||
// set window's background color
|
// set window's background color
|
||||||
GtkWidget *window = GTK_WIDGET(options->window);
|
// Get the name of the current locale
|
||||||
gchar *str = g_strdup_printf("window {background-color: rgba(%d, %d, %d, %d);}", options->r, options->g, options->b, options->a);
|
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)
|
if (windowCssProvider == NULL)
|
||||||
{
|
{
|
||||||
windowCssProvider = gtk_css_provider_new();
|
windowCssProvider = gtk_css_provider_new();
|
||||||
gtk_style_context_add_provider(
|
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(windowCssProvider),
|
||||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
GTK_STYLE_PROVIDER_PRIORITY_USER);
|
||||||
g_object_unref(windowCssProvider);
|
g_object_unref(windowCssProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ type Window struct {
|
|||||||
webview unsafe.Pointer
|
webview unsafe.Pointer
|
||||||
applicationMenu *menu.Menu
|
applicationMenu *menu.Menu
|
||||||
menubar *C.GtkWidget
|
menubar *C.GtkWidget
|
||||||
|
webviewBox *C.GtkWidget
|
||||||
vbox *C.GtkWidget
|
vbox *C.GtkWidget
|
||||||
accels *C.GtkAccelGroup
|
accels *C.GtkAccelGroup
|
||||||
minWidth, minHeight, maxWidth, maxHeight int
|
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))
|
C.g_object_ref_sink(C.gpointer(gtkWindow))
|
||||||
result.gtkWindow = unsafe.Pointer(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)
|
result.vbox = C.gtk_box_new(C.GTK_ORIENTATION_VERTICAL, 0)
|
||||||
C.gtk_container_add(result.asGTKContainer(), result.vbox)
|
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) {
|
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{
|
data := C.RGBAOptions{
|
||||||
r: C.uchar(r),
|
r: C.uchar(r),
|
||||||
g: C.uchar(g),
|
g: C.uchar(g),
|
||||||
b: C.uchar(b),
|
b: C.uchar(b),
|
||||||
a: C.uchar(a),
|
a: C.uchar(a),
|
||||||
webview: w.webview,
|
webview: w.webview,
|
||||||
window: w.gtkWindow,
|
webviewBox: unsafe.Pointer(w.webviewBox),
|
||||||
|
windowIsTranslucent: gtkBool(windowIsTranslucent),
|
||||||
}
|
}
|
||||||
invokeOnMainThread(func() { C.SetBackgroundColour(unsafe.Pointer(&data)) })
|
invokeOnMainThread(func() { C.SetBackgroundColour(unsafe.Pointer(&data)) })
|
||||||
|
|
||||||
@ -288,7 +299,9 @@ func (w *Window) Run(url string) {
|
|||||||
if w.menubar != nil {
|
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)), 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)
|
_url := C.CString(url)
|
||||||
C.LoadIndex(w.webview, _url)
|
C.LoadIndex(w.webview, _url)
|
||||||
defer C.free(unsafe.Pointer(_url))
|
defer C.free(unsafe.Pointer(_url))
|
||||||
|
@ -55,7 +55,8 @@ typedef struct RGBAOptions
|
|||||||
uint8_t b;
|
uint8_t b;
|
||||||
uint8_t a;
|
uint8_t a;
|
||||||
void *webview;
|
void *webview;
|
||||||
void *window;
|
void *webviewBox;
|
||||||
|
gboolean windowIsTranslucent;
|
||||||
} RGBAOptions;
|
} RGBAOptions;
|
||||||
|
|
||||||
typedef struct SetTitleArgs
|
typedef struct SetTitleArgs
|
||||||
|
Loading…
Reference in New Issue
Block a user