mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-08 02:45:38 +00:00
app{,/internal/window}: make app.Main blocking on desktop platforms
This CL implements the app.Main function as a blocking-forever function for JS, Wayland, Windows and X11. This works better for applications that can now programmatically close windows.
This commit is contained in:
committed by
Elias Naur
parent
facf5cbb9d
commit
6a9a870462
+4
-2
@@ -35,8 +35,10 @@ func DataDir() (string, error) {
|
|||||||
return dataDir()
|
return dataDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main must be called from the program main function. It
|
// Main must be called last from the program main function.
|
||||||
// blocks until there are no more windows active.
|
// On most platforms Main blocks forever, for Android and
|
||||||
|
// iOS it returns immediately to give control of the main
|
||||||
|
// thread back to the system.
|
||||||
//
|
//
|
||||||
// Calling Main is necessary because some operating systems
|
// Calling Main is necessary because some operating systems
|
||||||
// require control of the main thread of the program for
|
// require control of the main thread of the program for
|
||||||
|
|||||||
@@ -36,8 +36,6 @@ type window struct {
|
|||||||
animating bool
|
animating bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var mainDone = make(chan struct{})
|
|
||||||
|
|
||||||
func NewWindow(win Callbacks, opts *Options) error {
|
func NewWindow(win Callbacks, opts *Options) error {
|
||||||
doc := js.Global().Get("document")
|
doc := js.Global().Get("document")
|
||||||
cont := getContainer(doc)
|
cont := getContainer(doc)
|
||||||
@@ -70,7 +68,6 @@ func NewWindow(win Callbacks, opts *Options) error {
|
|||||||
w.draw(true)
|
w.draw(true)
|
||||||
select {}
|
select {}
|
||||||
w.cleanup()
|
w.cleanup()
|
||||||
close(mainDone)
|
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -449,7 +446,7 @@ func (w *window) config() (int, int, float32, unit.Metric) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
<-mainDone
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func translateKey(k string) (string, bool) {
|
func translateKey(k string) (string, bool) {
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ __attribute__ ((visibility ("hidden"))) CGFloat gio_getScreenBackingScale(void);
|
|||||||
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_readClipboard(void);
|
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_readClipboard(void);
|
||||||
__attribute__ ((visibility ("hidden"))) void gio_writeClipboard(unichar *chars, NSUInteger length);
|
__attribute__ ((visibility ("hidden"))) void gio_writeClipboard(unichar *chars, NSUInteger length);
|
||||||
__attribute__ ((visibility ("hidden"))) void gio_setNeedsDisplay(CFTypeRef viewRef);
|
__attribute__ ((visibility ("hidden"))) void gio_setNeedsDisplay(CFTypeRef viewRef);
|
||||||
__attribute__ ((visibility ("hidden"))) void gio_appTerminate(void);
|
|
||||||
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height, CGFloat minWidth, CGFloat minHeight, CGFloat maxWidth, CGFloat maxHeight);
|
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height, CGFloat minWidth, CGFloat minHeight, CGFloat maxWidth, CGFloat maxHeight);
|
||||||
__attribute__ ((visibility ("hidden"))) void gio_makeKeyAndOrderFront(CFTypeRef windowRef);
|
__attribute__ ((visibility ("hidden"))) void gio_makeKeyAndOrderFront(CFTypeRef windowRef);
|
||||||
__attribute__ ((visibility ("hidden"))) NSPoint gio_cascadeTopLeftFromPoint(CFTypeRef windowRef, NSPoint topLeft);
|
__attribute__ ((visibility ("hidden"))) NSPoint gio_cascadeTopLeftFromPoint(CFTypeRef windowRef, NSPoint topLeft);
|
||||||
@@ -271,9 +270,6 @@ func gio_onClose(view C.CFTypeRef) {
|
|||||||
w.view = 0
|
w.view = 0
|
||||||
C.CFRelease(w.window)
|
C.CFRelease(w.window)
|
||||||
w.window = 0
|
w.window = 0
|
||||||
if len(viewMap) == 0 {
|
|
||||||
C.gio_appTerminate()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export gio_onHide
|
//export gio_onHide
|
||||||
|
|||||||
@@ -153,12 +153,6 @@ CFTypeRef gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void gio_appTerminate(void) {
|
|
||||||
@autoreleasepool {
|
|
||||||
[NSApp terminate:nil];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void gio_close(CFTypeRef windowRef) {
|
void gio_close(CFTypeRef windowRef) {
|
||||||
NSWindow* window = (__bridge NSWindow *)windowRef;
|
NSWindow* window = (__bridge NSWindow *)windowRef;
|
||||||
[window performClose:nil];
|
[window performClose:nil];
|
||||||
|
|||||||
@@ -8,16 +8,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// windowCounter keeps track of the number of windows.
|
|
||||||
// A send of +1 or -1 represents a change in window count.
|
|
||||||
var windowCounter = make(chan int)
|
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
// Wait for first window
|
select {}
|
||||||
count := <-windowCounter
|
|
||||||
for count > 0 {
|
|
||||||
count += <-windowCounter
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// instead of creating files with build tags for each combination of wayland +/- x11
|
// instead of creating files with build tags for each combination of wayland +/- x11
|
||||||
|
|||||||
@@ -232,14 +232,8 @@ func newWLWindow(window Callbacks, opts *Options) error {
|
|||||||
d.destroy()
|
d.destroy()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Increment window counter.
|
|
||||||
windowCounter <- +1
|
|
||||||
w.w = window
|
w.w = window
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
|
||||||
// Decrement window counter.
|
|
||||||
windowCounter <- -1
|
|
||||||
}()
|
|
||||||
defer d.destroy()
|
defer d.destroy()
|
||||||
defer w.destroy()
|
defer w.destroy()
|
||||||
w.w.SetDriver(w)
|
w.w.SetDriver(w)
|
||||||
|
|||||||
@@ -57,10 +57,6 @@ type window struct {
|
|||||||
|
|
||||||
const _WM_REDRAW = windows.WM_USER + 0
|
const _WM_REDRAW = windows.WM_USER + 0
|
||||||
|
|
||||||
// windowCounter keeps track of the number of windows.
|
|
||||||
// A send of +1 or -1 represents a change in window count.
|
|
||||||
var windowCounter = make(chan int)
|
|
||||||
|
|
||||||
type gpuAPI struct {
|
type gpuAPI struct {
|
||||||
priority int
|
priority int
|
||||||
initializer func(w *window) (Context, error)
|
initializer func(w *window) (Context, error)
|
||||||
@@ -84,11 +80,7 @@ var resources struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
// Wait for first window
|
select {}
|
||||||
count := <-windowCounter
|
|
||||||
for count > 0 {
|
|
||||||
count += <-windowCounter
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWindow(window Callbacks, opts *Options) error {
|
func NewWindow(window Callbacks, opts *Options) error {
|
||||||
@@ -104,11 +96,7 @@ func NewWindow(window Callbacks, opts *Options) error {
|
|||||||
defer w.destroy()
|
defer w.destroy()
|
||||||
cerr <- nil
|
cerr <- nil
|
||||||
winMap.Store(w.hwnd, w)
|
winMap.Store(w.hwnd, w)
|
||||||
windowCounter <- +1
|
defer winMap.Delete(w.hwnd)
|
||||||
defer func() {
|
|
||||||
winMap.Delete(w.hwnd)
|
|
||||||
windowCounter <- -1
|
|
||||||
}()
|
|
||||||
w.w = window
|
w.w = window
|
||||||
w.w.SetDriver(w)
|
w.w.SetDriver(w)
|
||||||
defer w.w.Event(system.DestroyEvent{})
|
defer w.w.Event(system.DestroyEvent{})
|
||||||
|
|||||||
@@ -576,12 +576,7 @@ func newX11Window(gioWin Callbacks, opts *Options) error {
|
|||||||
// make the window visible on the screen
|
// make the window visible on the screen
|
||||||
C.XMapWindow(dpy, win)
|
C.XMapWindow(dpy, win)
|
||||||
|
|
||||||
// Increment window counter.
|
|
||||||
windowCounter <- +1
|
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
|
||||||
windowCounter <- -1
|
|
||||||
}()
|
|
||||||
w.w.SetDriver(w)
|
w.w.SetDriver(w)
|
||||||
w.setStage(system.StageRunning)
|
w.setStage(system.StageRunning)
|
||||||
w.loop()
|
w.loop()
|
||||||
|
|||||||
Reference in New Issue
Block a user