1. 修改初始化逻辑,防止用户的配置文件更新的时候被修改。

2. 解决一些BUG(监听输入法状态卡死,咖啡因状态栏图标无故消失)
3. 选中文本后,输入翻译快捷键自动翻译选中的文本
This commit is contained in:
sugood
2020-11-13 16:15:27 +08:00
parent cfc23162b2
commit 82df63193b
9 changed files with 361 additions and 317 deletions
+28 -9
View File
@@ -3,14 +3,33 @@
--- Created by sugood(https://github.com/sugood).
--- DateTime: 2020/10/24 14:13
---
if config ~=nil and config[1].caffeine == 'on' then
print("设置状态栏")
local menuBarItem = hs.menubar.new()
menuBarItem:setTitle("")
menuBarItem:setIcon("~/.hammerspoon/icon/caffeine-on.pdf")
hs.caffeinate.set("displayIdle", true)
else
hs.caffeinate.set("displayIdle", false)
local menuBarItem = nil
local setCaffeine= function()
if config ~=nil and config[1].caffeine == 'on' and menuBarItem == nil then
print("设置状态栏")
menuBarItem= hs.menubar.new()
menuBarItem:setTitle("")
menuBarItem:setIcon("~/.hammerspoon/icon/caffeine-on.pdf")
hs.caffeinate.set("displayIdle", true)
else
hs.caffeinate.set("displayIdle", false)
end
end
function resetCaffeineMeun()
if(config ~=nil and config[1].caffeine == 'on' and menuBarItem:isInMenuBar() == false) then
print("重置状态栏")
menuBarItem:delete()
menuBarItem= hs.menubar.new()
menuBarItem:setTitle("")
menuBarItem:setIcon("~/.hammerspoon/icon/caffeine-on.pdf")
--hs.caffeinate.set("displayIdle", true)
end
end
function initData()
setCaffeine()
--监听咖啡因的状态,判断是否要重置
hs.timer.doEvery(1, resetCaffeineMeun)
end
-- 初始化
initData()
+37 -1
View File
@@ -4,11 +4,28 @@
--- DateTime: 2020/10/24 14:13
---
local console = require("hs.console")
version = "v0.1.1"
version = "v0.1.2"
configPath= "~/.hammerspoon/data/config.json"
initConfigPath= "~/.hammerspoon/data/initConfig.json"
config = {}
colorDialog = hs.dialog.color
--检查文件是否存在
function checkFileExist(path)
local file = hs.fs.pathToAbsolute(path)
return file ~= nil
end
--复制文件
function copyFile(source,destination)
print(destination)
sourcefile = io.open(source, "r")
destinationfile = io.open(destination, "w")
destinationfile:write(sourcefile:read("*all"))
sourcefile:close()
destinationfile:close()
end
function switchDict()
if config[1].dictEngine == '百度' then
config[1].dictEngine = '有道'
@@ -138,6 +155,15 @@ function initMenu()
end
function initData()
--第一次安装需要复制一个配置文件。以后更新则不会修改用户配置文件,防止被覆盖
if(checkFileExist(configPath) == false) then
print("初始化配置文件")
--获取绝对路径,io.open只支持绝对路径
local source = hs.fs.pathToAbsolute(initConfigPath)
local destination = string.gsub(source, "initConfig.json$", "config.json")
copyFile(source,destination)
end
if hs.json.read(configPath) ~= nil then
config = hs.json.read(configPath)
end
@@ -223,4 +249,14 @@ function SubStringGetByteCount(str, index)
byteCount = 4
end
return byteCount;
end
--判断是否复制成功
function isCopySuccess()
local num = hs.pasteboard.changeCount()
print("复制前数量:"..num)
hs.eventtap.keyStroke({ "cmd" }, "C")
local numAfter = hs.pasteboard.changeCount()
print("复制后数量:"..numAfter)
return numAfter > num
end
+14 -6
View File
@@ -115,7 +115,7 @@ function getYoudaoApi(q)
else
searchResult = {}
results = hs.json.decode(body)
print('results:'..hs.inspect(results))
--print('results:'..hs.inspect(results))
if(results['web']) then
--逆序插入
for i=#results['web'],1,-1 do
@@ -193,12 +193,20 @@ function initData()
if hs.json.read(dictHistoryPath) ~= nil then
searchResult = hs.json.read(dictHistoryPath)
end
local num = hs.pasteboard.changeCount()
hs.eventtap.keyStroke({ "cmd" }, "C")
mChooser = chooser.new(completionFn)
:choices(searchResult)
:queryChangedCallback(queryChangedCallbackFn)
:rightClickCallback(rightClickCallbackFn)
:searchSubText(true)
:show()
:choices(searchResult)
:queryChangedCallback(queryChangedCallbackFn)
:rightClickCallback(rightClickCallbackFn)
:searchSubText(true)
:show()
local numAfter = hs.pasteboard.changeCount()
--如果复制成功则自动查询复制的内容,否者只打开翻译界面
if(numAfter > num) then
--粘贴选中的文本进行查询
hs.eventtap.keyStroke({ "cmd" }, "V")
end
end)
end
+4 -1
View File
@@ -96,8 +96,11 @@ function initData()
return false
end)
mEventtap:start()
hs.timer.doEvery(1, listenerEvent)
hs.timer.doEvery(1, listenerEvent) --有BUG,经常假死
hs.keycodes.inputSourceChanged(function()
--resetCaffeineMeun()
--切换输入法后查询下监听状态
listenerEvent()
-- 用于保存当前输入法
local currentSourceID = hs.keycodes.currentSourceID()