Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clip element and fix clipping bug on iOS #254

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions element/api/element.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ public abstract interface class com/juul/krayon/element/ClickHandler {
public abstract fun onClick (Ljava/lang/Object;)V
}

public final class com/juul/krayon/element/ClipElement : com/juul/krayon/element/Element {
public static final field Companion Lcom/juul/krayon/element/ClipElement$Companion;
public fun <init> ()V
public fun draw (Lcom/juul/krayon/kanvas/Kanvas;)V
public final fun getClip ()Lcom/juul/krayon/kanvas/Clip;
public fun getTag ()Ljava/lang/String;
public final fun setClip (Lcom/juul/krayon/kanvas/Clip;)V
}

public final class com/juul/krayon/element/ClipElement$Companion : com/juul/krayon/element/ElementBuilder, com/juul/krayon/element/ElementSelector {
public fun build ()Lcom/juul/krayon/element/ClipElement;
public synthetic fun build ()Lcom/juul/krayon/element/Element;
public fun trySelect (Lcom/juul/krayon/element/Element;)Lcom/juul/krayon/element/ClipElement;
public synthetic fun trySelect (Lcom/juul/krayon/element/Element;)Lcom/juul/krayon/element/Element;
}

public abstract class com/juul/krayon/element/Element {
public static final field Companion Lcom/juul/krayon/element/Element$Companion;
public fun <init> ()V
Expand Down
31 changes: 31 additions & 0 deletions element/src/commonMain/kotlin/ClipElement.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.juul.krayon.element

import com.juul.krayon.kanvas.Clip
import com.juul.krayon.kanvas.Kanvas
import com.juul.krayon.kanvas.withClip

/**
* An element that clips rendering of child elements.
*
* Note that this does not prevent clipped children from being hit-tested for interactions.
*/
public class ClipElement : Element() {

override val tag: String get() = "clip"

public var clip: Clip? by attributes.withDefault { null }

override fun draw(kanvas: Kanvas) {
when (val clip = this.clip) {
null -> children.forEach { it.draw(kanvas) }
else -> kanvas.withClip(clip) {
children.forEach { it.draw(kanvas) }
}
}
}

public companion object : ElementBuilder<ClipElement>, ElementSelector<ClipElement> {
override fun build(): ClipElement = ClipElement()
override fun trySelect(element: Element): ClipElement? = element as? ClipElement
}
}
8 changes: 5 additions & 3 deletions kanvas/src/nativeDarwinMain/kotlin/CGContextKanvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ public class CGContextKanvas(

override fun pushClip(clip: Clip) {
CGContextSaveGState(unmanagedContext)
CGContextBeginPath(unmanagedContext)
clip.path.withCGPath { cgPath ->
CGContextAddPath(unmanagedContext, cgPath)
withInverseY {
CGContextBeginPath(unmanagedContext)
clip.path.withCGPath { cgPath ->
CGContextAddPath(unmanagedContext, cgPath)
}
}
CGContextClip(unmanagedContext)
}
Expand Down