- Unify clipboard

This commit is contained in:
ParticleG
2024-11-01 08:00:01 +08:00
parent eb3b8c9e9d
commit b50147cace
19 changed files with 137 additions and 69 deletions

43
Zframework/clipboard.lua Normal file
View 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

View 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()

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -949,16 +949,16 @@ end
local combKey={
x=function()
love.system.setClipboardText(inputBox:getText())
CLIPBOARD.set(inputBox:getText())
inputBox:clear()
SFX.play('reach')
end,
c=function()
love.system.setClipboardText(inputBox:getText())
CLIPBOARD.set(inputBox:getText())
SFX.play('reach')
end,
v=function()
inputBox:addText(love.system.getClipboardText())
inputBox:addText(CLIPBOARD.get())
SFX.play('reach')
end,
}

View File

@@ -204,7 +204,7 @@ function scene.update(dt)
end
ct=ct-1
if ct==0 then
local t=love.system.getClipboardText()
local t=CLIPBOARD.get()
if type(t)=='string' then
t=t:lower():match("^s=(%d+)$")
t=t and tonumber(t) and tonumber(t)>0 and tonumber(t)<=8000 and floor(tonumber(t))

View File

@@ -181,7 +181,7 @@ function reset()
time=0
score=0
local t=love.system.getClipboardText()
local t=CLIPBOARD.get()
if type(t)=='string' then
t=t:lower():match("^s=(.+)")
t=t and tonumber(t) and tonumber(t)*2

View File

@@ -188,10 +188,10 @@ function scene.keyDown(key,isRep)
if #CUSTOMGAME_LOCAL.bag>0 then str=str..DATA.copySequence(CUSTOMGAME_LOCAL.bag) end
str=str.."!"
if #CUSTOMGAME_LOCAL.mission>0 then str=str..DATA.copyMission(CUSTOMGAME_LOCAL.mission) end
sys.setClipboardText(str.."!"..DATA.copyBoards(CUSTOMGAME_LOCAL.field).."!")
CLIPBOARD.set(str.."!"..DATA.copyBoards(CUSTOMGAME_LOCAL.field).."!")
MES.new('check',text.exportSuccess)
elseif key=='v' and kb.isDown('lctrl','rctrl') or key=='cV' then
local str=sys.getClipboardText()
local str=CLIPBOARD.get()
local args=str:sub((str:find(":") or 0)+1):split("!")
local hasTooHighField=false
repeat

View File

@@ -226,10 +226,10 @@ function scene.keyDown(key)
SFX.play('clear_4',.8)
SFX.play('fall',.8)
elseif key=='c' and kb.isDown('lctrl','rctrl') or key=='cC' then
sys.setClipboardText("Techmino Field:"..DATA.copyBoard(FIELD[page]))
CLIPBOARD.set("Techmino Field:"..DATA.copyBoard(FIELD[page]))
MES.new('check',text.exportSuccess)
elseif key=='v' and kb.isDown('lctrl','rctrl') or key=='cV' then
local str=sys.getClipboardText()
local str=CLIPBOARD.get()
local p=str:find(":")-- ptr*
if p then
if not str:sub(1,p-1):find("Field") then

View File

@@ -68,11 +68,11 @@ function scene.keyDown(key)
end
elseif key=='c' and kb.isDown('lctrl','rctrl') or key=='cC' then
if #MISSION>0 then
sys.setClipboardText("Techmino Target:"..DATA.copyMission(MISSION))
CLIPBOARD.set("Techmino Target:"..DATA.copyMission(MISSION))
MES.new('check',text.exportSuccess)
end
elseif key=='v' and kb.isDown('lctrl','rctrl') or key=='cV' then
local str=sys.getClipboardText()
local str=CLIPBOARD.get()
local p=str:find(":")-- ptr*
if p then
if not str:sub(1,p-1):find("Target") then

View File

@@ -1,4 +1,3 @@
local sys=love.system
local kb=love.keyboard
local sin=math.sin
@@ -76,11 +75,11 @@ function scene.keyDown(key)
scene.widgetList.sequence:scroll(kb.isDown('lshift','rshift') and -1 or 1)
elseif key=='c' and kb.isDown('lctrl','rctrl') or key=='cC' then
if #BAG>0 then
sys.setClipboardText("Techmino SEQ:"..DATA.copySequence(BAG))
CLIPBOARD.set("Techmino SEQ:"..DATA.copySequence(BAG))
MES.new('check',text.exportSuccess)
end
elseif key=='v' and kb.isDown('lctrl','rctrl') or key=='cV' then
local str=sys.getClipboardText()
local str=CLIPBOARD.get()
local p=str:find(":")-- ptr*
if p then
if not str:sub(1,p-1):find("SEQ") then

View File

@@ -124,7 +124,7 @@ end
local function _copy()
local t=_getList()[listBox.selected]
t=t.title_Org..":\n"..t.content_Org..(t.url and "\n[ "..t.url.." ]\n" or "\n")..text.dictNote
love.system.setClipboardText(t)
CLIPBOARD.set(t)
scene.widgetList.copy.hide=true
MES.new('info',text.copyDone)
end

View File

@@ -14,7 +14,7 @@ local function _submit()
end
end
local function _paste()
local t=love.system.getClipboardText()
local t=CLIPBOARD.get()
if t then
t=STRING.trim(t)
if #t==128 and t:match("[0-9A-Z]+") then

View File

@@ -97,7 +97,7 @@ function scene.keyDown(key)
if rep.available and rep.fileName then
local repStr=loadFile(rep.fileName,'-string')
if repStr then
love.system.setClipboardText(love.data.encode('string','base64',repStr))
CLIPBOARD.set(love.data.encode('string','base64',repStr))
MES.new('info',text.exportSuccess)
else
MES.new('error',text.replayBroken)
@@ -107,7 +107,7 @@ function scene.keyDown(key)
end
end
elseif key=='v' and kb.isDown('lctrl','rctrl') or key=='cV' then
local repStr=love.system.getClipboardText()
local repStr=CLIPBOARD.get()
local res,fileData=pcall(love.data.decode,'string','base64',repStr)
if res then
local fileName=os.date("replay/%Y_%m_%d_%H%M%S_import.rep")

View File

@@ -15,7 +15,7 @@ local function _setPW()
end
end
local function _paste()
local t=love.system.getClipboardText()
local t=CLIPBOARD.get()
if t then
t=STRING.trim(t)
if #t==8 and t:match("[0-9]+") then

View File

@@ -7,12 +7,12 @@ function scene.enter()
end
local function _dumpCB(T)
love.system.setClipboardText(STRING.packText(TABLE.dump(T)))
CLIPBOARD.set(STRING.packText(TABLE.dump(T)))
MES.new('check',text.exportSuccess)
end
local function _parseCB()
local _
local s=love.system.getClipboardText()
local s=CLIPBOARD.get()
-- Decode
s=STRING.unpackText(s)

View File

@@ -136,7 +136,7 @@ scene.widgetList={
},
WIDGET.newKey{name='bg_custom_base64',x=1010,y=1502.5,w=420,h=135,align='M',
code=function()
local okay,data=pcall(love.data.decode,"data","base64",love.system.getClipboardText())
local okay,data=pcall(love.data.decode,"data","base64",CLIPBOARD.get())
if okay and pcall(gc.newImage,data) then
love.filesystem.write('conf/customBG',data)
SETTING.bg='custom'

View File

@@ -164,7 +164,7 @@ scene.widgetList={
textBox,
WIDGET.newButton {name='home',x=1140,y= 90,w=170,h=80,sound='click',font=60,fText=CHAR.key.macHome,code=pressKey('home')},
WIDGET.newButton {name='endd',x=1140,y=190,w=170,h=80,sound='click',font=60,fText=CHAR.key.macEnd ,code=pressKey('end')},
WIDGET.newButton {name='copy',x=1140,y=290,w=170,h=80,sound='click',font=60,fText=CHAR.icon.copy ,code=function()love.system.setClipboardText(table.concat(textBox.texts,'\n'))end,color='lC'},
WIDGET.newButton {name='copy',x=1140,y=290,w=170,h=80,sound='click',font=60,fText=CHAR.icon.copy ,code=function()CLIPBOARD.set(table.concat(textBox.texts,'\n'))end,color='lC'},
logList,