-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
12 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
48 changes: 48 additions & 0 deletions
48
src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/FillerAnimation.kt
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 @@ | ||
package de.atennert.lcarsde.statusbar | ||
|
||
import de.atennert.lcarsde.statusbar.widgets.StatusFillerWidget | ||
import kotlinx.coroutines.* | ||
import kotlin.random.Random | ||
|
||
/** | ||
* Controls the update/animation lifecycle for all filler widgets. | ||
*/ | ||
class FillerAnimation { | ||
|
||
private val fillerWidgets = mutableListOf<StatusFillerWidget>() | ||
|
||
private var updateJob: Job? = null | ||
|
||
private val minimalSwitchDelay = 2_000L // ms | ||
private val maximalSwitchDelay = 10_000L // ms | ||
|
||
fun addFillerWidget(fillerWidget: StatusFillerWidget) { | ||
fillerWidgets.add(fillerWidget) | ||
} | ||
|
||
fun clearWidgets() { | ||
fillerWidgets.clear() | ||
} | ||
|
||
private fun animateFiller() { | ||
val widget = fillerWidgets.randomOrNull() ?: return | ||
|
||
widget.update() | ||
} | ||
|
||
fun startAnimation() { | ||
fillerWidgets.forEach(StatusFillerWidget::start) | ||
updateJob = GlobalScope.launch(Dispatchers.Unconfined) { | ||
while (true) { | ||
delay(Random.nextLong(minimalSwitchDelay, maximalSwitchDelay)) | ||
animateFiller() | ||
} | ||
} | ||
} | ||
|
||
fun stopAnimation() { | ||
updateJob?.cancel() | ||
updateJob = null | ||
fillerWidgets.forEach(StatusFillerWidget::stop) | ||
} | ||
} |
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
93 changes: 82 additions & 11 deletions
93
src/nativeMain/kotlin/de/atennert/lcarsde/statusbar/widgets/StatusFillerWidget.kt
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 |
---|---|---|
@@ -1,33 +1,104 @@ | ||
package de.atennert.lcarsde.statusbar.widgets | ||
|
||
import de.atennert.lcarsde.statusbar.configuration.WidgetConfiguration | ||
import de.atennert.lcarsde.statusbar.extensions.setStyling | ||
import kotlinx.cinterop.CPointer | ||
import kotlinx.cinterop.reinterpret | ||
import de.atennert.lcarsde.statusbar.extensions.gSignalConnect | ||
import kotlinx.cinterop.* | ||
import statusbar.* | ||
import kotlin.math.PI | ||
import kotlin.random.Random | ||
|
||
class StatusFillerWidget(widgetConfiguration: WidgetConfiguration, cssProvider: CPointer<GtkCssProvider>) | ||
: StatusWidget(widgetConfiguration, cssProvider, null) { | ||
|
||
private var ref: StableRef<StatusFillerWidget>? = null | ||
|
||
private var lastColor = "123" | ||
|
||
init { | ||
val text = "${Random.nextInt(10000)}".padStart(4, '0') | ||
widget = gtk_label_new(text)!! | ||
widget = gtk_drawing_area_new()!! | ||
gtk_widget_set_size_request(widget, widthPx, heightPx) | ||
gtk_label_set_xalign(widget.reinterpret(), 1f) | ||
gtk_label_set_yalign(widget.reinterpret(), 1f) | ||
} | ||
|
||
override fun start() { | ||
ref = StableRef.create(this) | ||
|
||
gSignalConnect(widget, "draw", | ||
staticCFunction { _: CPointer<GtkWidget>, c: CPointer<cairo_t>, p: COpaquePointer -> draw(c, p) }, | ||
ref!!.asCPointer()) | ||
|
||
val colorIdx = Random.nextInt(colors.size) | ||
val color = colors[colorIdx] | ||
super.start() | ||
|
||
widget.setStyling(cssProvider, "button--$color", "button--long") | ||
update() | ||
} | ||
|
||
override fun stop() { | ||
super.stop() | ||
|
||
ref!!.dispose() | ||
} | ||
|
||
override fun update() { | ||
// Nothing to do | ||
gtk_widget_queue_draw(widget) | ||
} | ||
|
||
companion object { | ||
private val colors = arrayOf("c9c", "99c", "f96", "000") | ||
|
||
private fun draw(context: CPointer<cairo_t>, ref: COpaquePointer) { | ||
val widget = ref.asStableRef<StatusFillerWidget>().get() | ||
|
||
val availableColors = colors.filterNot { it == widget.lastColor } | ||
val colorIdx = Random.nextInt(availableColors.size) | ||
val newColor = availableColors[colorIdx] | ||
val text = "${Random.nextInt(10000)}".padStart(4, '0') | ||
widget.lastColor = newColor | ||
|
||
cairo_set_source_rgb(context, convertColor(newColor[0]), convertColor(newColor[1]), convertColor(newColor[2])) | ||
createBorderPath(context, widget) | ||
cairo_fill(context) | ||
|
||
drawText(context, widget, text) | ||
} | ||
|
||
private fun convertColor(char: Char): Double { | ||
return when(char) { | ||
'f' -> 1.0 | ||
'c' -> 0.8 | ||
'9' -> 0.6 | ||
'6' -> 0.4 | ||
else -> 0.0 | ||
} | ||
} | ||
|
||
private fun createBorderPath(context: CPointer<cairo_t>, widget: StatusFillerWidget) { | ||
cairo_arc(context, 20.0, 20.0, 20.0, 1.0 * PI, 1.5 * PI) | ||
cairo_line_to(context, widget.widthPx - 20.0, 0.0) | ||
cairo_arc(context, widget.widthPx - 20.0, 20.0, 20.0, 1.5 * PI, 2.0 * PI) | ||
cairo_line_to(context, widget.widthPx.toDouble(), widget.heightPx - 20.0) | ||
cairo_arc(context, widget.widthPx - 20.0, widget.heightPx - 20.0, 20.0, 0.0 * PI, 0.5 * PI) | ||
cairo_line_to(context, 20.0, widget.heightPx.toDouble()) | ||
cairo_arc(context, 20.0, widget.heightPx - 20.0, 20.0, 0.5 * PI, 1.0 * PI) | ||
cairo_close_path(context) | ||
} | ||
|
||
private fun drawText(context: CPointer<cairo_t>, widget: StatusFillerWidget, text: String) { | ||
cairo_set_source_rgb(context, 0.0, 0.0, 0.0) | ||
val layout = pango_cairo_create_layout(context)!! | ||
|
||
val fontDescription = pango_font_description_from_string("Ubuntu Condensed, 12") | ||
pango_layout_set_font_description(layout, fontDescription) | ||
pango_layout_set_text(layout, text, text.length) | ||
|
||
val layoutSize = IntArray(2) | ||
pango_layout_get_size(layout, layoutSize.refTo(0), layoutSize.refTo(1)) | ||
cairo_move_to(context, | ||
widget.widthPx - (layoutSize[0].toFloat() / 1024.0) - 16, | ||
widget.heightPx - (layoutSize[1].toFloat() / 1024.0) | ||
) | ||
|
||
pango_cairo_show_layout(context, layout) | ||
pango_font_description_free(fontDescription) | ||
g_object_unref(layout) | ||
} | ||
} | ||
} |