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

Koltin multiplatform support #3

Merged
merged 5 commits into from
Jan 12, 2025
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
# Ignore Gradle build output directory
build


.idea

.kotlin
12 changes: 8 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
[versions]
kotlin = "2.0.21"
dokka = "1.9.20"
publisher = "0.30.0"
kover = "0.8.3"

[plugins]
jvm = { id = "org.jetbrains.kotlin.jvm", version = "1.9.23" }
dokka = { id = "org.jetbrains.dokka", version = "1.9.20" }
publisher = { id = "com.vanniktech.maven.publish", version = "0.29.0" }
kover = { id = "org.jetbrains.kotlinx.kover", version = "0.8.3" }
kmp = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
publisher = { id = "com.vanniktech.maven.publish", version.ref = "publisher" }
kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
2,081 changes: 2,081 additions & 0 deletions kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

34 changes: 22 additions & 12 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import com.vanniktech.maven.publish.KotlinMultiplatform
import com.vanniktech.maven.publish.SonatypeHost

plugins {
alias(libs.plugins.jvm)
alias(libs.plugins.kmp)
alias(libs.plugins.dokka)
alias(libs.plugins.publisher)
alias(libs.plugins.kover)
}

dependencies {
testImplementation(kotlin("test"))
}
kotlin {
jvm()
js {
nodejs()
browser()
}

tasks.test {
useJUnitPlatform()
sourceSets {
commonMain.dependencies {}
commonTest.dependencies {
implementation(kotlin("test"))
}
}
}


mavenPublishing {
configure(KotlinJvm(
javadocJar = JavadocJar.Dokka("dokkaHtml"),
sourcesJar = true,
))
configure(
KotlinMultiplatform(
javadocJar = JavadocJar.Dokka("dokkaHtml"),
sourcesJar = true,
)
)

coordinates(
groupId = rootProject.group as String,
Expand Down Expand Up @@ -58,7 +68,7 @@ mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

// Enable GPG signing for all publications
signAllPublications()
// signAllPublications()
}

tasks.withType(Javadoc::class.java) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.github.allangomes.kotlinwind.css.core

import io.github.allangomes.kotlinwind.css.utils.roundToDecimalPlaces

class WithPercentual<T>(
private val block: Function1<Float, T>
) {

operator fun get(value: Int): T {
return block(value.toFloat())
return block(value.toFloat().roundToDecimalPlaces())
}

/**
* Fractions {dividend} / {divisor}
*/
operator fun get(dividend: Int, divisor: Int): T {
val value = dividend.toFloat() / divisor.toFloat()
return block(value * 100)
return block(value.roundToDecimalPlaces() * 100)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.allangomes.kotlinwind.css.core.BOTTOM
import io.github.allangomes.kotlinwind.css.core.StyleValueMarker
import io.github.allangomes.kotlinwind.css.core.WithNumber
import io.github.allangomes.kotlinwind.css.core.WithPercentual
import io.github.allangomes.kotlinwind.css.utils.normalizeDecimal

@Suppress("PropertyName")
interface PositionBottom<T> : KWScope<T> {
Expand All @@ -15,8 +16,8 @@ interface PositionBottom<T> : KWScope<T> {
@StyleValueMarker
val bottom: WithNumber<T>
get() = WithNumber {
val value = it * 0.25
BOTTOM value "${value}rem"
val value = it * 0.25f
BOTTOM value "${normalizeDecimal(value)}rem"
}

/**
Expand All @@ -25,7 +26,7 @@ interface PositionBottom<T> : KWScope<T> {
@StyleValueMarker
val bottom_pct: WithPercentual<T>
get() = WithPercentual {
BOTTOM value "$it%"
BOTTOM value "${normalizeDecimal(it)}%"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.allangomes.kotlinwind.css.core.LEFT
import io.github.allangomes.kotlinwind.css.core.StyleValueMarker
import io.github.allangomes.kotlinwind.css.core.WithNumber
import io.github.allangomes.kotlinwind.css.core.WithPercentual
import io.github.allangomes.kotlinwind.css.utils.normalizeDecimal

@Suppress("PropertyName")
interface PositionLeft<T> : KWScope<T> {
Expand All @@ -15,7 +16,7 @@ interface PositionLeft<T> : KWScope<T> {
@StyleValueMarker
val left: WithNumber<T>
get() = WithNumber {
val value = it * 0.25
val value = normalizeDecimal(it * 0.25f)
LEFT value "${value}rem"
}

Expand All @@ -25,7 +26,7 @@ interface PositionLeft<T> : KWScope<T> {
@StyleValueMarker
val left_pct: WithPercentual<T>
get() = WithPercentual {
LEFT value "$it%"
LEFT value "${normalizeDecimal(it)}%"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.allangomes.kotlinwind.css.core.RIGHT
import io.github.allangomes.kotlinwind.css.core.StyleValueMarker
import io.github.allangomes.kotlinwind.css.core.WithNumber
import io.github.allangomes.kotlinwind.css.core.WithPercentual
import io.github.allangomes.kotlinwind.css.utils.normalizeDecimal

@Suppress("PropertyName")
interface PositionRight<T> : KWScope<T> {
Expand All @@ -15,7 +16,7 @@ interface PositionRight<T> : KWScope<T> {
@StyleValueMarker
val right: WithNumber<T>
get() = WithNumber {
val value = it * 0.25
val value = normalizeDecimal(it * 0.25f)
RIGHT value "${value}rem"
}

Expand All @@ -25,7 +26,7 @@ interface PositionRight<T> : KWScope<T> {
@StyleValueMarker
val right_pct: WithPercentual<T>
get() = WithPercentual {
RIGHT value "$it%"
RIGHT value "${normalizeDecimal(it)}%"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.allangomes.kotlinwind.css.core.StyleValueMarker
import io.github.allangomes.kotlinwind.css.core.TOP
import io.github.allangomes.kotlinwind.css.core.WithNumber
import io.github.allangomes.kotlinwind.css.core.WithPercentual
import io.github.allangomes.kotlinwind.css.utils.normalizeDecimal

@Suppress("PropertyName")
interface PositionTop<T> : KWScope<T> {
Expand All @@ -15,7 +16,7 @@ interface PositionTop<T> : KWScope<T> {
@StyleValueMarker
val top: WithNumber<T>
get() = WithNumber {
val value = it * 0.25
val value = normalizeDecimal(it * 0.25f)
TOP value "${value}rem"
}

Expand All @@ -25,7 +26,7 @@ interface PositionTop<T> : KWScope<T> {
@StyleValueMarker
val top_pct: WithPercentual<T>
get() = WithPercentual {
TOP value "$it%"
TOP value "${normalizeDecimal(it)}%"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.allangomes.kotlinwind.css.features.commom

import io.github.allangomes.kotlinwind.css.api.KWScope
import io.github.allangomes.kotlinwind.css.core.*
import io.github.allangomes.kotlinwind.css.utils.normalizeDecimal


@Suppress("PropertyName")
Expand All @@ -12,23 +13,23 @@ interface Gap<T> : KWScope<T> {
* - [documentation](https://tailwindcss.com/docs/gap)
* */
val gap: WithNumber<T> get() = WithNumber {
GAP value "${(it * 0.25f)}rem"
GAP value "${normalizeDecimal(it * 0.25f)}rem"
}

@StyleValueMarker
/** column-gap: {number} * 0.25rem
* - [documentation](https://tailwindcss.com/docs/gap)
* */
val gap_x: WithNumber<T> get() = WithNumber {
GAP_COLUMN value "${(it * 0.25f)}rem"
GAP_COLUMN value "${normalizeDecimal(it * 0.25f)}rem"
}

@StyleValueMarker
/** row-gap: {number} * 0.25rem
* - [documentation](https://tailwindcss.com/docs/gap)
* */
val gap_y: WithNumber<T> get() = WithNumber {
GAP_ROW value "${(it * 0.25f)}rem"
GAP_ROW value "${normalizeDecimal(it * 0.25f)}rem"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.allangomes.kotlinwind.css.utils

import kotlin.math.pow
import kotlin.math.ceil
import kotlin.math.floor

/**
* Normalize the decimal value to a string and round it to the nearest integer to avoid inconsistencies
* between different platforms.
* @see https://youtrack.jetbrains.com/issue/KT-57189
*/
fun normalizeDecimal(value: Float): String {
val roundedValue = value.roundToDecimalPlaces()
return if (roundedValue % 1.0f == 0.0f) {
roundedValue.toInt().toString()
} else {
roundedValue.toString().trimEnd('0').trimEnd('.')
}
}

/**
* Rounds a Float to a specific number of decimal places.
* Ensure rounding behaves consistently across platforms.
*/
fun Float.roundToDecimalPlaces(decimalPlaces: Int = 5): Float {
val factor = 10.0.pow(decimalPlaces).toFloat()
return (this * factor).roundToNearest() / factor
}

fun Float.roundToNearest(): Float {
return if (this >= 0) floor(this + 0.5f) else ceil(this - 0.5f)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.test.assertEquals
class ColorsTest {

@Test
fun `Change color`() {
fun change_color() {
val expected = "RED_ALTER"
Theme.color[RED.I50] = expected
assertEquals(expected, Theme.color[RED.I50])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class PositionBottomTest {

@Test
fun bottom_fraction() {
val expected = Style(BOTTOM, "25.0%").toString()
val expected = Style(BOTTOM, "25%").toString()
val returned = KW.inline { bottom_pct[1, 4] }
assertEquals(expected, returned)
}

@Test
fun bottom_pct() {
val expected = Style(BOTTOM, "32.0%").toString()
val expected = Style(BOTTOM, "32%").toString()
val returned = KW.inline { bottom_pct[32] }
assertEquals(expected, returned)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.allangomes.kotlinwind.css.features

import io.github.allangomes.kotlinwind.css.KW
import io.github.allangomes.kotlinwind.css.api.Style
import io.github.allangomes.kotlinwind.css.core.LEFT
import kotlin.test.Test
import kotlin.test.assertEquals

class PositionLeftTest {

@Test
fun left() {
val expected = Style(LEFT, "2.5rem").toString()
val returned = KW.inline { left[10] }
assertEquals(expected, returned)
}

@Test
fun left_fraction() {
val expected = Style(LEFT, "25%").toString()
val returned = KW.inline { left_pct[1, 4] }
assertEquals(expected, returned)
}

@Test
fun left_pct() {
val expected = Style(LEFT, "32%").toString()
val returned = KW.inline { left_pct[32] }
assertEquals(expected, returned)
}

@Test
fun left_auto() {
val expected = Style(LEFT, "auto").toString()
val returned = KW.inline { left_auto }
assertEquals(expected, returned)
}

@Test
fun left_full() {
val expected = Style(LEFT, "100%").toString()
val returned = KW.inline { left_full }
assertEquals(expected, returned)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class PositionRightTest {

@Test
fun right_fraction() {
val expected = Style(RIGHT, "25.0%").toString()
val expected = Style(RIGHT, "25%").toString()
val returned = KW.inline { right_pct[1, 4] }
assertEquals(expected, returned)
}

@Test
fun right_pct() {
val expected = Style(RIGHT, "32.0%").toString()
val expected = Style(RIGHT, "32%").toString()
val returned = KW.inline { right_pct[32] }
assertEquals(expected, returned)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ class PositionTopTest {

@Test
fun top_fraction() {
val expected = Style(TOP, "25.0%").toString()
val expected = Style(TOP, "25%").toString()
val returned = KW.inline { top_pct[1, 4] }
assertEquals(expected, returned)
}

@Test
fun top_fraction_with_decimal() {
val expected = Style(TOP, "33.333%").toString()
val returned = KW.inline { top_pct[1, 3] }
assertEquals(expected, returned)
}

@Test
fun top_pct() {
val expected = Style(TOP, "32.0%").toString()
val expected = Style(TOP, "32%").toString()
val returned = KW.inline { top_pct[32] }
assertEquals(expected, returned)
}
Expand Down
Loading