Add ability to open files from Finder in macOS

This commit is contained in:
akiyosi 2024-06-05 23:55:03 +09:00
parent 8d8ef2d75a
commit 392e7c7a5f
9 changed files with 2541 additions and 68 deletions

2
.gitignore vendored
View file

@ -5,6 +5,8 @@ debug.log
tags*
moc*
editor/version.txt
editor/objcbridge.h
editor/objcbridge.m
cmd/goneovim/goneovim
cmd/goneovim/debug
cmd/goneovim/deploy/*

File diff suppressed because it is too large Load diff

View file

@ -112,6 +112,7 @@ type Editor struct {
fontCh chan []*Font
cbChan chan *string
chUiPrepared chan bool
openingFileCh chan string
geometryUpdateTimer *time.Timer
sysTray *widgets.QSystemTrayIcon
side *WorkspaceSide
@ -606,7 +607,33 @@ func (e *Editor) connectAppSignals() {
if e.app == nil {
return
}
if runtime.GOOS == "darwin" {
e.openingFileCh = make(chan string, 2)
go func() {
for {
openingFile := <-e.openingFileCh
file, err := os.OpenFile("/Users/akiyosi/debug3.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
log.SetOutput(file)
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Println(
fmt.Sprintf("%07.3f", float64(time.Now().UnixNano()/1000-e.startuptime)/1000),
strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", openingFile), "["), "]"),
)
e.loadFileInDarwin(
openingFile,
)
}
}()
e.app.ConnectEvent(func(event *core.QEvent) bool {
switch event.Type() {
case core.QEvent__FileOpen:
@ -615,8 +642,9 @@ func (e *Editor) connectAppSignals() {
return false
}
fileOpenEvent := gui.NewQFileOpenEventFromPointer(event.Pointer())
e.macAppArg = fileOpenEvent.File()
e.loadFileInDarwin()
e.loadFileInDarwin(
fileOpenEvent.File(),
)
}
return true
})
@ -734,7 +762,7 @@ func (e *Editor) AdjustSizeBasedOnFontmetrics(windowWidth, windowHeight int) {
}
}
func (e *Editor) loadFileInDarwin() {
func (e *Editor) loadFileInDarwin(file string) {
if runtime.GOOS != "darwin" {
return
}
@ -743,9 +771,9 @@ func (e *Editor) loadFileInDarwin() {
isModified := ""
isModified, _ = goneovim.CommandOutput("echo &modified")
if isModified == "1" {
goneovim.Command(fmt.Sprintf(":tabe %s", e.macAppArg))
goneovim.Command(fmt.Sprintf(":tabe %s", file))
} else {
goneovim.Command(fmt.Sprintf("%s %s", e.config.Editor.FileOpenCmd, e.macAppArg))
goneovim.Command(fmt.Sprintf("%s %s", e.config.Editor.FileOpenCmd, file))
}
}

View file

@ -3,28 +3,17 @@ package editor
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
@interface MyApplicationDelegate : NSObject <NSApplicationDelegate>
@end
@implementation MyApplicationDelegate
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}
@end
void SetMyApplicationDelegate() {
NSApplication *app = [NSApplication sharedApplication];
app.delegate = [[MyApplicationDelegate alloc] init];
[app activateIgnoringOtherApps:YES]; // make application foreground
}
#include "objcbridge.h"
#include <stdlib.h>
*/
import "C"
//export GetOpeningFilepath
func GetOpeningFilepath(str *C.char) {
goStr := C.GoString(str)
editor.openingFileCh <- goStr
}
func setMyApplicationDelegate() {
C.SetMyApplicationDelegate()
}

View file

@ -3,5 +3,10 @@
package editor
import "C"
func GetOpeningFilepath(str *C.char) {
}
func setMyApplicationDelegate() {
}

View file

@ -1,4 +1,9 @@
package editor
import "C"
func GetOpeningFilepath(str *C.char) {
}
func setMyApplicationDelegate() {
}

View file

@ -1794,10 +1794,12 @@ func (ws *Workspace) scrollMinimap() {
func (ws *Workspace) handleGui(updates []interface{}) {
event := updates[0].(string)
switch event {
case "gonvim_enter":
editor.putLog("vim enter")
case "gonvim_vimenter":
// openingFile := getOpeningFilePath()
// if openingFile != "" {
// go ws.nvim.Command(fmt.Sprintf(":e %s", openingFile))
// }
case "gonvim_uienter":
editor.putLog("ui enter")
case "gonvim_resize":
width, height := editor.setWindowSize(updates[1].(string))
editor.window.Resize2(width, height)

73
generate_objcbridge.go Normal file
View file

@ -0,0 +1,73 @@
//go:build generate
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"text/template"
)
const objcbridgeH = `void SetMyApplicationDelegate();`
const objcbridgeM = `#import <Cocoa/Cocoa.h>
char *cFilename;
// Forward declaration of the Go function to be called from C
extern void GetOpeningFilepath(char* str);
@interface MyApplicationDelegate : NSObject <NSApplicationDelegate>
@end
@implementation MyApplicationDelegate
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
char *utf8String = [filename UTF8String];
cFilename = strdup(utf8String);
GetOpeningFilepath(cFilename);
return YES;
}
@end
void SetMyApplicationDelegate() {
NSApplication *app = [NSApplication sharedApplication];
app.delegate = [[MyApplicationDelegate alloc] init];
[app activateIgnoringOtherApps:YES]; // make application foreground
}`
func main() {
if runtime.GOOS != "darwin" {
return
}
generateFile(filepath.Join("editor", "objcbridge.h"), objcbridgeH)
generateFile(filepath.Join("editor", "objcbridge.m"), objcbridgeM)
}
func generateFile(filename, content string) {
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
tmpl, err := template.New("file").Parse(content)
if err != nil {
fmt.Println("Error parsing template:", err)
return
}
err = tmpl.Execute(file, nil)
if err != nil {
fmt.Println("Error executing template:", err)
}
}

View file

@ -1,3 +1,4 @@
package goneovim
//go:generate sh -c "printf %s $(git describe --tags) > editor/version.txt"
//go:generate go run generate_objcbridge.go