pikapika/go/cmd/options.go

89 lines
2.2 KiB
Go
Raw Normal View History

2021-09-29 23:57:09 +00:00
package main
import (
2021-10-22 02:24:39 +00:00
"errors"
2021-09-29 23:57:09 +00:00
"github.com/go-flutter-desktop/go-flutter"
2021-10-22 02:24:39 +00:00
"github.com/go-flutter-desktop/go-flutter/plugin"
2021-09-29 23:57:09 +00:00
"github.com/go-flutter-desktop/plugins/url_launcher"
2021-10-22 02:24:39 +00:00
"github.com/go-gl/glfw/v3.3/glfw"
2021-09-29 23:57:09 +00:00
"github.com/miguelpruivo/flutter_file_picker/go"
2021-12-10 11:38:17 +00:00
"pikapika/pikapika"
"pikapika/pikapika/database/properties"
2021-10-22 02:24:39 +00:00
"strconv"
"sync"
2021-09-29 23:57:09 +00:00
)
var options = []flutter.Option{
2021-11-11 03:00:38 +00:00
flutter.AddPlugin(&PikapikaPlugin{}),
2021-09-29 23:57:09 +00:00
flutter.AddPlugin(&file_picker.FilePickerPlugin{}),
flutter.AddPlugin(&url_launcher.UrlLauncherPlugin{}),
}
2021-10-22 02:24:39 +00:00
var eventMutex = sync.Mutex{}
var eventSink *plugin.EventSink
type EventHandler struct {
}
func (s *EventHandler) OnListen(arguments interface{}, sink *plugin.EventSink) {
eventMutex.Lock()
defer eventMutex.Unlock()
eventSink = sink
}
func (s *EventHandler) OnCancel(arguments interface{}) {
eventMutex.Lock()
defer eventMutex.Unlock()
eventSink = nil
}
const channelName = "method"
2021-11-11 03:00:38 +00:00
type PikapikaPlugin struct {
2021-10-22 02:24:39 +00:00
}
2021-11-11 03:00:38 +00:00
func (p *PikapikaPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
2021-10-22 02:24:39 +00:00
channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
channel.HandleFunc("flatInvoke", func(arguments interface{}) (interface{}, error) {
if argumentsMap, ok := arguments.(map[interface{}]interface{}); ok {
if method, ok := argumentsMap["method"].(string); ok {
if params, ok := argumentsMap["params"].(string); ok {
2021-12-10 11:38:17 +00:00
return pikapika.FlatInvoke(method, params)
2021-10-22 02:24:39 +00:00
}
}
}
return nil, errors.New("params error")
})
exporting := plugin.NewEventChannel(messenger, "flatEvent", plugin.StandardMethodCodec{})
exporting.Handle(&EventHandler{})
2021-12-10 11:38:17 +00:00
pikapika.EventNotify = func(message string) {
2021-10-22 02:24:39 +00:00
eventMutex.Lock()
defer eventMutex.Unlock()
sink := eventSink
if sink != nil {
sink.Success(message)
}
}
return nil // no error
}
2021-11-11 03:00:38 +00:00
func (p *PikapikaPlugin) InitPluginGLFW(window *glfw.Window) error {
2021-10-22 02:24:39 +00:00
window.SetSizeCallback(func(w *glfw.Window, width int, height int) {
2021-10-24 06:37:46 +00:00
go func() {
properties.SaveProperty("window_width", strconv.Itoa(width))
properties.SaveProperty("window_height", strconv.Itoa(height))
}()
2021-10-22 02:24:39 +00:00
})
2021-12-04 04:20:51 +00:00
window.SetMaximizeCallback(func(w *glfw.Window, iconified bool) {
go func() {
properties.SaveProperty("full_screen", strconv.FormatBool(iconified))
}()
})
2021-10-22 02:24:39 +00:00
return nil
}