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

Add Window creation hooks

Add InjectJS() to plugins interface
This commit is contained in:
Lea Anthony 2023-03-23 06:51:24 +11:00
parent d5808fde59
commit bf86b0d9c1
3 changed files with 32 additions and 2 deletions

View File

@ -166,6 +166,9 @@ type App struct {
contextMenusLock sync.Mutex
assets *assetserver.AssetServer
// Hooks
windowCreatedCallbacks []func(window *WebviewWindow)
}
func (a *App) getSystemTrayID() uint {
@ -243,6 +246,11 @@ func (a *App) NewWebviewWindowWithOptions(windowOptions *WebviewWindowOptions) *
a.windows[id] = newWindow
a.windowsLock.Unlock()
// Call hooks
for _, hook := range a.windowCreatedCallbacks {
hook(newWindow)
}
if a.running {
newWindow.run()
}
@ -560,3 +568,7 @@ func (a *App) getContextMenu(name string) (*Menu, bool) {
return menu, ok
}
func (a *App) OnWindowCreation(callback func(window *WebviewWindow)) {
a.windowCreatedCallbacks = append(a.windowCreatedCallbacks, callback)
}

View File

@ -4,8 +4,8 @@ type Plugin interface {
Name() string
Init(app *App) error
Shutdown()
// Exported is a list of method names that should be exposed to the frontend
CallableByJS() []string
InjectJS() string
}
type PluginManager struct {
@ -13,9 +13,11 @@ type PluginManager struct {
}
func NewPluginManager(plugins map[string]Plugin) *PluginManager {
return &PluginManager{
result := &PluginManager{
plugins: plugins,
}
globalApplication.OnWindowCreation(result.onWindowCreation)
return result
}
func (p *PluginManager) Init() error {
@ -23,6 +25,10 @@ func (p *PluginManager) Init() error {
err := plugin.Init(globalApplication)
if err != nil {
return err
}
injectJS := plugin.InjectJS()
if injectJS != "" {
}
globalApplication.info("Plugin '%s' initialised", plugin.Name())
}
@ -35,3 +41,12 @@ func (p *PluginManager) Shutdown() {
globalApplication.info("Plugin '%s' shutdown", plugin.Name())
}
}
func (p *PluginManager) onWindowCreation(window *WebviewWindow) {
for _, plugin := range p.plugins {
injectJS := plugin.InjectJS()
if injectJS != "" {
window.ExecJS(injectJS)
}
}
}

3
v3/plugins/TODO.md Normal file
View File

@ -0,0 +1,3 @@
# TODO
- [ ] Add InjectCSS() to the plugin API?