11import SwiftUI
22
3- /// Generation settings: backend, voice, model, voice tuning, cache.
3+ /// Settings split into two conceptual channels: ① 음성 합성 (the TTS engine) and
4+ /// ② 텍스트 생성 (the LLM used for 해설/대본). The TTS channel renders from the
5+ /// engine seam (BackendKind descriptors) so it's not hardcoded to ElevenLabs;
6+ /// each engine keeps its own typed tuning (ElevenVoiceSettings) untouched.
47struct SettingsView : View {
58 @ EnvironmentObject var app : AppState
9+ @ State private var channel = 0 // 0 = 음성 합성, 1 = 텍스트 생성
610 @ State private var geminiKeyInput = " "
711 @ State private var keyInput = " " // ElevenLabs key entry
812
913 var body : some View {
10- Form {
11- Section ( " 백엔드 " ) {
12- Picker ( " 엔진 " , selection : $app . backendKind ) {
13- ForEach ( AppState . BackendKind . allCases ) { Text ( $0 . label ) . tag ( $0 ) }
14- }
14+ VStack ( spacing : 0 ) {
15+ Picker ( " " , selection : $channel ) {
16+ Text ( " 음성 합성 (TTS) " ) . tag ( 0 )
17+ Text ( " 텍스트 생성 (LLM) " ) . tag ( 1 )
18+ }
19+ . pickerStyle ( . segmented ) . labelsHidden ( )
20+ . padding ( [ . horizontal , . top ] )
21+
22+ Form {
23+ if channel == 0 { ttsChannel } else { llmChannel }
24+ }
25+ . formStyle ( . grouped )
26+ }
27+ . onAppear {
28+ if app . ollamaModels . isEmpty { app . refreshOllamaModels ( ) }
29+ if app . voices . isEmpty && app . keyPresent { app . refreshVoices ( ) }
30+ }
31+ . onChange ( of : app . localBaseURL ) { _ , _ in app . saveSettings ( ) }
32+ . onChange ( of : app . normalizeEnabled ) { _ , _ in app . saveSettings ( ) }
33+ . onChange ( of : app . normalizeProvider ) { _ , _ in app . saveSettings ( ) }
34+ . onChange ( of : app . geminiModel ) { _ , _ in app . saveSettings ( ) }
35+ . onChange ( of : app . geminiBaseURL ) { _ , _ in app . saveSettings ( ) }
36+ . onChange ( of : app . ollamaModel ) { _ , _ in app . saveSettings ( ) }
37+ . onChange ( of : app . ollamaURL ) { _ , _ in app . saveSettings ( ) }
38+ . onChange ( of : app . maxChunkChars ) { _ , _ in app . saveSettings ( ) }
39+ . onChange ( of : app . voiceId ) { _ , newID in
40+ if let v = app . voices . first ( where : { $0 . id == newID } ) { app . voiceName = v . name }
41+ app . saveSettings ( )
42+ }
43+ . onChange ( of : app . modelId ) { _ , _ in app . saveSettings ( ) }
44+ . onChange ( of : app . backendKind ) { _ , _ in app . saveSettings ( ) }
45+ . onChange ( of : app . useCache ) { _ , _ in app . saveSettings ( ) }
46+ . onChange ( of : app . voiceSettings ) { _ , _ in app . saveSettings ( ) }
47+ . onDisappear { app . saveSettings ( ) }
48+ }
49+
50+ // MARK: - ① 음성 합성 (TTS engine)
51+
52+ @ ViewBuilder private var ttsChannel : some View {
53+ Section ( " 엔진 " ) {
54+ Picker ( " 합성 엔진 " , selection : $app . backendKind ) {
55+ ForEach ( AppState . BackendKind . allCases ) { Text ( $0 . label ) . tag ( $0 ) }
1556 }
57+ Text ( " 음성을 만드는 백엔드입니다. 엔진을 바꾸면 아래 보이스·모델·튜닝이 그 엔진 기준으로 바뀝니다. " )
58+ . font ( . caption ) . foregroundStyle ( . secondary )
59+ }
1660
17- Section ( " 보이스 / 모델 " ) {
61+ Section ( " 보이스 / 모델 " ) {
62+ if app . backendKind . hasVoiceList {
1863 if app . voices . isEmpty {
1964 labeledField ( " 보이스 ID " , placeholder : " voice id " , text : $app . voiceId ,
20- hint : " 연결 탭에서 ‘보이스 새로고침’을 누르면 목록에서 고를 수 있어요." )
65+ hint : " 아래 ‘연결’에서 보이스를 새로고침하면 목록에서 고를 수 있어요." )
2166 } else {
2267 Picker ( " 보이스 " , selection : $app . voiceId ) {
2368 ForEach ( app . voices ) { v in
2469 Text ( " \( v . name ) \( v . category == " cloned " ? " · 클론 " : " " ) " ) . tag ( v . id )
2570 }
2671 }
2772 }
73+ } else {
74+ labeledField ( " 보이스 ID " , placeholder : " voice id " , text : $app . voiceId ,
75+ hint : " 이 엔진은 보이스 목록을 제공하지 않습니다. 식별자를 직접 입력하세요. " )
76+ }
77+ if ! app . backendKind . ttsModels . isEmpty {
2878 Picker ( " 모델 " , selection : $app . modelId ) {
29- ForEach ( ElevenLabs . koreanModels , id : \. id ) { Text ( $0 . label ) . tag ( $0 . id ) }
79+ ForEach ( app . backendKind . ttsModels , id : \. id ) { Text ( $0 . label ) . tag ( $0 . id ) }
3080 }
3181 }
82+ }
3283
33- Section ( " 보이스 설정 (ElevenLabs) " ) {
84+ if app . backendKind == . elevenlabs {
85+ Section ( " 보이스 튜닝 (ElevenLabs) " ) {
3486 slider ( " 안정성 (stability) " , $app . voiceSettings . stability )
3587 slider ( " 유사도 (similarity) " , $app . voiceSettings . similarityBoost )
3688 slider ( " 스타일 (style) " , $app . voiceSettings . style )
3789 Toggle ( " speaker boost " , isOn : $app . voiceSettings . useSpeakerBoost )
3890 }
91+ }
3992
40- Section ( " 캐시 / 문단 분할 " ) {
41- Toggle ( " 같은 텍스트는 캐시에서 재생 (API 재호출 안 함) " , isOn : $app . useCache )
42- LabeledContent ( " 문단 최대 글자수 " ) {
43- TextField ( " " , value : $app . maxChunkChars , format : . number )
44- . frame ( width : 80 ) . multilineTextAlignment ( . trailing )
45- }
46- Text ( " 긴 문단은 이 글자수 기준으로 나눠 따로 요청한 뒤 이어 재생합니다. 캐시도 이 단위. " )
47- . font ( . caption ) . foregroundStyle ( . secondary )
48- }
49-
50- Section ( " TTS 친화 정규화 " ) {
51- Toggle ( " 숫자·수식 표기를 발음대로 정규화 " , isOn : $app . normalizeEnabled )
52- Picker ( " 제공자 " , selection : $app . normalizeProvider ) {
53- ForEach ( AppState . NormalizeProvider . allCases ) { Text ( $0 . label ) . tag ( $0 ) }
54- }
55-
56- if app . normalizeProvider == . gemini {
57- labeledField ( " Gemini 엔드포인트 " , placeholder : GeminiNormalizer . defaultBaseURL ,
58- text : $app . geminiBaseURL ,
59- hint : " API 루트. 끝에 /models/{모델}:generateContent 가 붙습니다. 프록시·게이트웨이 사용 시 변경. " )
60- if app . geminiModels . isEmpty {
61- Picker ( " 대본 모델 " , selection : $app . geminiModel ) {
62- ForEach ( GeminiNormalizer . models , id : \. self ) { Text ( $0 ) . tag ( $0 ) }
63- }
64- } else {
65- Picker ( " 대본 모델 " , selection : $app . geminiModel ) {
66- ForEach ( app . geminiModels , id : \. self ) { Text ( $0 ) . tag ( $0 ) }
67- }
68- }
69- labeledField ( " Gemini API 키 " , placeholder : " AIza… " , secure : true ,
70- text : $geminiKeyInput ,
71- hint : app . geminiKeyPresent ? " 현재: 설정됨 (App Support) " : " 현재: 없음 " )
72- HStack {
73- Button ( " 키 저장 " ) { app . saveGeminiKey ( geminiKeyInput ) ; geminiKeyInput = " " }
74- . disabled ( geminiKeyInput . trimmingCharacters ( in : . whitespaces ) . isEmpty )
75- Button ( " 연결 확인 / 모델 목록 " ) { app . refreshGeminiModels ( ) }
76- Spacer ( )
77- Text ( app . geminiStatus ) . font ( . caption ) . foregroundStyle ( . secondary )
78- }
79- } else {
80- if app . ollamaModels . isEmpty {
81- labeledField ( " 모델 " , placeholder : " qwen2.5:3b " , text : $app . ollamaModel )
82- } else {
83- Picker ( " 모델 " , selection : $app . ollamaModel ) {
84- ForEach ( app . ollamaModels , id : \. self ) { Text ( $0 ) . tag ( $0 ) }
85- }
86- }
87- labeledField ( " Ollama 엔드포인트 " , placeholder : " http://localhost:11434 " ,
88- text : $app . ollamaURL ,
89- hint : " 로컬/원격 모두 가능: 예) http://192.168.0.10:11434 " )
90- HStack {
91- Button ( " 연결 확인 / 모델 목록 " ) { app . refreshOllamaModels ( ) }
92- Spacer ( )
93- Text ( app . ollamaStatus ) . font ( . caption ) . foregroundStyle ( . secondary )
94- }
95- }
96- Text ( " 0.5→영 점 오, w1→더블유 일 처럼 다듬어 합성합니다. 경량 로컬 모델은 숫자 읽기가 부정확할 수 있어 Gemini를 권장합니다. " )
97- . font ( . caption ) . foregroundStyle ( . secondary )
98- }
99-
100- Section ( " 연결 상태 " ) {
101- LabeledContent ( " 백엔드 " , value : app . backendKind . label )
102- LabeledContent ( " ElevenLabs 키 " , value : app . keyPresent ? " 설정됨 " : " 없음 " )
93+ if app . backendKind . requiresAPIKey {
94+ Section ( " 연결 " ) {
95+ LabeledContent ( " API 키 " , value : app . keyPresent ? " 설정됨 " : " 없음 " )
10396 LabeledContent ( " 연결 " , value : app . connectionStatus . isEmpty ? " — " : app . connectionStatus )
104- Button {
105- app . refreshVoices ( )
106- } label : {
107- Label ( " 연결 테스트 / 보이스 새로고침 " , systemImage : " arrow.clockwise " )
108- }
109- }
110-
111- Section ( " ElevenLabs API 키 " ) {
112- SecureField ( " sk_... " , text : $keyInput )
97+ labeledField ( " ElevenLabs API 키 " , placeholder : " sk_… " , secure : true , text : $keyInput ,
98+ hint : " App Support에 저장됩니다. " )
11399 HStack {
114- Button ( " 저장 " ) { app . saveKey ( keyInput ) ; keyInput = " " }
100+ Button ( " 키 저장" ) { app . saveKey ( keyInput ) ; keyInput = " " }
115101 . disabled ( keyInput . trimmingCharacters ( in : . whitespaces ) . isEmpty )
102+ Button { app . refreshVoices ( ) } label : {
103+ Label ( " 연결 테스트 / 보이스 새로고침 " , systemImage : " arrow.clockwise " )
104+ }
116105 Spacer ( )
117- Text ( " App Support에 저장됩니다 " ) . font ( . caption ) . foregroundStyle ( . secondary )
118106 }
119107 }
108+ }
120109
110+ if app . backendKind . usesLocalSidecar {
121111 Section ( " 로컬 sidecar " ) {
122112 LabeledContent ( " Base URL " ) {
123113 TextField ( " http://127.0.0.1:8765 " , text : $app . localBaseURL ) . frame ( width : 240 )
@@ -126,29 +116,66 @@ struct SettingsView: View {
126116 . font ( . caption ) . foregroundStyle ( . secondary )
127117 }
128118 }
129- . formStyle ( . grouped )
130- . onAppear {
131- if app . ollamaModels . isEmpty { app . refreshOllamaModels ( ) }
132- if app . voices . isEmpty && app . keyPresent { app . refreshVoices ( ) }
119+
120+ Section ( " 캐시 / 합성 " ) {
121+ Toggle ( " 같은 텍스트는 캐시에서 재생 (API 재호출 안 함) " , isOn : $app . useCache )
122+ LabeledContent ( " 문단 최대 글자수 " ) {
123+ TextField ( " " , value : $app . maxChunkChars , format : . number )
124+ . frame ( width : 80 ) . multilineTextAlignment ( . trailing )
125+ }
126+ Text ( " 긴 대본은 이 글자수 기준으로 문단을 나눠 합성·캐시·재생합니다 (오디오 분할 단위). " )
127+ . font ( . caption ) . foregroundStyle ( . secondary )
133128 }
134- . onChange ( of : app . localBaseURL ) { _ , _ in app . saveSettings ( ) }
135- . onChange ( of : app . normalizeEnabled ) { _ , _ in app . saveSettings ( ) }
136- . onChange ( of : app . normalizeProvider ) { _ , _ in app . saveSettings ( ) }
137- . onChange ( of : app . geminiModel ) { _ , _ in app . saveSettings ( ) }
138- . onChange ( of : app . geminiBaseURL ) { _ , _ in app . saveSettings ( ) }
139- . onChange ( of : app . ollamaModel ) { _ , _ in app . saveSettings ( ) }
140- . onChange ( of : app . ollamaURL ) { _ , _ in app . saveSettings ( ) }
141- . onChange ( of : app . maxChunkChars ) { _ , _ in app . saveSettings ( ) }
142- . onChange ( of : app . voiceId ) { _ , newID in
143- if let v = app . voices . first ( where : { $0 . id == newID } ) { app . voiceName = v . name }
144- app . saveSettings ( )
129+ }
130+
131+ // MARK: - ② 텍스트 생성 (LLM for 해설/대본)
132+
133+ @ ViewBuilder private var llmChannel : some View {
134+ Section ( " 제공자 " ) {
135+ Picker ( " LLM 제공자 " , selection : $app . normalizeProvider ) {
136+ ForEach ( AppState . NormalizeProvider . allCases ) { Text ( $0 . label ) . tag ( $0 ) }
137+ }
138+ Text ( " 코드 해설과 음성 대본(정규화) 생성에 사용하는 텍스트 생성 모델입니다. 모델은 해설/TTS 패널에서, temperature는 각 패널의 인스펙터(⊟)에서 조절합니다. " )
139+ . font ( . caption ) . foregroundStyle ( . secondary )
140+ }
141+
142+ if app . normalizeProvider == . gemini {
143+ Section ( " Gemini " ) {
144+ labeledField ( " 엔드포인트 " , placeholder : GeminiNormalizer . defaultBaseURL ,
145+ text : $app . geminiBaseURL ,
146+ hint : " API 루트. 끝에 /models/{모델}:generateContent 가 붙습니다. 프록시·게이트웨이 사용 시 변경. " )
147+ labeledField ( " API 키 " , placeholder : " AIza… " , secure : true , text : $geminiKeyInput ,
148+ hint : app . geminiKeyPresent ? " 현재: 설정됨 (App Support) " : " 현재: 없음 " )
149+ HStack {
150+ Button ( " 키 저장 " ) { app . saveGeminiKey ( geminiKeyInput ) ; geminiKeyInput = " " }
151+ . disabled ( geminiKeyInput . trimmingCharacters ( in : . whitespaces ) . isEmpty )
152+ Button ( " 연결 확인 / 모델 목록 " ) { app . refreshGeminiModels ( ) }
153+ Spacer ( )
154+ Text ( app . geminiStatus ) . font ( . caption ) . foregroundStyle ( . secondary )
155+ }
156+ }
157+ } else {
158+ Section ( " Ollama " ) {
159+ labeledField ( " 엔드포인트 " , placeholder : " http://localhost:11434 " ,
160+ text : $app . ollamaURL ,
161+ hint : " 로컬/원격 모두 가능: 예) http://192.168.0.10:11434 " )
162+ HStack {
163+ Button ( " 연결 확인 / 모델 목록 " ) { app . refreshOllamaModels ( ) }
164+ Spacer ( )
165+ Text ( app . ollamaStatus ) . font ( . caption ) . foregroundStyle ( . secondary )
166+ }
167+ }
168+ }
169+
170+ Section ( " 음성 대본 정규화 " ) {
171+ Toggle ( " 대본 생성 시 LLM으로 발음·표기 정규화 " , isOn : $app . normalizeEnabled )
172+ Text ( " 끄면 해설을 거의 그대로 합성합니다. 켜면 숫자·기호·코드 명칭을 발음대로 다듬습니다 (경량 로컬 모델은 부정확할 수 있어 강한 모델 권장). " )
173+ . font ( . caption ) . foregroundStyle ( . secondary )
145174 }
146- . onChange ( of : app . modelId ) { _ , _ in app . saveSettings ( ) }
147- . onChange ( of : app . backendKind ) { _ , _ in app . saveSettings ( ) }
148- . onChange ( of : app . useCache ) { _ , _ in app . saveSettings ( ) }
149- . onDisappear { app . saveSettings ( ) }
150175 }
151176
177+ // MARK: - Helpers
178+
152179 @ ViewBuilder
153180 private func slider ( _ label : String , _ value : Binding < Double > ) -> some View {
154181 VStack ( alignment : . leading , spacing : 2 ) {
@@ -171,7 +198,7 @@ struct SettingsView: View {
171198 if secure { SecureField ( placeholder , text : text ) }
172199 else { TextField ( placeholder , text : text ) }
173200 }
174- . labelsHidden ( ) // avoid the Form auto-label duplicating our caption
201+ . labelsHidden ( )
175202 . textFieldStyle ( . roundedBorder )
176203 if let hint { Text ( hint ) . font ( . caption2 ) . foregroundStyle ( . tertiary ) }
177204 }
0 commit comments