Skip to content

Commit

Permalink
Move some ui method to router.go
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed May 7, 2024
1 parent fe7bcd0 commit 49923f1
Show file tree
Hide file tree
Showing 24 changed files with 269 additions and 259 deletions.
18 changes: 9 additions & 9 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ call_lefthook()
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/lefthook/bin/index.js"
then
"$dir/Wox/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox.UI.React/node_modules/lefthook/bin/index.js"
then
"$dir/Wox.UI.React/node_modules/lefthook/bin/index.js" "$@"
Expand All @@ -47,6 +38,15 @@ call_lefthook()
elif test -f "$dir/Wox.UI.React/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox.UI.React/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/lefthook/bin/index.js"
then
"$dir/Wox/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
Expand Down
18 changes: 9 additions & 9 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ call_lefthook()
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/lefthook/bin/index.js"
then
"$dir/Wox/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox.UI.React/node_modules/lefthook/bin/index.js"
then
"$dir/Wox.UI.React/node_modules/lefthook/bin/index.js" "$@"
Expand All @@ -47,6 +38,15 @@ call_lefthook()
elif test -f "$dir/Wox.UI.React/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox.UI.React/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/lefthook/bin/index.js"
then
"$dir/Wox/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/Wox/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
Expand Down
19 changes: 17 additions & 2 deletions Wox.UI.Flutter/wox/lib/api/wox_api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:core';

import 'package:wox/entity/wox_plugin.dart';
import 'package:wox/entity/wox_query.dart';
import 'package:wox/entity/wox_setting.dart';
import 'package:wox/entity/wox_theme.dart';
import 'package:wox/utils/wox_http_util.dart';
Expand Down Expand Up @@ -72,7 +73,21 @@ class WoxApi {
return await WoxHttpUtil.instance.postData("/hotkey/available", {"hotkey": hotkey});
}

Future<void> uiReady() async {
await WoxHttpUtil.instance.postData("/ui/ready", {});
Future<void> onUIReady() async {
await WoxHttpUtil.instance.postData("/on/ready", {});
}

Future<void> onFocusLost() async {
await WoxHttpUtil.instance.postData("/on/focus/lost", {});
}

Future<void> onShow() async {
await WoxHttpUtil.instance.postData("/on/show", {});
}

Future<void> onHide(PlainQuery query) async {
await WoxHttpUtil.instance.postData("/on/hide", {
"query": query.toJson(),
});
}
}
18 changes: 9 additions & 9 deletions Wox.UI.Flutter/wox/lib/entity/wox_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import 'package:wox/enums/wox_position_type_enum.dart';
import 'package:wox/enums/wox_query_type_enum.dart';
import 'package:wox/enums/wox_selection_type_enum.dart';

class WoxChangeQuery {
class PlainQuery {
late String queryId;
late WoxQueryType queryType;
late String queryText;
late Selection querySelection;

WoxChangeQuery({required this.queryId, required this.queryType, required this.queryText, required this.querySelection});
PlainQuery({required this.queryId, required this.queryType, required this.queryText, required this.querySelection});

WoxChangeQuery.fromJson(Map<String, dynamic> json) {
PlainQuery.fromJson(Map<String, dynamic> json) {
queryId = json['QueryId'] ?? "";
queryType = json['QueryType'];
queryText = json['QueryText'];
Expand All @@ -32,12 +32,12 @@ class WoxChangeQuery {

bool get isEmpty => queryText.isEmpty && querySelection.type.isEmpty;

static WoxChangeQuery empty() {
return WoxChangeQuery(queryId: "", queryType: "", queryText: "", querySelection: Selection.empty());
static PlainQuery empty() {
return PlainQuery(queryId: "", queryType: "", queryText: "", querySelection: Selection.empty());
}

static WoxChangeQuery emptyInput() {
return WoxChangeQuery(queryId: "", queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code, queryText: "", querySelection: Selection.empty());
static PlainQuery emptyInput() {
return PlainQuery(queryId: "", queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code, queryText: "", querySelection: Selection.empty());
}
}

Expand Down Expand Up @@ -72,11 +72,11 @@ class Selection {
}

class QueryHistory {
WoxChangeQuery? query;
PlainQuery? query;
int? timestamp;

QueryHistory.fromJson(Map<String, dynamic> json) {
query = json['Query'] != null ? WoxChangeQuery.fromJson(json['Query']) : null;
query = json['Query'] != null ? PlainQuery.fromJson(json['Query']) : null;
timestamp = json['Timestamp'];
}
}
Expand Down
5 changes: 1 addition & 4 deletions Wox.UI.Flutter/wox/lib/enums/wox_msg_method_enum.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
typedef WoxMsgMethod = String;

enum WoxMsgMethodEnum {
WOX_MSG_METHOD_PING("Ping", "Ping"),
WOX_MSG_METHOD_Log("Log", "Log"),
WOX_MSG_METHOD_QUERY("Query", "Query"),
WOX_MSG_METHOD_ACTION("Action", "Action"),
WOX_MSG_METHOD_REFRESH("Refresh", "Refresh"),
WOX_MSG_METHOD_REGISTER_MAIN_HOTKEY("RegisterMainHotkey", "Register Main Hotkey"),
WOX_MSG_METHOD_VISIBILITY_CHANGED("VisibilityChanged", "Visibility changed"),
WOX_MSG_METHOD_LOST_FOCUS("LostFocus", "Lost focus");
WOX_MSG_METHOD_VISIBILITY_CHANGED("VisibilityChanged", "Visibility changed");

final String code;
final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class WoxLauncherInterface {

/// When the query is changed, this method will be called.
/// [query] is the changed query.
void onQueryChanged(String traceId, WoxChangeQuery query, String changeReason) {}
void onQueryChanged(String traceId, PlainQuery query, String changeReason) {}

/// When the query box on action panel is changed, this method will be called.
/// [queryAction] is the changed query action.
Expand Down
7 changes: 6 additions & 1 deletion Wox.UI.Flutter/wox/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class _WoxAppState extends State<WoxApp> with WindowListener {

// notify server that ui is ready
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
WoxApi.instance.uiReady();
WoxApi.instance.onUIReady();
});
}

Expand All @@ -149,6 +149,11 @@ class _WoxAppState extends State<WoxApp> with WindowListener {
}
}

@override
void onWindowBlur() {
WoxApi.instance.onFocusLost();
}

@override
Widget build(BuildContext context) {
final launcherController = Get.find<WoxLauncherController>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:uuid/v4.dart';
import 'package:window_manager/window_manager.dart';
import 'package:wox/api/wox_api.dart';
import 'package:wox/entity/wox_preview.dart';
import 'package:wox/entity/wox_query.dart';
import 'package:wox/entity/wox_setting.dart';
Expand All @@ -30,7 +31,7 @@ import 'package:wox/utils/wox_theme_util.dart';
import 'package:wox/utils/wox_websocket_msg_util.dart';

class WoxLauncherController extends GetxController implements WoxLauncherInterface {
final _query = WoxChangeQuery.empty().obs;
final _query = PlainQuery.empty().obs;
final _activeResultIndex = 0.obs;
final _activeActionIndex = 0.obs;
final _resultItemGlobalKeys = <GlobalKey>[];
Expand Down Expand Up @@ -91,23 +92,15 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
await windowManager.setPosition(Offset(params.position.x.toDouble(), params.position.y.toDouble()));
}
await windowManager.show();
if(Platform.isWindows) {
if (Platform.isWindows) {
// on windows, it is somehow necessary to invoke show twice to make the window show
// otherwise, the window will not show up if it is the first time to invoke showApp
await windowManager.show();
}
await windowManager.focus();
queryBoxFocusNode.requestFocus();

WoxWebsocketMsgUtil.instance.sendMessage(
WoxWebsocketMsg(
requestId: const UuidV4().generate(),
traceId: traceId,
type: WoxMsgTypeEnum.WOX_MSG_TYPE_REQUEST.code,
method: WoxMsgMethodEnum.WOX_MSG_METHOD_VISIBILITY_CHANGED.code,
data: {"isVisible": "true", "query": _query.value.toJson()},
),
);
WoxApi.instance.onShow();
}

@override
Expand All @@ -124,21 +117,13 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa

//clear query box text if query type is selection
if (getCurrentQuery().queryType == WoxQueryTypeEnum.WOX_QUERY_TYPE_SELECTION.code) {
onQueryChanged(traceId, WoxChangeQuery.emptyInput(), "clear input after hide app");
onQueryChanged(traceId, PlainQuery.emptyInput(), "clear input after hide app");
}

WoxWebsocketMsgUtil.instance.sendMessage(
WoxWebsocketMsg(
requestId: const UuidV4().generate(),
traceId: traceId,
type: WoxMsgTypeEnum.WOX_MSG_TYPE_REQUEST.code,
method: WoxMsgMethodEnum.WOX_MSG_METHOD_VISIBILITY_CHANGED.code,
data: {"isVisible": "false", "query": _query.value.toJson()},
),
);
WoxApi.instance.onHide(_query.value);
}

WoxChangeQuery getCurrentQuery() {
PlainQuery getCurrentQuery() {
return _query.value;
}

Expand Down Expand Up @@ -220,7 +205,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
final queryText = queryResults[_activeResultIndex.value].title;
onQueryChanged(
traceId,
WoxChangeQuery(
PlainQuery(
queryId: const UuidV4().generate(),
queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code,
queryText: queryText.value,
Expand All @@ -234,7 +219,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
void onQueryBoxTextChanged(String value) {
canArrowUpHistory = false;

WoxChangeQuery woxChangeQuery = WoxChangeQuery(
PlainQuery woxChangeQuery = PlainQuery(
queryId: const UuidV4().generate(),
queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code,
queryText: value,
Expand All @@ -251,7 +236,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
}

@override
void onQueryChanged(String traceId, WoxChangeQuery query, String changeReason, {bool moveCursorToEnd = false}) {
void onQueryChanged(String traceId, PlainQuery query, String changeReason, {bool moveCursorToEnd = false}) {
Logger.instance.debug(traceId, "query changed: ${query.queryText}, reason: $changeReason");

//hide setting view if query changed
Expand Down Expand Up @@ -373,7 +358,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
showApp(msg.traceId, ShowAppParams.fromJson(msg.data));
responseWoxWebsocketRequest(msg, true, null);
} else if (msg.method == "ChangeQuery") {
onQueryChanged(msg.traceId, WoxChangeQuery.fromJson(msg.data), "receive change query from wox", moveCursorToEnd: true);
onQueryChanged(msg.traceId, PlainQuery.fromJson(msg.data), "receive change query from wox", moveCursorToEnd: true);
responseWoxWebsocketRequest(msg, true, null);
} else if (msg.method == "ChangeTheme") {
final theme = WoxTheme.fromJson(msg.data);
Expand Down Expand Up @@ -723,7 +708,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa

canArrowUpHistory = false;

WoxChangeQuery woxChangeQuery = WoxChangeQuery(
PlainQuery woxChangeQuery = PlainQuery(
queryId: const UuidV4().generate(),
queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_SELECTION.code,
queryText: "",
Expand Down
4 changes: 2 additions & 2 deletions Wox/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
)

type API interface {
ChangeQuery(ctx context.Context, query share.ChangedQuery)
ChangeQuery(ctx context.Context, query share.PlainQuery)
HideApp(ctx context.Context)
ShowApp(ctx context.Context)
Notify(ctx context.Context, title string, description string)
Expand All @@ -42,7 +42,7 @@ type APIImpl struct {
dynamicSettingCallbacks []func(key string) definition.PluginSettingDefinitionItem
}

func (a *APIImpl) ChangeQuery(ctx context.Context, query share.ChangedQuery) {
func (a *APIImpl) ChangeQuery(ctx context.Context, query share.PlainQuery) {
GetPluginManager().GetUI().ChangeQuery(ctx, query)
}

Expand Down
4 changes: 2 additions & 2 deletions Wox/plugin/host/host_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (w *WebsocketHost) handleRequestFromPlugin(ctx context.Context, request Jso
util.GetLogger().Error(ctx, fmt.Sprintf("[%s] ChangeQuery method must have a queryText parameter", request.PluginName))
return
}
pluginInstance.API.ChangeQuery(ctx, share.ChangedQuery{
pluginInstance.API.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeInput,
QueryText: queryText,
})
Expand All @@ -250,7 +250,7 @@ func (w *WebsocketHost) handleRequestFromPlugin(ctx context.Context, request Jso
return
}

pluginInstance.API.ChangeQuery(ctx, share.ChangedQuery{
pluginInstance.API.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeSelection,
QuerySelection: selection,
})
Expand Down
2 changes: 1 addition & 1 deletion Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func (m *Manager) GetUI() share.UI {
return m.ui
}

func (m *Manager) NewQuery(ctx context.Context, changedQuery share.ChangedQuery) (Query, error) {
func (m *Manager) NewQuery(ctx context.Context, changedQuery share.PlainQuery) (Query, error) {
if changedQuery.QueryType == QueryTypeInput {
newQuery := changedQuery.QueryText
woxSetting := setting.GetSettingManager().GetWoxSetting(ctx)
Expand Down
2 changes: 1 addition & 1 deletion Wox/plugin/system/app/app_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type emptyAPIImpl struct {
func (e emptyAPIImpl) OnGetDynamicSetting(ctx context.Context, callback func(key string) definition.PluginSettingDefinitionItem) {
}

func (e emptyAPIImpl) ChangeQuery(ctx context.Context, query share.ChangedQuery) {
func (e emptyAPIImpl) ChangeQuery(ctx context.Context, query share.PlainQuery) {
}

func (e emptyAPIImpl) HideApp(ctx context.Context) {
Expand Down
2 changes: 1 addition & 1 deletion Wox/plugin/system/calculator/calculator_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *CalculatorPlugin) Query(ctx context.Context, query plugin.Query) []plug
{
Name: "Recalculate",
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
c.api.ChangeQuery(ctx, share.ChangedQuery{
c.api.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeInput,
QueryText: h.Expression,
})
Expand Down
4 changes: 2 additions & 2 deletions Wox/plugin/system/indicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (i *IndicatorPlugin) Query(ctx context.Context, query plugin.Query) []plugi
Name: "activate",
PreventHideAfterAction: true,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
i.api.ChangeQuery(ctx, share.ChangedQuery{
i.api.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeInput,
QueryText: fmt.Sprintf("%s ", triggerKeyword),
})
Expand All @@ -87,7 +87,7 @@ func (i *IndicatorPlugin) Query(ctx context.Context, query plugin.Query) []plugi
Name: "activate",
PreventHideAfterAction: true,
Action: func(ctx context.Context, actionContext plugin.ActionContext) {
i.api.ChangeQuery(ctx, share.ChangedQuery{
i.api.ChangeQuery(ctx, share.PlainQuery{
QueryType: plugin.QueryTypeInput,
QueryText: fmt.Sprintf("%s %s ", triggerKeyword, metadataCommand.Command),
})
Expand Down

0 comments on commit 49923f1

Please sign in to comment.