2023-03-27 18:44:39 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "io"
|
2023-03-28 22:50:23 -06:00
|
|
|
import "image"
|
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/theme"
|
|
|
|
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/all"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/containers"
|
|
|
|
|
|
|
|
func main () {
|
|
|
|
tomo.Run(run)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run () {
|
2023-03-28 22:50:23 -06:00
|
|
|
window, _ := tomo.NewWindow(256, 2)
|
2023-03-27 18:44:39 -06:00
|
|
|
window.SetTitle("Clipboard")
|
|
|
|
|
|
|
|
container := containers.NewContainer(basicLayouts.Vertical { true, true })
|
|
|
|
textInput := basicElements.NewTextBox("", "")
|
|
|
|
controlRow := containers.NewContainer(basicLayouts.Horizontal { true, false })
|
|
|
|
copyButton := basicElements.NewButton("Copy")
|
|
|
|
copyButton.SetIcon(theme.IconCopy)
|
|
|
|
pasteButton := basicElements.NewButton("Paste")
|
|
|
|
pasteButton.SetIcon(theme.IconPaste)
|
2023-03-28 22:50:23 -06:00
|
|
|
pasteImageButton := basicElements.NewButton("Image")
|
|
|
|
pasteImageButton.SetIcon(theme.IconPictures)
|
2023-03-27 18:44:39 -06:00
|
|
|
|
2023-03-28 22:50:23 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
imageData, ok := clipboard[data.M("image", "png")]
|
|
|
|
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(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
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:50:23 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:50:23 -06:00
|
|
|
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 () {
|
2023-03-28 22:50:23 -06:00
|
|
|
window.Paste(clipboardCallback, data.MimePlain)
|
|
|
|
})
|
|
|
|
pasteImageButton.OnClick (func () {
|
|
|
|
window.Paste(imageClipboardCallback, data.M("image", "png"))
|
2023-03-27 18:44:39 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
container.Adopt(textInput, true)
|
|
|
|
controlRow.Adopt(copyButton, true)
|
|
|
|
controlRow.Adopt(pasteButton, true)
|
2023-03-28 22:50:23 -06:00
|
|
|
controlRow.Adopt(pasteImageButton, true)
|
2023-03-27 18:44:39 -06:00
|
|
|
container.Adopt(controlRow, false)
|
|
|
|
window.Adopt(container)
|
|
|
|
|
|
|
|
window.OnClose(tomo.Stop)
|
|
|
|
window.Show()
|
|
|
|
}
|
2023-03-28 22:50:23 -06:00
|
|
|
|
|
|
|
func imageWindow (image image.Image) {
|
|
|
|
window, _ := tomo.NewWindow(2, 2)
|
|
|
|
window.SetTitle("Clipboard Image")
|
|
|
|
container := containers.NewContainer(basicLayouts.Vertical { true, true })
|
|
|
|
closeButton := basicElements.NewButton("Ok")
|
|
|
|
closeButton.SetIcon(theme.IconYes)
|
|
|
|
closeButton.OnClick(window.Close)
|
|
|
|
|
|
|
|
container.Adopt(basicElements.NewImage(image), true)
|
|
|
|
container.Adopt(closeButton, false)
|
|
|
|
window.Adopt(container)
|
|
|
|
window.Show()
|
|
|
|
}
|