Skip to content

Commit

Permalink
Optimize flutter repaint
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Dec 13, 2023
1 parent 64140af commit faf45de
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 161 deletions.
15 changes: 9 additions & 6 deletions Wox.UI.Flutter/wox/lib/components/wox_list_item_view.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:flutter/material.dart';
import 'package:from_css_color/from_css_color.dart';
import 'package:get/get.dart';
import 'package:wox/components/wox_image_view.dart';
import 'package:wox/entity/wox_image.dart';
import 'package:wox/entity/wox_theme.dart';
import 'package:wox/enums/wox_list_view_type_enum.dart';

class WoxListItemView extends StatelessWidget {
final bool isActive;
final WoxImage icon;
final Rx<WoxImage> icon;
final String title;
final String subTitle;
final WoxTheme woxTheme;
Expand Down Expand Up @@ -53,11 +54,13 @@ class WoxListItemView extends StatelessWidget {
children: [
Padding(
padding: const EdgeInsets.only(left: 5.0, right: 10.0),
child: WoxImageView(
woxImage: icon,
width: 30,
height: 30,
)),
child: Obx(() {
return WoxImageView(
woxImage: icon.value,
width: 30,
height: 30,
);
})),
Expanded(
child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
Expand Down
64 changes: 0 additions & 64 deletions Wox.UI.Flutter/wox/lib/components/wox_list_view.dart

This file was deleted.

11 changes: 6 additions & 5 deletions Wox.UI.Flutter/wox/lib/entity/wox_query.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:get/get.dart';
import 'package:wox/entity/wox_image.dart';
import 'package:wox/entity/wox_preview.dart';
import 'package:wox/enums/wox_last_query_mode_enum.dart';
Expand Down Expand Up @@ -81,7 +82,7 @@ class WoxQueryResult {
late String id;
late String title;
late String subTitle;
late WoxImage icon;
late Rx<WoxImage> icon;
late WoxPreview preview;
late int score;
late String contextData;
Expand All @@ -105,7 +106,7 @@ class WoxQueryResult {
id = json['Id'];
title = json['Title'];
subTitle = json['SubTitle'];
icon = (json['Icon'] != null ? WoxImage.fromJson(json['Icon']) : null)!;
icon = (json['Icon'] != null ? WoxImage.fromJson(json['Icon']).obs : null)!;
preview = (json['Preview'] != null ? WoxPreview.fromJson(json['Preview']) : null)!;
score = json['Score'];
contextData = json['ContextData'];
Expand Down Expand Up @@ -137,7 +138,7 @@ class WoxQueryResult {
class WoxResultAction {
late String id;
late String name;
late WoxImage icon;
late Rx<WoxImage> icon;
late bool isDefault;
late bool preventHideAfterAction;

Expand All @@ -146,7 +147,7 @@ class WoxResultAction {
WoxResultAction.fromJson(Map<String, dynamic> json) {
id = json['Id'];
name = json['Name'];
icon = (json['Icon'] != null ? WoxImage.fromJson(json['Icon']) : null)!;
icon = (json['Icon'] != null ? WoxImage.fromJson(json['Icon']).obs : null)!;
isDefault = json['IsDefault'];
preventHideAfterAction = json['PreventHideAfterAction'];
}
Expand All @@ -162,7 +163,7 @@ class WoxResultAction {
}

static WoxResultAction empty() {
return WoxResultAction(id: "", name: "", icon: WoxImage.empty(), isDefault: false, preventHideAfterAction: false);
return WoxResultAction(id: "", name: "", icon: WoxImage.empty().obs, isDefault: false, preventHideAfterAction: false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/services.dart';
import 'package:from_css_color/from_css_color.dart';
import 'package:get/get.dart';
import 'package:wox/components/wox_list_item_view.dart';
import 'package:wox/components/wox_list_view.dart';
import 'package:wox/components/wox_preview_view.dart';
import 'package:wox/entity/wox_query.dart';
import 'package:wox/enums/wox_direction_enum.dart';
Expand All @@ -21,31 +20,31 @@ class WoxQueryResultView extends GetView<WoxLauncherController> {
Widget getResultListView() {
return Scrollbar(
controller: controller.resultListViewScrollController,
child: Listener(
onPointerSignal: (event) {
if (event is PointerScrollEvent) {
controller.changeResultScrollPosition(WoxEventDeviceTypeEnum.WOX_EVENT_DEVEICE_TYPE_MOUSE.code,
event.scrollDelta.dy > 0 ? WoxDirectionEnum.WOX_DIRECTION_DOWN.code : WoxDirectionEnum.WOX_DIRECTION_UP.code);
}
child: Listener(onPointerSignal: (event) {
if (event is PointerScrollEvent) {
controller.changeResultScrollPosition(WoxEventDeviceTypeEnum.WOX_EVENT_DEVEICE_TYPE_MOUSE.code,
event.scrollDelta.dy > 0 ? WoxDirectionEnum.WOX_DIRECTION_DOWN.code : WoxDirectionEnum.WOX_DIRECTION_UP.code);
}
}, child: Obx(() {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
controller: controller.resultListViewScrollController,
itemCount: controller.queryResults.length,
itemExtent: WoxThemeUtil.instance.getResultListViewHeightByCount(1),
itemBuilder: (context, index) {
WoxQueryResult woxQueryResult = controller.getQueryResultByIndex(index);
return WoxListItemView(
key: controller.getResultItemGlobalKeyByIndex(index),
woxTheme: controller.woxTheme.value,
icon: woxQueryResult.icon,
title: woxQueryResult.title,
subTitle: woxQueryResult.subTitle,
isActive: controller.isQueryResultActiveByIndex(index),
listViewType: WoxListViewTypeEnum.WOX_LIST_VIEW_TYPE_RESULT.code);
},
child: ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
controller: controller.resultListViewScrollController,
itemCount: controller.queryResults.length,
itemExtent: WoxThemeUtil.instance.getResultListViewHeightByCount(1),
itemBuilder: (context, index) {
WoxQueryResult woxQueryResult = controller.getQueryResultByIndex(index);
return WoxListItemView(
key: controller.getResultItemGlobalKeyByIndex(index),
woxTheme: controller.woxTheme.value,
icon: woxQueryResult.icon,
title: woxQueryResult.title,
subTitle: woxQueryResult.subTitle,
isActive: controller.isQueryResultActiveByIndex(index),
listViewType: WoxListViewTypeEnum.WOX_LIST_VIEW_TYPE_RESULT.code);
},
)));
);
})));
}

Widget getResultActionListView() {
Expand Down Expand Up @@ -142,64 +141,63 @@ class WoxQueryResultView extends GetView<WoxLauncherController> {

@override
Widget build(BuildContext context) {
return Obx(() {
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: WoxThemeUtil.instance.getResultContainerMaxHeight()),
child: Stack(fit: (controller.isShowActionPanel.value || controller.isShowPreviewPanel.value) ? StackFit.expand : StackFit.loose, children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (controller.queryResults.isNotEmpty)
Expanded(
child: Container(
padding: EdgeInsets.only(
top: controller.woxTheme.value.resultContainerPaddingTop.toDouble(),
right: controller.woxTheme.value.resultContainerPaddingRight.toDouble(),
bottom: controller.woxTheme.value.resultContainerPaddingBottom.toDouble(),
left: controller.woxTheme.value.resultContainerPaddingLeft.toDouble(),
),
child: getResultListView()),
),
if (controller.isShowPreviewPanel.value)
Expanded(
child: WoxPreviewView(
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: WoxThemeUtil.instance.getResultContainerMaxHeight()),
child: Stack(fit: (controller.isShowActionPanel.value || controller.isShowPreviewPanel.value) ? StackFit.expand : StackFit.loose, children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (controller.queryResults.isNotEmpty)
Expanded(
child: Container(
padding: EdgeInsets.only(
top: controller.woxTheme.value.resultContainerPaddingTop.toDouble(),
right: controller.woxTheme.value.resultContainerPaddingRight.toDouble(),
bottom: controller.woxTheme.value.resultContainerPaddingBottom.toDouble(),
left: controller.woxTheme.value.resultContainerPaddingLeft.toDouble(),
),
child: getResultListView()),
),
if (controller.isShowPreviewPanel.value)
Expanded(child: Obx(() {
return WoxPreviewView(
woxPreview: controller.currentPreview.value,
woxTheme: controller.woxTheme.value,
)),
],
),
if (controller.isShowActionPanel.value)
Positioned(
right: 10,
bottom: 10,
child: Container(
padding: EdgeInsets.only(
top: controller.woxTheme.value.actionContainerPaddingTop.toDouble(),
bottom: controller.woxTheme.value.actionContainerPaddingBottom.toDouble(),
left: controller.woxTheme.value.actionContainerPaddingLeft.toDouble(),
right: controller.woxTheme.value.actionContainerPaddingRight.toDouble(),
),
decoration: BoxDecoration(
color: fromCssColor(controller.woxTheme.value.actionContainerBackgroundColor),
borderRadius: BorderRadius.circular(controller.woxTheme.value.actionQueryBoxBorderRadius.toDouble()),
),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 320),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Actions", style: TextStyle(color: fromCssColor(controller.woxTheme.value.actionContainerHeaderFontColor), fontSize: 16.0)),
const Divider(),
getResultActionListView(),
getActionQueryBox(),
],
),
);
})),
],
),
if (controller.isShowActionPanel.value)
Positioned(
right: 10,
bottom: 10,
child: Container(
padding: EdgeInsets.only(
top: controller.woxTheme.value.actionContainerPaddingTop.toDouble(),
bottom: controller.woxTheme.value.actionContainerPaddingBottom.toDouble(),
left: controller.woxTheme.value.actionContainerPaddingLeft.toDouble(),
right: controller.woxTheme.value.actionContainerPaddingRight.toDouble(),
),
decoration: BoxDecoration(
color: fromCssColor(controller.woxTheme.value.actionContainerBackgroundColor),
borderRadius: BorderRadius.circular(controller.woxTheme.value.actionQueryBoxBorderRadius.toDouble()),
),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 320),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Actions", style: TextStyle(color: fromCssColor(controller.woxTheme.value.actionContainerHeaderFontColor), fontSize: 16.0)),
const Divider(),
getResultActionListView(),
getActionQueryBox(),
],
),
),
),
]),
);
});
),
]),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
resultId: result.id,
title: result.title,
subTitle: result.subTitle,
icon: result.icon,
icon: result.icon.value,
preview: result.preview,
contextData: result.contextData,
refreshInterval: result.refreshInterval,
Expand All @@ -461,8 +461,8 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
queryResults[i].subTitle = result.subTitle;
hasAnyChange = true;
}
if (queryResults[i].icon != result.icon) {
queryResults[i].icon = result.icon;
if (queryResults[i].icon.value != result.icon) {
queryResults[i].icon.value = result.icon;
hasAnyChange = true;
}
if (queryResults[i].preview != result.preview) {
Expand All @@ -479,10 +479,6 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
hasAnyChange = true;
}

if (hasAnyChange) {
queryResults.refresh();
}

break;
}
}
Expand Down

0 comments on commit faf45de

Please sign in to comment.