diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 2f6f286fa..654edae17 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -76,6 +76,7 @@ import "C" import ( "context" "encoding/json" + "errors" "fmt" "log" "net" @@ -83,6 +84,7 @@ import ( "os" "runtime" "strings" + "sync" "text/template" "unsafe" @@ -96,6 +98,8 @@ import ( "github.com/wailsapp/wails/v2/pkg/options" ) +var initOnce = sync.Once{} + const startURL = "wails://wails/" type Frontend struct { @@ -126,18 +130,19 @@ func (f *Frontend) WindowClose() { f.mainWindow.Destroy() } -func init() { - runtime.LockOSThread() - - // Set GDK_BACKEND=x11 if currently unset and XDG_SESSION_TYPE is unset, unspecified or x11 to prevent warnings - if os.Getenv("GDK_BACKEND") == "" && (os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") { - _ = os.Setenv("GDK_BACKEND", "x11") - } - - C.gtk_init(nil, nil) -} - func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend { + initOnce.Do(func() { + runtime.LockOSThread() + + // Set GDK_BACKEND=x11 if currently unset and XDG_SESSION_TYPE is unset, unspecified or x11 to prevent warnings + if os.Getenv("GDK_BACKEND") == "" && (os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") { + _ = os.Setenv("GDK_BACKEND", "x11") + } + + if ok := C.gtk_init_check(nil, nil); ok != 1 { + panic(errors.New("failed to init GTK")) + } + }) result := &Frontend{ frontendOptions: appoptions, diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index de16faf1f..b5312d9b9 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -21,9 +21,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Avoid app crashing when the Linux GTK key is empty. Fixed by @aminya in [PR](https://github.com/wailsapp/wails/pull/2672) +- Fix issue where app would exit before main() on linux if $DISPLAY env var was not set. Fixed by @phildrip in [PR](https://github.com/wailsapp/wails/pull/2841) - Fixed a race condition when positioning the window on Linux. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2850) - Fixed `SetBackgroundColour` so it sets the window's background color to reduce resize flickering on Linux. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2853) + ### Added - Added correct NodeJS and Docker package names for DNF package manager of Fedora 38. Added by @aranggitoar in [PR](https://github.com/wailsapp/wails/pull/2790)