This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/examples/clipboard/main.go

134 lines
3.2 KiB
Go
Raw Normal View History

2023-03-27 18:44:39 -06:00
package main
import "io"
import "image"
import _ "image/png"
import _ "image/gif"
import _ "image/jpeg"
2023-03-27 18:44:39 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/popups"
2023-03-30 21:19:04 -06:00
import "git.tebibyte.media/sashakoshka/tomo/elements"
2023-03-27 18:44:39 -06:00
import _ "git.tebibyte.media/sashakoshka/tomo/backends/all"
func main () {
tomo.Run(run)
}
var validImageTypes = []data.Mime {
data.M("image", "png"),
data.M("image", "gif"),
data.M("image", "jpeg"),
}
2023-03-27 18:44:39 -06:00
func run () {
window, _ := tomo.NewWindow(tomo.Bounds(0, 0, 256, 0))
2023-03-27 18:44:39 -06:00
window.SetTitle("Clipboard")
container := elements.NewVBox(elements.SpaceBoth)
2023-03-30 21:19:04 -06:00
textInput := elements.NewTextBox("", "")
controlRow := elements.NewHBox(elements.SpaceMargin)
2023-03-30 21:19:04 -06:00
copyButton := elements.NewButton("Copy")
2023-03-30 23:06:29 -06:00
copyButton.SetIcon(tomo.IconCopy)
2023-03-30 21:19:04 -06:00
pasteButton := elements.NewButton("Paste")
2023-03-30 23:06:29 -06:00
pasteButton.SetIcon(tomo.IconPaste)
2023-03-30 21:19:04 -06:00
pasteImageButton := elements.NewButton("Image")
2023-03-30 23:06:29 -06:00
pasteImageButton.SetIcon(tomo.IconPictures)
2023-03-27 18:44:39 -06:00
imageClipboardCallback := func (clipboard data.Data, err error) {
if err != nil {
popups.NewDialog (
popups.DialogKindError,
window,
"Error",
"Cannot get clipboard:\n" + err.Error())
return
}
var imageData io.Reader
var ok bool
for mime, reader := range clipboard {
for _, mimeCheck := range validImageTypes {
if mime == mimeCheck {
imageData = reader
ok = true
}}}
if !ok {
popups.NewDialog (
popups.DialogKindError,
window,
"Clipboard Empty",
"No image data in clipboard")
return
}
img, _, err := image.Decode(imageData)
if err != nil {
popups.NewDialog (
popups.DialogKindError,
window,
"Error",
"Cannot decode image:\n" + err.Error())
return
}
imageWindow(window, img)
}
clipboardCallback := func (clipboard data.Data, err error) {
2023-03-27 18:44:39 -06:00
if err != nil {
popups.NewDialog (
popups.DialogKindError,
window,
"Error",
2023-03-27 23:00:54 -06:00
"Cannot get clipboard:\n" + err.Error())
return
}
textData, ok := clipboard[data.MimePlain]
if !ok {
2023-03-27 23:00:54 -06:00
popups.NewDialog (
popups.DialogKindError,
window,
"Clipboard Empty",
"No text data in clipboard")
2023-03-27 18:44:39 -06:00
return
}
text, _ := io.ReadAll(textData)
textInput.SetValue(string(text))
2023-03-27 18:44:39 -06:00
}
copyButton.OnClick (func () {
window.Copy(data.Text(textInput.Value()))
})
pasteButton.OnClick (func () {
window.Paste(clipboardCallback, data.MimePlain)
})
pasteImageButton.OnClick (func () {
window.Paste(imageClipboardCallback, validImageTypes...)
2023-03-27 18:44:39 -06:00
})
container.AdoptExpand(textInput)
controlRow.AdoptExpand(copyButton)
controlRow.AdoptExpand(pasteButton)
controlRow.AdoptExpand(pasteImageButton)
container.Adopt(controlRow)
2023-03-27 18:44:39 -06:00
window.Adopt(container)
window.OnClose(tomo.Stop)
window.Show()
}
func imageWindow (parent tomo.Window, image image.Image) {
window, _ := parent.NewModal(tomo.Bounds(0, 0, 0, 0))
window.SetTitle("Clipboard Image")
container := elements.NewVBox(elements.SpaceBoth)
2023-03-30 21:19:04 -06:00
closeButton := elements.NewButton("Ok")
2023-03-30 23:06:29 -06:00
closeButton.SetIcon(tomo.IconYes)
closeButton.OnClick(window.Close)
container.AdoptExpand(elements.NewImage(image))
container.Adopt(closeButton)
window.Adopt(container)
window.Show()
}