Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add OS.has_clipboard() to check clipboard content #56950

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class _OS : public Object {

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;
Expand Down
5 changes: 5 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class OS {

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;
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,14 @@ public void onPause() {
}
}

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();
}
Expand Down
16 changes: 16 additions & 0 deletions platform/android/java_godot_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions platform/android/java_godot_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class GodotJavaWrapper {
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;
Expand Down Expand Up @@ -95,6 +96,8 @@ class GodotJavaWrapper {
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;
Expand Down
9 changes: 9 additions & 0 deletions platform/android/os_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "")
Expand Down
1 change: 1 addition & 0 deletions platform/android/os_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class OS_Android : public OS_Unix {
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;

Expand Down