Skip to content

Commit

Permalink
initial ExternalOption
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Sep 6, 2024
1 parent f4efdda commit 0ecf114
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 66 deletions.
19 changes: 12 additions & 7 deletions src/BasicConfig.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script setup lang="ts">
import { computed } from 'vue'
import { NAlert, NForm, NFormItem } from 'naive-ui'
import { NAlert, NDialogProvider, NForm, NFormItem } from 'naive-ui'
import type { Config } from 'fcitx5-js'
import TooltipButton from './TooltipButton.vue'
import IntegerOption from './option/IntegerOption.vue'
import BooleanOption from './option/BooleanOption.vue'
import EnumOption from './option/EnumOption.vue'
import KeyOption from './option/KeyOption.vue'
import StringOption from './option/StringOption.vue'
import ExternalOption from './option/ExternalOption.vue'
import ListOption from './option/ListOption.vue'
import GroupOption from './option/GroupOption.vue'
import UnknownOption from './option/UnknownOption.vue'
Expand All @@ -34,6 +35,8 @@ function toComponent(child: { Type: string, Children: any[] | null }) {
return KeyOption
case 'String':
return StringOption
case 'External':
return ExternalOption
default: {
if (child.Type.startsWith('List|')) {
return ListOption
Expand Down Expand Up @@ -67,12 +70,14 @@ function toComponent(child: { Type: string, Children: any[] | null }) {
:text="child.Tooltip"
/>
</template>
<component
:is="toComponent(child)"
:config="child"
:value="value[child.Option]"
@update="v => onUpdate({ ...value, [child.Option]: v })"
/>
<NDialogProvider>
<component
:is="toComponent(child)"
:config="child"
:value="value[child.Option]"
@update="v => onUpdate({ ...value, [child.Option]: v })"
/>
</NDialogProvider>
</NFormItem>
</NForm>
</template>
45 changes: 45 additions & 0 deletions src/FooterButtons.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script setup lang="ts">
import { NButton, NFlex } from 'naive-ui'
defineProps<{
reset: () => void
close: () => void
apply: () => void
}>()
</script>

<template>
<NFlex
style="padding: 8px; justify-content: space-between"
>
<NFlex>
<NButton
secondary
@click="reset"
>
Reset to default
</NButton>
<NButton
secondary
@click="close"
>
Cancel
</NButton>
</NFlex>
<NFlex>
<NButton
secondary
@click="apply"
>
Apply
</NButton>
<NButton
secondary
type="info"
@click="apply(); close()"
>
OK
</NButton>
</NFlex>
</NFlex>
</template>
14 changes: 14 additions & 0 deletions src/GearButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
import { NButton, NIcon } from 'naive-ui'
import GearIcon from './GearIcon.vue'
</script>

<template>
<NButton secondary>
<template #icon>
<NIcon>
<GearIcon />
</NIcon>
</template>
</NButton>
</template>
9 changes: 9 additions & 0 deletions src/GearIcon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
export default {
name: 'MdiCog',
}
</script>

<template>
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M12 15.5A3.5 3.5 0 0 1 8.5 12A3.5 3.5 0 0 1 12 8.5a3.5 3.5 0 0 1 3.5 3.5a3.5 3.5 0 0 1-3.5 3.5m7.43-2.53c.04-.32.07-.64.07-.97s-.03-.66-.07-1l2.11-1.63c.19-.15.24-.42.12-.64l-2-3.46c-.12-.22-.39-.31-.61-.22l-2.49 1c-.52-.39-1.06-.73-1.69-.98l-.37-2.65A.506.506 0 0 0 14 2h-4c-.25 0-.46.18-.5.42l-.37 2.65c-.63.25-1.17.59-1.69.98l-2.49-1c-.22-.09-.49 0-.61.22l-2 3.46c-.13.22-.07.49.12.64L4.57 11c-.04.34-.07.67-.07 1s.03.65.07.97l-2.11 1.66c-.19.15-.25.42-.12.64l2 3.46c.12.22.39.3.61.22l2.49-1.01c.52.4 1.06.74 1.69.99l.37 2.65c.04.24.25.42.5.42h4c.25 0 .46-.18.5-.42l.37-2.65c.63-.26 1.17-.59 1.69-.99l2.49 1.01c.22.08.49 0 .61-.22l2-3.46c.12-.22.07-.49-.12-.64z" /></svg>
</template>
60 changes: 7 additions & 53 deletions src/InputMethodConfig.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { computed, h, ref, watchEffect } from 'vue'
import type { MenuOption } from 'naive-ui'
import type { Config } from 'fcitx5-js'
import { NButton, NCheckbox, NCheckboxGroup, NFlex, NLayout, NLayoutFooter, NLayoutSider, NMenu } from 'naive-ui'
import MinusButton from './MinusButton.vue'
import PlusButton from './PlusButton.vue'
import BasicConfig from './BasicConfig.vue'
import FooterButtons from './FooterButtons.vue'
import { extractValue } from './util'
const languageName = new Intl.DisplayNames(navigator.language, { type: 'language' })
</script>
Expand Down Expand Up @@ -137,20 +138,6 @@ const filteredLanguageOptions = computed(() => {
const form = ref({})
function extractValue(config: Config, reset: boolean) {
const value: { [key: string]: any } = {}
if ('Children' in config) {
for (const child of config.Children) {
value[child.Option] = reset
? (
'DefaultValue' in child ? child.DefaultValue : extractValue(child, true)
)
: child.Value
}
}
return value
}
watchEffect(() => {
form.value = extractValue(config.value, false)
})
Expand All @@ -162,11 +149,6 @@ function reset() {
function apply() {
window.fcitx.setConfig(uri.value, form.value)
}
function confirm() {
apply()
props.onClose()
}
</script>

<template>
Expand Down Expand Up @@ -265,39 +247,11 @@ function confirm() {
/>
</NLayout>
<NLayoutFooter position="absolute">
<NFlex
style="padding: 8px; justify-content: space-between"
>
<NFlex>
<NButton
secondary
@click="reset()"
>
Reset to default
</NButton>
<NButton
secondary
@click="onClose()"
>
Cancel
</NButton>
</NFlex>
<NFlex>
<NButton
secondary
@click="apply()"
>
Apply
</NButton>
<NButton
secondary
type="info"
@click="confirm()"
>
OK
</NButton>
</NFlex>
</NFlex>
<FooterButtons
:reset="reset"
:apply="apply"
:close="onClose"
/>
</NLayoutFooter>
</template>
</NLayout>
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as InputMethodConfig } from './InputMethodConfig.vue'
export { default as GearButton } from './GearButton.vue'
4 changes: 2 additions & 2 deletions src/option/EnumOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { NSelect } from 'naive-ui'
const props = defineProps<{
config: {
Enum: { [key: string]: string }
EnumI18n: { [key: string]: string }
EnumI18n?: { [key: string]: string }
}
value: string
onUpdate: (value: string) => void
}>()
const options = computed(() => Object.entries(props.config.Enum).map(([key, value]) => ({
label: props.config.EnumI18n[key],
label: (props.config.EnumI18n || props.config.Enum)[key],
value,
})))
</script>
Expand Down
63 changes: 63 additions & 0 deletions src/option/ExternalOption.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<script setup lang="ts">
import { h, ref } from 'vue'
import { useDialog } from 'naive-ui'
import GearButton from '../GearButton.vue'
import BasicConfig from '../BasicConfig.vue'
import FooterButtons from '../FooterButtons.vue'
import { extractValue } from '../util'
const props = defineProps<{
config: {
Description: string
External: string
Option: string
LaunchSubConfig?: string
}
}>()
const dialog = useDialog()
const form = ref({})
function click() {
switch (props.config.Option) {
default:
if (props.config.LaunchSubConfig === 'True') {
const config = window.fcitx.getConfig(props.config.External)
form.value = extractValue(config, false)
const instance = dialog.info({
title: props.config.Description,
content: () => h(BasicConfig, {
path: props.config.Option,
config,
value: form.value,
onUpdate(v) {
form.value = v
},
}),
action: () => h(FooterButtons, {
reset() {
form.value = extractValue(config, true)
},
close() {
instance.destroy()
},
apply() {
window.fcitx.setConfig(props.config.External, form.value)
},
}),
})
}
else {
dialog.error({
title: 'Error',
content: `Unknown External Option: ${props.config.Option}`,
positiveText: 'OK',
})
}
}
}
</script>

<template>
<GearButton @click="click" />
</template>
4 changes: 0 additions & 4 deletions src/option/GroupOption.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import { ref } from 'vue'
import { NCard } from 'naive-ui'
import BasicConfig from '../BasicConfig.vue'
Expand All @@ -10,14 +9,11 @@ defineProps<{
value: any
onUpdate: (value: any) => void
}>()
const basicConfig = ref()
</script>

<template>
<NCard>
<BasicConfig
ref="basicConfig"
path=""
:config="config"
:value="value"
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { computed } from 'vue'
import { useBreakpoint } from 'vooks'
import type { Config } from 'fcitx5-js'

const breakpoint = useBreakpoint()
export const isMobile = computed(() => breakpoint.value === 'xs' || breakpoint.value === 's')

export function extractValue(config: Config, reset: boolean) {
const value: { [key: string]: any } = {}
if ('Children' in config) {
for (const child of config.Children) {
value[child.Option] = reset
? (
'DefaultValue' in child ? child.DefaultValue : extractValue(child, true)
)
: child.Value
}
}
return value
}

0 comments on commit 0ecf114

Please sign in to comment.