This should have been several separate commits
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import "io"
|
||||
import "image"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/data"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
||||
@@ -15,7 +16,7 @@ func main () {
|
||||
}
|
||||
|
||||
func run () {
|
||||
window, _ := tomo.NewWindow(2, 2)
|
||||
window, _ := tomo.NewWindow(256, 2)
|
||||
window.SetTitle("Clipboard")
|
||||
|
||||
container := containers.NewContainer(basicLayouts.Vertical { true, true })
|
||||
@@ -25,8 +26,41 @@ func run () {
|
||||
copyButton.SetIcon(theme.IconCopy)
|
||||
pasteButton := basicElements.NewButton("Paste")
|
||||
pasteButton.SetIcon(theme.IconPaste)
|
||||
pasteImageButton := basicElements.NewButton("Image")
|
||||
pasteImageButton.SetIcon(theme.IconPictures)
|
||||
|
||||
clipboardCallback := func (clipboard io.Reader, err error) {
|
||||
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) {
|
||||
if err != nil {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindError,
|
||||
@@ -36,7 +70,8 @@ func run () {
|
||||
return
|
||||
}
|
||||
|
||||
if clipboard == nil {
|
||||
textData, ok := clipboard[data.MimePlain]
|
||||
if !ok {
|
||||
popups.NewDialog (
|
||||
popups.DialogKindError,
|
||||
window,
|
||||
@@ -45,24 +80,40 @@ func run () {
|
||||
return
|
||||
}
|
||||
|
||||
text, _ := io.ReadAll(clipboard)
|
||||
tomo.Do (func () {
|
||||
textInput.SetValue(string(text))
|
||||
})
|
||||
text, _ := io.ReadAll(textData)
|
||||
textInput.SetValue(string(text))
|
||||
}
|
||||
copyButton.OnClick (func () {
|
||||
window.Copy(data.Text(textInput.Value()))
|
||||
})
|
||||
pasteButton.OnClick (func () {
|
||||
window.Paste(data.MimePlain, clipboardCallback)
|
||||
window.Paste(clipboardCallback, data.MimePlain)
|
||||
})
|
||||
pasteImageButton.OnClick (func () {
|
||||
window.Paste(imageClipboardCallback, data.M("image", "png"))
|
||||
})
|
||||
|
||||
container.Adopt(textInput, true)
|
||||
controlRow.Adopt(copyButton, true)
|
||||
controlRow.Adopt(pasteButton, true)
|
||||
controlRow.Adopt(pasteImageButton, true)
|
||||
container.Adopt(controlRow, false)
|
||||
window.Adopt(container)
|
||||
|
||||
window.OnClose(tomo.Stop)
|
||||
window.Show()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user