Last active
February 17, 2020 01:19
-
-
Save asukakenji/e3e7545e5ef376f306067ee9efc999d7 to your computer and use it in GitHub Desktop.
Shortest GUI program written in Golang. It displays a window and exits when the "close" button of the window is clicked.
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
// Shortest GUI program written in Golang. | |
// It displays a window and exits when the "close" button of the window is clicked. | |
package main | |
import ( | |
"golang.org/x/exp/shiny/driver" | |
"golang.org/x/exp/shiny/screen" | |
// Despite that the package names have a "mobile" prefix, | |
// these packages works on desktop. | |
"golang.org/x/mobile/event/lifecycle" | |
"golang.org/x/mobile/event/paint" | |
"golang.org/x/mobile/event/size" | |
) | |
func main() { | |
driver.Main(func(s screen.Screen) { | |
w, err := s.NewWindow(nil) | |
if err != nil { | |
panic(err) | |
} | |
defer w.Release() | |
for { | |
switch e := w.NextEvent().(type) { | |
case size.Event: | |
if e.WidthPx == 0 && e.HeightPx == 0 { | |
return | |
} | |
case paint.Event: | |
w.Publish() | |
case lifecycle.Event: | |
if e.To == lifecycle.StageDead { | |
return | |
} | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment