Skip to content

Commit

Permalink
Improve app plugin in macos
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed May 13, 2024
1 parent 1080d95 commit 12ae963
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 34 deletions.
47 changes: 41 additions & 6 deletions Wox/plugin/system/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ func (a *ApplicationPlugin) Init(ctx context.Context, initParams plugin.InitPara
func (a *ApplicationPlugin) Query(ctx context.Context, query plugin.Query) []plugin.QueryResult {
var results []plugin.QueryResult
for _, info := range a.apps {
if isMatch, score := system.IsStringMatchScore(ctx, info.Name, query.Search); isMatch {
isNameMatch, nameScore := system.IsStringMatchScore(ctx, info.Name, query.Search)
isPathNameMatch, pathNameScore := system.IsStringMatchScore(ctx, filepath.Base(info.Path), query.Search)
if isNameMatch || isPathNameMatch {
results = append(results, plugin.QueryResult{
Id: uuid.NewString(),
Title: info.Name,
SubTitle: info.Path,
Icon: info.Icon,
Score: score,
Score: util.MaxInt64(nameScore, pathNameScore),
Preview: plugin.WoxPreview{
PreviewType: plugin.WoxPreviewTypeText,
PreviewData: info.Path,
Expand Down Expand Up @@ -213,6 +215,30 @@ func (a *ApplicationPlugin) indexApps(ctx context.Context) {
startTimestamp := util.GetSystemTimestamp()
a.api.Log(ctx, plugin.LogLevelInfo, "start to get apps")

appInfos := a.indexAppsByDirectory(ctx)
extraApps := a.indexExtraApps(ctx)

//merge extra apps
for _, extraApp := range extraApps {
var isExist = false
for _, app := range appInfos {
if app.Path == extraApp.Path {
isExist = true
break
}
}
if !isExist {
appInfos = append(appInfos, extraApp)
}
}

a.apps = appInfos
a.saveAppToCache(ctx)

a.api.Log(ctx, plugin.LogLevelInfo, fmt.Sprintf("indexed %d apps, cost %d ms", len(a.apps), util.GetSystemTimestamp()-startTimestamp))
}

func (a *ApplicationPlugin) indexAppsByDirectory(ctx context.Context) []appInfo {
appDirectories := a.getRetriever(ctx).GetAppDirectories(ctx)
appPaths := a.getAppPaths(ctx, appDirectories)

Expand Down Expand Up @@ -257,12 +283,21 @@ func (a *ApplicationPlugin) indexApps(ctx context.Context) {

waitGroup.Wait()

if len(appInfos) > 0 {
a.apps = appInfos
a.saveAppToCache(ctx)
return appInfos
}

func (a *ApplicationPlugin) indexExtraApps(ctx context.Context) []appInfo {
apps, err := a.retriever.GetExtraApps(ctx)
if err != nil {
return []appInfo{}
}

a.api.Log(ctx, plugin.LogLevelInfo, fmt.Sprintf("indexed %d apps, cost %d ms", len(a.apps), util.GetSystemTimestamp()-startTimestamp))
//preprocess icon
for i := range apps {
apps[i].Icon = plugin.ConvertIcon(ctx, apps[i].Icon, a.pluginDirectory)
}

return apps
}

func (a *ApplicationPlugin) getAppPaths(ctx context.Context, appDirectories []appDirectory) (appPaths []string) {
Expand Down
75 changes: 75 additions & 0 deletions Wox/plugin/system/app/app_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"fmt"
"github.com/disintegration/imaging"
"github.com/mitchellh/go-homedir"
"github.com/tidwall/gjson"
"howett.net/plist"
"image"
"io"
"os"
"os/exec"
"path"
"strings"
"sync"
"unsafe"
"wox/plugin"
"wox/util"
Expand Down Expand Up @@ -151,6 +153,79 @@ func (a *MacRetriever) getMacAppIcon(ctx context.Context, appPath string) (plugi
}, nil
}

func (a *MacRetriever) GetExtraApps(ctx context.Context) ([]appInfo, error) {
//use `system_profiler SPApplicationsDataType -json` to get all apps
out, err := util.ShellRunOutput("system_profiler", "SPApplicationsDataType", "-json")
if err != nil {
return nil, fmt.Errorf("failed to get extra apps: %s", err.Error())
}

//parse json
results := gjson.Get(string(out), "SPApplicationsDataType")
if !results.Exists() {
return nil, errors.New("failed to parse extra apps")
}
var appPaths []string
for _, app := range results.Array() {
appPath := app.Get("path").String()
if appPath == "" {
continue
}
if strings.HasPrefix(appPath, "/System/Library/CoreServices/") {
continue
}
if strings.HasPrefix(appPath, "/System/Library/PrivateFrameworks/") {
continue
}
if strings.HasPrefix(appPath, "/System/Library/Frameworks/") {
continue
}

appPaths = append(appPaths, appPath)
}

// split into groups, so we can index apps in parallel
var appPathGroups [][]string
var groupSize = 25
for i := 0; i < len(appPaths); i += groupSize {
var end = i + groupSize
if end > len(appPaths) {
end = len(appPaths)
}
appPathGroups = append(appPathGroups, appPaths[i:end])
}
a.api.Log(ctx, plugin.LogLevelInfo, fmt.Sprintf("found extra %d apps in %d groups", len(appPaths), len(appPathGroups)))

// index apps in parallel
var appInfos []appInfo
var waitGroup sync.WaitGroup
var lock sync.Mutex
waitGroup.Add(len(appPathGroups))
for groupIndex := range appPathGroups {
var appPathGroup = appPathGroups[groupIndex]
util.Go(ctx, fmt.Sprintf("index extra app group: %d", groupIndex), func() {
for _, appPath := range appPathGroup {
info, getErr := a.ParseAppInfo(ctx, appPath)
if getErr != nil {
a.api.Log(ctx, plugin.LogLevelError, fmt.Sprintf("error getting extra app info for %s: %s", appPath, getErr.Error()))
continue
}

lock.Lock()
appInfos = append(appInfos, info)
lock.Unlock()
}
waitGroup.Done()
}, func() {
waitGroup.Done()
})
}

waitGroup.Wait()

return appInfos, nil
}

func (a *MacRetriever) getMacAppIconImagePath(ctx context.Context, appPath string) (string, error) {
iconPath, infoPlistErr := a.parseMacAppIconFromInfoPlist(ctx, appPath)
if infoPlistErr == nil {
Expand Down
55 changes: 28 additions & 27 deletions Wox/plugin/system/app/app_icons_darwin.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Wox/plugin/system/app/app_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ func (a *LinuxRetriever) GetAppExtensions(ctx context.Context) []string {
func (a *LinuxRetriever) ParseAppInfo(ctx context.Context, path string) (appInfo, error) {
return appInfo{}, errors.New("not implemented")
}

func (a *LinuxRetriever) GetExtraApps(ctx context.Context) ([]appInfo, error) {
return []appInfo{}, nil
}
5 changes: 4 additions & 1 deletion Wox/plugin/system/app/app_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"github.com/lxn/win"
"github.com/parsiya/golnk"
"image"
"image/color"
Expand Down Expand Up @@ -166,3 +165,7 @@ func (a *WindowsRetriever) GetAppIcon(ctx context.Context, path string) (image.I

return img, nil
}

func (a *WindowsRetriever) GetExtraApps(ctx context.Context) ([]appInfo, error) {
return []appInfo{}, nil
}
1 change: 1 addition & 0 deletions Wox/plugin/system/app/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ type Retriever interface {
GetAppDirectories(ctx context.Context) []appDirectory
GetAppExtensions(ctx context.Context) []string
ParseAppInfo(ctx context.Context, path string) (appInfo, error)
GetExtraApps(ctx context.Context) ([]appInfo, error)
}
8 changes: 8 additions & 0 deletions Wox/util/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package util

func MaxInt64(a int64, b int64) int64 {
if a > b {
return a
}
return b
}

0 comments on commit 12ae963

Please sign in to comment.