Bläddra i källkod

Add `OS.has_clipboard()` to check clipboard content

Haoyu Qiu 3 år sedan
förälder
incheckning
76297e744d

+ 5 - 0
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));

+ 1 - 0
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;

+ 5 - 0
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;
 }

+ 1 - 0
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;

+ 6 - 0
doc/classes/OS.xml

@@ -601,6 +601,12 @@
 				[b]Note:[/b] This method is implemented on macOS.
 			</description>
 		</method>
+		<method name="has_clipboard" qualifiers="const">
+			<return type="bool" />
+			<description>
+				Returns [code]true[/code] if there is content on the clipboard.
+			</description>
+		</method>
 		<method name="has_environment" qualifiers="const">
 			<return type="bool" />
 			<argument index="0" name="variable" type="String" />

+ 5 - 1
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();
 		}

+ 16 - 0
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();

+ 3 - 0
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<String> get_granted_permissions() const;

+ 9 - 0
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 != "")

+ 1 - 0
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;