Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add shared icon button styles

Neeraj Gupta78f3dfa3a11d
diff --git a/android/packages/components/src/main/kotlin/io/ente/components/IconAction.kt b/android/packages/components/src/main/kotlin/io/ente/components/IconAction.kt
new file mode 100644
index 0000000000..3885ae8a45
--- /dev/null
+++ b/android/packages/components/src/main/kotlin/io/ente/components/IconAction.kt
@@ -0,0 +1,120 @@
+package io.ente.components
+
+import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.tween
+import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.interaction.MutableInteractionSource
+import androidx.compose.foundation.interaction.collectIsPressedAsState
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.sizeIn
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.LocalContentColor
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.semantics.Role
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+
+@Composable
+public fun IconAction(
+ onClick: () -> Unit,
+ modifier: Modifier = Modifier,
+ kind: IconActionKind = IconActionKind.Unfilled,
+ enabled: Boolean = true,
+ size: Dp = 40.dp,
+ content: @Composable () -> Unit,
+) {
+ val palette = LocalEntePalette.current
+ val interactionSource = remember { MutableInteractionSource() }
+ val pressed by interactionSource.collectIsPressedAsState()
+ val colors = iconColors(kind, palette, enabled, pressed)
+ val scale by
+ animateFloatAsState(
+ targetValue = if (pressed && enabled) 0.98f else 1f,
+ animationSpec = tween(EnteMotion.quick),
+ label = "iconActionScale",
+ )
+
+ Box(
+ modifier =
+ modifier
+ .sizeIn(minWidth = 48.dp, minHeight = 48.dp)
+ .clickable(
+ enabled = enabled,
+ role = Role.Button,
+ interactionSource = interactionSource,
+ indication = null,
+ onClick = onClick,
+ ),
+ contentAlignment = Alignment.Center,
+ ) {
+ CompositionLocalProvider(LocalContentColor provides colors.foreground) {
+ Box(
+ modifier =
+ Modifier.size(size)
+ .graphicsLayer {
+ scaleX = scale
+ scaleY = scale
+ }
+ .background(
+ colors.background,
+ RoundedCornerShape(
+ if (kind == IconActionKind.Circular) size / 2 else EnteRadius.medium
+ ),
+ ),
+ contentAlignment = Alignment.Center,
+ ) {
+ content()
+ }
+ }
+ }
+}
+
+public enum class IconActionKind {
+ Primary,
+ Unfilled,
+ Accent,
+ Circular,
+ OnMedia,
+}
+
+private data class IconColors(val background: Color, val foreground: Color)
+
+private fun iconColors(
+ kind: IconActionKind,
+ palette: Palette,
+ enabled: Boolean,
+ pressed: Boolean,
+): IconColors {
+ if (!enabled && kind != IconActionKind.OnMedia) {
+ return IconColors(
+ if (kind == IconActionKind.Unfilled) Color.Transparent else palette.fill,
+ palette.hintText,
+ )
+ }
+ return when (kind) {
+ IconActionKind.Primary,
+ IconActionKind.Circular ->
+ IconColors(if (pressed) palette.fillDarker else palette.surface, palette.text)
+ IconActionKind.Unfilled ->
+ IconColors(
+ Color.Transparent,
+ if (palette.isDark) palette.text else palette.text.copy(alpha = 0.75f),
+ )
+ IconActionKind.Accent ->
+ IconColors(if (pressed) palette.primaryDarker else palette.primary, Color.White)
+ IconActionKind.OnMedia ->
+ IconColors(
+ if (enabled && pressed) Color.White.copy(alpha = 0.12f) else Color.Transparent,
+ Color.White.copy(alpha = if (enabled) 1f else 0.4f),
+ )
+ }
+}
diff --git a/android/packages/components/src/main/kotlin/io/ente/components/Theme.kt b/android/packages/components/src/main/kotlin/io/ente/components/Theme.kt
index b797c89576..66f515503b 100644
--- a/android/packages/components/src/main/kotlin/io/ente/components/Theme.kt
+++ b/android/packages/components/src/main/kotlin/io/ente/components/Theme.kt
@@ -24,12 +24,16 @@ public enum class EnteApp {
@Immutable
public class Palette
internal constructor(
+ public val isDark: Boolean,
public val primary: Color,
public val primaryDarker: Color,
public val text: Color,
+ public val hintText: Color,
public val disabledText: Color,
public val reverseText: Color,
+ public val surface: Color,
public val fill: Color,
+ public val fillDarker: Color,
public val fillDarkest: Color,
public val danger: Color,
public val dangerDarker: Color,
@@ -44,6 +48,7 @@ public object EnteSpacing {
}
public object EnteRadius {
+ public val medium: Dp = 12.dp
public val button: Dp = 20.dp
}
@@ -87,12 +92,16 @@ private fun palette(app: EnteApp, dark: Boolean): Palette {
EnteApp.Locker -> Color(0xFF1071FF) to Color(0xFF0B4CAD)
}
return Palette(
+ isDark = dark,
primary = primary,
primaryDarker = primaryDarker,
text = if (dark) Color.White else Color.Black,
+ hintText = Color(0xFF969696),
disabledText = Color(if (dark) 0xFF414141 else 0xFFD6D6D6),
reverseText = if (dark) Color.Black else Color.White,
+ surface = Color(if (dark) 0xFF212121 else 0xFFFFFFFF),
fill = Color(if (dark) 0xFF0A0A0A else 0xFFEAEAEA),
+ fillDarker = Color(if (dark) 0xFF141414 else 0xFFDEDEDE),
fillDarkest = Color(if (dark) 0xFF292929 else 0xFFD2D2D2),
danger = Color(0xFFF63A3A),
dangerDarker = Color(0xFFC52E2E),
diff --git a/apple/packages/EnteComponents/Sources/EnteComponents/IconAction.swift b/apple/packages/EnteComponents/Sources/EnteComponents/IconAction.swift
new file mode 100644
index 0000000000..b4f512b704
--- /dev/null
+++ b/apple/packages/EnteComponents/Sources/EnteComponents/IconAction.swift
@@ -0,0 +1,83 @@
+import SwiftUI
+
+public struct IconAction<Content: View>: View {
+ @Environment(\.entePalette) private var palette
+ private let kind: IconActionKind
+ private let enabled: Bool
+ private let size: CGFloat
+ private let action: () -> Void
+ private let content: Content
+
+ public init(
+ kind: IconActionKind = .unfilled,
+ enabled: Bool = true,
+ size: CGFloat = 40,
+ action: @escaping () -> Void,
+ @ViewBuilder content: () -> Content
+ ) {
+ self.kind = kind
+ self.enabled = enabled
+ self.size = size
+ self.action = action
+ self.content = content()
+ }
+
+ public var body: some View {
+ Button(action: action) {
+ content
+ .frame(width: size, height: size)
+ .contentShape(Rectangle())
+ }
+ .buttonStyle(
+ IconActionStyle(
+ kind: kind,
+ palette: palette,
+ cornerRadius: kind == .circular ? size / 2 : EnteRadius.medium
+ )
+ )
+ .disabled(!enabled)
+ }
+}
+
+public enum IconActionKind: Sendable {
+ case primary
+ case unfilled
+ case accent
+ case circular
+ case onMedia
+}
+
+private struct IconActionStyle: ButtonStyle {
+ @Environment(\.isEnabled) private var enabled
+ let kind: IconActionKind
+ let palette: Palette
+ let cornerRadius: CGFloat
+
+ func makeBody(configuration: Configuration) -> some View {
+ let colors = colors(pressed: configuration.isPressed)
+ return configuration.label
+ .foregroundStyle(colors.foreground)
+ .background(colors.background)
+ .clipShape(RoundedRectangle(cornerRadius: cornerRadius, style: .continuous))
+ .scaleEffect(configuration.isPressed ? 0.98 : 1)
+ .frame(minWidth: 44, minHeight: 44)
+ .contentShape(Rectangle())
+ .animation(.easeOut(duration: EnteMotion.quick), value: configuration.isPressed)
+ }
+
+ private func colors(pressed: Bool) -> (background: Color, foreground: Color) {
+ if !enabled, kind != .onMedia {
+ return (kind == .unfilled ? .clear : palette.fill, palette.hintText)
+ }
+ return switch kind {
+ case .primary, .circular: (pressed ? palette.fillDarker : palette.surface, palette.text)
+ case .unfilled: (.clear, palette.text.opacity(palette.isDark ? 1 : 0.75))
+ case .accent: (pressed ? palette.primaryDarker : palette.primary, .white)
+ case .onMedia:
+ (
+ enabled && pressed ? .white.opacity(0.12) : .clear,
+ .white.opacity(enabled ? 1 : 0.4)
+ )
+ }
+ }
+}
diff --git a/apple/packages/EnteComponents/Sources/EnteComponents/Theme.swift b/apple/packages/EnteComponents/Sources/EnteComponents/Theme.swift
index 1ff3ebf77a..45f87a64bb 100644
--- a/apple/packages/EnteComponents/Sources/EnteComponents/Theme.swift
+++ b/apple/packages/EnteComponents/Sources/EnteComponents/Theme.swift
@@ -8,12 +8,16 @@ public enum EnteApp: Sendable {
}
public struct Palette: Sendable {
+ public let isDark: Bool
public let primary: Color
public let primaryDarker: Color
public let text: Color
+ public let hintText: Color
public let disabledText: Color
public let reverseText: Color
+ public let surface: Color
public let fill: Color
+ public let fillDarker: Color
public let fillDarkest: Color
public let danger: Color
public let dangerDarker: Color
@@ -33,12 +37,16 @@ public struct Palette: Sendable {
primaryDarker = Color(hex: 0x0B4CAD)
}
return Palette(
+ isDark: dark,
primary: primary,
primaryDarker: primaryDarker,
text: dark ? .white : .black,
+ hintText: Color(hex: 0x969696),
disabledText: Color(hex: dark ? 0x414141 : 0xD6D6D6),
reverseText: dark ? .black : .white,
+ surface: Color(hex: dark ? 0x212121 : 0xFFFFFF),
fill: Color(hex: dark ? 0x0A0A0A : 0xEAEAEA),
+ fillDarker: Color(hex: dark ? 0x141414 : 0xDEDEDE),
fillDarkest: Color(hex: dark ? 0x292929 : 0xD2D2D2),
danger: Color(hex: 0xF63A3A),
dangerDarker: Color(hex: 0xC52E2E)
@@ -51,6 +59,7 @@ public enum EnteSpacing {
}
public enum EnteRadius {
+ public static let medium: CGFloat = 12
public static let button: CGFloat = 20
}