Skip to content

Commit

Permalink
- Unify clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ParticleG committed Nov 1, 2024
1 parent eb3b8c9 commit b50147c
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 69 deletions.
43 changes: 43 additions & 0 deletions Zframework/clipboard.lua
Original file line number Diff line number Diff line change
@@ -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 changes: 65 additions & 0 deletions Zframework/clipboard_thread.lua
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 5 additions & 44 deletions Zframework/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Zframework/profile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Zframework/stringExtend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions parts/scenes/app_console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/app_cubefield.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/app_dtw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parts/scenes/customGame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parts/scenes/custom_field.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parts/scenes/custom_mission.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions parts/scenes/custom_sequence.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local sys=love.system
local kb=love.keyboard

local sin=math.sin
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/dict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/login.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parts/scenes/replays.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/reset_password.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parts/scenes/savedata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/setting_video.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion parts/scenes/viewlog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down

0 comments on commit b50147c

Please sign in to comment.