5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 04:42:00 +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 contextMenusLock sync.Mutex
assets *assetserver.AssetServer assets *assetserver.AssetServer
// Hooks
windowCreatedCallbacks []func(window *WebviewWindow)
} }
func (a *App) getSystemTrayID() uint { func (a *App) getSystemTrayID() uint {
@ -243,6 +246,11 @@ func (a *App) NewWebviewWindowWithOptions(windowOptions *WebviewWindowOptions) *
a.windows[id] = newWindow a.windows[id] = newWindow
a.windowsLock.Unlock() a.windowsLock.Unlock()
// Call hooks
for _, hook := range a.windowCreatedCallbacks {
hook(newWindow)
}
if a.running { if a.running {
newWindow.run() newWindow.run()
} }
@ -560,3 +568,7 @@ func (a *App) getContextMenu(name string) (*Menu, bool) {
return menu, ok 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 Name() string
Init(app *App) error Init(app *App) error
Shutdown() Shutdown()
// Exported is a list of method names that should be exposed to the frontend
CallableByJS() []string CallableByJS() []string
InjectJS() string
} }
type PluginManager struct { type PluginManager struct {
@ -13,9 +13,11 @@ type PluginManager struct {
} }
func NewPluginManager(plugins map[string]Plugin) *PluginManager { func NewPluginManager(plugins map[string]Plugin) *PluginManager {
return &PluginManager{ result := &PluginManager{
plugins: plugins, plugins: plugins,
} }
globalApplication.OnWindowCreation(result.onWindowCreation)
return result
} }
func (p *PluginManager) Init() error { func (p *PluginManager) Init() error {
@ -23,6 +25,10 @@ func (p *PluginManager) Init() error {
err := plugin.Init(globalApplication) err := plugin.Init(globalApplication)
if err != nil { if err != nil {
return err return err
}
injectJS := plugin.InjectJS()
if injectJS != "" {
} }
globalApplication.info("Plugin '%s' initialised", plugin.Name()) globalApplication.info("Plugin '%s' initialised", plugin.Name())
} }
@ -35,3 +41,12 @@ func (p *PluginManager) Shutdown() {
globalApplication.info("Plugin '%s' shutdown", plugin.Name()) 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?