Skip to content

Commit

Permalink
Refactor clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Nov 14, 2023
1 parent 6d07b65 commit 5a22f54
Show file tree
Hide file tree
Showing 16 changed files with 691 additions and 503 deletions.
3 changes: 0 additions & 3 deletions Wox/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/tidwall/gjson v1.17.0
github.com/wissance/stringFormatter v1.1.1
go.uber.org/zap v1.26.0
golang.design/x/clipboard v0.7.0
golang.design/x/hotkey v0.4.1
howett.net/plist v1.0.0
)
Expand Down Expand Up @@ -70,9 +69,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
golang.design/x/mainthread v0.3.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/image v0.13.0 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
Expand Down
6 changes: 0 additions & 6 deletions Wox/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
golang.design/x/hotkey v0.4.1 h1:zLP/2Pztl4WjyxURdW84GoZ5LUrr6hr69CzJFJ5U1go=
golang.design/x/hotkey v0.4.1/go.mod h1:M8SGcwFYHnKRa83FpTFQoZvPO5vVT+kWPztFqTQKmXA=
golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8=
Expand All @@ -167,14 +165,10 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d h1:grE48C8cjIY0aiHVmFyYgYxxSARQWBABLXKZfQPrBhY=
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200801110659-972c09e46d76/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg=
golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c h1:Gk61ECugwEHL6IiyyNLXNzmu8XslmRP2dS0xjIYhbb4=
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
12 changes: 3 additions & 9 deletions Wox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ func main() {
return
}

clipboardErr := util.ClipboardInit()
if clipboardErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to initialize clipboard: %s", clipboardErr.Error()))
return
}

settingErr := setting.GetSettingManager().Init(ctx)
if settingErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to initialize settings: %s", settingErr.Error()))
Expand Down Expand Up @@ -97,11 +91,11 @@ func main() {

t := util.Hotkey{}
t.Register(ctx, "command+m", func() {
data, selectedErr := util.GetSelectedText()
data, selectedErr := util.GetSelected()
if selectedErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to get selected text: %s", selectedErr.Error()))
util.GetLogger().Error(ctx, fmt.Sprintf("failed to get selected: %s", selectedErr.Error()))
} else {
util.GetLogger().Info(ctx, fmt.Sprintf("selected text: %s", data.Data))
util.GetLogger().Info(ctx, fmt.Sprintf("selected %s: %s", data.GetType(), data.String()))
}
})

Expand Down
10 changes: 8 additions & 2 deletions Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"wox/setting"
"wox/share"
"wox/util"
"wox/util/clipboard"
)

var managerInstance *Manager
Expand Down Expand Up @@ -634,11 +635,16 @@ func (m *Manager) ExecuteRefresh(ctx context.Context, resultId string, refreshab

func (m *Manager) ReplaceQueryVariable(ctx context.Context, query string) string {
if strings.Contains(query, QueryVariableSelectedText) {
data, selectedErr := util.GetSelectedText()
data, selectedErr := util.GetSelected()
if selectedErr != nil {
logger.Error(ctx, fmt.Sprintf("failed to get selected text: %s", selectedErr.Error()))
} else {
query = strings.ReplaceAll(query, QueryVariableSelectedText, data.Data)
if data.GetType() == clipboard.ClipboardTypeText {
textData := data.(*clipboard.TextData)
query = strings.ReplaceAll(query, QueryVariableSelectedText, textData.Text)
} else {
logger.Error(ctx, fmt.Sprintf("selected data is not text, type: %s", data.GetType()))
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Wox/plugin/system/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"wox/plugin/system"
"wox/setting"
"wox/util"
"wox/util/clipboard"
)

func init() {
Expand Down Expand Up @@ -124,7 +125,7 @@ func (a *ApplicationPlugin) Query(ctx context.Context, query plugin.Query) []plu
Name: "i18n:plugin_app_copy_path",
Icon: plugin.NewWoxImageBase64(`data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA+0lEQVR4nO3VLQ7CQBAF4L3Eih6HOQScAIfmDMhq0NVgcegaCAq7GoMpBJKyBEc2/GyZaafTzkue36/7sjVGo9FoICs8toOsmLF9Sezhh8szLwIL2B5LP1oxIrAAV9x5ERQAR41I86vHdrK+VAI4SgT28PPdLRrhXgBkCCzgcCr9IhLhAgAJAgt4HiIW4d4AQgQLoAoCfpQNQIUwnAAKhOEGYBGmDYAQAW0GxNSONx+rgLSBGwDpEwLpAPtl87UAkun+73YSANInBNIBtun/AHYynQOA9AmBdICV/oxa6QCQPiFQQN+e0UQ6ICWuqTMKyPUGej4hjUZjROQBwgDUDcPYwFwAAAAASUVORK5CYII=`),
Action: func(actionContext plugin.ActionContext) {
util.ClipboardWriteText(info.Path)
clipboard.WriteText(info.Path)
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions Wox/plugin/system/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"wox/plugin"
"wox/util"
"wox/util/clipboard"
)

func init() {
Expand Down Expand Up @@ -78,7 +79,7 @@ func (c *CalculatorPlugin) Query(ctx context.Context, query plugin.Query) []plug
Result: result,
AddDate: util.FormatDateTime(util.GetSystemTime()),
})
util.ClipboardWriteText(result)
clipboard.WriteText(result)
},
},
},
Expand All @@ -101,7 +102,7 @@ func (c *CalculatorPlugin) Query(ctx context.Context, query plugin.Query) []plug
Result: result,
AddDate: util.FormatDateTime(util.GetSystemTime()),
})
util.ClipboardWriteText(result)
clipboard.WriteText(result)
},
},
},
Expand All @@ -128,7 +129,7 @@ func (c *CalculatorPlugin) Query(ctx context.Context, query plugin.Query) []plug
Name: "Copy result",
IsDefault: true,
Action: func(actionContext plugin.ActionContext) {
util.ClipboardWriteText(h.Result)
clipboard.WriteText(h.Result)
},
},
{
Expand Down
Loading

0 comments on commit 5a22f54

Please sign in to comment.