~eliasnaur/gio

ref: f24232dac52e gio/io/router/clipboard.go -rw-r--r-- 1.5 KiB
f24232daElias Naur app/internal/wm: [X11] ignore zero-sized FrameEvents a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: Unlicense OR MIT

package router

import (
	"gioui.org/internal/opconst"
	"gioui.org/internal/ops"
	"gioui.org/io/event"
)

type clipboardQueue struct {
	receivers map[event.Tag]struct{}
	// request avoid read clipboard every frame while waiting.
	requested bool
	text      *string
	reader    ops.Reader
}

// WriteClipboard returns the most recent text to be copied
// to the clipboard, if any.
func (q *clipboardQueue) WriteClipboard() (string, bool) {
	if q.text == nil {
		return "", false
	}
	text := *q.text
	q.text = nil
	return text, true
}

// ReadClipboard reports if any new handler is waiting
// to read the clipboard.
func (q *clipboardQueue) ReadClipboard() bool {
	if len(q.receivers) <= 0 || q.requested {
		return false
	}
	q.requested = true
	return true
}

func (q *clipboardQueue) Push(e event.Event, events *handlerEvents) {
	for r := range q.receivers {
		events.Add(r, e)
		delete(q.receivers, r)
	}
}

func (q *clipboardQueue) ProcessWriteClipboard(d []byte, refs []interface{}) {
	if opconst.OpType(d[0]) != opconst.TypeClipboardWrite {
		panic("invalid op")
	}
	q.text = refs[0].(*string)
}

func (q *clipboardQueue) ProcessReadClipboard(d []byte, refs []interface{}) {
	if opconst.OpType(d[0]) != opconst.TypeClipboardRead {
		panic("invalid op")
	}
	if q.receivers == nil {
		q.receivers = make(map[event.Tag]struct{})
	}
	tag := refs[0].(event.Tag)
	if _, ok := q.receivers[tag]; !ok {
		q.receivers[tag] = struct{}{}
		q.requested = false
	}
}