-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from cybozu/add-clear-all-method
Add a clearAll method
- Loading branch information
Showing
9 changed files
with
147 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#if os(iOS) | ||
import UIKit | ||
typealias OSView = UIView | ||
#elseif os(macOS) | ||
import AppKit | ||
typealias OSView = NSView | ||
#endif | ||
|
||
final class Remakeable<Content: OSView>: OSView { | ||
private(set) var wrappedValue: Content { | ||
didSet { | ||
action?(wrappedValue) | ||
} | ||
} | ||
private let content: () -> Content | ||
private var action: ((Content) -> Void)? | ||
|
||
init(content: @escaping () -> Content) { | ||
self.content = content | ||
wrappedValue = content() | ||
super.init(frame: .zero) | ||
addSubview(wrappedValue) | ||
setConstraints() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func remake() { | ||
wrappedValue.removeFromSuperview() | ||
wrappedValue = content() | ||
addSubview(wrappedValue) | ||
setConstraints() | ||
} | ||
|
||
func onRemake(perform action: @escaping (Content) -> Void) { | ||
self.action = action | ||
} | ||
|
||
private func setConstraints() { | ||
wrappedValue.translatesAutoresizingMaskIntoConstraints = false | ||
wrappedValue.topAnchor.constraint(equalTo: topAnchor).isActive = true | ||
wrappedValue.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | ||
wrappedValue.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true | ||
wrappedValue.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters