Merge pull request #65 from kkk669/issues/63

Fix laggy TextFields (#63)
This commit is contained in:
Kenta Kubo 2023-04-25 21:44:43 +09:00 committed by GitHub
commit e28358acfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 11 deletions

View file

@ -29,6 +29,7 @@
8998041628DCDED800C8B421 /* DoTSections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8998041528DCDED800C8B421 /* DoTSections.swift */; };
8998041828DCDEEF00C8B421 /* DoHSections.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8998041728DCDEEF00C8B421 /* DoHSections.swift */; };
89CB922125209DD100B6983C /* HowToActivateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89CB922025209DD100B6983C /* HowToActivateView.swift */; };
89E8B71A29F80164002C2AEF /* LazyTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89E8B71929F80164002C2AEF /* LazyTextField.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -79,6 +80,7 @@
8998041528DCDED800C8B421 /* DoTSections.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoTSections.swift; sourceTree = "<group>"; };
8998041728DCDEEF00C8B421 /* DoHSections.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoHSections.swift; sourceTree = "<group>"; };
89CB922025209DD100B6983C /* HowToActivateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HowToActivateView.swift; sourceTree = "<group>"; };
89E8B71929F80164002C2AEF /* LazyTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyTextField.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -116,6 +118,7 @@
894958AC2548405E009691D5 /* RuleView.swift */,
8998041528DCDED800C8B421 /* DoTSections.swift */,
8998041728DCDEEF00C8B421 /* DoHSections.swift */,
89E8B71929F80164002C2AEF /* LazyTextField.swift */,
);
path = Views;
sourceTree = "<group>";
@ -353,6 +356,7 @@
8940023E24ACBD2700EBE74B /* ContentView.swift in Sources */,
894958AD2548405E009691D5 /* RuleView.swift in Sources */,
8998041828DCDEEF00C8B421 /* DoHSections.swift in Sources */,
89E8B71A29F80164002C2AEF /* LazyTextField.swift in Sources */,
890B80DF251DC6B50046BAA0 /* Presets.swift in Sources */,
893AA858258F996F0060B022 /* NEOnDemandRuleInterfaceType+CustomStringConvertible.swift in Sources */,
8940023C24ACBD2700EBE74B /* DNSecureApp.swift in Sources */,

View file

@ -36,7 +36,7 @@ extension DetailView: View {
Toggle("Use This Server", isOn: self.$isOn)
}
Section("Name") {
TextField("Name", text: self.$server.name)
LazyTextField("Name", text: self.$server.name)
}
self.serverConfigurationSections
Section {
@ -73,7 +73,7 @@ extension DetailView: View {
Toggle("Use This Server", isOn: self.$isOn)
}
Section("Name") {
TextField("Name", text: self.$server.name)
LazyTextField("Name", text: self.$server.name)
}
self.serverConfigurationSections
Section {

View file

@ -15,7 +15,7 @@ extension DoHSections: View {
var body: some View {
Section {
ForEach(0..<self.configuration.servers.count, id: \.self) { i in
TextField("IP address", text: self.$configuration.servers[i])
LazyTextField("IP address", text: self.$configuration.servers[i])
.textContentType(.URL)
.keyboardType(.numbersAndPunctuation)
.textInputAutocapitalization(.never)
@ -36,7 +36,7 @@ extension DoHSections: View {
Text("The DNS server IP addresses.")
}
Section {
TextField(
LazyTextField(
"Server URL",
text: .init(
get: { self.configuration.serverURL?.absoluteString ?? "" },

View file

@ -15,7 +15,7 @@ extension DoTSections: View {
var body: some View {
Section {
ForEach(0..<self.configuration.servers.count, id: \.self) { i in
TextField("IP address", text: self.$configuration.servers[i])
LazyTextField("IP address", text: self.$configuration.servers[i])
.textContentType(.URL)
.keyboardType(.numbersAndPunctuation)
.textInputAutocapitalization(.never)
@ -36,7 +36,7 @@ extension DoTSections: View {
Text("The DNS server IP addresses.")
}
Section {
TextField(
LazyTextField(
"Server Name",
text: .init(
get: { self.configuration.serverName ?? "" },

View file

@ -0,0 +1,42 @@
//
// LazyTextField.swift
// DNSecure
//
// Created by Kenta Kubo on 4/25/23.
//
import SwiftUI
struct LazyTextField {
private let title: String
@Binding private var text: String
@State private var localText: String
@FocusState private var isFocused
init(_ title: some StringProtocol, text: Binding<String>) {
self.title = String(title)
self._text = text
self._localText = .init(initialValue: text.wrappedValue)
}
}
extension LazyTextField: View {
var body: some View {
TextField(self.title, text: self.$localText)
.focused(self.$isFocused)
.onChange(of: self.isFocused) { isFocused in
if !isFocused {
self.text = self.localText
}
}
.onChange(of: self.text) { text in
self.localText = text
}
}
}
struct LazyTextField_Previews: PreviewProvider {
static var previews: some View {
LazyTextField("Name", text: .constant(""))
}
}

View file

@ -16,7 +16,7 @@ extension RuleView: View {
var body: some View {
Form {
Section("Name") {
TextField("Name", text: self.$rule.name)
LazyTextField("Name", text: self.$rule.name)
}
Section {
@ -42,7 +42,7 @@ extension RuleView: View {
if self.rule.interfaceType.ssidIsUsed {
Section {
ForEach(0..<self.rule.ssidMatch.count, id: \.self) { i in
TextField(
LazyTextField(
"SSID",
// self.$rule.ssidMatch[i] causes crash on deletion
text: .init(
@ -71,7 +71,7 @@ extension RuleView: View {
Section {
ForEach(0..<self.rule.dnsSearchDomainMatch.count, id: \.self) { i in
TextField(
LazyTextField(
"Search Domain",
// self.$rule.dnsSearchDomainMatch[i] causes crash on deletion
text: .init(
@ -97,7 +97,7 @@ extension RuleView: View {
Section {
ForEach(0..<self.rule.dnsServerAddressMatch.count, id: \.self) { i in
TextField(
LazyTextField(
"IP Address",
// self.$rule.dnsServerAddressMatch[i] causes crash on deletion
text: .init(
@ -122,7 +122,7 @@ extension RuleView: View {
}
Section {
TextField(
LazyTextField(
"Probe URL",
text: .init(
get: { self.rule.probeURL?.absoluteString ?? "" },