-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
2915 lines (2821 loc) · 94 KB
/
Copy pathindex.tsx
File metadata and controls
2915 lines (2821 loc) · 94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
Button,
ColorPicker,
ForEach,
HStack,
Image,
List,
Navigation,
NavigationLink,
NavigationStack,
Path,
Picker,
Script,
Section,
Slider,
Spacer,
Text,
TextField,
Toggle,
useObservable,
useRef,
useState,
VStack,
} from "scripting";
import {
type ActionSendMode,
CANDIDATE_BAR_HEIGHT_MAX,
CANDIDATE_BAR_HEIGHT_MIN,
type CandidateMenuAction,
type CandidateRightButtonMode,
COMPOSING_FUNCTION_KEYS,
DEFAULT_CANDIDATE_MENU_ACTIONS,
DEFAULT_KEY_COLORS,
DEFAULT_KEY_FONT_COLORS,
DEFAULT_KEY_HINT_COLORS,
DEFAULT_LETTER_SWIPE_DOWN,
DEFAULT_LETTER_SWIPE_DOWN_SYMBOLS,
DEFAULT_RIME_KEYBOARD_SETTINGS,
FUNCTION_KEYS,
FUNCTION_ROW_OFF_LETTER_SWIPE_DOWN,
FUNCTION_ROW_OFF_LETTER_SWIPE_DOWN_SYMBOLS,
HAPTIC_LEVEL_MAX,
HAPTIC_LEVEL_MIN,
KEY_VISUAL_INSET_MAX,
KEY_VISUAL_INSET_MIN,
KEYBOARD_HEIGHT_MAX,
KEYBOARD_HEIGHT_MIN,
type KeyColorPair,
type KeyColorScheme,
type KeyColorSettings,
LETTER_KEYS,
LETTER_LONG_PRESS_DURATION_MAX,
LETTER_LONG_PRESS_DURATION_MIN,
loadRimeKeyboardSettings,
normalizeRimeKeyboardSettings,
type RimeKeyboardSettings,
type RimeKeyboardTheme,
saveRimeKeyboardSettings,
SWIPE_TRIGGER_DISTANCE_MAX,
SWIPE_TRIGGER_DISTANCE_MIN,
TOOLBAR_LEFT_BUTTON_MAX,
type ToolbarButtonConfig,
} from "./settings";
const THEME_OPTIONS: Array<{ value: RimeKeyboardTheme; label: string }> = [
{ value: "system", label: "跟随系统" },
{ value: "light", label: "浅色" },
{ value: "dark", label: "深色" },
];
const CANDIDATE_RIGHT_BUTTON_OPTIONS: Array<
{ value: CandidateRightButtonMode; label: string }
> = [
{ value: "dismiss", label: "收起键盘" },
{ value: "expand", label: "展开候选" },
{ value: "hidden", label: "不显示" },
];
const ACTION_MODE_OPTIONS: Array<{ value: ActionSendMode; label: string }> = [
{ value: "auto", label: "自动" },
{ value: "rime", label: "发送给 Rime" },
{ value: "direct", label: "直接上屏" },
];
const FUNCTION_KEY_LABELS: Record<string, string> = {
left: "左移",
head: "行首",
select: "全选",
cut: "剪切",
copy: "复制",
paste: "粘贴",
tail: "行尾",
right: "右移",
page: "翻页",
tone1: "一声",
tone2: "二声",
tone3: "三声",
tone4: "四声",
filter: "包裹",
};
function moveItems<T>(items: T[], indices: number[], newOffset: number) {
const result = items.slice();
const moving = indices
.slice()
.sort((a, b) => b - a)
.map((index) => result.splice(index, 1)[0])
.reverse()
.filter((item): item is T => item != null);
const removedBeforeOffset = indices.filter((index) => index < newOffset)
.length;
const insertAt = Math.max(
0,
Math.min(result.length, newOffset - removedBeforeOffset),
);
result.splice(insertAt, 0, ...moving);
return result;
}
const COMMAND_REFERENCE_GROUPS: Array<{
title: string;
items: Array<{ command: string; description: string }>;
}> = [
{
title: "编辑与光标",
items: [
{ command: "{left}", description: "光标左移一位" },
{ command: "{right}", description: "光标右移一位" },
{ command: "{home}", description: "移动到行首" },
{ command: "{end}", description: "移动到行尾" },
{ command: "{selectAll}", description: "执行全选" },
{
command: "{toggleSelectAll}",
description: "全选/取消全选切换",
},
{ command: "{cut}", description: "剪切选中内容" },
{ command: "{copy}", description: "复制选中内容" },
{ command: "{paste}", description: "粘贴剪贴板文本" },
],
},
{
title: "Rime 候选与预编辑",
items: [
{ command: "{rimeUp}", description: "发送 Rime 上方向键" },
{ command: "{rimeDown}", description: "发送 Rime 下方向键" },
{ command: "{rimePageUp}", description: "发送 Rime 上翻页" },
{ command: "{rimePageDown}", description: "发送 Rime 下翻页" },
{ command: "{commitComposition}", description: "提交当前预编辑" },
],
},
{
title: "文本处理",
items: [
{ command: "{deleteAll}", description: "删除当前可删除文本" },
{ command: "{restoreDeleted}", description: "恢复最近删除内容" },
{ command: "{clearComposition}", description: "清空/删除当前预编辑拼音" },
],
},
{
title: "工具栏动作",
items: [
{ command: "{keyboardHome}", description: "返回 Scripting 键盘主界面" },
{ command: "{keyboardSettings}", description: "运行当前设置脚本" },
{ command: "{schemaMenu}", description: "打开 Rime 方案选单" },
{ command: "{dismissKeyboard}", description: "收起键盘" },
{ command: "keyboard:CAIS", description: "切换到指定键盘脚本" },
{ command: "https://example.com", description: "用 Safari 打开网页" },
{ command: "url:https://example.com", description: "用 Safari 打开网页" },
{ command: "script:脚本名", description: "运行指定 Scripting 脚本" },
{
command: 'js:await fetch("https://example.com")',
description: "执行简单 JS 脚本",
},
{
command:
'js:const r = await fetch("https://example.com"); ctx.insertText(await r.text())',
description: "在 JS 脚本中手动将结果上屏",
},
{
command: "{clipboard}",
description: "在 js: 脚本中替换为当前剪切板文本",
},
],
},
{
title: "Rime 按键名示例",
items: [
{ command: "Break", description: "发送 Rime 支持的 Break 键" },
{ command: "Page_Up", description: "发送 Rime Page Up 键" },
{ command: "Page_Down", description: "发送 Rime Page Down 键" },
{ command: "Up", description: "发送 Rime 上方向键" },
{ command: "Down", description: "发送 Rime 下方向键" },
{ command: "Left", description: "发送 Rime 左方向键" },
{ command: "Right", description: "发送 Rime 右方向键" },
{ command: "Home", description: "发送 Rime Home 键" },
{ command: "End", description: "发送 Rime End 键" },
{ command: "BackSpace", description: "发送 Rime BackSpace 键" },
{ command: "Delete", description: "发送 Rime Delete 键" },
{ command: "Escape", description: "发送 Rime Escape 键" },
{ command: "Tab", description: "发送 Rime Tab 键" },
{ command: "Return", description: "发送 Rime Return 键" },
{ command: "space", description: "发送 Rime 空格键" },
{ command: "backslash", description: "发送 Rime 反斜杠键" },
{ command: "slash", description: "发送 Rime 斜杠键" },
{ command: "grave", description: "发送 Rime 反引号键" },
{ command: "asciitilde", description: "发送 Rime 波浪号键" },
{ command: "bracketleft", description: "发送 Rime 左方括号键" },
{ command: "bracketright", description: "发送 Rime 右方括号键" },
{ command: "comma", description: "发送 Rime 逗号键" },
{ command: "period", description: "发送 Rime 句号键" },
{ command: "minus", description: "发送 Rime 减号键" },
{ command: "equal", description: "发送 Rime 等号键" },
{ command: "semicolon", description: "发送 Rime 分号键" },
{ command: "apostrophe", description: "发送 Rime 单引号键" },
],
},
{
title: "组合按键示例",
items: [
{ command: "Control+j", description: "发送 Control + j" },
{ command: "Control+k", description: "发送 Control + k" },
{ command: "Control+l", description: "发送 Control + l" },
{ command: "Control+p", description: "发送 Control + p" },
{ command: "Control+Delete", description: "发送 Control + Delete" },
{ command: "Control+grave", description: "发送 Control + `" },
{ command: "Shift+Tab", description: "发送 Shift + Tab" },
{ command: "Alt+Left", description: "发送 Alt + Left" },
],
},
];
const KEY_COLOR_GROUPS: Array<{
title: string;
keys: Array<{ id: string; label: string }>;
}> = [
{
title: "字母键",
keys: LETTER_KEYS.map((key) => ({ id: key, label: key.toUpperCase() })),
},
{
title: "控制键",
keys: [
{ id: "shift", label: "Shift" },
{ id: "backspace", label: "Delete" },
{ id: "numbers", label: "数字切换" },
{ id: "comma", label: "逗号" },
{ id: "space", label: "空格" },
{ id: "mode", label: "中英切换" },
{ id: "enter", label: "回车" },
],
},
{
title: "功能行",
keys: [
{ id: "idle-left", label: "左移" },
{ id: "idle-head", label: "行首" },
{ id: "idle-schema", label: "全选" },
{ id: "idle-cut", label: "剪切" },
{ id: "idle-copy", label: "复制" },
{ id: "idle-paste", label: "粘贴" },
{ id: "idle-tail", label: "行尾" },
{ id: "idle-right", label: "右移" },
],
},
{
title: "预编辑功能行",
keys: [
{ id: "func-left", label: "左括号" },
{ id: "func-page-down", label: "翻页" },
{ id: "tone-1", label: "一声" },
{ id: "tone-2", label: "二声" },
{ id: "tone-3", label: "三声" },
{ id: "tone-4", label: "四声" },
{ id: "func-backslash", label: "包裹" },
{ id: "func-right", label: "右括号" },
],
},
{
title: "数字键盘",
keys: [
..."123456789".split("").map((key) => ({
id: `numeric-${key}`,
label: key,
})),
{ id: "numeric-abc", label: "ABC" },
{ id: "numeric-0", label: "0" },
{ id: "numeric-space", label: "空格" },
{ id: "numeric-backspace", label: "Delete" },
{ id: "numeric-dot", label: "小数点" },
{ id: "numeric-equal", label: "等号" },
{ id: "numeric-enter", label: "换行" },
],
},
];
const LETTER_KEY_COLOR_GROUP = KEY_COLOR_GROUPS[0];
function SettingHint({ children }: { children: any }) {
return <Text font="caption" foregroundStyle="secondaryLabel">{children}
</Text>;
}
function LabeledTextField(props: {
title: string;
value: string;
prompt?: string;
titleWidth?: number;
titleSymbol?: string;
onChanged: (value: string) => void;
draftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
const [draftValue, setDraftValue] = useState(props.value);
const value = props.draftKey ? draftValue : props.value;
function handleChanged(next: string) {
if (props.draftKey) {
setDraftValue(next);
props.onDraftChanged?.(props.draftKey, next);
return;
}
props.onChanged(next);
}
return (
<HStack
spacing={10}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<HStack
spacing={6}
frame={{
width: props.titleSymbol
? Math.max(props.titleWidth ?? 76, 116)
: props.titleWidth ?? 76,
alignment: "leading" as any,
}}
>
<Text font="body" lineLimit={1}>{props.title}</Text>
{props.titleSymbol
? <Image systemName={props.titleSymbol} font="body" />
: null}
</HStack>
<TextField
title=""
value={value}
prompt={props.prompt ?? ""}
onChanged={handleChanged}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
/>
</HStack>
);
}
function ColorPairConfigRow(props: {
title: string;
value: KeyColorPair;
overridden?: boolean;
resetVisible?: boolean;
onLightChanged: (value: string) => void;
onDarkChanged: (value: string) => void;
onReset?: () => void;
}) {
return (
<VStack
alignment="leading"
spacing={8}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<HStack spacing={8}>
<Text font="headline">{props.title}</Text>
<Text
font="caption"
foregroundStyle="secondaryLabel"
frame={{ maxWidth: "infinity" as any, alignment: "trailing" as any }}
>
{props.overridden ? "单独设置" : "跟随默认"}
</Text>
</HStack>
<HStack spacing={12}>
<ColorPicker
title="浅色主题"
value={props.value.light as any}
supportsOpacity={false}
onChanged={(value) => props.onLightChanged(String(value))}
/>
<ColorPicker
title="深色主题"
value={props.value.dark as any}
supportsOpacity={false}
onChanged={(value) => props.onDarkChanged(String(value))}
/>
</HStack>
{(props.resetVisible || props.overridden) && props.onReset
? (
<Button
title="恢复默认颜色"
systemImage="arrow.counterclockwise"
action={props.onReset}
/>
)
: null}
</VStack>
);
}
function SwipeConfigRow(props: {
title: string;
action: string;
symbol: string;
mode: ActionSendMode;
onActionChanged: (value: string) => void;
onSymbolChanged: (value: string) => void;
onModeChanged: (value: ActionSendMode) => void;
actionDraftKey?: string;
symbolDraftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
const [actionDraft, setActionDraft] = useState(props.action);
const [symbolDraft, setSymbolDraft] = useState(props.symbol);
function handleActionChanged(next: string) {
if (props.actionDraftKey) {
setActionDraft(next);
props.onDraftChanged?.(props.actionDraftKey, next);
return;
}
props.onActionChanged(next);
}
function handleSymbolChanged(next: string) {
if (props.symbolDraftKey) {
setSymbolDraft(next);
props.onDraftChanged?.(props.symbolDraftKey, next);
return;
}
props.onSymbolChanged(next);
}
return (
<VStack
alignment="leading"
spacing={6}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<Text font="headline">{props.title}</Text>
<HStack spacing={10}>
<Text
font="caption"
foregroundStyle="secondaryLabel"
frame={{ width: 38, alignment: "leading" as any }}
>
动作
</Text>
<TextField
title=""
value={props.actionDraftKey ? actionDraft : props.action}
prompt="发送内容"
onChanged={handleActionChanged}
/>
</HStack>
<Picker
title="发送方式"
value={props.mode}
onChanged={(value: string) =>
props.onModeChanged(value as ActionSendMode)}
pickerStyle="segmented"
>
{ACTION_MODE_OPTIONS.map((option) => (
<Text key={option.value} tag={option.value}>{option.label}</Text>
))}
</Picker>
<HStack spacing={10}>
<HStack spacing={5} frame={{ width: 54, alignment: "leading" as any }}>
<Text font="caption" foregroundStyle="secondaryLabel" lineLimit={1}>
图标
</Text>
{props.symbol
? <Image systemName={props.symbol} font="caption" />
: null}
</HStack>
<TextField
title=""
value={props.symbolDraftKey ? symbolDraft : props.symbol}
prompt="SF Symbol,可留空"
onChanged={handleSymbolChanged}
/>
</HStack>
</VStack>
);
}
function CommandReferencePage() {
const [showCopiedToast, setShowCopiedToast] = useState(false);
function copyCommand(command: string) {
void Pasteboard.setString(command);
setShowCopiedToast(false);
setTimeout(() => setShowCopiedToast(true), 20);
}
return (
<List
navigationTitle="特殊命令"
navigationBarTitleDisplayMode="inline"
toast={{
isPresented: showCopiedToast,
onChanged: setShowCopiedToast,
message: "已复制命令",
duration: 1.2,
position: "bottom",
}}
>
{COMMAND_REFERENCE_GROUPS.map((group) => (
<Section
key={group.title}
header={<Text>{group.title}</Text>}
>
{group.items.map((item) => (
<Button
key={item.command}
action={() => copyCommand(item.command)}
>
<HStack
spacing={8}
frame={{
maxWidth: "infinity" as any,
alignment: "leading" as any,
}}
>
<VStack
alignment="leading"
spacing={4}
frame={{ alignment: "leading" as any }}
>
<Text font="body" fontDesign="monospaced">
{item.command}
</Text>
<Text font="caption" foregroundStyle="secondaryLabel">
{item.description}
</Text>
</VStack>
<Spacer />
</HStack>
</Button>
))}
</Section>
))}
<Section
footer={
<SettingHint>
“自动”模式会先识别上面的脚本特殊命令,再尝试按 Rime
按键名或普通文本发送;“发送给 Rime”会跳过脚本特殊命令,直接按 Rime
按键/文本处理。
</SettingHint>
}
/>
</List>
);
}
function ActionConfigRow(props: {
title: string;
action: string;
mode: ActionSendMode;
onActionChanged: (value: string) => void;
onModeChanged: (value: ActionSendMode) => void;
actionDraftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
return (
<VStack
alignment="leading"
spacing={6}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<LabeledTextField
title={props.title}
value={props.action}
titleWidth={126}
onChanged={props.onActionChanged}
draftKey={props.actionDraftKey}
onDraftChanged={props.onDraftChanged}
/>
<Picker
title="发送方式"
value={props.mode}
onChanged={(value: string) =>
props.onModeChanged(value as ActionSendMode)}
pickerStyle="segmented"
>
{ACTION_MODE_OPTIONS.map((option) => (
<Text key={option.value} tag={option.value}>{option.label}</Text>
))}
</Picker>
</VStack>
);
}
function CandidateMenuActionRow(props: {
index: number;
item: CandidateMenuAction;
onNameChanged: (value: string) => void;
onActionChanged: (value: string) => void;
onClear: () => void;
nameDraftKey?: string;
actionDraftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
return (
<VStack
alignment="leading"
spacing={8}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<HStack>
<Text font="headline">菜单 {props.index + 1}</Text>
<Button
title="清空"
systemImage="xmark.circle"
action={props.onClear}
buttonStyle="plain"
/>
</HStack>
<LabeledTextField
title="名称"
value={props.item.name}
titleWidth={54}
onChanged={props.onNameChanged}
draftKey={props.nameDraftKey}
onDraftChanged={props.onDraftChanged}
/>
<LabeledTextField
title="动作"
value={props.item.action}
titleWidth={54}
onChanged={props.onActionChanged}
draftKey={props.actionDraftKey}
onDraftChanged={props.onDraftChanged}
/>
</VStack>
);
}
function SFSymbolPreviewRow(props: { symbol: string }) {
return (
<HStack>
<Text frame={{ width: 86, alignment: "leading" as any }}>
图标预览
</Text>
{props.symbol
? <Image systemName={props.symbol} font="title2" />
: <Text foregroundStyle="secondaryLabel">未设置</Text>}
<Spacer />
</HStack>
);
}
function SFSymbolInputRow(props: {
title: string;
value: string;
onChanged: (value: string) => void;
draftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
const [draftValue, setDraftValue] = useState(props.value);
const value = props.draftKey ? draftValue : props.value;
function handleChanged(next: string) {
if (props.draftKey) {
setDraftValue(next);
props.onDraftChanged?.(props.draftKey, next);
return;
}
props.onChanged(next);
}
return (
<HStack
spacing={10}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<HStack spacing={6} frame={{ width: 116, alignment: "leading" as any }}>
<Text lineLimit={1}>{props.title}</Text>
{value ? <Image systemName={value} font="body" /> : null}
</HStack>
<TextField
title=""
value={value}
prompt=""
onChanged={handleChanged}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
/>
</HStack>
);
}
function FullRowButton(props: {
title: string;
systemImage?: string;
role?: "destructive" | "cancel";
action: () => void;
}) {
return (
<Button
buttonStyle="plain"
role={props.role}
frame={{ maxWidth: "infinity" as any }}
action={props.action}
>
<HStack
spacing={8}
frame={{ width: "100%" as any }}
padding={{ top: 12, bottom: 12 }}
background={"rgba(0,0,0,0.001)" as any}
>
<Spacer />
{props.systemImage
? <Image systemName={props.systemImage} font="body" />
: null}
<Text
font="headline"
foregroundStyle={props.role === "destructive"
? "systemRed"
: undefined}
>
{props.title}
</Text>
<Spacer />
</HStack>
</Button>
);
}
function IconOnlyOrderRow(props: { symbol: string; index: number }) {
return (
<HStack
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
{props.symbol
? <Image systemName={props.symbol} font="title3" />
: <Text foregroundStyle="secondaryLabel">未设置</Text>}
<Text opacity={0} frame={{ width: 1 }}>
.
</Text>
<Spacer />
<Text foregroundStyle="secondaryLabel">{props.index + 1}</Text>
</HStack>
);
}
function FunctionSwipeConfigRow(props: {
title: string;
pressAction?: string;
pressMode?: ActionSendMode;
symbol?: string;
upAction: string;
upMode: ActionSendMode;
downAction: string;
downMode: ActionSendMode;
onPressActionChanged?: (value: string) => void;
onPressModeChanged?: (value: ActionSendMode) => void;
onSymbolChanged?: (value: string) => void;
onUpActionChanged: (value: string) => void;
onUpModeChanged: (value: ActionSendMode) => void;
onDownActionChanged: (value: string) => void;
onDownModeChanged: (value: ActionSendMode) => void;
pressActionDraftKey?: string;
symbolDraftKey?: string;
upActionDraftKey?: string;
downActionDraftKey?: string;
onDraftChanged?: (key: string, value: string) => void;
}) {
return (
<VStack
alignment="leading"
spacing={8}
frame={{ maxWidth: "infinity" as any, alignment: "leading" as any }}
>
<Text font="headline">{props.title}</Text>
{props.pressAction != null && props.pressMode != null
? (
<VStack
alignment="leading"
spacing={8}
frame={{
maxWidth: "infinity" as any,
alignment: "leading" as any,
}}
>
<LabeledTextField
title="点击动作"
value={props.pressAction}
titleWidth={78}
onChanged={(value) => props.onPressActionChanged?.(value)}
draftKey={props.pressActionDraftKey}
onDraftChanged={props.onDraftChanged}
/>
<Picker
title="点击发送"
value={props.pressMode}
onChanged={(value: string) =>
props.onPressModeChanged?.(value as ActionSendMode)}
pickerStyle="segmented"
>
{ACTION_MODE_OPTIONS.map((option) => (
<Text key={option.value} tag={option.value}>
{option.label}
</Text>
))}
</Picker>
</VStack>
)
: null}
{props.symbol != null
? (
<LabeledTextField
title="显示图标"
value={props.symbol}
titleWidth={78}
titleSymbol={props.symbol}
onChanged={(value) => props.onSymbolChanged?.(value)}
draftKey={props.symbolDraftKey}
onDraftChanged={props.onDraftChanged}
/>
)
: null}
<LabeledTextField
title="上划动作"
value={props.upAction}
titleWidth={78}
onChanged={props.onUpActionChanged}
draftKey={props.upActionDraftKey}
onDraftChanged={props.onDraftChanged}
/>
<Picker
title="上划发送"
value={props.upMode}
onChanged={(value: string) =>
props.onUpModeChanged(value as ActionSendMode)}
pickerStyle="segmented"
>
{ACTION_MODE_OPTIONS.map((option) => (
<Text key={option.value} tag={option.value}>{option.label}</Text>
))}
</Picker>
<LabeledTextField
title="下划动作"
value={props.downAction}
titleWidth={78}
onChanged={props.onDownActionChanged}
draftKey={props.downActionDraftKey}
onDraftChanged={props.onDraftChanged}
/>
<Picker
title="下划发送"
value={props.downMode}
onChanged={(value: string) =>
props.onDownModeChanged(value as ActionSendMode)}
pickerStyle="segmented"
>
{ACTION_MODE_OPTIONS.map((option) => (
<Text key={option.value} tag={option.value}>{option.label}</Text>
))}
</Picker>
</VStack>
);
}
function SettingsView() {
const [settings, setSettings] = useState<RimeKeyboardSettings>(() =>
loadRimeKeyboardSettings()
);
const pendingTextDraftsRef = useRef<Record<string, string>>({});
const [showSavedToast, setShowSavedToast] = useState(false);
const functionOrderEditMode = useObservable(() => EditMode.inactive());
const toolbarEditMode = useObservable(() => EditMode.inactive());
const [functionOrderEditing, setFunctionOrderEditing] = useState(false);
const [toolbarEditing, setToolbarEditing] = useState(false);
function customThemeFlag(
settingKey: "keyColors" | "keyFontColors" | "keyHintColors",
scheme: KeyColorScheme,
) {
if (settingKey === "keyFontColors") {
return scheme === "light"
? "customKeyFontColorLight"
: "customKeyFontColorDark";
}
if (settingKey === "keyHintColors") {
return scheme === "light"
? "customKeyHintColorLight"
: "customKeyHintColorDark";
}
return scheme === "light" ? "customKeyColorLight" : "customKeyColorDark";
}
function updateSettings(next: RimeKeyboardSettings) {
const saved = saveRimeKeyboardSettings(next);
setSettings(saved);
}
function patchSettings(patch: Partial<RimeKeyboardSettings>) {
updateSettings({ ...settings, ...patch });
}
function recordTextDraft(key: string, value: string) {
pendingTextDraftsRef.current[key] = value;
}
function clonePathValue(value: any) {
if (Array.isArray(value)) return [...value];
if (value != null && typeof value === "object") return { ...value };
return {};
}
function applyTextDrafts(base: RimeKeyboardSettings) {
const drafts = pendingTextDraftsRef.current;
const entries = Object.entries(drafts);
if (entries.length === 0) return base;
const next = { ...base } as any;
for (const [path, value] of entries) {
const parts = path.split(".");
let cursor = next;
for (let index = 0; index < parts.length - 1; index += 1) {
const part = parts[index];
cursor[part] = clonePathValue(cursor[part]);
cursor = cursor[part];
}
cursor[parts[parts.length - 1]] = value;
}
return next as RimeKeyboardSettings;
}
function saveTextDrafts() {
const next = applyTextDrafts(settings);
pendingTextDraftsRef.current = {};
updateSettings(next);
setShowSavedToast(false);
setTimeout(() => setShowSavedToast(true), 20);
}
function textInputToolbar(extra?: any) {
return {
topBarTrailing: (
<HStack spacing={12}>
{extra}
<Button
title="保存"
systemImage="checkmark.circle"
action={saveTextDrafts}
/>
</HStack>
),
};
}
function editModeButton(
isEditing: boolean,
setEditing: (value: boolean) => void,
editMode: any,
) {
return (
<Button
title=""
systemImage={isEditing ? "checkmark.circle" : "pencil.circle"}
action={() => {
const next = !isEditing;
setEditing(next);
editMode.setValue(next ? EditMode.active() : EditMode.inactive());
}}
/>
);
}
const savedToast = {
isPresented: showSavedToast,
onChanged: setShowSavedToast,
message: "设置已保存",
duration: 1.2,
position: "bottom" as const,
};
function setFunctionRowVisible(value: boolean) {
const next: RimeKeyboardSettings = {
...settings,
showFunctionRow: value,
letterSwipeDown: { ...settings.letterSwipeDown },
letterSwipeDownSymbols: { ...settings.letterSwipeDownSymbols },
letterSwipeDownModes: { ...settings.letterSwipeDownModes },
};
for (const key of Object.keys(FUNCTION_ROW_OFF_LETTER_SWIPE_DOWN)) {
if (value) {
if (
next.letterSwipeDown[key] ===
FUNCTION_ROW_OFF_LETTER_SWIPE_DOWN[key]
) {
next.letterSwipeDown[key] = DEFAULT_LETTER_SWIPE_DOWN[key];
next.letterSwipeDownModes[key] = "auto";
}