-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a single ComposeModifierView for all modifiers
Add ability to peek at unmodified inner view. Support SwiftUI-like variable default spacing in VStack
- Loading branch information
Showing
8 changed files
with
107 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// This is free software: you can redistribute and/or modify it | ||
// under the terms of the GNU Lesser General Public License 3.0 | ||
// as published by the Free Software Foundation https://fsf.org | ||
|
||
#if SKIP | ||
import androidx.compose.runtime.Composable | ||
|
||
/// Recognized modifier roles. | ||
public enum ComposeModifierRole { // View.strippingModifiers becomes public in Kotlin and exposes this type, so it must be public | ||
case accessibility | ||
case spacing | ||
case unspecified | ||
} | ||
|
||
/// Used internally by modifiers to apply changes to the context supplied to modified views. | ||
struct ComposeModifierView: View { | ||
let view: View | ||
let role: ComposeModifierRole | ||
private let contextTransform: (@Composable (inout ComposeContext) -> Void)? | ||
private let composeContent: (@Composable (any View, ComposeContext) -> Void)? | ||
|
||
/// A modfiier that transforms the compose context. | ||
init(contextView: any View, role: ComposeModifierRole = .unspecified, contextTransform: @Composable (inout ComposeContext) -> Void) { | ||
// Don't copy view | ||
// SKIP REPLACE: this.view = contextView | ||
self.view = contextView | ||
self.role = role | ||
self.contextTransform = contextTransform | ||
self.composeContent = nil | ||
} | ||
|
||
/// A modifier that takes over the composition. | ||
init(contentView: any View, role: ComposeModifierRole = .unspecified, composeContent: @Composable (any View, ComposeContext) -> Void) { | ||
// Don't copy view | ||
// SKIP REPLACE: this.view = contentView | ||
self.view = contentView | ||
self.role = role | ||
self.contextTransform = nil | ||
self.composeContent = composeContent | ||
} | ||
|
||
@Composable override func ComposeContent(context: ComposeContext) { | ||
if let composeContent { | ||
composeContent(view, context) | ||
} else if let contextTransform { | ||
var context = context | ||
contextTransform(&context) | ||
view.ComposeContent(context) | ||
} | ||
} | ||
|
||
func strippingModifiers<R>(whileRole: (ComposeModifierRole) -> Bool = { _ in true}, perform: (any View?) -> R) -> R { | ||
if whileRole(role) { | ||
return view.strippingModifiers(whileRole: whileRole, perform: perform) | ||
} else { | ||
return perform(self) | ||
} | ||
} | ||
} | ||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2023 Skip | ||
// | ||
// This is free software: you can redistribute and/or modify it | ||
// under the terms of the GNU Lesser General Public License 3.0 | ||
// as published by the Free Software Foundation https://fsf.org | ||
|
||
import SwiftUI | ||
import XCTest | ||
|
||
final class ModifierTests: XCTestCase { | ||
func testModifierViewDoesNotCopy() { | ||
#if SKIP | ||
let text = Text("test") | ||
let modified = text.font(.title).padding(.all, 10.0) | ||
modified.strippingModifiers { XCTAssertEqual($0, text) } | ||
#endif | ||
} | ||
} |