Created
July 17, 2017 23:46
-
-
Save penciltester/276ea66ae4524b13ea710c7b4ee6dd5d to your computer and use it in GitHub Desktop.
Concurrent window changing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/lxn/walk" | |
. "github.com/lxn/walk/declarative" | |
"time" | |
) | |
var mainWindow *walk.MainWindow | |
func main() { | |
_, err := MainWindow{ | |
AssignTo: &mainWindow, | |
Title: "Main Window", | |
MinSize: Size{400, 300}, | |
Layout: VBox{}, | |
Children: []Widget{ | |
Label{Text: "something in main window"}, | |
PushButton{ | |
Text: "Do progress", | |
OnClicked: func() { | |
go askDialog() | |
}, | |
}, | |
}, | |
}.Run() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
func askDialog() { | |
var dialog *walk.Dialog | |
var acceptPB, cancelPB *walk.PushButton | |
_, err := Dialog{ | |
AssignTo: &dialog, | |
Title: "Do you want some progress?", | |
DefaultButton: &acceptPB, | |
CancelButton: &cancelPB, | |
MinSize: Size{300, 100}, | |
Layout: HBox{}, | |
Children: []Widget{ | |
PushButton{ | |
AssignTo: &acceptPB, | |
Text: "Yes", | |
OnClicked: func() { | |
dialog.Accept() | |
go doProgress() | |
}, | |
}, | |
PushButton{ | |
AssignTo: &cancelPB, | |
Text: "No", | |
OnClicked: func() { dialog.Cancel() }, | |
}, | |
}, | |
}.Run(mainWindow) | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
func doProgress() { | |
var dialog *walk.Dialog | |
var progressBar *walk.ProgressBar | |
err := Dialog{ | |
AssignTo: &dialog, | |
Title: "Progress dialog", | |
MinSize: Size{300, 200}, | |
Layout: VBox{}, | |
Children: []Widget{ | |
ProgressBar{AssignTo: &progressBar}, | |
}, | |
}.Create(mainWindow) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
dialog.Starting().Attach(func() { | |
go progressWorker(dialog, progressBar) | |
}) | |
dialog.Run() | |
} | |
func progressWorker(dialog *walk.Dialog, progressBar *walk.ProgressBar) { | |
defer dialog.Synchronize(func() { | |
dialog.Close(0) | |
}) | |
time.Sleep(1 * time.Millisecond) // waiting for some external event | |
n := 1234 // changes | |
dialog.Synchronize(func() { | |
progressBar.SetRange(0, n) | |
}) | |
for i := 0; i < n; i += 10 { | |
time.Sleep(1 * time.Millisecond) // event | |
fmt.Println("progress", i) | |
dialog.Synchronize(func() { | |
progressBar.SetValue(i) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment