-
-
Save inkeliz/ec4e95c7397a2ac166d58205e824e226 to your computer and use it in GitHub Desktop.
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" | |
"image/color" | |
"os" | |
"gioui.org/app" | |
"gioui.org/font/gofont" | |
"gioui.org/io/system" | |
"gioui.org/layout" | |
"gioui.org/op" | |
"gioui.org/op/clip" | |
"gioui.org/op/paint" | |
"gioui.org/unit" | |
"gioui.org/widget" | |
"gioui.org/widget/material" | |
) | |
type C = layout.Context | |
type D = layout.Dimensions | |
func loop(w *app.Window) { | |
th := material.NewTheme(gofont.Collection()) | |
var okButton, cancelButton widget.Clickable | |
var ops op.Ops | |
for e := range w.Events() { | |
switch e := e.(type) { | |
case system.DestroyEvent: | |
if e.Err != nil { | |
panic(e.Err) | |
} | |
os.Exit(0) | |
return | |
case system.FrameEvent: | |
if okButton.Clicked() { | |
fmt.Println("clicked ok") | |
} | |
gtx := layout.NewContext(&ops, e) | |
layout.Flex{ | |
Axis: layout.Vertical, | |
Spacing: layout.SpaceStart, | |
}.Layout(gtx, | |
layout.Flexed(50, | |
func(gtx C) D { | |
return layout.Inset{ | |
Top: unit.Dp(25), | |
Bottom: unit.Dp(0), | |
Right: unit.Dp(25), | |
Left: unit.Dp(25), | |
}.Layout(gtx, func(gtx C) D { | |
// Option 1: | |
// gtx.Constraints.Min = image.Point{} | |
// paint.FillShape(gtx.Ops, color.NRGBA{G: 255, A: 255}, clip.Rect(gtx.Constraints).Op()) | |
// Option 2: | |
paint.FillShape(gtx.Ops, color.NRGBA{G: 255, A: 255}, clip.Rect{Max: gtx.Constraints.Max}.Op()) | |
// Remain unchanged: | |
return layout.Dimensions{Size: gtx.Constraints.Max} | |
}) | |
}, | |
), | |
layout.Rigid( | |
func(gtx C) D { | |
return layout.Inset{ | |
Top: unit.Dp(25), | |
Bottom: unit.Dp(25), | |
Right: unit.Dp(25), | |
Left: unit.Dp(25), | |
}.Layout(gtx, | |
func(gtx C) D { | |
btnWidth := gtx.Px(unit.Dp(80)) | |
return layout.Flex{ | |
Alignment: layout.Middle, | |
Spacing: layout.SpaceBetween, | |
}.Layout(gtx, | |
layout.Rigid(func(gtx C) D { | |
gtx.Constraints.Min.X = btnWidth | |
return material.Button(th, &cancelButton, "Cancel").Layout(gtx) | |
}), | |
layout.Rigid(func(gtx C) D { | |
gtx.Queue = nil // disable button | |
gtx.Constraints.Min.X = btnWidth | |
return material.Button(th, &okButton, "OK").Layout(gtx) | |
}), | |
) | |
}, | |
) | |
}, | |
), | |
) | |
e.Frame(gtx.Ops) | |
} | |
} | |
} | |
func main() { | |
go loop(app.NewWindow( | |
app.Title("Example"), | |
app.Size(unit.Dp(400), unit.Dp(600)), | |
app.MinSize(unit.Dp(230), unit.Dp(200)), | |
)) | |
app.Main() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment