diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index af8e3f04..13769b3a 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -205,6 +205,8 @@ dependencies { implementation("androidx.profileinstaller:profileinstaller:1.4.1") implementation("com.squareup.okhttp3:okhttp:4.12.0") implementation("ru.noties:jlatexmath-android:0.2.0") + implementation("dev.chrisbanes.haze:haze:2.1.0") + implementation("dev.chrisbanes.haze:haze-glass:2.1.0") // Android provides org.json; only host JVM tests need the separate library. testImplementation("org.json:json:20260814") diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LiquidGlass.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LiquidGlass.kt new file mode 100644 index 00000000..10415dc9 --- /dev/null +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LiquidGlass.kt @@ -0,0 +1,72 @@ +package com.logseq.logseq.ui.extensions + +import android.os.Build +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.compositionLocalOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.shadow +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import dev.chrisbanes.haze.HazeInput +import dev.chrisbanes.haze.HazeState +import dev.chrisbanes.haze.glass.GlassStyle +import dev.chrisbanes.haze.glass.hazeGlass +import dev.chrisbanes.haze.rememberHazeState + +/** Shared capture state for the small floating chrome surfaces. */ +val LocalLogseqHazeState = compositionLocalOf { null } + +/** + * Android Liquid Glass implemented with Haze's source-backed Glass renderer. + * Only small floating chrome surfaces use the Glass effect; scrolling content + * is captured as the backdrop source and is otherwise ordinary Compose drawing. + */ +@Composable +fun Modifier.logseqLiquidGlass( + shape: RoundedCornerShape, + elevation: Dp = 8.dp, +): Modifier { + val state = LocalLogseqHazeState.current ?: rememberHazeState() + // Haze's non-shader Glass fallback (Android 12L and below) does not + // synthesize Regular's base fill, so give only that path a theme tint. + val fallbackTint = MaterialTheme.colorScheme.surface.copy(alpha = 0.82f) + val style = remember(shape, fallbackTint) { + GlassStyle.regular.then { + shape(shape) + // Centered light reads as a white dot inside small circular chrome. + lightPosition(Alignment.TopCenter) + if (Build.VERSION.SDK_INT < 33) { + tint(fallbackTint) + } + } + } + return this + .shadow( + elevation = elevation, + shape = shape, + clip = false, + ambientColor = Color.Black.copy(alpha = 0.16f), + spotColor = Color.Black.copy(alpha = 0.20f), + ) + .hazeGlass( + input = HazeInput.Backdrop(state), + style = style, + ) +} + +@Composable +fun LogseqLiquidGlassSurface( + shape: RoundedCornerShape, + modifier: Modifier = Modifier, + elevation: Dp = 8.dp, + content: @Composable BoxScope.() -> Unit, +) { + Box(modifier.logseqLiquidGlass(shape, elevation), content = content) +} diff --git a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt index f3593f65..80ffeaa3 100644 --- a/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt +++ b/android/app/src/main/kotlin/com/logseq/logseq/ui/extensions/LogseqExtensions.kt @@ -1,15 +1,18 @@ package com.logseq.logseq.ui.extensions import androidx.activity.compose.BackHandler -import androidx.compose.foundation.BorderStroke +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.spring import androidx.compose.foundation.background import androidx.compose.foundation.clickable +import androidx.compose.foundation.gestures.detectHorizontalDragGestures import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding @@ -37,6 +40,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.Alignment @@ -45,14 +49,17 @@ import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.draw.clip +import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.testTag import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.platform.LocalFocusManager +import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.logseq.logseq.runtime.logseqIconResolver @@ -63,6 +70,9 @@ import dev.lui.LuiExtensionProperty import dev.lui.LuiExtensionRegistry import dev.lui.LuiExtensionValueKind import dev.lui.luiThemeColor +import dev.chrisbanes.haze.hazeSource +import dev.chrisbanes.haze.rememberHazeState +import kotlin.math.roundToInt // Port of the former logseq_extensions.dart — the Logseq-specific // extension hosts. Fingerprints are generated by the OCaml core and must @@ -145,6 +155,17 @@ fun logseqExtensionRegistry( val group = luiThemeColor("settings-group") ?: colors.surface MaterialTheme(colorScheme = colors.copy(surface = group)) { Children() } } + registerTweak( + "liquid-glass", + "lui-tweak-v1|12:liquid-glass|profiles:android/kotlin,ios/swiftui|properties:5:shape:string:required:none", + properties = listOf(LuiExtensionProperty("shape", LuiExtensionValueKind.STRING, required = true)), + ) { + val shape = when (string("shape")) { + "container", "rounded-rectangle" -> RoundedCornerShape(20.dp) + else -> CircleShape + } + Box(Modifier.logseqLiquidGlass(shape)) { Children() } + } register( "outliner-row-swipe", "lui-extension-v1|18:outliner-row-swipe|profiles:android/kotlin|standard-children:1|children:|properties:12:marker-inset:int:required:none,7:enabled:bool:required:none,8:block-id:string:required:none|events:16:toggle-selection[4:uuid:string:required]", @@ -285,41 +306,106 @@ fun logseqExtensionRegistry( ) { val encoded = string("items") ?: "[]" val items = remember(encoded) { JSONArray(encoded) } + val count = items.length() val selected = int("selected-index") val cellWidth = (int("cell-width") ?: 50).dp val inset = (int("inset") ?: 4).dp val accent = luiThemeColor("link", foreground = true) ?: MaterialTheme.colorScheme.primary val foreground = luiThemeColor("foreground", foreground = true) ?: MaterialTheme.colorScheme.onSurface - val glass = luiThemeColor("glass") ?: MaterialTheme.colorScheme.surfaceContainer val selection = luiThemeColor("bottom-tab-selected") ?: MaterialTheme.colorScheme.secondaryContainer - Surface( - shape = CircleShape, - color = glass, - border = BorderStroke(1.dp, luiThemeColor("border") ?: MaterialTheme.colorScheme.outlineVariant), - ) { - Row(Modifier.padding(inset).selectableGroup(), verticalAlignment = Alignment.CenterVertically) { - for (index in 0 until items.length()) { - val item = items.getJSONObject(index) - val title = item.getString("title") - val name = item.getString("name") - val active = index == selected - val symbol = logseqIconResolver.icon(item.getString(if (active) "selectedImage" else "image")) - Box( - modifier = Modifier.size(width = cellWidth, height = 50.dp) - .clip(CircleShape) - .background(if (active) selection else Color.Transparent) - .selectable(selected = active, role = Role.Tab, onClick = { emit("select", "index" to index) }) - .testTag("tab.bottom.$name") - .semantics { contentDescription = title }, - contentAlignment = Alignment.Center, - ) { - if (symbol != null) { - Box(Modifier.size(26.dp)) { - Icon(symbol, contentDescription = null, modifier = Modifier.fillMaxSize(), - tint = if (active) accent else foreground) - if (item.optBoolean("unread")) { - Box(Modifier.align(Alignment.TopCenter).padding(top = 9.dp).size(4.dp) - .background(accent, CircleShape).testTag("notifications.unread-dot")) + var dragPosition by remember { mutableStateOf(null) } + var pendingSelection by remember { mutableStateOf(null) } + val currentSelected by rememberUpdatedState(selected) + LaunchedEffect(selected) { pendingSelection = null } + val settledPosition = (pendingSelection ?: selected ?: 0).coerceIn(0, maxOf(0, count - 1)).toFloat() + val animatedPosition by animateFloatAsState( + targetValue = settledPosition, + animationSpec = spring(), + label = "bottom-tab-indicator", + ) + val indicatorPosition = dragPosition ?: animatedPosition + val density = LocalDensity.current + val layoutDirection = LocalLayoutDirection.current + val cellWidthPx = with(density) { cellWidth.toPx() } + val insetPx = with(density) { inset.toPx() } + val stripWidthPx = count * cellWidthPx + 2 * insetPx + + fun rawIndexAt(x: Float): Float { + val horizontal = if (layoutDirection == LayoutDirection.Rtl) stripWidthPx - x else x + return (horizontal - insetPx) / cellWidthPx - 0.5f + } + + fun elasticIndex(raw: Float): Float { + val clamped = raw.coerceIn(0f, (count - 1).toFloat()) + val overshoot = ((raw - clamped) * 0.2f).coerceIn(-0.08f, 0.08f) + return clamped + overshoot + } + + LogseqLiquidGlassSurface(shape = CircleShape) { + Box( + Modifier.size(width = cellWidth * count + inset * 2, height = 50.dp + inset * 2) + .selectableGroup() + .pointerInput(count, cellWidthPx, insetPx, layoutDirection) { + if (count == 0 || cellWidthPx <= 0f) return@pointerInput + detectHorizontalDragGestures( + onDragStart = { offset -> dragPosition = elasticIndex(rawIndexAt(offset.x)) }, + onDragEnd = { + dragPosition?.let { position -> + val target = position.roundToInt().coerceIn(0, count - 1) + if (target != currentSelected) { + pendingSelection = target + emit("select", "index" to target) + } + } + dragPosition = null + }, + onDragCancel = { dragPosition = null }, + ) { change, _ -> + dragPosition = elasticIndex(rawIndexAt(change.position.x)) + change.consume() + } + }, + ) { + val hasIndicator = count > 0 && + (dragPosition != null || pendingSelection != null || (selected != null && selected in 0 until count)) + if (hasIndicator) { + val indicatorX = if (layoutDirection == LayoutDirection.Rtl) { + inset + cellWidth * (count - 1 - indicatorPosition) + } else { + inset + cellWidth * indicatorPosition + } + var indicatorModifier = Modifier.offset(x = indicatorX, y = inset) + .size(width = cellWidth, height = 50.dp) + .clip(CircleShape) + .background(selection) + if (dragPosition != null) { + indicatorModifier = indicatorModifier.logseqLiquidGlass(CircleShape, elevation = 0.dp) + } + Box(indicatorModifier) + } + Row(Modifier.padding(inset), verticalAlignment = Alignment.CenterVertically) { + for (index in 0 until count) { + val item = items.getJSONObject(index) + val title = item.getString("title") + val name = item.getString("name") + val active = index == (dragPosition?.roundToInt() ?: pendingSelection ?: selected) + val symbol = logseqIconResolver.icon(item.getString(if (active) "selectedImage" else "image")) + Box( + modifier = Modifier.size(width = cellWidth, height = 50.dp) + .clip(CircleShape) + .selectable(selected = active, role = Role.Tab, onClick = { emit("select", "index" to index) }) + .testTag("tab.bottom.$name") + .semantics { contentDescription = title }, + contentAlignment = Alignment.Center, + ) { + if (symbol != null) { + Box(Modifier.size(26.dp)) { + Icon(symbol, contentDescription = null, modifier = Modifier.fillMaxSize(), + tint = if (active) accent else foreground) + if (item.optBoolean("unread")) { + Box(Modifier.align(Alignment.TopCenter).padding(top = 9.dp).size(4.dp) + .background(accent, CircleShape).testTag("notifications.unread-dot")) + } } } } @@ -436,12 +522,14 @@ private fun NavigationBoundary(context: LuiExtensionContext) { } } var journalTitle by remember { mutableStateOf(null) } + val hazeState = rememberHazeState() BoxWithConstraints(Modifier.fillMaxSize()) { val width = maxWidth.value.toInt() LaunchedEffect(width) { context.emit("viewport-changed", "width" to width) } CompositionLocalProvider( dev.lui.LocalLuiSectionTitle provides journalTitle, dev.lui.LocalLuiSectionTitleChanged provides { journalTitle = it }, + LocalLogseqHazeState provides hazeState, ) { val childIds = context.children if (childIds.size == 2) { @@ -458,7 +546,9 @@ private fun NavigationBoundary(context: LuiExtensionContext) { val chromeHeight = remember { mutableIntStateOf(0) } Box(Modifier.fillMaxSize()) { CompositionLocalProvider(LocalBottomChromeHeight provides chromeHeight) { - Column(Modifier.fillMaxSize()) { NavigationContent(context, childIds[0], dismissesComposer) } + Column(Modifier.fillMaxSize().hazeSource(hazeState, zIndex = 1f)) { + NavigationContent(context, childIds[0], dismissesComposer) + } } val chromeModifier = Modifier .align(Alignment.BottomCenter) @@ -486,6 +576,13 @@ private fun NavigationContent(context: LuiExtensionContext, contentId: Long, dis val focus = LocalFocusManager.current val keyboard = LocalSoftwareKeyboardController.current Box(Modifier.fillMaxSize()) { + LocalLogseqHazeState.current?.let { state -> + Box( + Modifier.fillMaxSize() + .hazeSource(state, zIndex = 0f) + .background(MaterialTheme.colorScheme.background), + ) + } context.child(contentId) if (dismissesComposer) { Box(Modifier.fillMaxSize() diff --git a/apple/Sources/Logseq/LGLiquidGlassTweak.swift b/apple/Sources/Logseq/LGLiquidGlassTweak.swift index 7e9e12d1..4cd4cd50 100644 --- a/apple/Sources/Logseq/LGLiquidGlassTweak.swift +++ b/apple/Sources/Logseq/LGLiquidGlassTweak.swift @@ -5,7 +5,7 @@ import SwiftUI enum LGLiquidGlassTweak { static let identifier = "liquid-glass" static let fingerprint = - "lui-tweak-v1|12:liquid-glass|profiles:ios/swiftui|properties:5:shape:string:required:none" + "lui-tweak-v1|12:liquid-glass|profiles:android/kotlin,ios/swiftui|properties:5:shape:string:required:none" static func register(in registry: LUIAppleExtensionRegistry) throws { try registry.registerTweak( diff --git a/shared/src/logseq/view.ml b/shared/src/logseq/view.ml index 7b171e4d..bd97faba 100644 --- a/shared/src/logseq/view.ml +++ b/shared/src/logseq/view.ml @@ -328,7 +328,8 @@ let native_search_presentation_schema () = let liquid_glass_schema () = let open Lui_protocol in let open Lui_extension in - Lui_extension.tweak "liquid-glass" [ profile IOS SwiftUIHost ] + Lui_extension.tweak "liquid-glass" + [ profile IOS SwiftUIHost; profile AndroidOS KotlinHost ] [ Lui_extension.property "shape" StringScalar true None ] let native_control_tint_schema () = diff --git a/shared/src/logseq/view_base.ml b/shared/src/logseq/view_base.ml index c89b404b..4d4aa4a4 100644 --- a/shared/src/logseq/view_base.ml +++ b/shared/src/logseq/view_base.ml @@ -1682,6 +1682,23 @@ let with_liquid_glass shape (elem : Lui_elements.t) : Lui_elements.t = tweak | _ -> Lui_elements.mount context ?parent elem) +let with_native_liquid_glass shape (elem : Lui_elements.t) : Lui_elements.t = + Lui_elements.custom (fun context parent -> + match Lui_ui.profile context with + | { Lui_protocol.profile_os = Lui_protocol.IOS; + profile_host = Lui_protocol.SwiftUIHost; + } + | { Lui_protocol.profile_os = Lui_protocol.AndroidOS; + profile_host = Lui_protocol.KotlinHost; + } -> + let tweak = Lui_ui.platform_tweak context "liquid-glass" in + Lui_ui.extension_property context tweak "shape" + (StringValue shape); + ignore (Lui_elements.mount context ~parent:tweak elem); + Lui_elements.attach context parent tweak; + tweak + | _ -> Lui_elements.mount context ?parent elem) + let rich_block_content ?(show_code_language = true) ?interaction ?row_actions row_source ~on_event : Lui_elements.t = Lui_elements.custom (fun context parent -> let node = Lui_ui.extension context "outliner-block-content" in diff --git a/shared/src/logseq/view_base.mli b/shared/src/logseq/view_base.mli index cdf2148e..4ff06b1f 100644 --- a/shared/src/logseq/view_base.mli +++ b/shared/src/logseq/view_base.mli @@ -404,6 +404,7 @@ val with_float_prop_signal : float Signal.signal -> Lui_elements.t -> Lui_elements.t val with_liquid_glass : string -> Lui_elements.t -> Lui_elements.t +val with_native_liquid_glass : string -> Lui_elements.t -> Lui_elements.t val rich_block_content : ?show_code_language:bool -> ?interaction:string -> ?row_actions:Lui_protocol.wire_value Signal.signal -> Model.outline_row Signal.signal -> diff --git a/shared/src/logseq/view_screens.ml b/shared/src/logseq/view_screens.ml index 88b9d350..ca31cb09 100644 --- a/shared/src/logseq/view_screens.ml +++ b/shared/src/logseq/view_screens.ml @@ -272,9 +272,7 @@ let bottom_tab_row ~android ~tab_width ~action_width model_source send : t = action "capture-circle" "Capture" "button.composer.expand" Model.ExpandComposer ?on_long_press:(if android then None else Some (press send (Model.ChooseAttachment "quick-photos"))); ] in - let actions = - if android then box ~background:"glass" ~border_width:1 ~border_color:"border" ~corner_radius:29 [actions] - else View_base.with_liquid_glass "bottom-actions" actions in + let actions = View_base.with_native_liquid_glass "bottom-actions" actions in row ~grow:1.0 ~gap:8 ~main:`space_between ~cross:`center ~accessibility_identifier:"row.bottom.capture" [tabs; actions] @@ -721,8 +719,9 @@ let journal_overflow_menu ~android model_source send = ] let android_header_action action = - column ~width:44 ~height:44 ~corner_radius:22 - ~background:"glass" ~main:`center ~cross:`center [action] + View_base.with_native_liquid_glass "circle" + (column ~width:44 ~height:44 ~corner_radius:22 + ~main:`center ~cross:`center [action]) let tasks_options_visible current = View_base.tasks_destination_ current && View_base.node_navigation_inactive_ current diff --git a/shared/test/logseq/app_test.ml b/shared/test/logseq/app_test.ml index 6d6fa28b..023118f6 100644 --- a/shared/test/logseq/app_test.ml +++ b/shared/test/logseq/app_test.ml @@ -2006,7 +2006,7 @@ let native_search_presentation_extension_contract_is_pinned () = (Some "lui-extension-v1|26:native-search-presentation|profiles:android/kotlin,ios/swiftui|standard-children:1|children:|properties:10:back-title:string:optional:none,11:clear-title:string:optional:none,11:close-title:string:optional:none,12:status-title:string:optional:none,28:bottom-occupies-layout-space:bool:required:none,5:depth:int:required:none,5:query:string:required:none,5:title:string:required:none,9:presented:bool:required:none|events:13:query-changed[5:query:string:required],4:back[5:count:int:required],7:dismiss[],7:present[]") -let liquid_glass_remains_an_ios_local_tweak () = +let liquid_glass_is_a_native_local_tweak () = let registry = View.extension_registry () in let schema = Lui_extension.schema registry "liquid-glass" in check ~msg:"app-specific appearance does not expand the standard LUI protocol" @@ -2016,7 +2016,7 @@ let liquid_glass_remains_an_ios_local_tweak () = | Some current -> Some (Lui_extension.tweak_fingerprint current) | None -> None) (Some - "lui-tweak-v1|12:liquid-glass|profiles:ios/swiftui|properties:5:shape:string:required:none") + "lui-tweak-v1|12:liquid-glass|profiles:android/kotlin,ios/swiftui|properties:5:shape:string:required:none") let extension_kind runtime node = Hashtbl.find_opt runtime.Lui_runtime.runtime_extension_nodes node @@ -11868,7 +11868,7 @@ let cases = case "outliner-block-content-extension-contract-is-pinned" outliner_block_content_extension_contract_is_pinned; case "native-navigation-stack-extension-contract-is-pinned" native_navigation_stack_extension_contract_is_pinned; case "native-search-presentation-extension-contract-is-pinned" native_search_presentation_extension_contract_is_pinned; - case "liquid-glass-remains-an-ios-local-tweak" liquid_glass_remains_an_ios_local_tweak; + case "liquid-glass-is-a-native-local-tweak" liquid_glass_is_a_native_local_tweak; case "outliner-drag-selects-once-and-drop-keeps-placement" outliner_drag_selects_once_and_drop_keeps_placement; case "initial-shell-renders-the-graph-picker-without-a-selected-graph" initial_shell_renders_the_graph_picker_without_a_selected_graph; case "graph-picker-not-ready-status-matches-main-copy" graph_picker_not_ready_status_matches_main_copy;