From b50147caceac6e16e5d0890963cbd472e47f338a Mon Sep 17 00:00:00 2001 From: ParticleG Date: Fri, 1 Nov 2024 08:00:01 +0800 Subject: [PATCH] - Unify clipboard --- Zframework/clipboard.lua | 43 +++++++++++++++++++++ Zframework/clipboard_thread.lua | 65 ++++++++++++++++++++++++++++++++ Zframework/init.lua | 49 +++--------------------- Zframework/profile.lua | 2 +- Zframework/stringExtend.lua | 2 +- parts/scenes/app_console.lua | 6 +-- parts/scenes/app_cubefield.lua | 2 +- parts/scenes/app_dtw.lua | 2 +- parts/scenes/customGame.lua | 4 +- parts/scenes/custom_field.lua | 4 +- parts/scenes/custom_mission.lua | 4 +- parts/scenes/custom_sequence.lua | 5 +-- parts/scenes/dict.lua | 2 +- parts/scenes/login.lua | 2 +- parts/scenes/replays.lua | 4 +- parts/scenes/reset_password.lua | 2 +- parts/scenes/savedata.lua | 4 +- parts/scenes/setting_video.lua | 2 +- parts/scenes/viewlog.lua | 2 +- 19 files changed, 137 insertions(+), 69 deletions(-) create mode 100644 Zframework/clipboard.lua create mode 100644 Zframework/clipboard_thread.lua diff --git a/Zframework/clipboard.lua b/Zframework/clipboard.lua new file mode 100644 index 00000000..81931766 --- /dev/null +++ b/Zframework/clipboard.lua @@ -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 \ No newline at end of file diff --git a/Zframework/clipboard_thread.lua b/Zframework/clipboard_thread.lua new file mode 100644 index 00000000..c1598601 --- /dev/null +++ b/Zframework/clipboard_thread.lua @@ -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() diff --git a/Zframework/init.lua b/Zframework/init.lua index b9ff01b5..c3b3a7ae 100644 --- a/Zframework/init.lua +++ b/Zframework/init.lua @@ -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() diff --git a/Zframework/profile.lua b/Zframework/profile.lua index 85ba3a55..722ebfc4 100644 --- a/Zframework/profile.lua +++ b/Zframework/profile.lua @@ -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 diff --git a/Zframework/stringExtend.lua b/Zframework/stringExtend.lua index 272e773c..141fc897 100644 --- a/Zframework/stringExtend.lua +++ b/Zframework/stringExtend.lua @@ -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 diff --git a/parts/scenes/app_console.lua b/parts/scenes/app_console.lua index 23b93978..dd9b24bd 100644 --- a/parts/scenes/app_console.lua +++ b/parts/scenes/app_console.lua @@ -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, } diff --git a/parts/scenes/app_cubefield.lua b/parts/scenes/app_cubefield.lua index ba3b95e4..cd0f549d 100644 --- a/parts/scenes/app_cubefield.lua +++ b/parts/scenes/app_cubefield.lua @@ -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)) diff --git a/parts/scenes/app_dtw.lua b/parts/scenes/app_dtw.lua index 9a797fc8..d969188b 100644 --- a/parts/scenes/app_dtw.lua +++ b/parts/scenes/app_dtw.lua @@ -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 diff --git a/parts/scenes/customGame.lua b/parts/scenes/customGame.lua index 3c15b8a3..c2d000c4 100644 --- a/parts/scenes/customGame.lua +++ b/parts/scenes/customGame.lua @@ -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 diff --git a/parts/scenes/custom_field.lua b/parts/scenes/custom_field.lua index 76fc86e4..ab6851ce 100644 --- a/parts/scenes/custom_field.lua +++ b/parts/scenes/custom_field.lua @@ -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 diff --git a/parts/scenes/custom_mission.lua b/parts/scenes/custom_mission.lua index f5e2dc84..e730a959 100644 --- a/parts/scenes/custom_mission.lua +++ b/parts/scenes/custom_mission.lua @@ -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 diff --git a/parts/scenes/custom_sequence.lua b/parts/scenes/custom_sequence.lua index 47a76381..787ade53 100644 --- a/parts/scenes/custom_sequence.lua +++ b/parts/scenes/custom_sequence.lua @@ -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 diff --git a/parts/scenes/dict.lua b/parts/scenes/dict.lua index 13e13819..3fe36891 100644 --- a/parts/scenes/dict.lua +++ b/parts/scenes/dict.lua @@ -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 diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index d4f0b4e6..0a2b005f 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -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 diff --git a/parts/scenes/replays.lua b/parts/scenes/replays.lua index 757414ef..98067b41 100644 --- a/parts/scenes/replays.lua +++ b/parts/scenes/replays.lua @@ -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") diff --git a/parts/scenes/reset_password.lua b/parts/scenes/reset_password.lua index 7923b56a..e6228472 100644 --- a/parts/scenes/reset_password.lua +++ b/parts/scenes/reset_password.lua @@ -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 diff --git a/parts/scenes/savedata.lua b/parts/scenes/savedata.lua index 2ae08365..aaa32069 100644 --- a/parts/scenes/savedata.lua +++ b/parts/scenes/savedata.lua @@ -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) diff --git a/parts/scenes/setting_video.lua b/parts/scenes/setting_video.lua index f3c36bd8..6b8f947b 100644 --- a/parts/scenes/setting_video.lua +++ b/parts/scenes/setting_video.lua @@ -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' diff --git a/parts/scenes/viewlog.lua b/parts/scenes/viewlog.lua index 2c41e610..31b6de1c 100644 --- a/parts/scenes/viewlog.lua +++ b/parts/scenes/viewlog.lua @@ -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,