Added popups
This commit is contained in:
parent
519e0b9c6b
commit
26c1dc062b
@ -1,4 +1,4 @@
|
|||||||
package basic
|
package testing
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "image/color"
|
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/artist"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||||
|
|
||||||
// Test is a simple element that can be used as a placeholder.
|
// Mouse is an element capable of testing mouse input. When the mouse is clicked
|
||||||
type Test struct {
|
// and dragged on it, it draws a trail.
|
||||||
|
type Mouse struct {
|
||||||
*core.Core
|
*core.Core
|
||||||
core core.CoreControl
|
core core.CoreControl
|
||||||
drawing bool
|
drawing bool
|
||||||
@ -16,16 +17,16 @@ type Test struct {
|
|||||||
lastMousePos image.Point
|
lastMousePos image.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTest creates a new test element.
|
// NewMouse creates a new mouse test element.
|
||||||
func NewTest () (element *Test) {
|
func NewMouse () (element *Mouse) {
|
||||||
element = &Test { }
|
element = &Mouse { }
|
||||||
element.Core, element.core = core.NewCore(element)
|
element.Core, element.core = core.NewCore(element)
|
||||||
element.core.SetMinimumSize(32, 32)
|
element.core.SetMinimumSize(32, 32)
|
||||||
element.color = artist.NewUniform(color.Black)
|
element.color = artist.NewUniform(color.Black)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (element *Test) Handle (event tomo.Event) {
|
func (element *Mouse) Handle (event tomo.Event) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case tomo.EventResize:
|
case tomo.EventResize:
|
||||||
resizeEvent := event.(tomo.EventResize)
|
resizeEvent := event.(tomo.EventResize)
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/tomo"
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
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/elements/layouts"
|
||||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||||
|
|
||||||
@ -16,9 +17,9 @@ func run () {
|
|||||||
container := basic.NewContainer(layouts.Horizontal { true, true })
|
container := basic.NewContainer(layouts.Horizontal { true, true })
|
||||||
window.Adopt(container)
|
window.Adopt(container)
|
||||||
|
|
||||||
container.Adopt(basic.NewTest(), true)
|
container.Adopt(testing.NewMouse(), true)
|
||||||
container.Adopt(basic.NewLabel("<- left\nright ->", false), false)
|
container.Adopt(basic.NewLabel("<- left\nright ->", false), false)
|
||||||
container.Adopt(basic.NewTest(), true)
|
container.Adopt(testing.NewMouse(), true)
|
||||||
|
|
||||||
window.OnClose(tomo.Stop)
|
window.OnClose(tomo.Stop)
|
||||||
window.Show()
|
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
|
package main
|
||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/tomo"
|
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"
|
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||||
|
|
||||||
func main () {
|
func main () {
|
||||||
@ -11,7 +11,7 @@ func main () {
|
|||||||
func run () {
|
func run () {
|
||||||
window, _ := tomo.NewWindow(128, 128)
|
window, _ := tomo.NewWindow(128, 128)
|
||||||
window.SetTitle("hellorld!")
|
window.SetTitle("hellorld!")
|
||||||
window.Adopt(basic.NewTest())
|
window.Adopt(testing.NewMouse())
|
||||||
window.OnClose(tomo.Stop)
|
window.OnClose(tomo.Stop)
|
||||||
window.Show()
|
window.Show()
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/tomo"
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
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/elements/layouts"
|
||||||
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ func run () {
|
|||||||
button.OnClick (func () {
|
button.OnClick (func () {
|
||||||
container.DisownAll()
|
container.DisownAll()
|
||||||
container.Adopt(basic.NewLabel("Draw here:", false), false)
|
container.Adopt(basic.NewLabel("Draw here:", false), false)
|
||||||
container.Adopt(basic.NewTest(), true)
|
container.Adopt(testing.NewMouse(), true)
|
||||||
container.Adopt(okButton, false)
|
container.Adopt(okButton, false)
|
||||||
okButton.Select()
|
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