github.com/Seikaijyu/gio@v0.0.1/io/router/clipboard.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package router
     4  
     5  import (
     6  	"github.com/Seikaijyu/gio/io/event"
     7  )
     8  
     9  type clipboardQueue struct {
    10  	receivers map[event.Tag]struct{}
    11  	// request avoid read clipboard every frame while waiting.
    12  	requested bool
    13  	text      *string
    14  }
    15  
    16  // WriteClipboard returns the most recent text to be copied
    17  // to the clipboard, if any.
    18  func (q *clipboardQueue) WriteClipboard() (string, bool) {
    19  	if q.text == nil {
    20  		return "", false
    21  	}
    22  	text := *q.text
    23  	q.text = nil
    24  	return text, true
    25  }
    26  
    27  // ReadClipboard reports if any new handler is waiting
    28  // to read the clipboard.
    29  func (q *clipboardQueue) ReadClipboard() bool {
    30  	if len(q.receivers) == 0 || q.requested {
    31  		return false
    32  	}
    33  	q.requested = true
    34  	return true
    35  }
    36  
    37  func (q *clipboardQueue) Push(e event.Event, events *handlerEvents) {
    38  	for r := range q.receivers {
    39  		events.Add(r, e)
    40  		delete(q.receivers, r)
    41  	}
    42  }
    43  
    44  func (q *clipboardQueue) ProcessWriteClipboard(refs []interface{}) {
    45  	q.text = refs[0].(*string)
    46  }
    47  
    48  func (q *clipboardQueue) ProcessReadClipboard(refs []interface{}) {
    49  	if q.receivers == nil {
    50  		q.receivers = make(map[event.Tag]struct{})
    51  	}
    52  	tag := refs[0].(event.Tag)
    53  	if _, ok := q.receivers[tag]; !ok {
    54  		q.receivers[tag] = struct{}{}
    55  		q.requested = false
    56  	}
    57  }