You can fcucking PASTE now!!!

This commit is contained in:
Sasha Koshka 2023-03-27 20:44:39 -04:00
parent 02a27447b9
commit 01a0fc1bd3
8 changed files with 210 additions and 34 deletions

View File

@ -1,7 +1,6 @@
package tomo
import "errors"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/elements"
@ -24,12 +23,6 @@ type Backend interface {
// and returns a struct representing it that fulfills the MainWindow
// interface.
NewWindow (width, height int) (window elements.MainWindow, err error)
// Copy puts data into the clipboard.
Copy (data.Data)
// Paste returns the data currently in the clipboard.
Paste (accept []data.Mime) (data.Data)
// SetTheme sets the theme of all open windows.
SetTheme (theme.Theme)

View File

@ -1,11 +1,14 @@
package x
import "bytes"
import "image"
import "errors"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "github.com/jezek/xgbutil"
import "github.com/jezek/xgb/xproto"
import "github.com/jezek/xgbutil/xprop"
import "github.com/jezek/xgbutil/xevent"
type scrollSum struct {
@ -235,6 +238,54 @@ func (window *window) handleMotionNotify (
}
}
func (window *window) handleSelectionNotify (
connection *xgbutil.XUtil,
event xevent.SelectionNotifyEvent,
) {
// Follow:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.4
if window.selectionRequest == nil { return }
die := func (err error) { window.selectionRequest(nil, err) }
// When using GetProperty to retrieve the value of a selection, the
// property argument should be set to the corresponding value in the
// SelectionNotify event. Because the requestor has no way of knowing
// beforehand what type the selection owner will use, the type argument
// should be set to AnyPropertyType. Several GetProperty requests may be
// needed to retrieve all the data in the selection; each should set the
// long-offset argument to the amount of data received so far, and the
// size argument to some reasonable buffer size (see section 2.5). If
// the returned value of bytes-after is zero, the whole property has
// been transferred.
reply, err := xproto.GetProperty (
connection.Conn(), false, window.xWindow.Id, event.Property,
xproto.GetPropertyTypeAny, 0, (1 << 32) - 1).Reply()
if err != nil { die(err); return }
if reply.Format == 0 {
die(errors.New("x: missing selection property"))
return
}
// Once all the data in the selection has been retrieved (which may
// require getting the values of several properties &emdash; see section
// 2.7), the requestor should delete the property in the SelectionNotify
// request by using a GetProperty request with the delete argument set
// to True. As previously discussed, the owner has no way of knowing
// when the data has been transferred to the requestor unless the
// property is removed.
propertyAtom, err := xprop.Atm(window.backend.connection, "TOMO_SELECTION")
if err != nil { die(err); return }
err = xproto.DeletePropertyChecked (
window.backend.connection.Conn(),
window.xWindow.Id,
propertyAtom).Check()
if err != nil { die(err); return }
// TODO: possibly do some conversion here?
window.selectionRequest(bytes.NewReader(reply.Value), nil)
window.selectionRequest = nil
}
func (window *window) compressExpose (
firstEvent xproto.ExposeEvent,
) (

View File

@ -1,12 +1,16 @@
package x
import "io"
import "image"
import "errors"
import "github.com/jezek/xgb/xproto"
import "github.com/jezek/xgbutil/ewmh"
import "github.com/jezek/xgbutil/icccm"
import "github.com/jezek/xgbutil/xprop"
import "github.com/jezek/xgbutil/xevent"
import "github.com/jezek/xgbutil/xwindow"
import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/sashakoshka/tomo/data"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
@ -30,6 +34,8 @@ type window struct {
theme theme.Theme
config config.Config
selectionRequest func (io.Reader, error)
metrics struct {
width int
height int
@ -89,6 +95,8 @@ func (backend *Backend) newWindow (
Connect(backend.connection, window.xWindow.Id)
xevent.MotionNotifyFun(window.handleMotionNotify).
Connect(backend.connection, window.xWindow.Id)
xevent.SelectionNotifyFun(window.handleSelectionNotify).
Connect(backend.connection, window.xWindow.Id)
window.SetTheme(backend.theme)
window.SetConfig(backend.config)
@ -246,7 +254,6 @@ func (window *window) setType (ty string) error {
}
func (window *window) setClientLeader (leader *window) error {
// FIXME: doe not fucking work
hints, _ := icccm.WmHintsGet(window.backend.connection, window.xWindow.Id)
if hints == nil {
hints = &icccm.Hints { }
@ -275,6 +282,77 @@ func (window *window) Hide () {
window.xWindow.Unmap()
}
func (window *window) Copy (data data.Data) {
// TODO
}
func (window *window) Paste (accept data.Mime, callback func (io.Reader, error)) {
// Follow:
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.4
die := func (err error) { callback(nil, err) }
if window.selectionRequest != nil {
// TODO: add the request to a queue and take care of it when the
// current selection has completed
die(errors.New("there is already a selection request"))
}
selectionName := "CLIPBOARD"
propertyName := "TOMO_SELECTION"
// TODO: change based on mime type
targetName := "TEXT"
// get atoms
selectionAtom, err := xprop.Atm(window.backend.connection, selectionName)
if err != nil { die(err); return }
targetAtom, err := xprop.Atm(window.backend.connection, targetName)
if err != nil { die(err); return }
propertyAtom, err := xprop.Atm(window.backend.connection, propertyName)
if err != nil { die(err); return }
// The requestor should set the property argument to the name of a
// property that the owner can use to report the value of the selection.
// Requestors should ensure that the named property does not exist on
// the window before issuing the ConvertSelection. The exception to this
// rule is when the requestor intends to pass parameters with the
// request. Some targets may be defined such that requestors can pass
// parameters along with the request. If the requestor wishes to provide
// parameters to a request, they should be placed in the specified
// property on the requestor window before the requestor issues the
// ConvertSelection request, and this property should be named in the
// request.
err = xproto.DeletePropertyChecked (
window.backend.connection.Conn(),
window.xWindow.Id,
propertyAtom).Check()
if err != nil { die(err); return }
// The selection argument specifies the particular selection involved,
// and the target argument specifies the required form of the
// information. For information about the choice of suitable atoms to
// use, see section 2.6. The requestor should set the requestor argument
// to a window that it created; the owner will place the reply property
// there. The requestor should set the time argument to the timestamp on
// the event that triggered the request for the selection value. Note
// that clients should not specify CurrentTime*.
err = xproto.ConvertSelectionChecked (
window.backend.connection.Conn(),
window.xWindow.Id,
selectionAtom,
targetAtom,
propertyAtom,
// TODO: *possibly replace this zero with an actual timestamp
// received from the server. this is non-trivial as we cannot
// rely on the timestamp of the last received event, because
// there is a possibility that this method is invoked
// asynchronously from within tomo.Do().
0).Check()
if err != nil { die(err); return }
window.selectionRequest = callback
return
}
func (window *window) Close () {
if window.onClose != nil { window.onClose() }
if window.modalParent != nil {

View File

@ -1,7 +1,6 @@
package x
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/config"
@ -39,7 +38,7 @@ type Backend struct {
func NewBackend () (output tomo.Backend, err error) {
backend := &Backend {
windows: map[xproto.Window] *window { },
doChannel: make(chan func (), 0),
doChannel: make(chan func (), 32),
theme: theme.Default { },
config: config.Default { },
open: true,
@ -97,22 +96,6 @@ func (backend *Backend) Do (callback func ()) {
backend.doChannel <- callback
}
// Copy puts data into the clipboard. This method is not yet implemented and
// will do nothing!
func (backend *Backend) Copy (data data.Data) {
backend.assert()
// TODO
}
// Paste returns the data currently in the clipboard. This method may
// return nil. This method is not yet implemented and will do nothing!
func (backend *Backend) Paste (accept []data.Mime) (data data.Data) {
backend.assert()
// TODO
return
}
// SetTheme sets the theme of all open windows.
func (backend *Backend) SetTheme (theme theme.Theme) {
backend.assert()

View File

@ -1,6 +1,7 @@
package data
import "io"
import "bytes"
// Data represents arbitrary polymorphic data that can be used for data transfer
// between applications.
@ -18,3 +19,13 @@ type Mime struct {
var MimePlain = Mime { "text", "plain" }
var MimeFile = Mime { "text", "uri-list" }
type byteReadCloser struct { *bytes.Reader }
func (byteReadCloser) Close () error { return nil }
// Text returns plain text Data given a string.
func Text (text string) Data {
return Data {
MimePlain: byteReadCloser { bytes.NewReader([]byte(text)) },
}
}

View File

@ -32,10 +32,10 @@ type Window interface {
Copy (data.Data)
// Paste requests the data currently in the clipboard. When the data is
// available, it is sent on the returned channel. If there is no data on
// the clipboard matching the requested mime type, the channel will be
// sent nil.
Paste (accept data.Mime) <- chan io.Reader
// available, the callback is called with the clipboard data. If there
// was no data matching the requested mime type found, nil is passed to
// the callback instead.
Paste (accept data.Mime, callback func (io.Reader, error))
// Show shows the window. The window starts off hidden, so this must be
// called after initial setup to make sure it is visible.

View File

@ -0,0 +1,60 @@
package main
import "io"
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 () {
window, _ := tomo.NewWindow(2, 2)
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)
clipboardCallback := func (clipboard io.Reader, err error) {
if err != nil {
popups.NewDialog (
popups.DialogKindError,
window,
"Error",
"No text data in clipboard:\n" + err.Error())
return
}
text, _ := io.ReadAll(clipboard)
tomo.Do (func () {
textInput.SetValue(string(text))
})
}
copyButton.OnClick (func () {
window.Copy(data.Text(textInput.Value()))
})
pasteButton.OnClick (func () {
window.Paste(data.MimePlain, clipboardCallback)
})
container.Adopt(textInput, true)
controlRow.Adopt(copyButton, true)
controlRow.Adopt(pasteButton, true)
container.Adopt(controlRow, false)
window.Adopt(container)
window.OnClose(tomo.Stop)
window.Show()
}

View File

@ -13,7 +13,7 @@ func main () {
}
func run () {
window, _ := tomo.NewWindow(2, 2)
window, _ := tomo.NewWindow(256, 256)
window.SetTitle("Main")
container := containers.NewContainer(basicLayouts.Vertical { true, true })
@ -24,9 +24,9 @@ func run () {
window.Show()
createPanel(window, 0)
// createPanel(window, 1)
// createPanel(window, 2)
// createPanel(window, 3)
createPanel(window, 1)
createPanel(window, 2)
createPanel(window, 3)
}
func createPanel (parent elements.MainWindow, id int) {