Skip to content

Commit

Permalink
Update Wox plugin to include active window context in queries
Browse files Browse the repository at this point in the history
The Wox plugin and related files were updated to include the context of the active window name within queries. Additionally, "@wox-launcher/wox-plugin" was updated from version 0.0.63 to 0.0.66 and minor change to plugin description in plugin-store.json was made.
  • Loading branch information
qianlifeng committed May 7, 2024
1 parent 1ec8cc9 commit 459489d
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Wox.Plugin.Host.Nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@wox-launcher/wox-plugin": "^0.0.63",
"@wox-launcher/wox-plugin": "^0.0.66",
"dayjs": "^1.11.9",
"promise-deferred": "^2.0.4",
"winston": "^3.10.0",
Expand Down
8 changes: 4 additions & 4 deletions Wox.Plugin.Host.Nodejs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Wox.Plugin.Host.Nodejs/src/jsonrpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger } from "./logger"
import path from "path"
import { PluginAPI } from "./pluginAPI"
import { Context, Plugin, PluginInitParams, Query, RefreshableResult, Result, ResultAction, Selection } from "@wox-launcher/wox-plugin"
import { Context, Plugin, PluginInitParams, Query, QueryEnv, RefreshableResult, Result, ResultAction, Selection } from "@wox-launcher/wox-plugin"
import { WebSocket } from "ws"
import * as crypto from "crypto"

Expand Down Expand Up @@ -88,7 +88,7 @@ async function loadPlugin(ctx: Context, request: PluginJsonRpcRequest) {
}

function unloadPlugin(ctx: Context, request: PluginJsonRpcRequest) {
let pluginInstance = pluginInstances.get(request.PluginId)
const pluginInstance = pluginInstances.get(request.PluginId)
if (pluginInstance === undefined || pluginInstance === null) {
logger.error(ctx, `[${request.PluginName}] plugin instance not found: ${request.PluginName}`)
throw new Error(`plugin instance not found: ${request.PluginName}`)
Expand Down Expand Up @@ -162,7 +162,9 @@ async function query(ctx: Context, request: PluginJsonRpcRequest) {
Command: request.Params.Command,
Search: request.Params.Search,
ShortcutFrom: request.Params.ShortcutFrom,
Selection: JSON.parse(request.Params.Selection) as Selection
Selection: JSON.parse(request.Params.Selection) as Selection,
Env: JSON.parse(request.Params.Env) as QueryEnv,
IsGlobalQuery: () => request.Params.Type === "input" && request.Params.TriggerKeyword === ""
} as Query)

if (!results) {
Expand Down
2 changes: 1 addition & 1 deletion Wox.Plugin.Nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wox-launcher/wox-plugin",
"version": "0.0.63",
"version": "0.0.66",
"description": "All nodejs plugin for Wox should use types in this package",
"repository": {
"type": "git",
Expand Down
19 changes: 19 additions & 0 deletions Wox.Plugin.Nodejs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export interface Selection {
FilePaths: string[]
}

export interface QueryEnv {
/**
* Active window title when user query
*/
ActiveWindowTitle: string
}

export interface Query {
/**
* By default, Wox will only pass input query to plugin.
Expand Down Expand Up @@ -49,8 +56,20 @@ export interface Query {
* NOTE: Only available when query type is selection
*/
Selection: Selection

/**
* Additional query environment data
* expose more context env data to plugin, E.g. plugin A only show result when active window title is "Chrome"
*/
Env: QueryEnv

/**
* Whether current query is global query
*/
IsGlobalQuery(): boolean
}


export interface Result {
Id?: string
Title: string
Expand Down
2 changes: 2 additions & 0 deletions Wox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"wox/ui"
"wox/util"
"wox/util/hotkey"
"wox/util/window"
)

import _ "wox/plugin/host" // import all hosts
Expand Down Expand Up @@ -124,6 +125,7 @@ func main() {
t.Register(ctx, "ctrl+ctrl", func() {
//files := plugin.GetPluginManager().GetUI().PickFiles(ctx, share.PickFilesParams{IsDirectory: true})
//ui.GetUIManager().GetUI(ctx).Notify(ctx, "Picked files", fmt.Sprintf("%v", files))
util.GetLogger().Info(ctx, window.GetActiveWindowName())
})
if util.IsProd() {
util.Go(ctx, "start ui", func() {
Expand Down
7 changes: 7 additions & 0 deletions Wox/plugin/host/host_websocket_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ func (w *WebsocketPlugin) Query(ctx context.Context, query plugin.Query) []plugi
return []plugin.QueryResult{}
}

envJson, marshalEnvErr := json.Marshal(query.Env)
if marshalEnvErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("[%s] failed to marshal plugin query env: %s", w.metadata.Name, marshalEnvErr.Error()))
return []plugin.QueryResult{}
}

rawResults, queryErr := w.websocketHost.invokeMethod(ctx, w.metadata, "query", map[string]string{
"Type": query.Type,
"RawQuery": query.RawQuery,
"TriggerKeyword": query.TriggerKeyword,
"Command": query.Command,
"Search": query.Search,
"Selection": string(selectionJson),
"Env": string(envJson),
})
if queryErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("[%s] query failed: %s", w.metadata.Name, queryErr.Error()))
Expand Down
11 changes: 8 additions & 3 deletions Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"wox/setting"
"wox/share"
"wox/util"
"wox/util/window"
)

var managerInstance *Manager
Expand Down Expand Up @@ -692,16 +693,20 @@ func (m *Manager) NewQuery(ctx context.Context, changedQuery share.ChangedQuery)
newQuery = expandedQuery
}
}
return newQueryInputWithPlugins(newQuery, GetPluginManager().GetPluginInstances()), nil
query := newQueryInputWithPlugins(newQuery, GetPluginManager().GetPluginInstances())
query.Env.ActiveWindowTitle = window.GetActiveWindowName()
return query, nil
}

if changedQuery.QueryType == QueryTypeSelection {
return Query{
query := Query{
Type: QueryTypeSelection,
RawQuery: changedQuery.QueryText,
Search: changedQuery.QueryText,
Selection: changedQuery.QuerySelection,
}, nil
}
query.Env.ActiveWindowTitle = window.GetActiveWindowName()
return query, nil
}

return Query{}, errors.New("invalid query type")
Expand Down
8 changes: 8 additions & 0 deletions Wox/plugin/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type Query struct {
//
// NOTE: Only available when query type is QueryTypeSelection
Selection util.Selection

// additional query environment data
// expose more context env data to plugin, E.g. plugin A only show result when active window title is "Chrome"
Env QueryEnv
}

func (q *Query) IsGlobalQuery() bool {
Expand All @@ -65,6 +69,10 @@ func (q *Query) String() string {
return ""
}

type QueryEnv struct {
ActiveWindowTitle string // active window title when user query
}

// Query result return from plugin
type QueryResult struct {
// Result id, should be unique. It's optional, if you don't set it, Wox will assign a random id for you
Expand Down
6 changes: 6 additions & 0 deletions Wox/util/window/window_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package window
#include <stdlib.h>
int getActiveWindowIcon(unsigned char **iconData);
char* getActiveWindowName();
*/
import "C"
import (
Expand All @@ -32,3 +33,8 @@ func GetActiveWindowIcon() (image.Image, error) {

return img, nil
}

func GetActiveWindowName() string {
name := C.getActiveWindowName()
return C.GoString(name)
}
11 changes: 11 additions & 0 deletions Wox/util/window/window_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ int getActiveWindowIcon(unsigned char **iconData) {
return (int)length;
}
}

char* getActiveWindowName() {
@autoreleasepool {
NSRunningApplication *activeApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
if (!activeApp) {
return "";
}

return strdup([[activeApp localizedName] UTF8String]);
}
}
4 changes: 4 additions & 0 deletions Wox/util/window/window_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ import "image"
func GetActiveWindowIcon() (image.Image, error) {
return nil, errors.New("not implemented")
}

func GetActiveWindowName() string {
return ""
}
16 changes: 16 additions & 0 deletions Wox/util/window/window_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ char* getActiveWindowIcon(unsigned char **iconData, int *iconSize, int *width, i
CloseHandle(hProcess);
return result;
}

char* getActiveWindowName() {
HWND hwnd = GetForegroundWindow();
if (!hwnd) {
return "";
}

WCHAR windowTitle[1024];
if (0 == GetWindowTextW(hwnd, windowTitle, 1024)) {
return "";
}

char windowTitleA[1024];
WideCharToMultiByte(CP_ACP, 0, windowTitle, -1, windowTitleA, 1024, NULL, NULL);
return windowTitleA;
}
7 changes: 6 additions & 1 deletion Wox/util/window/window_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package window
#include <psapi.h>
#include <shellapi.h>
char* getIconData(HICON hIcon, unsigned char **iconData, int *iconSize, int *width, int *height);
char* getActiveWindowIcon(unsigned char **iconData, int *iconSize, int *width, int *height);
char* getActiveWindowName();
*/
import "C"
import (
Expand Down Expand Up @@ -47,3 +47,8 @@ func GetActiveWindowIcon() (image.Image, error) {

return img, nil
}

func GetActiveWindowName() string {
cname := C.getActiveWindowName()
return C.GoString(cname)
}
2 changes: 1 addition & 1 deletion plugin-store.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"Version": "0.0.1",
"MinWoxVersion": "2.0.0",
"Runtime": "nodejs",
"Description": "Search arc tabs and spaces",
"Description": "Search arc tabs",
"IconUrl": "https://raw.githubusercontent.com/Wox-launcher/Wox.Plugin.Arc/main/images/app.png",
"Website": "https://github.com/Wox-launcher/Wox.Plugin.Arc",
"DownloadUrl": "https://github.com/Wox-launcher/Wox.Plugin.Arc/releases/latest/download/wox.plugin.arc.wox",
Expand Down

0 comments on commit 459489d

Please sign in to comment.