Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RunWithContext function #826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion form.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build windows
// +build windows

package walk

import (
"context"
"fmt"
"math"
"sync"
Expand Down Expand Up @@ -341,6 +343,15 @@ func (fb *FormBase) SetRightToLeftLayout(rtl bool) error {
}

func (fb *FormBase) Run() int {
return fb.run(context.Background())
}

// RunWithContext does the same as Run, but with the context cancellation respect.
func (fb *FormBase) RunWithContext(ctx context.Context) int {
return fb.run(ctx)
}

func (fb *FormBase) run(ctx context.Context) int {
if fb.owner != nil {
win.EnableWindow(fb.owner.Handle(), false)

Expand Down Expand Up @@ -372,7 +383,7 @@ func (fb *FormBase) Run() int {

fb.SetSuspended(false)

return fb.mainLoop()
return fb.mainLoop(ctx)
}

func (fb *FormBase) handleKeyDown(msg *win.MSG) bool {
Expand Down
3 changes: 2 additions & 1 deletion mainloop_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build windows && walk_use_cgo
// +build windows,walk_use_cgo

package walk
Expand Down Expand Up @@ -51,6 +52,6 @@ func shimRunSynchronized(fb uintptr) {
(*FormBase)(unsafe.Pointer(fb)).group.RunSynchronized()
}

func (fb *FormBase) mainLoop() int {
func (fb *FormBase) mainLoop(_ context.Context) int {
return int(C.mainloop(C.uintptr_t(uintptr(unsafe.Pointer(&fb.hWnd))), C.uintptr_t(uintptr(unsafe.Pointer(fb)))))
}
8 changes: 7 additions & 1 deletion mainloop_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build windows && !walk_use_cgo
// +build windows,!walk_use_cgo

package walk

import (
"context"
"unsafe"

"github.com/lxn/win"
)

func (fb *FormBase) mainLoop() int {
func (fb *FormBase) mainLoop(ctx context.Context) int {
msg := (*win.MSG)(unsafe.Pointer(win.GlobalAlloc(0, unsafe.Sizeof(win.MSG{}))))
defer win.GlobalFree(win.HGLOBAL(unsafe.Pointer(msg)))

for fb.hWnd != 0 {
if err := ctx.Err(); err != nil {
return 0
}

switch win.GetMessage(msg, 0, 0, 0) {
case 0:
return int(msg.WParam)
Expand Down