Image element for showing images

This commit is contained in:
Sasha Koshka 2023-02-09 18:34:53 -05:00
parent 6e7cf285cc
commit cfc2b5e130
2 changed files with 81 additions and 0 deletions

23
elements/basic/image.go Normal file
View File

@ -0,0 +1,23 @@
package basicElements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
type Image struct {
*core.Core
core core.CoreControl
buffer artist.Pattern
}
func NewImage (image image.Image) (element *Image) {
element = &Image { buffer: artist.NewTexture(image) }
element.Core, element.core = core.NewCore(element.draw)
bounds := image.Bounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
return
}
func (element *Image) draw () {
artist.FillRectangle(element, element.buffer, element.Bounds())
}

58
examples/image/image.go Normal file
View File

@ -0,0 +1,58 @@
package main
import "os"
import "image"
import "bytes"
import _ "image/png"
import "github.com/jezek/xgbutil/gopher"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/popups"
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
func main () {
tomo.Run(run)
}
func run () {
window, _ := tomo.NewWindow(2, 2)
window.SetTitle("Tomo Logo")
file, err := os.Open("assets/banner.png")
if err != nil { fatalError(err); return }
logo, _, err := image.Decode(file)
file.Close()
if err != nil { fatalError(err); return }
container := basicElements.NewContainer(basicLayouts.Vertical { true, true })
logoImage := basicElements.NewImage(logo)
button := basicElements.NewButton("Show me a gopher instead")
button.OnClick (func () { container.Warp (func () {
container.DisownAll()
gopher, _, err :=
image.Decode(bytes.NewReader(gopher.GopherPng()))
if err != nil { fatalError(err); return }
container.Adopt(basicElements.NewImage(gopher),true)
}) })
container.Adopt(logoImage, true)
container.Adopt(button, false)
window.Adopt(container)
button.Focus()
window.OnClose(tomo.Stop)
window.Show()
}
func fatalError (err error) {
popups.NewDialog (
popups.DialogKindError,
"Error",
err.Error(),
popups.Button {
Name: "OK",
OnPress: tomo.Stop,
})
}