Skip to content

Commit ddf4766

Browse files
committed
Integer and Boolean option
1 parent c2e45fe commit ddf4766

File tree

8 files changed

+202
-7
lines changed

8 files changed

+202
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"naive-ui": "^2.39.0",
1919
"typescript": "^5.5.4",
2020
"vite": "^5.4.2",
21+
"vooks": "^0.2.12",
2122
"vue": "^3.4.38",
2223
"vue-tsc": "^2.0.29"
2324
}

src/BasicConfig.vue

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<script setup lang="ts">
2+
import { computed, ref, watchEffect } from 'vue'
3+
import { NAlert, NForm, NFormItem } from 'naive-ui'
4+
import type { Config } from 'fcitx5-js'
5+
import IntegerOption from './option/IntegerOption.vue'
6+
import BooleanOption from './option/BooleanOption.vue'
7+
import UnknownOption from './option/UnknownOption.vue'
8+
import { isMobile } from './util'
9+
10+
const props = defineProps<{
11+
path: string
12+
config: Config
13+
}>()
14+
15+
const labelPlacement = computed(() => isMobile.value ? 'top' : 'left')
16+
17+
function toComponent(type: string) {
18+
switch (type) {
19+
case 'Integer':
20+
return IntegerOption
21+
case 'Boolean':
22+
return BooleanOption
23+
default:
24+
return UnknownOption
25+
}
26+
}
27+
28+
function extractValue(reset: boolean) {
29+
const value: { [key: string]: any } = {}
30+
if ('Children' in props.config) {
31+
for (const child of props.config.Children) {
32+
value[child.Option] = reset ? child.DefaultValue : child.Value
33+
}
34+
}
35+
return value
36+
}
37+
38+
const form = ref<{ [key: string]: any }>({})
39+
40+
watchEffect(() => {
41+
form.value = extractValue(false)
42+
})
43+
44+
function reset() {
45+
form.value = extractValue(true)
46+
}
47+
48+
function get() {
49+
return form.value
50+
}
51+
52+
defineExpose({
53+
reset,
54+
get,
55+
})
56+
</script>
57+
58+
<template>
59+
<NAlert v-if="'ERROR' in config" title="Error" type="error">
60+
{{ config.ERROR }}
61+
</NAlert>
62+
<NForm
63+
v-else
64+
:model="form"
65+
:label-placement="labelPlacement"
66+
label-width="200px"
67+
>
68+
<NFormItem
69+
v-for="child in config.Children"
70+
:key="`${path}/${child.Option}`"
71+
:label="child.Description"
72+
>
73+
<component
74+
:is="toComponent(child.Type)"
75+
:config="child"
76+
:value="form[child.Option]"
77+
:on-update="(v) => { form[child.Option] = v }"
78+
/>
79+
</NFormItem>
80+
</NForm>
81+
</template>

src/InputMethodConfig.vue

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { MenuOption } from 'naive-ui'
44
import { NButton, NCheckbox, NCheckboxGroup, NFlex, NLayout, NLayoutFooter, NLayoutSider, NMenu } from 'naive-ui'
55
import MinusButton from './MinusButton.vue'
66
import PlusButton from './PlusButton.vue'
7+
import BasicConfig from './BasicConfig.vue'
78
89
const languageName = new Intl.DisplayNames(navigator.language, { type: 'language' })
910
</script>
@@ -15,9 +16,13 @@ const props = defineProps<{
1516
displayName: string
1617
name: string
1718
}[]
19+
onClose: () => void
1820
}>()
1921
2022
const selectedInputMethod = ref(props.inputMethod)
23+
const uri = computed(() => `fcitx://config/inputmethod/${selectedInputMethod.value}`)
24+
25+
const config = computed(() => window.fcitx.getConfig(uri.value))
2126
2227
const options = computed(() =>
2328
props.inputMethods.map(({ displayName, name }) => ({
@@ -128,6 +133,17 @@ const filteredLanguageOptions = computed(() => {
128133
}
129134
return languageOptions.value
130135
})
136+
137+
const basicConfig = ref()
138+
139+
function apply() {
140+
window.fcitx.setConfig(uri.value, basicConfig.value.get())
141+
}
142+
143+
function confirm() {
144+
apply()
145+
props.onClose()
146+
}
131147
</script>
132148

133149
<template>
@@ -182,8 +198,11 @@ const filteredLanguageOptions = computed(() => {
182198
>
183199
Select a language from the left list
184200
</div>
185-
<NLayout v-else position="absolute" style="bottom: 50px; padding: 16px 0 16px 16px">
186-
<NCheckboxGroup v-model:value="imsToAdd">
201+
<NLayout v-else position="absolute" style="bottom: 50px">
202+
<NCheckboxGroup
203+
v-model:value="imsToAdd"
204+
style="margin: 16px"
205+
>
187206
<NFlex vertical>
188207
<NCheckbox
189208
v-for="im of inputMethodsForLanguage"
@@ -196,8 +215,7 @@ const filteredLanguageOptions = computed(() => {
196215
</NLayout>
197216
<NLayoutFooter position="absolute">
198217
<NFlex
199-
justify="end"
200-
style="padding: 8px"
218+
style="padding: 8px; justify-content: space-between"
201219
>
202220
<NButton secondary @click="adding = false">
203221
Cancel
@@ -213,9 +231,51 @@ const filteredLanguageOptions = computed(() => {
213231
</NFlex>
214232
</NLayoutFooter>
215233
</template>
216-
<div v-else>
217-
{{ selectedInputMethod }}
218-
</div>
234+
<template v-else>
235+
<NLayout position="absolute" style="bottom: 50px">
236+
<BasicConfig
237+
ref="basicConfig"
238+
:path="selectedInputMethod"
239+
:config="config"
240+
style="margin: 16px"
241+
/>
242+
</NLayout>
243+
<NLayoutFooter position="absolute">
244+
<NFlex
245+
style="padding: 8px; justify-content: space-between"
246+
>
247+
<NFlex>
248+
<NButton
249+
secondary
250+
@click="basicConfig.reset()"
251+
>
252+
Reset to default
253+
</NButton>
254+
<NButton
255+
secondary
256+
@click="onClose()"
257+
>
258+
Cancel
259+
</NButton>
260+
</NFlex>
261+
<NFlex>
262+
<NButton
263+
secondary
264+
@click="apply()"
265+
>
266+
Apply
267+
</NButton>
268+
<NButton
269+
secondary
270+
type="info"
271+
@click="confirm()"
272+
>
273+
OK
274+
</NButton>
275+
</NFlex>
276+
</NFlex>
277+
</NLayoutFooter>
278+
</template>
219279
</NLayout>
220280
</NLayout>
221281
</template>

src/option/BooleanOption.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
import { NSwitch } from 'naive-ui'
3+
4+
defineProps<{
5+
value: string
6+
onUpdate: (value: string) => void
7+
}>()
8+
</script>
9+
10+
<template>
11+
<NSwitch
12+
:value="value === 'True'"
13+
@update:value="(v) => onUpdate(v ? 'True' : 'False')"
14+
/>
15+
</template>

src/option/IntegerOption.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script setup lang="ts">
2+
import { NInputNumber } from 'naive-ui'
3+
4+
defineProps<{
5+
config: {
6+
IntMax?: string
7+
IntMin?: string
8+
}
9+
value: string
10+
onUpdate: (value: string) => void
11+
}>()
12+
</script>
13+
14+
<template>
15+
<NInputNumber
16+
:value="Number(value)"
17+
:precision="0"
18+
:min="config.IntMin && Number(config.IntMin)"
19+
:max="config.IntMax && Number(config.IntMax)"
20+
clearable
21+
@update:value="(v) => onUpdate(String(v))"
22+
/>
23+
</template>

src/option/UnknownOption.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
defineProps<{
3+
config: any
4+
}>()
5+
</script>
6+
7+
<template>
8+
<div>{{ config }}</div>
9+
</template>

src/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { computed } from 'vue'
2+
import { useBreakpoint } from 'vooks'
3+
4+
const breakpoint = useBreakpoint()
5+
export const isMobile = computed(() => breakpoint.value === 'xs' || breakpoint.value === 's')

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default defineConfig({
1313
external: [
1414
'vue',
1515
'naive-ui',
16+
'vooks',
1617
],
1718
},
1819
},

0 commit comments

Comments
 (0)