Skip to content

Commit

Permalink
Focus on the first input field when opening sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Oct 26, 2024
1 parent 197fd98 commit e36de45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- Update the Swift syntax to add `@ViewLoading` and `@WindowLoading` attributes.
- [trivial] Rename “Move Focus” commands.
- [trivial] Focus on the first input field when opening a dialog.


### Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ import StringUtils

struct CustomSurroundView: View {

private enum Focus {

case beginField
case endField
}


weak var parent: NSHostingController<Self>?

@AppStorage("beginCustomSurroundString") private var defaultBeginString: String?
@AppStorage("endCustomSurroundString") private var defaultEndString: String?

@FocusState private var focus: Focus?

@State private var pair: Pair<String> = .init("", "")
private let completionHandler: (_ pair: Pair<String>) -> Void

Expand Down Expand Up @@ -66,12 +75,15 @@ struct CustomSurroundView: View {
LabeledContent(String(localized: "Begin:", table: "CustomSurround")) {
TextField(text: $pair.begin, label: EmptyView.init)
.frame(width: 48)
}.padding(.trailing)
}
.focused($focus, equals: .beginField)
.padding(.trailing)

LabeledContent(String(localized: "End:", table: "CustomSurround")) {
TextField(text: $pair.end, prompt: Text(verbatim: self.pair.begin), label: EmptyView.init)
.frame(width: 48)
}
.focused($focus, equals: .endField)
}
.onSubmit { self.submit() }
.fixedSize()
Expand All @@ -85,6 +97,9 @@ struct CustomSurroundView: View {
}
}
}
.onAppear {
self.focus = .beginField
}
.fixedSize()
.scenePadding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ import SwiftUI

struct CustomTabWidthView: View {

private enum Focus {

case field
}


weak var parent: NSHostingController<Self>?

@FocusState private var focus: Focus?

@State private var value: Int
private let defaultWidth: Int
private let completionHandler: (_ tabWidth: Int) -> Void
Expand All @@ -54,6 +62,7 @@ struct CustomTabWidthView: View {
VStack {
LabeledContent(String(localized: "Tab width:", table: "CustomTabWidth")) {
StepperNumberField(value: $value, default: self.defaultWidth, in: 1...99)
.focused($focus, equals: .field)
.onSubmit { self.submit() }
}

Expand All @@ -66,6 +75,9 @@ struct CustomTabWidthView: View {
}
}
}
.onAppear {
self.focus = .field
}
.fixedSize()
.scenePadding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import FuzzyRange

struct GoToLineView: View {

private enum Focus {

case field
}


weak var parent: NSHostingController<Self>?

/// The current line range.
Expand All @@ -36,6 +42,8 @@ struct GoToLineView: View {
/// The callback method to perform when the command was accepted.
let completionHandler: (_ lineRange: FuzzyRange) -> Bool

@FocusState private var focus: Focus?


// MARK: View

Expand All @@ -47,6 +55,7 @@ struct GoToLineView: View {
prompt: Text("Line Number", tableName: "GoToLine", comment: "placeholder"))
.monospacedDigit()
.multilineTextAlignment(.trailing)
.focused($focus, equals: .field)
.onSubmit { self.submit() }
}

Expand All @@ -62,6 +71,9 @@ struct GoToLineView: View {
}
}
}
.onAppear {
self.focus = .field
}
.fixedSize()
.scenePadding()
}
Expand Down

0 comments on commit e36de45

Please sign in to comment.