mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
[v3 windows] Initial support for start_on_login plugin for windows
This commit is contained in:
@@ -24,7 +24,7 @@ You can then call the methods from the frontend:
|
||||
To use this from Go, create a new instance of the plugin, then call the methods on that:
|
||||
|
||||
```go
|
||||
start_at_login := start_at_login.NewPlugin()
|
||||
start_at_login := start_at_login.NewPlugin(Options)
|
||||
start_at_login.StartAtLogin(true)
|
||||
```
|
||||
|
||||
|
||||
@@ -7,10 +7,19 @@ import (
|
||||
type Plugin struct {
|
||||
app *application.App
|
||||
disabled bool
|
||||
options Config
|
||||
}
|
||||
|
||||
func NewPlugin() *Plugin {
|
||||
return &Plugin{}
|
||||
type Config struct {
|
||||
// RegistryKey is the key in the registry to use for storing the start at login setting.
|
||||
// This defaults to the name of the executable
|
||||
RegistryKey string
|
||||
}
|
||||
|
||||
func NewPlugin(options Config) *Plugin {
|
||||
return &Plugin{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown is called when the app is shutting down
|
||||
|
||||
@@ -6,3 +6,11 @@ func (p *Plugin) init() error {
|
||||
// TBD
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) StartAtLogin(enabled bool) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (p *Plugin) IsStartAtLogin() (bool, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,87 @@
|
||||
|
||||
package start_at_login
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/sys/windows/registry"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (p *Plugin) init() error {
|
||||
// TBD
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) getRegistryKey() (string, string, error) {
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to get executable path: %s", err)
|
||||
}
|
||||
|
||||
registryKey := p.options.RegistryKey
|
||||
if p.options.RegistryKey == "" {
|
||||
registryKey = strings.Split(filepath.Base(exePath), ".")[0]
|
||||
}
|
||||
|
||||
return registryKey, exePath, nil
|
||||
}
|
||||
|
||||
func openRegKey() (registry.Key, error) {
|
||||
// Open the registry key
|
||||
return registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Run`, registry.ALL_ACCESS)
|
||||
}
|
||||
|
||||
func (p *Plugin) IsStartAtLogin() (bool, error) {
|
||||
registryKey, exePath, err := p.getRegistryKey()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
key, err := openRegKey()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
// Get the registry value
|
||||
value, _, err := key.GetStringValue(registryKey)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
return value == exePath, nil
|
||||
}
|
||||
|
||||
func (p *Plugin) StartAtLogin(enabled bool) error {
|
||||
registryKey, exePath, err := p.getRegistryKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if enabled {
|
||||
// Open the registry key
|
||||
key, err := openRegKey()
|
||||
defer key.Close()
|
||||
|
||||
// Set the registry value
|
||||
err = key.SetStringValue(registryKey, exePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set registry value: %s", err)
|
||||
}
|
||||
} else {
|
||||
// Remove registry key
|
||||
key, err := openRegKey()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open registry key: %s", err)
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
// Remove the registry value
|
||||
err = key.DeleteValue(registryKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete registry value: %s", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user