Skip to content

Commit

Permalink
Impl refresh function
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Dec 11, 2023
1 parent 12266ba commit 472d0af
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
12 changes: 11 additions & 1 deletion Wox.UI.Flutter/wox/lib/components/wox_preview_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ class WoxPreviewView extends StatelessWidget {

Widget contentWidget = const SizedBox();
if (woxPreview.previewType == WoxPreviewTypeEnum.WOX_PREVIEW_TYPE_MARKDOWN.code) {
contentWidget = Markdown(data: woxPreview.previewData, padding: EdgeInsets.zero);
var styleTheme = Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.apply(
bodyColor: fromCssColor(woxTheme.previewFontColor),
displayColor: fromCssColor(woxTheme.previewFontColor),
));
contentWidget = Markdown(
data: woxPreview.previewData,
padding: EdgeInsets.zero,
selectable: true,
styleSheet: MarkdownStyleSheet.fromTheme(styleTheme),
);
} else if (woxPreview.previewType == WoxPreviewTypeEnum.WOX_PREVIEW_TYPE_TEXT.code) {
contentWidget = SelectableText(woxPreview.previewData, style: TextStyle(color: fromCssColor(woxTheme.previewFontColor)));
} else if (woxPreview.previewType == WoxPreviewTypeEnum.WOX_PREVIEW_TYPE_IMAGE.code) {
Expand Down
2 changes: 1 addition & 1 deletion Wox.UI.Flutter/wox/lib/entity/wox_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WoxPreview {
final Map<String, dynamic> data = <String, dynamic>{};
data['PreviewType'] = previewType;
data['PreviewData'] = previewData;
data['PreviewProperties'] = const JsonEncoder().convert(previewProperties);
data['PreviewProperties'] = previewProperties;
return data;
}

Expand Down
4 changes: 2 additions & 2 deletions Wox.UI.Flutter/wox/lib/entity/wox_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ class WoxRefreshableResult {
resultId = json['ResultId'];
title = json['Title'];
subTitle = json['SubTitle'] ?? "";
icon = json['Icon'];
preview = json['Preview'];
icon = WoxImage.fromJson(json['Icon']);
preview = WoxPreview.fromJson(json['Preview']);
contextData = json['ContextData'];
refreshInterval = json['RefreshInterval'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa

void handleWebSocketMessage(event) {
var msg = WoxWebsocketMsg.fromJson(jsonDecode(event));
if (msg.success == false) {
Logger.instance.error("Received error message: ${msg.toJson()}");
return;
}

Logger.instance.info("Received message: ${msg.toJson()}");
if (msg.method == "ToggleApp") {
toggleApp(ShowAppParams.fromJson(msg.data));
Expand Down Expand Up @@ -448,6 +453,10 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
queryResults[i].preview = result.preview;
queryResults[i].contextData = result.contextData;
queryResults[i].refreshInterval = result.refreshInterval;

currentPreview.value = result.preview;

queryResults.refresh();
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Wox.UI.Flutter/wox/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 16208599a12443d53889ba2270a4985981cfb204

COCOAPODS: 1.11.3
COCOAPODS: 1.12.1
20 changes: 18 additions & 2 deletions Wox/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (m *Manager) calculateResultScore(ctx context.Context, pluginId, title, sub
return score
}

func (m *Manager) PolishRefreshableResult(ctx context.Context, pluginInstance *Instance, result RefreshableResult) RefreshableResult {
func (m *Manager) PolishRefreshableResult(ctx context.Context, pluginInstance *Instance, resultId string, result RefreshableResult) RefreshableResult {
// convert icon
result.Icon = ConvertIcon(ctx, result.Icon, pluginInstance.PluginDirectory)
// translate title
Expand All @@ -487,6 +487,22 @@ func (m *Manager) PolishRefreshableResult(ctx context.Context, pluginInstance *I
previewProperties[translatedKey] = value
}
result.Preview.PreviewProperties = previewProperties

// store preview for ui invoke later
// because preview may contain some heavy data (E.g. image or large text), we will store preview in cache and only send preview to ui when user select the result
if result.Preview.PreviewType != "" && result.Preview.PreviewType != WoxPreviewTypeRemote {
// update preview in cache
if v, ok := m.resultCache.Load(resultId); ok {
v.Preview = result.Preview
m.resultCache.Store(resultId, v)
}

result.Preview = WoxPreview{
PreviewType: WoxPreviewTypeRemote,
PreviewData: fmt.Sprintf("/preview?id=%s", resultId),
}
}

return result
}

Expand Down Expand Up @@ -677,7 +693,7 @@ func (m *Manager) ExecuteRefresh(ctx context.Context, refreshableResultWithId Re
}

newResult := resultCache.Refresh(refreshableResult)
newResult = m.PolishRefreshableResult(ctx, resultCache.PluginInstance, newResult)
newResult = m.PolishRefreshableResult(ctx, resultCache.PluginInstance, refreshableResultWithId.ResultId, newResult)

return RefreshableResultWithResultId{
ResultId: refreshableResultWithId.ResultId,
Expand Down
1 change: 1 addition & 0 deletions Wox/ui/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ func serveAndWait(ctx context.Context, port int) {

func requestUI(ctx context.Context, request WebsocketMsg) {
request.Type = WebsocketMsgTypeRequest
request.Success = true
marshalData, marshalErr := json.Marshal(request)
if marshalErr != nil {
logger.Error(ctx, fmt.Sprintf("failed to marshal websocket request: %s", marshalErr.Error()))
Expand Down
11 changes: 11 additions & 0 deletions Wox/ui/ui_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ func handleRefresh(ctx context.Context, request WebsocketMsg) {
return
}

// replace remote preview with local preview
if result.Preview.PreviewType == plugin.WoxPreviewTypeRemote {
preview, err := plugin.GetPluginManager().GetResultPreview(util.NewTraceContext(), result.ResultId)
if err != nil {
logger.Error(ctx, err.Error())
responseUIError(ctx, request, err.Error())
return
}
result.Preview = preview
}

newResult, refreshErr := plugin.GetPluginManager().ExecuteRefresh(ctx, result)
if refreshErr != nil {
logger.Error(ctx, refreshErr.Error())
Expand Down

0 comments on commit 472d0af

Please sign in to comment.