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