feat(windows): simplify tray controls and allow installer-user config edits

This commit is contained in:
cabbage committed 2026-10-27 08:54:23 +00:00
1 parent d59b2d5463
commit ae5e979761
18 files changed
+611 -88

No files matched your search

@@ -68,6 +68,13 @@ const (
StatePaused
)
// Status is read-only SCM state. Query never requests service mutation rights.
type Status struct {
Installed bool
State State
Startup StartupMode
}
type Command uint8
const (
@@ -118,6 +125,7 @@ func Transition(state State, command Command) (State, error) {
// Install, Uninstall, Start, and Stop are intentionally narrow. Their
// platform implementations return ErrUnsupported on non-Windows builds.
func Install(spec InstallSpec) error { return installNative(spec) }
func Query() (Status, error) { return queryNative() }
func Uninstall() error { return uninstallNative() }
func Configure(startup StartupMode) error { return configureNative(startup) }
func Start() error { return startNative() }
@@ -4,6 +4,7 @@ package windowsservice
import "context"
func queryNative() (Status, error) { return Status{}, ErrUnsupported }
func installNative(InstallSpec) error { return ErrUnsupported }
func uninstallNative() error { return ErrUnsupported }
func configureNative(StartupMode) error { return ErrUnsupported }
@@ -19,6 +19,52 @@ func connect() (*mgr.Mgr, error) {
return mgr.Connect()
}
func queryNative() (Status, error) {
manager, err := windows.OpenSCManager(nil, nil, windows.SC_MANAGER_CONNECT)
if err != nil {
return Status{}, err
}
defer windows.CloseServiceHandle(manager)
name, _ := windows.UTF16PtrFromString(Name)
handle, err := windows.OpenService(manager, name, windows.SERVICE_QUERY_STATUS|windows.SERVICE_QUERY_CONFIG)
if errors.Is(err, windows.ERROR_SERVICE_DOES_NOT_EXIST) {
return Status{State: StateStopped}, nil
}
if err != nil {
return Status{}, err
}
defer windows.CloseServiceHandle(handle)
service := mgr.Service{Name: Name, Handle: handle}
status, err := service.Query()
if err != nil {
return Status{}, err
}
configuration, err := service.Config()
if err != nil {
return Status{}, err
}
state := StateUnknown
switch status.State {
case svc.Running:
state = StateRunning
case svc.Stopped:
state = StateStopped
case svc.StartPending, svc.ContinuePending:
state = StateStartPending
case svc.StopPending:
state = StateStopPending
case svc.Paused, svc.PausePending:
state = StatePaused
}
startup := StartupMode("")
if configuration.StartType == mgr.StartAutomatic {
startup = StartupAutomatic
} else if configuration.StartType == mgr.StartManual {
startup = StartupManual
}
return Status{Installed: true, State: state, Startup: startup}, nil
}
func installNative(spec InstallSpec) error {
if err := spec.Validate(); err != nil {
return err