Skip to content

Commit

Permalink
Observe UserDefaults values
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed Oct 3, 2023
1 parent c1bde99 commit 5e75111
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ kotlin {
}

sourceSets {
val nativeMain by creating
val nativeMain by creating {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}
}
val macosX64Main by getting {
dependsOn(nativeMain)
}
Expand Down
18 changes: 18 additions & 0 deletions src/nativeMain/kotlin/Debouncer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.coroutines.EmptyCoroutineContext

class Debouncer(private val delayMs: Long) {
private val scope = CoroutineScope(EmptyCoroutineContext)
private var job: Job? = null

fun execute(task: () -> Unit) {
job?.cancel()
job = scope.launch {
delay(delayMs)
task()
}
}
}
32 changes: 22 additions & 10 deletions src/nativeMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.useContents
import platform.AppKit.*
import platform.Foundation.NSBundle
import platform.Foundation.NSMakeRect
import platform.Foundation.NSRect
import platform.Foundation.*
import platform.ScreenSaver.ScreenSaverView
import kotlin.math.pow
import kotlin.math.sqrt
Expand Down Expand Up @@ -35,17 +33,16 @@ abstract class KotlinScreenSaverView {
}

class KotlinLogosViewImpl : KotlinScreenSaverView() {
private val logos: List<BouncingLogo> by lazy {
List(Preferences.LOGO_COUNT) { BouncingLogo(view, bundle) }
}
private var logos: List<BouncingLogo> = emptyList()

override fun init(screenSaverView: ScreenSaverView, isPreview: Boolean, bundle: NSBundle) {
super.init(screenSaverView, isPreview, bundle)
screenSaverView.animationTimeInterval = 1 / 120.0
setupUserDefaultsObserver()
initLogos()
}

override fun draw(rect: CPointer<NSRect>) {
clearStage()
logos.forEach(BouncingLogo::draw)
}

Expand All @@ -54,9 +51,20 @@ class KotlinLogosViewImpl : KotlinScreenSaverView() {
view.setNeedsDisplayInRect(view.frame)
}

private fun clearStage() {
NSColor.blackColor.setFill()
NSRectFill(view.frame)
private val debouncer = Debouncer(500)

private fun setupUserDefaultsObserver() {
NSNotificationCenter.defaultCenter
.addObserverForName(NSUserDefaultsDidChangeNotification, null, null) {
debouncer.execute {
initLogos()
}
}
}

private fun initLogos() {
logos.forEach(BouncingLogo::dispose)
logos = List(Preferences.LOGO_COUNT) { BouncingLogo(view, bundle) }
}
}

Expand Down Expand Up @@ -171,4 +179,8 @@ class BouncingLogo(
logoWidth = area / logoHeight
}
}

fun dispose() {
imageView.removeFromSuperview()
}
}

0 comments on commit 5e75111

Please sign in to comment.