diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 9ee452ba289..24f1d67e570 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -222,6 +222,10 @@ String _OS::get_clipboard() const { return OS::get_singleton()->get_clipboard(); } +bool _OS::has_clipboard() const { + return OS::get_singleton()->has_clipboard(); +} + int _OS::get_video_driver_count() const { return OS::get_singleton()->get_video_driver_count(); } @@ -1227,6 +1231,7 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("set_clipboard", "clipboard"), &_OS::set_clipboard); ClassDB::bind_method(D_METHOD("get_clipboard"), &_OS::get_clipboard); + ClassDB::bind_method(D_METHOD("has_clipboard"), &_OS::has_clipboard); //will not delete for now, just unexpose //ClassDB::bind_method(D_METHOD("set_video_mode","size","fullscreen","resizable","screen"),&_OS::set_video_mode,DEFVAL(0)); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index d2dcd570c8d..7e9b70be900 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -164,6 +164,7 @@ public: void set_clipboard(const String &p_text); String get_clipboard() const; + bool has_clipboard() const; void set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen = 0); Size2 get_video_mode(int p_screen = 0) const; diff --git a/core/os/os.cpp b/core/os/os.cpp index bdac38412c6..35e3b64e850 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -157,10 +157,15 @@ int OS::get_low_processor_usage_mode_sleep_usec() const { void OS::set_clipboard(const String &p_text) { _local_clipboard = p_text; } + String OS::get_clipboard() const { return _local_clipboard; } +bool OS::has_clipboard() const { + return !get_clipboard().empty(); +} + String OS::get_executable_path() const { return _execpath; } diff --git a/core/os/os.h b/core/os/os.h index e44710b452d..bc33c27829d 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -176,6 +176,7 @@ public: virtual void set_clipboard(const String &p_text); virtual String get_clipboard() const; + virtual bool has_clipboard() const; virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0) = 0; virtual VideoMode get_video_mode(int p_screen = 0) const = 0; diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index 7363232c18d..85801f4af94 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -604,6 +604,12 @@ [b]Note:[/b] This method is implemented on macOS. + + + + Returns [code]true[/code] if there is content on the clipboard. + + diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.java b/platform/android/java/lib/src/org/godotengine/godot/Godot.java index 870d60d118a..4c1ae6f19d3 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.java +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.java @@ -786,10 +786,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(); } diff --git a/platform/android/java_godot_wrapper.cpp b/platform/android/java_godot_wrapper.cpp index 2a0943f0afb..943a8d67cc7 100644 --- a/platform/android/java_godot_wrapper.cpp +++ b/platform/android/java_godot_wrapper.cpp @@ -69,6 +69,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;"); @@ -270,6 +271,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(); diff --git a/platform/android/java_godot_wrapper.h b/platform/android/java_godot_wrapper.h index a790559cadb..47071300ded 100644 --- a/platform/android/java_godot_wrapper.h +++ b/platform/android/java_godot_wrapper.h @@ -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; @@ -95,6 +96,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 get_granted_permissions() const; diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index eb0d5e600cd..6d48dadedae 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -423,6 +423,15 @@ String OS_Android::get_clipboard() const { return OS_Unix::get_clipboard(); } +bool OS_Android::has_clipboard() const { + // DO we really need the fallback to OS_Unix here?! + if (godot_java->has_has_clipboard()) { + return godot_java->has_clipboard(); + } + + return OS_Unix::has_clipboard(); +} + String OS_Android::get_model_name() const { String model = godot_io_java->get_model(); if (model != "") diff --git a/platform/android/os_android.h b/platform/android/os_android.h index 90cb3bf487e..6a6a529478a 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -151,6 +151,7 @@ public: virtual String get_locale() const; virtual void set_clipboard(const String &p_text); virtual String get_clipboard() const; + virtual bool has_clipboard() const; virtual String get_model_name() const; virtual int get_screen_dpi(int p_screen = 0) const;