Add `DisplayServer.clipboard_has()` to check clipboard content

This commit is contained in:
Haoyu Qiu 2022-01-19 21:04:52 +08:00
parent 8b8e858778
commit 314f309035
8 changed files with 48 additions and 1 deletions

View File

@ -20,6 +20,12 @@
[b]Note:[/b] This method is only implemented on Linux.
</description>
</method>
<method name="clipboard_has" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if there is content on the user's clipboard.
</description>
</method>
<method name="clipboard_set">
<return type="void" />
<argument index="0" name="clipboard" type="String" />

View File

@ -95,6 +95,17 @@ String DisplayServerAndroid::clipboard_get() const {
}
}
bool DisplayServerAndroid::clipboard_has() const {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_COND_V(!godot_java, false);
if (godot_java->has_has_clipboard()) {
return godot_java->has_clipboard();
} else {
return DisplayServer::clipboard_has();
}
}
void DisplayServerAndroid::screen_set_keep_on(bool p_enable) {
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
ERR_FAIL_COND(!godot_java);

View File

@ -93,6 +93,7 @@ public:
virtual void clipboard_set(const String &p_text) override;
virtual String clipboard_get() const override;
virtual bool clipboard_has() const override;
virtual void screen_set_keep_on(bool p_enable) override;
virtual bool screen_is_kept_on() const override;

View File

@ -660,10 +660,14 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
}
}
public boolean hasClipboard() {
return mClipboard.hasPrimaryClip();
}
public String getClipboard() {
String copiedText = "";
if (mClipboard.getPrimaryClip() != null) {
if (mClipboard.hasPrimaryClip()) {
ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);
copiedText = item.getText().toString();
}

View File

@ -66,6 +66,7 @@ GodotJavaWrapper::GodotJavaWrapper(JNIEnv *p_env, jobject p_activity, jobject p_
_get_GLES_version_code = p_env->GetMethodID(godot_class, "getGLESVersionCode", "()I");
_get_clipboard = p_env->GetMethodID(godot_class, "getClipboard", "()Ljava/lang/String;");
_set_clipboard = p_env->GetMethodID(godot_class, "setClipboard", "(Ljava/lang/String;)V");
_has_clipboard = p_env->GetMethodID(godot_class, "hasClipboard", "()Z");
_request_permission = p_env->GetMethodID(godot_class, "requestPermission", "(Ljava/lang/String;)Z");
_request_permissions = p_env->GetMethodID(godot_class, "requestPermissions", "()Z");
_get_granted_permissions = p_env->GetMethodID(godot_class, "getGrantedPermissions", "()[Ljava/lang/String;");
@ -248,6 +249,21 @@ void GodotJavaWrapper::set_clipboard(const String &p_text) {
}
}
bool GodotJavaWrapper::has_has_clipboard() {
return _has_clipboard != 0;
}
bool GodotJavaWrapper::has_clipboard() {
if (_has_clipboard) {
JNIEnv *env = get_jni_env();
ERR_FAIL_COND_V(env == nullptr, false);
return env->CallBooleanMethod(godot_instance, _has_clipboard);
} else {
return false;
}
}
bool GodotJavaWrapper::request_permission(const String &p_name) {
if (_request_permission) {
JNIEnv *env = get_jni_env();

View File

@ -58,6 +58,7 @@ private:
jmethodID _get_GLES_version_code = 0;
jmethodID _get_clipboard = 0;
jmethodID _set_clipboard = 0;
jmethodID _has_clipboard = 0;
jmethodID _request_permission = 0;
jmethodID _request_permissions = 0;
jmethodID _get_granted_permissions = 0;
@ -92,6 +93,8 @@ public:
String get_clipboard();
bool has_set_clipboard();
void set_clipboard(const String &p_text);
bool has_has_clipboard();
bool has_clipboard();
bool request_permission(const String &p_name);
bool request_permissions();
Vector<String> get_granted_permissions() const;

View File

@ -155,6 +155,10 @@ String DisplayServer::clipboard_get() const {
ERR_FAIL_V_MSG(String(), "Clipboard is not supported by this display server.");
}
bool DisplayServer::clipboard_has() const {
return !clipboard_get().is_empty();
}
void DisplayServer::clipboard_set_primary(const String &p_text) {
WARN_PRINT("Primary clipboard is not supported by this display server.");
}
@ -359,6 +363,7 @@ void DisplayServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("clipboard_set", "clipboard"), &DisplayServer::clipboard_set);
ClassDB::bind_method(D_METHOD("clipboard_get"), &DisplayServer::clipboard_get);
ClassDB::bind_method(D_METHOD("clipboard_has"), &DisplayServer::clipboard_has);
ClassDB::bind_method(D_METHOD("clipboard_set_primary", "clipboard_primary"), &DisplayServer::clipboard_set_primary);
ClassDB::bind_method(D_METHOD("clipboard_get_primary"), &DisplayServer::clipboard_get_primary);

View File

@ -160,6 +160,7 @@ public:
virtual void clipboard_set(const String &p_text);
virtual String clipboard_get() const;
virtual bool clipboard_has() const;
virtual void clipboard_set_primary(const String &p_text);
virtual String clipboard_get_primary() const;