Skip to content

Commit

Permalink
Begin widget column theming options
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed Oct 29, 2024
1 parent 6444b03 commit 247fde7
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 123 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/plugins/spmp/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SpMpDeps(extra: Map<String, Any>) {
license_url = "https://github.com/toasterofbread/spmp-server/blob/6dde651ffc102d604ac7ecd5ac7471b1572fd2e6/LICENSE"
),
"dev.toastbits.composekit" to DependencyInfo(
version = "644f929843",
version = "0.0.3-SNAPSHOT",
name = "ComposeKit",
author = "toasterofbread",
url = "https://github.com/toasterofbread/composekit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.LocalSize
import androidx.glance.action.clickable
import androidx.glance.appwidget.AppWidgetId
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.layout.Alignment
import androidx.glance.layout.Box
import androidx.glance.layout.Column
import androidx.glance.layout.ColumnScope
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.layout.wrapContentSize
Expand All @@ -64,14 +62,18 @@ import com.toasterofbread.spmp.widget.action.WidgetClickAction.CommonWidgetClick
import com.toasterofbread.spmp.widget.action.WidgetClickAction.CommonWidgetClickAction.SEEK_PREVIOUS
import com.toasterofbread.spmp.widget.action.WidgetClickAction.CommonWidgetClickAction.TOGGLE_VISIBILITY
import com.toasterofbread.spmp.widget.component.GlanceText
import com.toasterofbread.spmp.widget.component.styledcolumn.GlanceStyledColumn
import com.toasterofbread.spmp.widget.configuration.SpMpWidgetConfiguration
import com.toasterofbread.spmp.widget.configuration.base.BaseWidgetConfig
import com.toasterofbread.spmp.widget.configuration.base.BaseWidgetConfig.ContentColour.DARK
import com.toasterofbread.spmp.widget.configuration.base.BaseWidgetConfig.ContentColour.LIGHT
import com.toasterofbread.spmp.widget.configuration.base.BaseWidgetConfig.ContentColour.THEME
import com.toasterofbread.spmp.widget.configuration.base.BaseWidgetConfigDefaultsMask
import com.toasterofbread.spmp.widget.configuration.SpMpWidgetConfiguration
import com.toasterofbread.spmp.widget.configuration.enum.WidgetSectionThemeMode
import com.toasterofbread.spmp.widget.configuration.enum.colour
import com.toasterofbread.spmp.widget.configuration.type.TypeConfigurationDefaultsMask
import com.toasterofbread.spmp.widget.configuration.type.TypeWidgetConfig
import com.toasterofbread.spmp.widget.modifier.systemCornerRadius
import dev.toastbits.composekit.platform.composable.theme.LocalApplicationTheme
import dev.toastbits.composekit.settings.ui.NamedTheme
import dev.toastbits.composekit.utils.common.thenIf
Expand Down Expand Up @@ -215,18 +217,6 @@ abstract class SpMpWidget<A: TypeWidgetClickAction, T: TypeWidgetConfig<A>>(
}
}

@Composable
fun GlanceModifier.systemCornerRadius(): GlanceModifier {
if (android.os.Build.VERSION.SDK_INT >= 31) {
val systemCornerRadiusDefined: Boolean =
androidx.glance.LocalContext.current.resources.getResourceName(android.R.dimen.system_app_widget_background_radius) != null
if (systemCornerRadiusDefined) {
return cornerRadius(android.R.dimen.system_app_widget_background_radius)
}
}
return this
}

@Composable
fun GlanceBorderBox(
border_radius: Dp,
Expand Down Expand Up @@ -347,6 +337,32 @@ abstract class SpMpWidget<A: TypeWidgetClickAction, T: TypeWidgetConfig<A>>(
)
}

@Composable
fun StyledColumn(
section_theme_modes: List<WidgetSectionThemeMode>,
vararg content: @Composable ColumnScope.() -> Unit,
modifier: GlanceModifier = GlanceModifier,
vertical_alignment: Alignment.Vertical = Alignment.Top,
spacing: Dp = 0.dp,
content_padding: PaddingValues = PaddingValues()
) {
GlanceStyledColumn(
border_mode = base_configuration.styled_border_mode,
section_theme_modes = section_theme_modes,
content = content,
modifier = modifier,
vertical_alignment = vertical_alignment,
spacing = spacing,
content_padding = content_padding,
getBackgroundColour = {
when (it) {
WidgetSectionThemeMode.BACKGROUND -> widget_background_colour
else -> it.colour
}
}
)
}

companion object {
private val active_widgets: MutableMap<Int, SpMpWidget<*, *>> = mutableMapOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.toasterofbread.spmp.widget.component

import LocalPlayerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.glance.ColorFilter
import androidx.glance.GlanceModifier
import androidx.glance.Image
Expand All @@ -26,18 +27,19 @@ import dev.toastbits.composekit.platform.composable.theme.LocalApplicationTheme
import dev.toastbits.composekit.settings.ui.ThemeValues
import dev.toastbits.composekit.settings.ui.on_accent
import dev.toastbits.composekit.settings.ui.vibrant_accent
import dev.toastbits.composekit.utils.common.getContrasted

@Composable
internal fun <T: TypeWidgetClickAction> CommonActionButton(
action: WidgetClickAction<T>,
modifier: GlanceModifier = GlanceModifier,
icon_modifier: GlanceModifier = GlanceModifier,
background_colour: Color = LocalApplicationTheme.current.vibrant_accent,
getTypeActionIcon: (T) -> Int?
) {
val theme: ThemeValues = LocalApplicationTheme.current
Box(
modifier
.background(theme.vibrant_accent)
.background(background_colour)
.clickable(
WidgetActionCallback(action)
),
Expand All @@ -54,7 +56,7 @@ internal fun <T: TypeWidgetClickAction> CommonActionButton(
ImageProvider(icon),
null,
icon_modifier,
colorFilter = ColorFilter.tint(ColorProvider(theme.on_accent.copy(alpha = 0.85f)))
colorFilter = ColorFilter.tint(ColorProvider(background_colour.getContrasted().copy(alpha = 0.85f)))
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.toasterofbread.spmp.widget.component

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.glance.GlanceModifier
Expand All @@ -15,6 +16,8 @@ import com.toasterofbread.spmp.widget.action.TypeWidgetClickAction
import com.toasterofbread.spmp.widget.action.WidgetClickAction
import com.toasterofbread.spmp.widget.impl.DEFAULT_WIDGET_AREA_SPACING
import com.toasterofbread.spmp.widget.modifier.size
import dev.toastbits.composekit.platform.composable.theme.LocalApplicationTheme
import dev.toastbits.composekit.settings.ui.vibrant_accent

enum class GlanceActionButtonGridMode {
FILL,
Expand Down Expand Up @@ -46,7 +49,8 @@ fun <T: TypeWidgetClickAction> GlanceActionButtonGrid(
modifier: GlanceModifier = GlanceModifier,
button_modifier: GlanceModifier = GlanceModifier,
spacing: Dp = DEFAULT_WIDGET_AREA_SPACING,
alignment: Alignment = Alignment.Center
alignment: Alignment = Alignment.Center,
button_background_colour: Color = LocalApplicationTheme.current.vibrant_accent
) {
val button_size: DpSize = mode.getButtonSize(size, spacing)
val button_icon_size: Dp = minOf(button_size.width, button_size.height) * 0.35f
Expand All @@ -63,7 +67,8 @@ fun <T: TypeWidgetClickAction> GlanceActionButtonGrid(
top_start_button_action,
button_modifier.size(button_size),
getTypeActionIcon = getTypeActionIcon,
icon_modifier = GlanceModifier.size(button_icon_size)
icon_modifier = GlanceModifier.size(button_icon_size),
background_colour = button_background_colour
)

Spacer(GlanceModifier.width(spacing))
Expand All @@ -72,7 +77,8 @@ fun <T: TypeWidgetClickAction> GlanceActionButtonGrid(
top_end_button_action,
button_modifier.size(button_size),
getTypeActionIcon = getTypeActionIcon,
icon_modifier = GlanceModifier.size(button_icon_size)
icon_modifier = GlanceModifier.size(button_icon_size),
background_colour = button_background_colour
)
}

Expand All @@ -86,7 +92,8 @@ fun <T: TypeWidgetClickAction> GlanceActionButtonGrid(
bottom_start_button_action,
button_modifier.size(button_size),
getTypeActionIcon = getTypeActionIcon,
icon_modifier = GlanceModifier.size(button_icon_size)
icon_modifier = GlanceModifier.size(button_icon_size),
background_colour = button_background_colour
)

Spacer(GlanceModifier.width(spacing))
Expand All @@ -95,7 +102,8 @@ fun <T: TypeWidgetClickAction> GlanceActionButtonGrid(
bottom_end_button_action,
button_modifier.size(button_size),
getTypeActionIcon = getTypeActionIcon,
icon_modifier = GlanceModifier.size(button_icon_size)
icon_modifier = GlanceModifier.size(button_icon_size),
background_colour = button_background_colour
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.toasterofbread.spmp.widget.component

import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.DpSize
import androidx.glance.GlanceModifier
import androidx.glance.Image
import androidx.glance.ImageProvider

@Composable
fun GlanceCanvas(size: DpSize, modifier: GlanceModifier, draw: @Composable Canvas.(Size) -> Unit) {
val image: ImageBitmap =
with (LocalDensity.current) {
ImageBitmap(size.width.roundToPx(), size.height.roundToPx())
}

val canvas: Canvas = Canvas(image)
draw(canvas, Size(image.width.toFloat(), image.height.toFloat()))

Image(
ImageProvider(image.asAndroidBitmap()),
null,
modifier
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.text.TextPaint
import androidx.compose.material3.LocalContentColor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
Expand All @@ -23,7 +24,6 @@ import androidx.glance.LocalContext
import androidx.glance.layout.ContentScale
import androidx.glance.layout.wrapContentWidth
import com.toasterofbread.spmp.widget.mapper.toAndroidTypeface
import dev.toastbits.composekit.platform.composable.theme.LocalApplicationTheme
import org.jetbrains.compose.resources.Font
import org.jetbrains.compose.resources.FontResource

Expand All @@ -37,7 +37,7 @@ internal fun GlanceText(
max_width: Dp? = null
) {
val context: Context = LocalContext.current
val colour: Color = LocalApplicationTheme.current.on_background.copy(alpha)
val colour: Color = LocalContentColor.current.copy(alpha)
val typeface: Typeface? = font?.let { Font(it) }?.toAndroidTypeface()

val max_width_px: Int? = with (LocalDensity.current) {
Expand Down
Loading

0 comments on commit 247fde7

Please sign in to comment.