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

paste should work on webgl for all input fields #1124

Merged
merged 9 commits into from
Sep 5, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4931,7 +4931,6 @@ GameObject:
- component: {fileID: 4029780974178710413}
- component: {fileID: 7234845548737740600}
- component: {fileID: 804371200852392299}
- component: {fileID: 6274013656548490657}
m_Layer: 0
m_Name: Web3AuthWalletGUI
m_TagString: Untagged
Expand Down Expand Up @@ -5112,18 +5111,6 @@ MonoBehaviour:
holdToRevealPrivateKeyButton: {fileID: 198969248047024504}
circleLoadingImage: {fileID: 1647118626164522998}
holdDuration: 2
--- !u!114 &6274013656548490657
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2911816339710442984}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 15ddd9b23c8d92f4db529a02cc14ecab, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &2991694514551052582
GameObject:
m_ObjectHideFlags: 0
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using ChainSafe.Gaming.InProcessSigner;
using ChainSafe.Gaming;
using ChainSafe.Gaming.UnityPackage;
using ChainSafe.GamingSdk.Web3Auth;
using Nethereum.Web3.Accounts;
Expand Down Expand Up @@ -115,7 +116,7 @@ private void CheckWalletToggleKeyInput()
/// </summary>
private void CopyWalletAddress()
{
Web3AuthWalletGUIClipboardManager.CopyText(walletAddressText.text);
ClipboardManager.CopyText(walletAddressText.text);
}

/// <summary>
Expand Down Expand Up @@ -205,7 +206,7 @@ private void SetPrivateKey()
/// </summary>
private void CopyPrivateKey()
{
Web3AuthWalletGUIClipboardManager.CopyText(privateKeyText.text);
ClipboardManager.CopyText(privateKeyText.text);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ public Task OnWeb3Initialized(Web3 web3)
{
if (enableWalletGui)
{
// TODO pass web3 instance here instead of using web3accessor
_web3AuthWalletGui = Instantiate(web3AuthWalletGUIPrefab);
_web3AuthWalletGui.Initialize(walletGuiConfig);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
mergeInto(LibraryManager.library, {
CopyToClipboard: function (text) {
var str = UTF8ToString(text);
navigator.clipboard.writeText(str).then(function () {
console.log('Text copied to clipboard: ' + str);
}).catch(function (error) {
console.error('Failed to copy text: ', error);
});
var ClipboardManager = {
$ClipboardManager: {},

SetPasteCallback: function (callback) {
ClipboardManager.callback = callback; // Use ClipboardManager instead of this.ClipboardManager
},

PasteFromClipboard: function () {
navigator.clipboard.readText().then(
clipText => {
web3UnityInstance.SendMessage('Web3AuthWalletGUI(Clone)', 'OnPasteWebGL', clipText);
Module.dynCall_vi(ClipboardManager.callback, stringToNewUTF8(clipText));
}
).catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
}
});
},

CopyToClipboard: function (text) {
var str = UTF8ToString(text);
navigator.clipboard.writeText(str).then(function () {
}).catch(function (error) {
console.error('Failed to copy text: ', error);
});
},
};

autoAddDeps(ClipboardManager, '$ClipboardManager');
mergeInto(LibraryManager.library, ClipboardManager);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#import <UIKit/UIKit.h>

// Define a function pointer for the callback
static void (*clipboardCallback)(const char* text) = NULL;

// Set the callback function that will be called when pasting from clipboard
void SetPasteCallback(void (*callback)(const char* text)) {
clipboardCallback = callback;
}

// Copy text to the clipboard
void CopyToClipboard(const char* text) {
NSString *textToCopy = [NSString stringWithUTF8String:text];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = textToCopy;
}

// Paste from the clipboard and call the callback
void PasteFromClipboard() {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *pastedText = pasteboard.string;

// Call the callback function if it is set
if (clipboardCallback != NULL) {
clipboardCallback([pastedText UTF8String]);
}
}
110 changes: 110 additions & 0 deletions Packages/io.chainsafe.web3-unity/Runtime/Scripts/ClipboardManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Runtime.InteropServices;
using AOT;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Scripting;

namespace ChainSafe.Gaming
{
public class ClipboardManager : MonoBehaviour
{
private static IClipboardHandler _clipboardHandler;

#if (UNITY_WEBGL || UNITY_IOS) && !UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static void CreateGameObject()
{
var go = new GameObject("ClipboardManager", typeof(ClipboardManager));
DontDestroyOnLoad(go);
}
#endif
private void Awake()
{
InitializeClipboardHandler();
_clipboardHandler?.SetTextPasteCallback(OnPaste);
}

private void InitializeClipboardHandler()
{
#if (UNITY_WEBGL || UNITY_IOS) && !UNITY_EDITOR
_clipboardHandler = new ClipboardHandler();
#else
_clipboardHandler = null;
#endif
}

private void Update()
{
#if ENABLE_INPUT_SYSTEM
if ((Keyboard.current.leftCtrlKey.isPressed || Keyboard.current.leftCommandKey.isPressed) && Keyboard.current.vKey.wasPressedThisFrame)
{
_clipboardHandler?.Paste();
}
#else
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) && Input.GetKeyDown(KeyCode.V))
{
_clipboardHandler?.Paste();
}
#endif
}


[MonoPInvokeCallback(typeof(Action))]
public static void OnPaste(string text)
{
var currentGo = EventSystem.current?.currentSelectedGameObject;
if (currentGo != null &&
currentGo.TryGetComponent(out TMP_InputField inputField))
inputField.text = text;
}

public static void CopyText(string text)
{
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
_clipboardHandler?.CopyTextToClipboard(text);
#else
GUIUtility.systemCopyBuffer = text;
#endif
}
}

public delegate void ClipboardPasted(string text);

public interface IClipboardHandler
{
void CopyTextToClipboard(string text);
void SetTextPasteCallback(ClipboardPasted callback);
void Paste();
}
#if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
public class ClipboardHandler : IClipboardHandler
{
[DllImport("__Internal")]
private static extern void SetPasteCallback(ClipboardPasted clipboardPasted);

[DllImport("__Internal")]
private static extern void PasteFromClipboard();

[DllImport("__Internal")]
private static extern void CopyToClipboard(string text);

public void CopyTextToClipboard(string text)
{
CopyToClipboard(text);
}

public void SetTextPasteCallback(ClipboardPasted callback)
{
SetPasteCallback(callback);
}

public void Paste()
{
PasteFromClipboard();
}

}
#endif
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading