FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/clipboard.py at main · andevsr/pythonnative · GitHub
andevsr
pythonnative
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
clipboard.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
143 lines (110 loc) · 3.69 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
clipboard.py
Copy path
File metadata and controls
143 lines (110 loc) · 3.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Cross-platform clipboard access.
[`Clipboard`][pythonnative.Clipboard] reads and writes the system
pasteboard. All methods are synchronous: ``UIPasteboard`` (iOS) and
``ClipboardManager`` (Android) both answer immediately, so there's no
need for a coroutine.
On a desktop machine (neither Android nor iOS) the module falls back to
a process-local string buffer. That keeps it usable in the desktop
mock target and unit tests instead of raising.
Example:
```python
import pythonnative as pn
pn.Clipboard.set_string("hello")
assert pn.Clipboard.get_string() == "hello"
```
"""
from
__future__
import
annotations
from
typing
import
Optional
from
..
import
diagnostics
from
..
utils
import
IS_ANDROID
,
IS_IOS
# Desktop fallback buffer so the API is usable off-device.
_desktop_buffer
:
str
=
""
class
Clipboard
:
"""System clipboard interface (synchronous)."""
@
staticmethod
def
set_string
(
text
:
str
)
->
None
:
"""Copy ``text`` onto the system clipboard."""
global
_desktop_buffer
value
=
""
if
text
is
None
else
str
(
text
)
if
IS_ANDROID
:
_android_set
(
value
)
elif
IS_IOS
:
_ios_set
(
value
)
else
:
_desktop_buffer
=
value
@
staticmethod
def
get_string
()
->
str
:
"""Return the current clipboard string (``""`` when empty)."""
if
IS_ANDROID
:
return
_android_get
()
if
IS_IOS
:
return
_ios_get
()
return
_desktop_buffer
@
staticmethod
def
has_string
()
->
bool
:
"""Return ``True`` when the clipboard holds non-empty text."""
return
bool
(
Clipboard
.
get_string
())
# ======================================================================
# iOS: UIPasteboard
# ======================================================================
def
_ios_pasteboard
()
->
Optional
[
object
]:
try
:
from
rubicon
.
objc
import
ObjCClass
return
ObjCClass
(
"UIPasteboard"
).
generalPasteboard
except
Exception
:
return
None
def
_ios_set
(
text
:
str
)
->
None
:
pb
=
_ios_pasteboard
()
if
pb
is
None
:
return
try
:
pb
.
string
=
text
except
Exception
:
diagnostics
.
swallowed
(
"clipboard._ios_set"
)
def
_ios_get
()
->
str
:
pb
=
_ios_pasteboard
()
if
pb
is
None
:
return
""
try
:
value
=
pb
.
string
return
str
(
value
)
if
value
is
not
None
else
""
except
Exception
:
return
""
# ======================================================================
# Android: ClipboardManager
# ======================================================================
def
_android_manager
()
->
Optional
[
object
]:
try
:
from
java
import
jclass
from
..
utils
import
get_android_context
ctx
=
get_android_context
()
Context
=
jclass
(
"android.content.Context"
)
return
ctx
.
getSystemService
(
Context
.
CLIPBOARD_SERVICE
)
except
Exception
:
return
None
def
_android_set
(
text
:
str
)
->
None
:
manager
=
_android_manager
()
if
manager
is
None
:
return
try
:
from
java
import
jclass
ClipData
=
jclass
(
"android.content.ClipData"
)
clip
=
ClipData
.
newPlainText
(
"pythonnative"
,
text
)
manager
.
setPrimaryClip
(
clip
)
except
Exception
:
diagnostics
.
swallowed
(
"clipboard._android_set"
)
def
_android_get
()
->
str
:
manager
=
_android_manager
()
if
manager
is
None
:
return
""
try
:
if
not
manager
.
hasPrimaryClip
():
return
""
clip
=
manager
.
getPrimaryClip
()
if
clip
is
None
or
clip
.
getItemCount
()
==
0
:
return
""
item
=
clip
.
getItemAt
(
0
)
text
=
item
.
getText
()
return
str
(
text
)
if
text
is
not
None
else
""
except
Exception
:
return
""
Back
|
FazBrowse Home
|
New Git URL