- Unify clipboard
This commit is contained in:
43
Zframework/clipboard.lua
Normal file
43
Zframework/clipboard.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
if SYSTEM~='Web' then
|
||||
return {
|
||||
get=function () return CLIPBOARD.get() or '' end,
|
||||
set=love.system.setClipboardText,
|
||||
_update=NULL,
|
||||
}
|
||||
end
|
||||
|
||||
local clipboard_thread=love.thread.newThread('Zframework/clipboard_thread.lua')
|
||||
local getCHN=love.thread.newChannel()
|
||||
local setCHN=love.thread.newChannel()
|
||||
local triggerCHN=love.thread.newChannel()
|
||||
|
||||
clipboard_thread:start(getCHN,setCHN,triggerCHN)
|
||||
|
||||
local clipboard={}
|
||||
|
||||
function clipboard.get()
|
||||
return getCHN:peek()
|
||||
end
|
||||
|
||||
function clipboard.set(content)
|
||||
if type(content)=='boolean' then
|
||||
content=content and 'true' or 'false'
|
||||
end
|
||||
if type(content)=='nil' then
|
||||
content=''
|
||||
end
|
||||
if type(content)=='number' then
|
||||
content=tostring(content)
|
||||
end
|
||||
if type(content)~='string' then
|
||||
MES.new('error',"Invalid content type!")
|
||||
MES.traceback()
|
||||
end
|
||||
setCHN:push(content)
|
||||
end
|
||||
|
||||
function clipboard._update()
|
||||
triggerCHN:push(0)
|
||||
end
|
||||
|
||||
return clipboard
|
||||
65
Zframework/clipboard_thread.lua
Normal file
65
Zframework/clipboard_thread.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
local getCHN,setCHN,triggerCHN=...
|
||||
|
||||
local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount
|
||||
local CHN_push,CHN_pop=triggerCHN.push,triggerCHN.pop
|
||||
|
||||
local yield=coroutine.yield
|
||||
local setThread=coroutine.wrap(function()
|
||||
while true do
|
||||
JS.callJS(JS.stringFunc(
|
||||
[[
|
||||
window.navigator.clipboard
|
||||
.writeText('%s')
|
||||
.then(() => console.log('Copied to clipboard'))
|
||||
.catch((e) => console.warn(e));
|
||||
]],
|
||||
CHN_pop(setCHN)
|
||||
))
|
||||
yield()
|
||||
end
|
||||
end)
|
||||
|
||||
local getThread=coroutine.wrap(function()
|
||||
while true do
|
||||
JS.newPromiseRequest(
|
||||
JS.stringFunc(
|
||||
[[
|
||||
window.navigator.clipboard
|
||||
.readText()
|
||||
.then((text) => _$_(text))
|
||||
.catch((e) => {
|
||||
console.warn(e);
|
||||
_$_('');
|
||||
});
|
||||
]]
|
||||
),
|
||||
function(data)
|
||||
while getCHN:getCount()>0 do
|
||||
CHN_pop(getCHN)
|
||||
end
|
||||
CHN_push(getCHN, data)
|
||||
end,
|
||||
function(id, error) print(id, error) end,
|
||||
2,
|
||||
'getClipboardText'
|
||||
)
|
||||
yield()
|
||||
end
|
||||
end)
|
||||
|
||||
local success,err
|
||||
|
||||
while true do-- Running
|
||||
CHN_demand(triggerCHN)
|
||||
if CHN_getCount(setCHN)>0 then
|
||||
while CHN_getCount(setCHN)>1 do
|
||||
CHN_pop(setCHN)
|
||||
end
|
||||
print('Running setThread')
|
||||
setThread()
|
||||
end
|
||||
print('Running getThread')
|
||||
getThread()
|
||||
end
|
||||
|
||||
error()
|
||||
@@ -72,6 +72,7 @@ do
|
||||
end
|
||||
|
||||
-- Love-based modules (basic)
|
||||
CLIPBOARD= require'Zframework.clipboard'
|
||||
HTTP= require'Zframework.http'
|
||||
WS= require'Zframework.websocket'
|
||||
FILE= require'Zframework.file'
|
||||
@@ -179,49 +180,6 @@ local function updatePowerInfo()
|
||||
gc.setCanvas()
|
||||
end
|
||||
|
||||
if JS then
|
||||
JS.callJS(JS.stringFunc(
|
||||
[[
|
||||
console.log("Love.js Api Player initialized: Techmino %s");
|
||||
]],
|
||||
VERSION.string
|
||||
))
|
||||
|
||||
love.system.setClipboardText = function (str)
|
||||
JS.callJS(JS.stringFunc(
|
||||
[[
|
||||
window.navigator.clipboard
|
||||
.writeText('%s')
|
||||
.then(() => console.log('Copied to clipboard'))
|
||||
.catch((e) => console.warn(e));
|
||||
]],
|
||||
str
|
||||
))
|
||||
end
|
||||
|
||||
local _clipboardBuffer=''
|
||||
love.system.getClipboardText = function ()
|
||||
JS.newPromiseRequest(
|
||||
JS.stringFunc(
|
||||
[[
|
||||
window.navigator.clipboard
|
||||
.readText()
|
||||
.then((text) => _$_(text))
|
||||
.catch((e) => {
|
||||
console.warn(e);
|
||||
_$_('');
|
||||
});
|
||||
]]
|
||||
),
|
||||
function(data) _clipboardBuffer=data end,
|
||||
function(id, error) print(id, error) end,
|
||||
3,
|
||||
'getClipboardText'
|
||||
)
|
||||
return _clipboardBuffer
|
||||
end
|
||||
end
|
||||
|
||||
-------------------------------------------------------------
|
||||
local lastX,lastY=0,0-- Last click pos
|
||||
local function _updateMousePos(x,y,dx,dy)
|
||||
@@ -768,7 +726,10 @@ function love.run()
|
||||
|
||||
-- UPDATE
|
||||
STEP()
|
||||
if JS then JS.retrieveData(dt) end
|
||||
if JS then
|
||||
JS.retrieveData(dt)
|
||||
CLIPBOARD._update()
|
||||
end
|
||||
if mouseShow then mouse_update(dt) end
|
||||
if next(jsState) then gp_update(jsState[1],dt) end
|
||||
VOC.update()
|
||||
|
||||
@@ -140,7 +140,7 @@ function profile.switch()
|
||||
switch=not switch
|
||||
if not switch then
|
||||
profile.stop()
|
||||
love.system.setClipboardText(profile.report())
|
||||
CLIPBOARD.set(profile.report())
|
||||
profile.reset()
|
||||
return false
|
||||
else
|
||||
|
||||
@@ -189,7 +189,7 @@ do-- functions to shorted big numbers
|
||||
function STRING.bigInt(t)
|
||||
if t<1000 then
|
||||
return tostring(t)
|
||||
elseif t~=1e999 then
|
||||
elseif t~=1/0 then
|
||||
local e=floorint(lg(t)/3)
|
||||
return(t/10^(e*3))..units[e+1]
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user