fix(windows): start a responsive tray immediately after installation
This commit is contained in:
1 parent
14cb3570c1
commit
d59b2d5463
13 files changed
+850
-106
No files matched your search
@@ -0,0 +1,66 @@
|
||||
//go:build windows
|
||||
|
||||
package windowstray
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"runtime"
|
||||
|
||||
winapi "golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// Each operation owns its OVERLAPPED/event until cancellation has actually
|
||||
// completed. Closing a synchronous pipe from another goroutine does not provide
|
||||
// the bounded shutdown/deadline contract needed by a desktop frontend.
|
||||
func trayOverlapped(ctx context.Context, handle winapi.Handle, start func(*winapi.Overlapped, *uint32) error) (uint32, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
event, err := winapi.CreateEvent(nil, 1, 0, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer winapi.CloseHandle(event)
|
||||
overlapped := winapi.Overlapped{HEvent: event}
|
||||
var transferred uint32
|
||||
err = start(&overlapped, &transferred)
|
||||
if !errors.Is(err, winapi.ERROR_IO_PENDING) {
|
||||
return transferred, err
|
||||
}
|
||||
cancelled := make(chan struct{})
|
||||
stop := context.AfterFunc(ctx, func() {
|
||||
_ = winapi.CancelIoEx(handle, &overlapped)
|
||||
close(cancelled)
|
||||
})
|
||||
err = winapi.GetOverlappedResult(handle, &overlapped, &transferred, true)
|
||||
if !stop() {
|
||||
<-cancelled
|
||||
}
|
||||
if err != nil && ctx.Err() != nil {
|
||||
return transferred, ctx.Err()
|
||||
}
|
||||
return transferred, err
|
||||
}
|
||||
|
||||
func connectTrayPipe(ctx context.Context, handle winapi.Handle) error {
|
||||
_, err := trayOverlapped(ctx, handle, func(overlapped *winapi.Overlapped, _ *uint32) error {
|
||||
err := winapi.ConnectNamedPipe(handle, overlapped)
|
||||
if errors.Is(err, winapi.ERROR_PIPE_CONNECTED) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func transferTrayPipe(ctx context.Context, handle winapi.Handle, buffer []byte, write bool) (uint32, error) {
|
||||
n, err := trayOverlapped(ctx, handle, func(overlapped *winapi.Overlapped, transferred *uint32) error {
|
||||
if write {
|
||||
return winapi.WriteFile(handle, buffer, transferred, overlapped)
|
||||
}
|
||||
return winapi.ReadFile(handle, buffer, transferred, overlapped)
|
||||
})
|
||||
runtime.KeepAlive(buffer)
|
||||
return n, err
|
||||
}
|
||||
Reference in new issue
Block a user