Added popups
This commit is contained in:
parent
519e0b9c6b
commit
26c1dc062b
@ -1,4 +1,4 @@
|
||||
package basic
|
||||
package testing
|
||||
|
||||
import "image"
|
||||
import "image/color"
|
||||
@ -7,8 +7,9 @@ import "git.tebibyte.media/sashakoshka/tomo/theme"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||
|
||||
// Test is a simple element that can be used as a placeholder.
|
||||
type Test struct {
|
||||
// Mouse is an element capable of testing mouse input. When the mouse is clicked
|
||||
// and dragged on it, it draws a trail.
|
||||
type Mouse struct {
|
||||
*core.Core
|
||||
core core.CoreControl
|
||||
drawing bool
|
||||
@ -16,16 +17,16 @@ type Test struct {
|
||||
lastMousePos image.Point
|
||||
}
|
||||
|
||||
// NewTest creates a new test element.
|
||||
func NewTest () (element *Test) {
|
||||
element = &Test { }
|
||||
// NewMouse creates a new mouse test element.
|
||||
func NewMouse () (element *Mouse) {
|
||||
element = &Mouse { }
|
||||
element.Core, element.core = core.NewCore(element)
|
||||
element.core.SetMinimumSize(32, 32)
|
||||
element.color = artist.NewUniform(color.Black)
|
||||
return
|
||||
}
|
||||
|
||||
func (element *Test) Handle (event tomo.Event) {
|
||||
func (element *Mouse) Handle (event tomo.Event) {
|
||||
switch event.(type) {
|
||||
case tomo.EventResize:
|
||||
resizeEvent := event.(tomo.EventResize)
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/testing"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/layouts"
|
||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||
|
||||
@ -16,9 +17,9 @@ func run () {
|
||||
container := basic.NewContainer(layouts.Horizontal { true, true })
|
||||
window.Adopt(container)
|
||||
|
||||
container.Adopt(basic.NewTest(), true)
|
||||
container.Adopt(testing.NewMouse(), true)
|
||||
container.Adopt(basic.NewLabel("<- left\nright ->", false), false)
|
||||
container.Adopt(basic.NewTest(), true)
|
||||
container.Adopt(testing.NewMouse(), true)
|
||||
|
||||
window.OnClose(tomo.Stop)
|
||||
window.Show()
|
||||
|
68
examples/popups/main.go
Normal file
68
examples/popups/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/popups"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/layouts"
|
||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||
|
||||
func main () {
|
||||
tomo.Run(run)
|
||||
}
|
||||
|
||||
func run () {
|
||||
window, _ := tomo.NewWindow(2, 2)
|
||||
window.SetTitle("Dialog Boxes")
|
||||
|
||||
container := basic.NewContainer(layouts.Vertical { true, true })
|
||||
window.Adopt(container)
|
||||
|
||||
container.Adopt(basic.NewLabel("Try out different dialogs:", false), true)
|
||||
|
||||
infoButton := basic.NewButton("popups.DialogKindInfo")
|
||||
infoButton.OnClick (func () {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindInfo,
|
||||
"Information",
|
||||
"You are wacky")
|
||||
})
|
||||
container.Adopt(infoButton, false)
|
||||
infoButton.Select()
|
||||
|
||||
questionButton := basic.NewButton("popups.DialogKindQuestion")
|
||||
questionButton.OnClick (func () {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindQuestion,
|
||||
"The Big Question",
|
||||
"Are you real?",
|
||||
popups.Button { "Yes", func () { } },
|
||||
popups.Button { "No", func () { } },
|
||||
popups.Button { "Not sure", func () { } })
|
||||
})
|
||||
container.Adopt(questionButton, false)
|
||||
|
||||
warningButton := basic.NewButton("popups.DialogKindWarning")
|
||||
warningButton.OnClick (func () {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindQuestion,
|
||||
"Warning",
|
||||
"They are fast approaching.")
|
||||
})
|
||||
container.Adopt(warningButton, false)
|
||||
|
||||
errorButton := basic.NewButton("popups.DialogKindError")
|
||||
errorButton.OnClick (func () {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindQuestion,
|
||||
"Error",
|
||||
"There is nowhere left to go.")
|
||||
})
|
||||
container.Adopt(errorButton, false)
|
||||
|
||||
cancelButton := basic.NewButton("No thank you.")
|
||||
cancelButton.OnClick(tomo.Stop)
|
||||
container.Adopt(cancelButton, false)
|
||||
|
||||
window.OnClose(tomo.Stop)
|
||||
window.Show()
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/testing"
|
||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||
|
||||
func main () {
|
||||
@ -11,7 +11,7 @@ func main () {
|
||||
func run () {
|
||||
window, _ := tomo.NewWindow(128, 128)
|
||||
window.SetTitle("hellorld!")
|
||||
window.Adopt(basic.NewTest())
|
||||
window.Adopt(testing.NewMouse())
|
||||
window.OnClose(tomo.Stop)
|
||||
window.Show()
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/testing"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/layouts"
|
||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||
|
||||
@ -22,7 +23,7 @@ func run () {
|
||||
button.OnClick (func () {
|
||||
container.DisownAll()
|
||||
container.Adopt(basic.NewLabel("Draw here:", false), false)
|
||||
container.Adopt(basic.NewTest(), true)
|
||||
container.Adopt(testing.NewMouse(), true)
|
||||
container.Adopt(okButton, false)
|
||||
okButton.Select()
|
||||
})
|
||||
|
49
popups/dialog.go
Normal file
49
popups/dialog.go
Normal file
@ -0,0 +1,49 @@
|
||||
package popups
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/layouts"
|
||||
|
||||
type DialogKind int
|
||||
|
||||
const (
|
||||
DialogKindInfo DialogKind = iota
|
||||
DialogKindQuestion
|
||||
DialogKindWarning
|
||||
DialogKindError
|
||||
)
|
||||
|
||||
type Button struct {
|
||||
Name string
|
||||
OnPress func ()
|
||||
}
|
||||
|
||||
func NewDialog (kind DialogKind, title, message string, buttons ...Button) {
|
||||
window, _ := tomo.NewWindow(2, 2)
|
||||
window.SetTitle(title)
|
||||
|
||||
container := basic.NewContainer(layouts.Dialog { true, true })
|
||||
window.Adopt(container)
|
||||
|
||||
container.Adopt(basic.NewLabel(message, false), true)
|
||||
if len(buttons) == 0 {
|
||||
button := basic.NewButton("OK")
|
||||
button.OnClick(window.Close)
|
||||
container.Adopt(button, false)
|
||||
button.Select()
|
||||
} else {
|
||||
var button *basic.Button
|
||||
for _, buttonDescriptor := range buttons {
|
||||
button = basic.NewButton(buttonDescriptor.Name)
|
||||
button.SetEnabled(buttonDescriptor.OnPress != nil)
|
||||
button.OnClick (func () {
|
||||
buttonDescriptor.OnPress()
|
||||
window.Close()
|
||||
})
|
||||
container.Adopt(button, false)
|
||||
}
|
||||
button.Select()
|
||||
}
|
||||
|
||||
window.Show()
|
||||
}
|
Reference in New Issue
Block a user