Skip to content

Commit 8b323aa

Browse files
committed
fix(setting): fix issues that some settings can't be saved
* Changed the type of `value` in `PostSettingUpdate` and `handleSettingWoxUpdate` functions from `any` to `string`. * Improved parsing logic for boolean and float values from string input. * Enhanced error handling for AI provider settings in the Flutter UI.
1 parent 4ae8dac commit 8b323aa

File tree

3 files changed

+99
-76
lines changed

3 files changed

+99
-76
lines changed

wox.core/ui/manager.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99
"os"
1010
"path/filepath"
11+
"strconv"
1112
"strings"
1213
"sync"
1314
"wox/common"
@@ -482,18 +483,24 @@ func (m *Manager) HideTray() {
482483
tray.RemoveTray()
483484
}
484485

485-
func (m *Manager) PostSettingUpdate(ctx context.Context, key string, value any) {
486+
func (m *Manager) PostSettingUpdate(ctx context.Context, key string, value string) {
487+
var vb bool
488+
var vs = value
489+
if vb1, err := strconv.ParseBool(vs); err == nil {
490+
vb = vb1
491+
}
492+
486493
switch key {
487494
case "ShowTray":
488-
if value.(bool) {
495+
if vb {
489496
m.ShowTray()
490497
} else {
491498
m.HideTray()
492499
}
493500
case "MainHotkey":
494-
m.RegisterMainHotkey(ctx, value.(string))
501+
m.RegisterMainHotkey(ctx, vs)
495502
case "SelectionHotkey":
496-
m.RegisterSelectionHotkey(ctx, value.(string))
503+
m.RegisterSelectionHotkey(ctx, vs)
497504
case "QueryHotkeys":
498505
// unregister previous hotkeys
499506
logger.Info(ctx, "post update query hotkeys, unregister previous query hotkeys")
@@ -507,13 +514,13 @@ func (m *Manager) PostSettingUpdate(ctx context.Context, key string, value any)
507514
m.RegisterQueryHotkey(ctx, queryHotkey)
508515
}
509516
case "LangCode":
510-
langCode := value.(string)
517+
langCode := vs
511518
langErr := i18n.GetI18nManager().UpdateLang(ctx, i18n.LangCode(langCode))
512519
if langErr != nil {
513520
logger.Error(ctx, fmt.Sprintf("failed to update lang: %s", langErr.Error()))
514521
}
515522
case "EnableAutostart":
516-
enabled := value.(bool)
523+
enabled := vb
517524
err := autostart.SetAutostart(ctx, enabled)
518525
if err != nil {
519526
logger.Error(ctx, fmt.Sprintf("failed to set autostart: %s", err.Error()))

wox.core/ui/router.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func handleSettingWox(w http.ResponseWriter, r *http.Request) {
507507
func handleSettingWoxUpdate(w http.ResponseWriter, r *http.Request) {
508508
type keyValuePair struct {
509509
Key string
510-
Value any
510+
Value string
511511
}
512512

513513
decoder := json.NewDecoder(r.Body)
@@ -523,22 +523,12 @@ func handleSettingWoxUpdate(w http.ResponseWriter, r *http.Request) {
523523

524524
var vb bool
525525
var vf float64
526-
var vs string
527-
switch v := kv.Value.(type) {
528-
case bool:
529-
vb = v
530-
case float64:
531-
vf = v
532-
case string:
533-
vs = v
534-
vb1, err := strconv.ParseBool(vs)
535-
if err == nil {
536-
vb = vb1
537-
}
538-
vf1, err := strconv.ParseFloat(vs, 64)
539-
if err == nil {
540-
vf = vf1
541-
}
526+
var vs = kv.Value
527+
if vb1, err := strconv.ParseBool(vs); err == nil {
528+
vb = vb1
529+
}
530+
if vf1, err := strconv.ParseFloat(vs, 64); err == nil {
531+
vf = vf1
542532
}
543533

544534
switch kv.Key {
@@ -560,10 +550,31 @@ func handleSettingWoxUpdate(w http.ResponseWriter, r *http.Request) {
560550
woxSetting.ShowTray.Set(vb)
561551
case "LangCode":
562552
woxSetting.LangCode.Set(i18n.LangCode(vs))
553+
case "QueryHotkeys":
554+
var queryHotkeys []setting.QueryHotkey
555+
if err := json.Unmarshal([]byte(vs), &queryHotkeys); err != nil {
556+
writeErrorResponse(w, err.Error())
557+
return
558+
}
559+
woxSetting.QueryHotkeys.Set(queryHotkeys)
560+
case "QueryShortcuts":
561+
var queryShortcuts []setting.QueryShortcut
562+
if err := json.Unmarshal([]byte(vs), &queryShortcuts); err != nil {
563+
writeErrorResponse(w, err.Error())
564+
return
565+
}
566+
woxSetting.QueryShortcuts.Set(queryShortcuts)
563567
case "QueryMode":
564568
woxSetting.QueryMode.Set(vs)
565569
case "ShowPosition":
566570
woxSetting.ShowPosition.Set(setting.PositionType(vs))
571+
case "AIProviders":
572+
var aiProviders []setting.AIProvider
573+
if err := json.Unmarshal([]byte(vs), &aiProviders); err != nil {
574+
writeErrorResponse(w, err.Error())
575+
return
576+
}
577+
woxSetting.AIProviders.Set(aiProviders)
567578
case "EnableAutoBackup":
568579
woxSetting.EnableAutoBackup.Set(vb)
569580
case "EnableAutoUpdate":
@@ -572,10 +583,12 @@ func handleSettingWoxUpdate(w http.ResponseWriter, r *http.Request) {
572583
woxSetting.CustomPythonPath.Set(vs)
573584
case "CustomNodejsPath":
574585
woxSetting.CustomNodejsPath.Set(vs)
586+
575587
case "HttpProxyEnabled":
576588
woxSetting.HttpProxyEnabled.Set(vb)
577589
case "HttpProxyUrl":
578590
woxSetting.HttpProxyUrl.Set(vs)
591+
579592
case "AppWidth":
580593
woxSetting.AppWidth.Set(int(vf))
581594
case "MaxResultCount":

wox.ui.flutter/wox/lib/modules/setting/views/wox_setting_ai_view.dart

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22

33
import 'package:fluent_ui/fluent_ui.dart';
4+
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
45
import 'package:wox/api/wox_api.dart';
56
import 'package:wox/components/plugin/wox_setting_plugin_table_view.dart';
67
import 'package:wox/entity/setting/wox_plugin_setting_table.dart';
@@ -18,60 +19,62 @@ class WoxSettingAIView extends WoxSettingBaseView {
1819
return form(children: [
1920
formField(
2021
label: controller.tr("ui_ai_model"),
21-
child: WoxSettingPluginTable(
22-
value: json.encode(controller.woxSetting.value.aiProviders),
23-
item: PluginSettingValueTable.fromJson({
24-
"Key": "AIProviders",
25-
"Columns": [
26-
{
27-
"Key": "Name",
28-
"Label": "i18n:ui_ai_providers_name",
29-
"Tooltip": "i18n:ui_ai_providers_name_tooltip",
30-
"Width": 100,
31-
"Type": "select",
32-
"SelectOptions": snapshot.data!.map((e) => {"Label": e.name, "Value": e.name}).toList(),
33-
"TextMaxLines": 1,
34-
"Validators": [
35-
{"Type": "not_empty"}
36-
],
37-
},
38-
{
39-
"Key": "ApiKey",
40-
"Label": "i18n:ui_ai_providers_api_key",
41-
"Tooltip": "i18n:ui_ai_providers_api_key_tooltip",
42-
"Type": "text",
43-
"TextMaxLines": 1,
44-
"Width": 250,
45-
},
46-
{
47-
"Key": "Host",
48-
"Label": "i18n:ui_ai_providers_host",
49-
"Tooltip": "i18n:ui_ai_providers_host_tooltip",
50-
"Width": 160,
51-
"Type": "text",
52-
},
53-
{
54-
"Key": "Status",
55-
"Label": "i18n:ui_ai_providers_status",
56-
"HideInUpdate": true,
57-
"Width": 60,
58-
"Type": PluginSettingValueType.pluginSettingValueTableColumnTypeAIModelStatus,
22+
child: Obx(() {
23+
return WoxSettingPluginTable(
24+
value: json.encode(controller.woxSetting.value.aiProviders),
25+
item: PluginSettingValueTable.fromJson({
26+
"Key": "AIProviders",
27+
"Columns": [
28+
{
29+
"Key": "Name",
30+
"Label": "i18n:ui_ai_providers_name",
31+
"Tooltip": "i18n:ui_ai_providers_name_tooltip",
32+
"Width": 100,
33+
"Type": "select",
34+
"SelectOptions": snapshot.data!.map((e) => {"Label": e.name, "Value": e.name}).toList(),
35+
"TextMaxLines": 1,
36+
"Validators": [
37+
{"Type": "not_empty"}
38+
],
39+
},
40+
{
41+
"Key": "ApiKey",
42+
"Label": "i18n:ui_ai_providers_api_key",
43+
"Tooltip": "i18n:ui_ai_providers_api_key_tooltip",
44+
"Type": "text",
45+
"TextMaxLines": 1,
46+
"Width": 250,
47+
},
48+
{
49+
"Key": "Host",
50+
"Label": "i18n:ui_ai_providers_host",
51+
"Tooltip": "i18n:ui_ai_providers_host_tooltip",
52+
"Width": 160,
53+
"Type": "text",
54+
},
55+
{
56+
"Key": "Status",
57+
"Label": "i18n:ui_ai_providers_status",
58+
"HideInUpdate": true,
59+
"Width": 60,
60+
"Type": PluginSettingValueType.pluginSettingValueTableColumnTypeAIModelStatus,
61+
}
62+
],
63+
"SortColumnKey": "Name"
64+
}),
65+
onUpdate: (key, value) {
66+
controller.updateConfig("AIProviders", value);
67+
},
68+
onUpdateValidate: (rowValues) async {
69+
if (rowValues["Name"] != "ollama") {
70+
if (rowValues["ApiKey"] == null || rowValues["ApiKey"] == "") {
71+
return controller.tr("ui_ai_providers_api_key_required");
72+
}
5973
}
60-
],
61-
"SortColumnKey": "Name"
62-
}),
63-
onUpdate: (key, value) {
64-
controller.updateConfig("AIProviders", value);
65-
},
66-
onUpdateValidate: (rowValues) async {
67-
if (rowValues["Name"] != "ollama") {
68-
if (rowValues["ApiKey"] == null || rowValues["ApiKey"] == "") {
69-
return controller.tr("ui_ai_providers_api_key_required");
70-
}
71-
}
72-
return null;
73-
},
74-
),
74+
return null;
75+
},
76+
);
77+
}),
7578
),
7679
]);
7780
}

0 commit comments

Comments
 (0)