From dec111f01c496311453992e9b57f135d3e1ff25a Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkk669@users.noreply.github.com> Date: Tue, 25 Apr 2023 21:42:47 +0900 Subject: [PATCH] Fix laggy TextFields (#63) fixes #63 --- DNSecure.xcodeproj/project.pbxproj | 4 +++ DNSecure/Views/DetailView.swift | 4 +-- DNSecure/Views/DoHSections.swift | 4 +-- DNSecure/Views/DoTSections.swift | 4 +-- DNSecure/Views/LazyTextField.swift | 42 ++++++++++++++++++++++++++++++ DNSecure/Views/RuleView.swift | 10 +++---- 6 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 DNSecure/Views/LazyTextField.swift diff --git a/DNSecure.xcodeproj/project.pbxproj b/DNSecure.xcodeproj/project.pbxproj index 5911144..70f73f7 100644 --- a/DNSecure.xcodeproj/project.pbxproj +++ b/DNSecure.xcodeproj/project.pbxproj @@ -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 = ""; }; 8998041728DCDEEF00C8B421 /* DoHSections.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoHSections.swift; sourceTree = ""; }; 89CB922025209DD100B6983C /* HowToActivateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HowToActivateView.swift; sourceTree = ""; }; + 89E8B71929F80164002C2AEF /* LazyTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyTextField.swift; sourceTree = ""; }; /* 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 = ""; @@ -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 */, diff --git a/DNSecure/Views/DetailView.swift b/DNSecure/Views/DetailView.swift index f9f37ea..b3736a1 100644 --- a/DNSecure/Views/DetailView.swift +++ b/DNSecure/Views/DetailView.swift @@ -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 { diff --git a/DNSecure/Views/DoHSections.swift b/DNSecure/Views/DoHSections.swift index 8c2a7d8..df180a1 100644 --- a/DNSecure/Views/DoHSections.swift +++ b/DNSecure/Views/DoHSections.swift @@ -15,7 +15,7 @@ extension DoHSections: View { var body: some View { Section { ForEach(0..) { + 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("")) + } +} diff --git a/DNSecure/Views/RuleView.swift b/DNSecure/Views/RuleView.swift index 657c529..5d46ebd 100644 --- a/DNSecure/Views/RuleView.swift +++ b/DNSecure/Views/RuleView.swift @@ -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..