Prefer using .onGeometryChange(for:of:action:)

This commit is contained in:
1024jp 2024-06-29 10:51:16 +09:00
parent 9ce8f7dffb
commit b0481ce3d4
4 changed files with 11 additions and 72 deletions

View File

@ -542,8 +542,6 @@
2AAFA7BD2B7A2DB000A2B228 /* MultipleReplaceListView.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2AAFA7BA2B7A2DAF00A2B228 /* MultipleReplaceListView.storyboard */; };
2AB1BD1C287D60DF00C6FEAF /* CharacterCountOptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD1B287D60DF00C6FEAF /* CharacterCountOptionsView.swift */; };
2AB1BD1D287D60DF00C6FEAF /* CharacterCountOptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD1B287D60DF00C6FEAF /* CharacterCountOptionsView.swift */; };
2AB1BD1F287D747200C6FEAF /* SizeGetter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD1E287D747200C6FEAF /* SizeGetter.swift */; };
2AB1BD20287D747200C6FEAF /* SizeGetter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD1E287D747200C6FEAF /* SizeGetter.swift */; };
2AB1BD24287DA73D00C6FEAF /* CharacterCountOptionsSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD23287DA73D00C6FEAF /* CharacterCountOptionsSheetView.swift */; };
2AB1BD25287DA73D00C6FEAF /* CharacterCountOptionsSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB1BD23287DA73D00C6FEAF /* CharacterCountOptionsSheetView.swift */; };
2AB541DA20A5B6A400367DD5 /* NSView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB541D920A5B6A400367DD5 /* NSView.swift */; };
@ -1082,7 +1080,6 @@
2AAFA7D42B7A2F5800A2B228 /* mul */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; name = mul; path = mul.lproj/MultipleReplaceView.xcstrings; sourceTree = "<group>"; };
2AAFA7D52B7A2F6D00A2B228 /* mul */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; name = mul; path = mul.lproj/MultipleReplaceListView.xcstrings; sourceTree = "<group>"; };
2AB1BD1B287D60DF00C6FEAF /* CharacterCountOptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCountOptionsView.swift; sourceTree = "<group>"; };
2AB1BD1E287D747200C6FEAF /* SizeGetter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizeGetter.swift; sourceTree = "<group>"; };
2AB1BD23287DA73D00C6FEAF /* CharacterCountOptionsSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCountOptionsSheetView.swift; sourceTree = "<group>"; };
2AB541D920A5B6A400367DD5 /* NSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSView.swift; sourceTree = "<group>"; };
2AB857E72B922D7D0079CFA2 /* ModeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModeManager.swift; sourceTree = "<group>"; };
@ -1777,7 +1774,7 @@
isa = PBXGroup;
children = (
2AF601CB296F925200F6F1E8 /* Views */,
2AF601CC296F925900F6F1E8 /* Helpers */,
2AF601CC296F925900F6F1E8 /* Styles */,
);
name = SwiftUI;
sourceTree = "<group>";
@ -2162,13 +2159,12 @@
name = Views;
sourceTree = "<group>";
};
2AF601CC296F925900F6F1E8 /* Helpers */ = {
2AF601CC296F925900F6F1E8 /* Styles */ = {
isa = PBXGroup;
children = (
2A24F9122BEDF6D000CB6CCF /* CapsuleButtonStyle.swift */,
2AB1BD1E287D747200C6FEAF /* SizeGetter.swift */,
);
name = Helpers;
name = Styles;
sourceTree = "<group>";
};
2AF98CAD294C063E009AD47F /* Views */ = {
@ -2860,7 +2856,6 @@
2A64F2491D26327C001B229F /* Shortcut+Error.swift in Sources */,
2AACB1CE1D195ABD0073775B /* ShortcutField.swift in Sources */,
2A30C7DB2B1380BE002F6381 /* ShortcutView.swift in Sources */,
2AB1BD1F287D747200C6FEAF /* SizeGetter.swift in Sources */,
2AEC48341E641E4F00FB0F89 /* Snippet.swift in Sources */,
2A64F2461D259E49001B229F /* SnippetManager.swift in Sources */,
2ACDA2532B813FA500B2EBA8 /* SnippetsSettingsView.swift in Sources */,
@ -3177,7 +3172,6 @@
2A64F2481D26327C001B229F /* Shortcut+Error.swift in Sources */,
2AACB1CD1D195ABD0073775B /* ShortcutField.swift in Sources */,
2A30C7DC2B1380BE002F6381 /* ShortcutView.swift in Sources */,
2AB1BD20287D747200C6FEAF /* SizeGetter.swift in Sources */,
2AEC48331E641E4F00FB0F89 /* Snippet.swift in Sources */,
2A64F2451D259E49001B229F /* SnippetManager.swift in Sources */,
2ACDA2542B813FA500B2EBA8 /* SnippetsSettingsView.swift in Sources */,

View File

@ -105,8 +105,11 @@ struct CharacterCountOptionsView: View {
Divider()
}
}
}.fixedSize()
.background(SizeGetter(key: MaxSizeKey.self))
}
.fixedSize()
.onGeometryChange(for: CGFloat.self, of: \.size.width) { newWidth in
self.contentWidth = newWidth
}
}
if self.unit != .graphemeCluster {
@ -133,7 +136,6 @@ struct CharacterCountOptionsView: View {
}
.accessibilityElement(children: .contain)
}
.onPreferenceChange(MaxSizeKey.self) { self.contentWidth = $0.width }
.animation(.default, value: self.unit)
}
}

View File

@ -1,54 +0,0 @@
//
// SizeGetter.swift
//
// CotEditor
// https://coteditor.com
//
// Created by 1024jp on 2022-07-12.
//
// ---------------------------------------------------------------------------
//
// © 2022-2024 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
struct SizeGetter<Key: PreferenceKey>: View where Key.Value == CGSize {
let key: Key.Type
var body: some View {
GeometryReader { geometry in
Color.clear.preference(key: self.key.self, value: geometry.size)
}
}
}
/// Stores max size.
struct MaxSizeKey: PreferenceKey {
static var defaultValue: CGSize { .zero }
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
let next = nextValue()
value = CGSize(width: max(value.width, next.width),
height: max(value.height, next.height))
}
}

View File

@ -178,12 +178,9 @@ struct SyntaxEditView: View {
self.errors = self.syntax.validate()
}
.alert(error: $error)
.background { // store last view size
GeometryReader { geometry in
Color.clear.onChange(of: geometry.size) { (_, newValue) in
Self.viewSize = newValue
}
}
.onGeometryChange(for: CGSize.self, of: \.size) { newValue in
// store last view size
Self.viewSize = newValue
}
.frame(idealWidth: Self.viewSize.width, minHeight: 525, idealHeight: Self.viewSize.height)
}