I write macOS menu app with TextField by SwiftUI on Japanese Input mode. On some conditions, the TextFiled lost focus, no key input, no mouse click. User cannot do anything.
Setup
- MacOS Ventura 13.3.1 (a)
- Install Japanese Romaji Input source by System Preferences -> Keyboard
- Set input mode as "Romaji"
Build test source code
- On Xcode 14.3, create new macOS app project "FocusTest" with SwiftUI, Swift.
- Replace FocusTestApp.swift with attached code.
- Build on Xcode
Steps
- Set input mode as "Romaji"
- Run FocusTestApp
- Click T square icon on top menu
- Small windows with globe appear
- Click Desktop background area
- Click T square icon on top menu
- Click PIN
- T with PIN textField View appear
- That textField lost focus, click inside of textField
- Key or click is not accepted.
With US keyboard mode, key input become possible on Step 10. But Focused blue square is missing.
Code of FocusTestApp.swift
import SwiftUI
@main
struct focusTestApp: App {
var body: some Scene {
MenuBarExtra("Test", systemImage: "t.square") {
MainView()
}.menuBarExtraStyle(.window)
}
}
struct MainView: View {
@State private var showingPIN: Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Button("PIN") {
print("clicked")
showingPIN = true
}
}
.padding()
.sheet(isPresented: $showingPIN) {
PinView()
}
}
}
struct PinView: View {
@Environment(\.presentationMode) var presentationMode
@State private var pin: String = ""
@FocusState private var pinIsFocused: Bool
var body: some View {
VStack {
Image(systemName: "t.square")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 64.0, height: 64.0)
.foregroundColor(.accentColor)
Text("Enter PIN code")
HStack {
TextField("", text: $pin)
.font(Font.system(size: 28, design: .default))
.frame(width:4*28.0, height:28.0)
.focusable()
.focused($pinIsFocused)
}
.onAppear(){
pinIsFocused = true
}
}
.padding()
}
}