Skip to content

Commit

Permalink
Add theme plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Dec 22, 2023
1 parent 51e7501 commit aebd386
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 102 deletions.
10 changes: 9 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 @@ -82,7 +82,15 @@ class WoxPreviewView extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: contentWidget),
Expanded(
child: Theme(
data: ThemeData(
textSelectionTheme: TextSelectionThemeData(
selectionColor: fromCssColor(woxTheme.previewTextSelectionColor),
),
),
child: contentWidget),
),
//show previewProperties
if (woxPreview.previewProperties.isNotEmpty)
Container(
Expand Down
10 changes: 9 additions & 1 deletion Wox.UI.Flutter/wox/lib/entity/wox_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class WoxTheme {
late String queryBoxBackgroundColor;
late int queryBoxBorderRadius;
late String queryBoxCursorColor;
late String queryBoxTextSelectionColor;
late String actionContainerBackgroundColor;
late String actionContainerHeaderFontColor;
late int actionContainerPaddingLeft;
Expand All @@ -45,6 +46,7 @@ class WoxTheme {
late String previewSplitLineColor;
late String previewPropertyTitleColor;
late String previewPropertyContentColor;
late String previewTextSelectionColor;

WoxTheme(
{themeId,
Expand Down Expand Up @@ -77,6 +79,7 @@ class WoxTheme {
queryBoxBackgroundColor,
queryBoxBorderRadius,
queryBoxCursorColor,
queryBoxTextSelectionColor,
actionContainerBackgroundColor,
actionContainerHeaderFontColor,
actionContainerPaddingLeft,
Expand All @@ -92,7 +95,8 @@ class WoxTheme {
previewFontColor,
previewSplitLineColor,
previewPropertyTitleColor,
previewPropertyContentColor});
previewPropertyContentColor,
previewTextSelectionColor});

WoxTheme.fromJson(Map<String, dynamic> json) {
themeId = json['ThemeId'];
Expand Down Expand Up @@ -125,6 +129,7 @@ class WoxTheme {
queryBoxBackgroundColor = json['QueryBoxBackgroundColor'];
queryBoxBorderRadius = json['QueryBoxBorderRadius'];
queryBoxCursorColor = json['QueryBoxCursorColor'];
queryBoxTextSelectionColor = json['QueryBoxTextSelectionColor'];
actionContainerBackgroundColor = json['ActionContainerBackgroundColor'];
actionContainerHeaderFontColor = json['ActionContainerHeaderFontColor'];
actionContainerPaddingLeft = json['ActionContainerPaddingLeft'];
Expand All @@ -141,6 +146,7 @@ class WoxTheme {
previewSplitLineColor = json['PreviewSplitLineColor'];
previewPropertyTitleColor = json['PreviewPropertyTitleColor'];
previewPropertyContentColor = json['PreviewPropertyContentColor'];
previewTextSelectionColor = json['PreviewTextSelectionColor'];
}

Map<String, dynamic> toJson() {
Expand Down Expand Up @@ -175,6 +181,7 @@ class WoxTheme {
data['QueryBoxBackgroundColor'] = queryBoxBackgroundColor;
data['QueryBoxBorderRadius'] = queryBoxBorderRadius;
data['QueryBoxCursorColor'] = queryBoxCursorColor;
data['QueryBoxTextSelectionColor'] = queryBoxTextSelectionColor;
data['ActionContainerBackgroundColor'] = actionContainerBackgroundColor;
data['ActionContainerHeaderFontColor'] = actionContainerHeaderFontColor;
data['ActionContainerPaddingLeft'] = actionContainerPaddingLeft;
Expand All @@ -191,6 +198,7 @@ class WoxTheme {
data['PreviewSplitLineColor'] = previewSplitLineColor;
data['PreviewPropertyTitleColor'] = previewPropertyTitleColor;
data['PreviewPropertyContentColor'] = previewPropertyContentColor;
data['PreviewTextSelectionColor'] = previewTextSelectionColor;
return data;
}
}
4 changes: 0 additions & 4 deletions Wox.UI.Flutter/wox/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:io';
import 'package:chinese_font_library/chinese_font_library.dart';
import 'package:flutter/material.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart';
import 'package:from_css_color/from_css_color.dart';
import 'package:get/get.dart';
import 'package:window_manager/window_manager.dart';
import 'package:wox/modules/launcher/views/wox_launcher_view.dart';
Expand Down Expand Up @@ -87,9 +86,6 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textSelectionTheme: TextSelectionThemeData(
selectionColor: fromCssColor(WoxThemeUtil.instance.currentTheme.resultItemActiveBackgroundColor),
),
textTheme: SystemChineseFont.textTheme(Brightness.light),
),
debugShowCheckedModeBanner: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,51 +57,58 @@ class WoxQueryBoxView extends GetView<WoxLauncherController> {
}),
child: SizedBox(
height: 55.0,
child: TextField(
style: TextStyle(
fontSize: 28.0,
color: fromCssColor(controller.woxTheme.value.queryBoxFontColor),
child: Theme(
data: ThemeData(
textSelectionTheme: TextSelectionThemeData(
selectionColor: fromCssColor(controller.woxTheme.value.queryBoxTextSelectionColor),
),
),
decoration: InputDecoration(
isCollapsed: true,
contentPadding: const EdgeInsets.only(
left: 8,
right: 8,
top: 10,
bottom: 18,
child: TextField(
style: TextStyle(
fontSize: 28.0,
color: fromCssColor(controller.woxTheme.value.queryBoxFontColor),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(controller.woxTheme.value.queryBoxBorderRadius.toDouble()),
borderSide: BorderSide.none,
decoration: InputDecoration(
isCollapsed: true,
contentPadding: const EdgeInsets.only(
left: 8,
right: 8,
top: 10,
bottom: 18,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(controller.woxTheme.value.queryBoxBorderRadius.toDouble()),
borderSide: BorderSide.none,
),
filled: true,
fillColor: fromCssColor(controller.woxTheme.value.queryBoxBackgroundColor),
),
filled: true,
fillColor: fromCssColor(controller.woxTheme.value.queryBoxBackgroundColor),
),
cursorColor: fromCssColor(controller.woxTheme.value.queryBoxCursorColor),
autofocus: true,
focusNode: controller.queryBoxFocusNode,
controller: controller.queryBoxTextFieldController,
scrollController: controller.queryBoxScrollController,
onChanged: (value) {
// isComposingRangeValid is not reliable on Windows, we need to use inside post frame callback to check the value
// see https://github.com/flutter/flutter/issues/128565#issuecomment-1772016743
WidgetsBinding.instance.addPostFrameCallback((_) {
// if the composing range is valid, which means the text is changed by IME and the query is not finished yet,
// we should not trigger the query until the composing is finished.
if (controller.queryBoxTextFieldController.value.isComposingRangeValid) {
return;
}
cursorColor: fromCssColor(controller.woxTheme.value.queryBoxCursorColor),
autofocus: true,
focusNode: controller.queryBoxFocusNode,
controller: controller.queryBoxTextFieldController,
scrollController: controller.queryBoxScrollController,
onChanged: (value) {
// isComposingRangeValid is not reliable on Windows, we need to use inside post frame callback to check the value
// see https://github.com/flutter/flutter/issues/128565#issuecomment-1772016743
WidgetsBinding.instance.addPostFrameCallback((_) {
// if the composing range is valid, which means the text is changed by IME and the query is not finished yet,
// we should not trigger the query until the composing is finished.
if (controller.queryBoxTextFieldController.value.isComposingRangeValid) {
return;
}

controller.canArrowUpHistory = false;
WoxChangeQuery woxChangeQuery = WoxChangeQuery(
queryId: const UuidV4().generate(),
queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code,
queryText: value,
querySelection: Selection.empty(),
);
controller.onQueryChanged(woxChangeQuery);
});
},
controller.canArrowUpHistory = false;
WoxChangeQuery woxChangeQuery = WoxChangeQuery(
queryId: const UuidV4().generate(),
queryType: WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code,
queryText: value,
querySelection: Selection.empty(),
);
controller.onQueryChanged(woxChangeQuery);
});
},
),
),
))),
Positioned(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,16 @@ class WoxLauncherController extends GetxController implements WoxLauncherInterfa
isShowActionPanel.value = false;

if (query.queryType == WoxQueryTypeEnum.WOX_QUERY_TYPE_INPUT.code) {
// save the cursor position
var cursorPosition = queryBoxTextFieldController.selection.baseOffset;
queryBoxTextFieldController.text = query.queryText;

if (moveCursorToEnd) {
moveQueryBoxCursorToEnd();
} else {
// try to restore the cursor position after set text, which will reset the cursor position
if (queryBoxTextFieldController.text != query.queryText) {
// save the cursor position and then restore it after set text
var cursorPosition = queryBoxTextFieldController.selection.baseOffset;
queryBoxTextFieldController.text = query.queryText;
cursorPosition = cursorPosition > queryBoxTextFieldController.text.length ? queryBoxTextFieldController.text.length : cursorPosition;
queryBoxTextFieldController.selection = TextSelection(baseOffset: cursorPosition, extentOffset: cursorPosition);
}
if (moveCursorToEnd) {
moveQueryBoxCursorToEnd();
}
} else {
queryBoxTextFieldController.text = query.toString();
}
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.12.1
COCOAPODS: 1.11.3
70 changes: 70 additions & 0 deletions Wox/plugin/system/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package system

import (
"context"
"github.com/samber/lo"
"wox/plugin"
"wox/share"
)

var themeIcon = plugin.NewWoxImageBase64(`data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAFnUlEQVR4nO2Ye0xTVxzHK1vibNzmlkVYFl0mvmI2Mx9To3PqBE3Axflc1EBEBApGDWocRLFVHpPhA2RqpoLKotOB8q48CgVK8IEUqkCx3HsujyiYIMJEAa39Lvdut7QC5YJW68Iv+fzT3PZ+PvScU1qRaHAGZ3D+n3P/XL6s80imHgfl6InO6Ax945/5QSJbnc7ojF7leTqiM/QiWx30Ic9j6TUaGraPbGbW7W8mXm6ity3gIbN1RDvt+MRARGBpo2fcfVDjO/utCWglS3J5eR49eQ8ttEseG2fTAU3M5ll6epiZvCkd5LOnDxg3mc0GPKKnN/Qmb0oLvSzJ5gKaa9yChMjz74TIWnOtRAMhmD5Hp9MNbSfjO4QGPGamNdpUgJbU7a5mbuIhWQ4DGWJR/jl5By3Ee5XVAjpXSSAE/vr6a5JZRJv4RMvUgaWWiUc7mdhrQCvtVGY1+YEEtBTNuNuROwRNN5ZDR6m5i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9ic5T5EAIfUHrWq5s8y4KYLVCpSyH2TITYp7u4uB8BLIIDFLl5EAIb8CzaGZMP/mgmPvrQSszbswc3b1egnCK9ioutFZCdkwtT1vsHwMM/0OwxFjbgzOmujftRuCvmHQzAaFcp7J2CjWKvPSBLkQNTPPwDsN4/0OwxlpqopcaNOz3KFxPXSmG/cC8cFoXCYVHYmwvIzFZACG6H1mDs4bWYvUMG+++lcHAO4cR53lhARmYWhDBHthWfOu+Bg3OwmfgbD8hS5OivZGTCEhcupWDk/EDjcrF2gGt/jlHV1etBmdkKvfxKBnoiISkdCz0iYO+0r1f5VxngWtKqD9I07RYcMDiDMziit2L+Af+A5zjc04biAAAAAElFTkSuQmCC`)

func init() {
plugin.AllSystemPlugin = append(plugin.AllSystemPlugin, &ThemePlugin{})
}

type ThemePlugin struct {
api plugin.API
}

func (c *ThemePlugin) GetMetadata() plugin.Metadata {
return plugin.Metadata{
Id: "58a59382-8b3a-48c2-89ac-0a9a0e12e03f",
Name: "Theme manager",
Author: "Wox Launcher",
Website: "https://github.com/Wox-launcher/Wox",
Version: "1.0.0",
MinWoxVersion: "2.0.0",
Runtime: "Nodejs",
Description: "Theme manager",
Icon: themeIcon.String(),
Entry: "",
TriggerKeywords: []string{
"theme",
},
Commands: []plugin.MetadataCommand{},
SupportedOS: []string{
"Windows",
"Macos",
"Linux",
},
}
}

func (c *ThemePlugin) Init(ctx context.Context, initParams plugin.InitParams) {
c.api = initParams.API
}

func (c *ThemePlugin) Query(ctx context.Context, query plugin.Query) []plugin.QueryResult {
ui := plugin.GetPluginManager().GetUI()
return lo.FilterMap(ui.GetAllThemes(ctx), func(theme share.Theme, _ int) (plugin.QueryResult, bool) {
match, _ := IsStringMatchScore(ctx, theme.ThemeName, query.Search)
if match {
return plugin.QueryResult{
Title: theme.ThemeName,
Icon: themeIcon,
Actions: []plugin.QueryResultAction{
{
Name: "Change theme",
PreventHideAfterAction: true,
Action: func(actionContext plugin.ActionContext) {
ui.ChangeTheme(ctx, theme)
},
},
},
}, true
} else {
return plugin.QueryResult{}, false
}
})
}
4 changes: 3 additions & 1 deletion Wox/resource/ui/themes/dark.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"QueryBoxBackgroundColor": "rgba(24, 25, 26, 0.4)",
"QueryBoxBorderRadius": 8,
"QueryBoxCursorColor": "#e4e7e7",
"QueryBoxTextSelectionColor": "rgba(31, 118, 124, 0.8)",
"ActionContainerBackgroundColor": "#373839",
"ActionContainerHeaderFontColor": "#e4e7e7",
"ActionContainerPaddingLeft": 14,
Expand All @@ -44,5 +45,6 @@
"PreviewFontColor": "#f0f0f0",
"PreviewSplitLineColor": "#3a474e",
"PreviewPropertyTitleColor": "#b0b3b5",
"PreviewPropertyContentColor": "#f0f0f0"
"PreviewPropertyContentColor": "#f0f0f0",
"PreviewTextSelectionColor": "rgba(31, 118, 124, 0.8)"
}
4 changes: 3 additions & 1 deletion Wox/resource/ui/themes/light.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"QueryBoxBackgroundColor": "rgba(255,255,255,0.5)",
"QueryBoxBorderRadius": 8,
"QueryBoxCursorColor": "#000000",
"QueryBoxTextSelectionColor": "#529df7",
"ActionContainerBackgroundColor": "rgba(236, 240, 240, 1)",
"ActionContainerHeaderFontColor": "#454545",
"ActionContainerPaddingLeft": 14,
Expand All @@ -44,5 +45,6 @@
"PreviewFontColor": "#454545",
"PreviewSplitLineColor": "#caced0",
"PreviewPropertyTitleColor": "#716b6b",
"PreviewPropertyContentColor": "#716b6b"
"PreviewPropertyContentColor": "#716b6b",
"PreviewTextSelectionColor": "#529df7"
}
4 changes: 3 additions & 1 deletion Wox/ui/theme.go → Wox/share/theme.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ui
package share

type Theme struct {
ThemeId string
Expand Down Expand Up @@ -32,6 +32,7 @@ type Theme struct {
QueryBoxBackgroundColor string
QueryBoxBorderRadius int
QueryBoxCursorColor string
QueryBoxTextSelectionColor string
ActionContainerBackgroundColor string
ActionContainerHeaderFontColor string
ActionContainerPaddingLeft int
Expand All @@ -48,4 +49,5 @@ type Theme struct {
PreviewSplitLineColor string
PreviewPropertyTitleColor string
PreviewPropertyContentColor string
PreviewTextSelectionColor string
}
3 changes: 2 additions & 1 deletion Wox/share/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ type UI interface {
ToggleApp(ctx context.Context)
ShowMsg(ctx context.Context, title string, description string, icon string)
GetServerPort(ctx context.Context) int
ChangeTheme(ctx context.Context, theme string)
ChangeTheme(ctx context.Context, theme Theme)
OpenSettingWindow(ctx context.Context)
OpenDevTools(ctx context.Context)
GetAllThemes(ctx context.Context) []Theme
}

type ShowContext struct {
Expand Down
2 changes: 2 additions & 0 deletions Wox/ui/dto/theme_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Theme struct {
QueryBoxBackgroundColor string
QueryBoxBorderRadius int
QueryBoxCursorColor string
QueryBoxTextSelectionColor string
ActionContainerBackgroundColor string
ActionContainerHeaderFontColor string
ActionContainerPaddingLeft int
Expand All @@ -51,4 +52,5 @@ type Theme struct {
PreviewSplitLineColor string
PreviewPropertyTitleColor string
PreviewPropertyContentColor string
PreviewTextSelectionColor string
}
Loading

0 comments on commit aebd386

Please sign in to comment.