-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclipboard_session.go
More file actions
75 lines (63 loc) · 1.21 KB
/
clipboard_session.go
File metadata and controls
75 lines (63 loc) · 1.21 KB
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
67
68
69
70
71
72
73
74
75
package main
import (
"sync"
"time"
"zee/clipboard"
)
type clipboardSession struct {
mu sync.Mutex
restoreMu sync.Mutex
restoreCancel func()
textMu sync.Mutex
lastText string
}
var clip clipboardSession
func (c *clipboardSession) PasteText(text string) {
c.mu.Lock()
clipboard.Copy(text)
clipboard.Paste()
c.mu.Unlock()
}
func (c *clipboardSession) SaveCurrent() string {
prev, _ := clipboard.Read()
return prev
}
func (c *clipboardSession) CancelRestore() {
c.restoreMu.Lock()
if c.restoreCancel != nil {
c.restoreCancel()
c.restoreCancel = nil
}
c.restoreMu.Unlock()
}
func (c *clipboardSession) ScheduleRestore(prev string) {
if prev == "" {
return
}
cancelled := make(chan struct{})
c.restoreMu.Lock()
c.restoreCancel = func() { close(cancelled) }
c.restoreMu.Unlock()
go func() {
select {
case <-time.After(600 * time.Millisecond):
c.mu.Lock()
clipboard.Copy(prev)
c.mu.Unlock()
case <-cancelled:
}
}()
}
func (c *clipboardSession) CopyLast() {
c.textMu.Lock()
text := c.lastText
c.textMu.Unlock()
if text != "" {
clipboard.Copy(text)
}
}
func (c *clipboardSession) SetLastText(text string) {
c.textMu.Lock()
c.lastText = text
c.textMu.Unlock()
}