mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
kind of working finally
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
// Copyright 2022 The golang.design Initiative Authors.
|
||||
// All rights reserved. Use of this source code is governed
|
||||
// by a MIT license that can be found in the LICENSE file.
|
||||
//
|
||||
// Written by Changkun Ou <changkun.de>
|
||||
|
||||
// Package mainthread wrapps the golang.design/x/mainthread, and
|
||||
// provides a different implementation for macOS so that it can
|
||||
// handle main thread events for the NSApplication.
|
||||
package mainthread
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2022 The golang.design Initiative Authors.
|
||||
// All rights reserved. Use of this source code is governed
|
||||
// by a MIT license that can be found in the LICENSE file.
|
||||
//
|
||||
// Written by Changkun Ou <changkun.de>
|
||||
|
||||
//go:build windows || linux || (darwin && !cgo)
|
||||
|
||||
package mainthread
|
||||
|
||||
import "golang.design/x/mainthread"
|
||||
|
||||
// Call calls f on the main thread and blocks until f finishes.
|
||||
func Call(f func()) { mainthread.Call(f) }
|
||||
|
||||
// Init initializes the functionality of running arbitrary subsequent functions be called on the main system thread.
|
||||
//
|
||||
// Init must be called in the main.main function.
|
||||
func Init(main func()) { mainthread.Init(main) }
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright 2022 The golang.design Initiative Authors.
|
||||
// All rights reserved. Use of this source code is governed
|
||||
// by a MIT license that can be found in the LICENSE file.
|
||||
//
|
||||
// Written by Changkun Ou <changkun.de>
|
||||
|
||||
//go:build darwin
|
||||
|
||||
package mainthread
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c
|
||||
#cgo LDFLAGS: -framework Cocoa
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <Dispatch/Dispatch.h>
|
||||
|
||||
extern void os_main(void);
|
||||
extern void wakeupMainThread(void);
|
||||
static bool isMainThread() {
|
||||
return [NSThread isMainThread];
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
// Call calls f on the main thread and blocks until f finishes.
|
||||
func Call(f func()) {
|
||||
if C.isMainThread() {
|
||||
f()
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
mainFuncs <- f
|
||||
C.wakeupMainThread()
|
||||
}()
|
||||
}
|
||||
|
||||
// Init initializes the functionality of running arbitrary subsequent functions be called on the main system thread.
|
||||
//
|
||||
// Init must be called in the main.main function.
|
||||
func Init(f func()) {
|
||||
go func() {
|
||||
f()
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
C.os_main()
|
||||
}
|
||||
|
||||
var mainFuncs = make(chan func(), 1)
|
||||
|
||||
//export dispatchMainFuncs
|
||||
func dispatchMainFuncs() {
|
||||
for {
|
||||
select {
|
||||
case f := <-mainFuncs:
|
||||
f()
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2022 The golang.design Initiative Authors.
|
||||
// All rights reserved. Use of this source code is governed
|
||||
// by a MIT license that can be found in the LICENSE file.
|
||||
//
|
||||
// Written by Changkun Ou <changkun.de>
|
||||
|
||||
//go:build darwin
|
||||
|
||||
#include <stdint.h>
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
extern void dispatchMainFuncs();
|
||||
|
||||
void wakeupMainThread(void) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
dispatchMainFuncs();
|
||||
});
|
||||
}
|
||||
|
||||
// The following three lines of code must run on the main thread.
|
||||
// It must handle it using golang.design/x/mainthread.
|
||||
//
|
||||
// inspired from here: https://github.com/cehoffman/dotfiles/blob/4be8e893517e970d40746a9bdc67fe5832dd1c33/os/mac/iTerm2HotKey.m
|
||||
void os_main(void) {
|
||||
[NSApplication sharedApplication];
|
||||
[NSApp disableRelaunchOnLogin];
|
||||
[NSApp run];
|
||||
}
|
||||
Reference in New Issue
Block a user