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

Init GTK in NewFrontend, not init (#2841)

* Init GTK in `NewFrontend`, not `init`

So apps that have a headless / non-gui mode will be able to run, since
they needn't call `NewFrontend` (which is called by `CreateApp`).
Previously, `init` would call `C.gtk_init` regardless of whether
CreateApp was called.

Also change to call `C.gtk_init_check` with a panic, instead of
`C.gtk_init`, since `gtk_init` just exits the process if it fails,
without a sensible error message.

Fixes #2628.

* Update changelog
This commit is contained in:
Phil Richards 2023-08-25 21:41:53 +01:00 committed by GitHub
parent 98bb3b6361
commit 427fe7e8d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View File

@ -76,6 +76,7 @@ import "C"
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -83,6 +84,7 @@ import (
"os" "os"
"runtime" "runtime"
"strings" "strings"
"sync"
"text/template" "text/template"
"unsafe" "unsafe"
@ -96,6 +98,8 @@ import (
"github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options"
) )
var initOnce = sync.Once{}
const startURL = "wails://wails/" const startURL = "wails://wails/"
type Frontend struct { type Frontend struct {
@ -126,18 +130,19 @@ func (f *Frontend) WindowClose() {
f.mainWindow.Destroy() 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 { 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{ result := &Frontend{
frontendOptions: appoptions, frontendOptions: appoptions,

View File

@ -21,9 +21,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Avoid app crashing when the Linux GTK key is empty. Fixed by @aminya in [PR](https://github.com/wailsapp/wails/pull/2672) - 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 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) - 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
- 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) - 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)