|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/andlabs/ui" |
| 8 | +) |
| 9 | + |
| 10 | +// Example showing how to update the UI using the QueueMain function |
| 11 | +// especially if the update is coming from another goroutine |
| 12 | +// |
| 13 | +// see QueueMain in 'main.go' for detailed description |
| 14 | + |
| 15 | +var countLabel *ui.Label |
| 16 | +var count int |
| 17 | + |
| 18 | +func setupUI() { |
| 19 | + mainWindow := ui.NewWindow("libui Updating UI", 640, 480, true) |
| 20 | + mainWindow.OnClosing(func(*ui.Window) bool { |
| 21 | + ui.Quit() |
| 22 | + return true |
| 23 | + }) |
| 24 | + ui.OnShouldQuit(func() bool { |
| 25 | + mainWindow.Destroy() |
| 26 | + return true |
| 27 | + }) |
| 28 | + |
| 29 | + vbContainer := ui.NewVerticalBox() |
| 30 | + vbContainer.SetPadded(true) |
| 31 | + |
| 32 | + inputGroup := ui.NewGroup("Input") |
| 33 | + inputGroup.SetMargined(true) |
| 34 | + |
| 35 | + vbInput := ui.NewVerticalBox() |
| 36 | + vbInput.SetPadded(true) |
| 37 | + |
| 38 | + inputForm := ui.NewForm() |
| 39 | + inputForm.SetPadded(true) |
| 40 | + |
| 41 | + message := ui.NewEntry() |
| 42 | + message.SetText("Hello World") |
| 43 | + inputForm.Append("What message do you want to show?", message, false) |
| 44 | + |
| 45 | + showMessageButton := ui.NewButton("Show message") |
| 46 | + clearMessageButton := ui.NewButton("Clear message") |
| 47 | + |
| 48 | + vbInput.Append(inputForm, false) |
| 49 | + vbInput.Append(showMessageButton, false) |
| 50 | + vbInput.Append(clearMessageButton, false) |
| 51 | + |
| 52 | + inputGroup.SetChild(vbInput) |
| 53 | + |
| 54 | + messageGroup := ui.NewGroup("Message") |
| 55 | + messageGroup.SetMargined(true) |
| 56 | + |
| 57 | + vbMessage := ui.NewVerticalBox() |
| 58 | + vbMessage.SetPadded(true) |
| 59 | + |
| 60 | + messageLabel := ui.NewLabel("") |
| 61 | + |
| 62 | + vbMessage.Append(messageLabel, false) |
| 63 | + |
| 64 | + messageGroup.SetChild(vbMessage) |
| 65 | + |
| 66 | + countGroup := ui.NewGroup("Counter") |
| 67 | + countGroup.SetMargined(true) |
| 68 | + |
| 69 | + vbCounter := ui.NewVerticalBox() |
| 70 | + vbCounter.SetPadded(true) |
| 71 | + |
| 72 | + countLabel = ui.NewLabel(fmt.Sprintf("%d", count)) |
| 73 | + |
| 74 | + vbCounter.Append(countLabel, false) |
| 75 | + countGroup.SetChild(vbCounter) |
| 76 | + |
| 77 | + vbContainer.Append(inputGroup, false) |
| 78 | + vbContainer.Append(messageGroup, false) |
| 79 | + vbContainer.Append(countGroup, false) |
| 80 | + |
| 81 | + mainWindow.SetChild(vbContainer) |
| 82 | + |
| 83 | + showMessageButton.OnClicked(func(*ui.Button) { |
| 84 | + // Update the UI directly as it is called from the main thread |
| 85 | + messageLabel.SetText(message.Text()) |
| 86 | + }) |
| 87 | + |
| 88 | + clearMessageButton.OnClicked(func(*ui.Button) { |
| 89 | + // Update the UI directly as it is called from the main thread |
| 90 | + messageLabel.SetText("") |
| 91 | + }) |
| 92 | + |
| 93 | + mainWindow.Show() |
| 94 | + |
| 95 | + // Counting and updating the UI from another goroutine |
| 96 | + go counter() |
| 97 | +} |
| 98 | + |
| 99 | +func counter() { |
| 100 | + for { |
| 101 | + time.Sleep(1 * time.Second) |
| 102 | + count++ |
| 103 | + |
| 104 | + // Update the UI using the QueueMain function |
| 105 | + ui.QueueMain(func() { |
| 106 | + countLabel.SetText(fmt.Sprintf("%d", count)) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func main() { |
| 112 | + count = 0 |
| 113 | + |
| 114 | + ui.Main(setupUI) |
| 115 | +} |
0 commit comments