Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ internal class HybridSearchEngine(
near: Int,
): String = lexical.buildSnippet(rawText, query, near)

override fun findInBookCandidates(
query: String,
bookId: Long,
): LongArray? = lexical.findInBookCandidates(query, bookId)

override suspend fun semanticSpan(
query: String,
text: String,
Expand Down
4 changes: 4 additions & 0 deletions SeforimApp/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@
<string name="add_bookmark">הוסף סימנייה</string>
<string name="search_in_page_tooltip">חפש בתוך העמוד</string>
<string name="search_in_page">הקלד כדי לחפש בעמוד...</string>
<string name="find_previous_match">התוצאה הקודמת</string>
<string name="find_next_match">התוצאה הבאה</string>
<string name="find_close">סגירת החיפוש</string>
<string name="find_smart_mode">חיפוש חכם לפי משמעות</string>
<string name="print_tooltip">הדפס עמוד זה</string>
<string name="print">הדפס</string>
<string name="report_tooltip">דווח על בעיה בעמוד זה</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.github.kdroidfilter.seforimapp.core.history.VisitEntry
import io.github.kdroidfilter.seforimapp.core.history.VisitKind
import io.github.kdroidfilter.seforimapp.core.presentation.theme.AccentColor
import io.github.kdroidfilter.seforimapp.core.presentation.theme.IntUiThemes
import io.github.kdroidfilter.seforimapp.core.presentation.utils.hasFindInPage
import io.github.kdroidfilter.seforimapp.features.home.widgets.availableTools
import io.github.kdroidfilter.seforimapp.features.settings.SettingsWindowEvents
import io.github.kdroidfilter.seforimapp.features.settings.SettingsWindowViewModel
Expand All @@ -44,6 +45,8 @@ fun AppNativeMenuBar(
onQuit: () -> Unit,
) {
val appSettings = LocalAppGraph.current.appSettings
val selectionContext = LocalAppGraph.current.selectionContext
val tabStateStore = LocalAppGraph.current.tabPersistedStateStore
val theme by mainAppState.theme.collectAsState()
val accentColor by mainAppState.accentColor.collectAsState()
val compactMode by appSettings.compactModeFlow.collectAsState()
Expand Down Expand Up @@ -241,8 +244,10 @@ fun AppNativeMenuBar(
) {
val tabs = tabsViewModel.tabs.value
val selectedIndex = tabsViewModel.selectedTabIndex.value
val tabId = tabs.getOrNull(selectedIndex)?.destination?.tabId ?: return@Item
appSettings.toggleFindBar(tabId)
val destination = tabs.getOrNull(selectedIndex)?.destination ?: return@Item
if (!destination.hasFindInPage(tabStateStore)) return@Item
val tabId = destination.tabId
appSettings.showFindBar(tabId, selectionContext.selectedTextIn(tabId))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,117 +1,248 @@
package io.github.kdroidfilter.seforimapp.core.presentation.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.*
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.github.kdroidfilter.seforimapp.icons.MaterialSymbolsMagicButton
import kotlinx.coroutines.delay
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.ui.Orientation
import org.jetbrains.jewel.ui.component.Divider
import org.jetbrains.jewel.ui.component.Icon
import org.jetbrains.jewel.ui.component.IconButton
import org.jetbrains.jewel.ui.component.IconActionButton
import org.jetbrains.jewel.ui.component.Text
import org.jetbrains.jewel.ui.component.TextField
import org.jetbrains.jewel.ui.component.ToggleableIconButton
import org.jetbrains.jewel.ui.component.Tooltip
import org.jetbrains.jewel.ui.icons.AllIconsKeys
import seforimapp.seforimapp.generated.resources.Res
import seforimapp.seforimapp.generated.resources.chevron_icon_description
import seforimapp.seforimapp.generated.resources.find_close
import seforimapp.seforimapp.generated.resources.find_next_match
import seforimapp.seforimapp.generated.resources.find_previous_match
import seforimapp.seforimapp.generated.resources.find_smart_mode
import seforimapp.seforimapp.generated.resources.search_in_page
import org.jetbrains.jewel.ui.component.TextField as JewelTextField

/**
* Find-in-page bar, laid out as Chromium's and IntelliJ's: one floating container holding the
* borderless field and its match counter, then the actions after a divider.
*/
@Composable
fun FindInPageBar(
state: TextFieldState,
modifier: Modifier = Modifier,
onEnterNext: () -> Unit = {},
onEnterPrev: () -> Unit = {},
onClose: () -> Unit = {},
// Ctrl+Enter, as in Chromium: ends the session acting on the current match
onActivate: () -> Unit = onClose,
// When true, requests focus on the text field when composed
autoFocus: Boolean = true,
// Smart mode uses dictionary expansion for highlighting
// Bumped to focus the field again with its text selected (Ctrl/Cmd+F while shown)
focusRequest: Int = 0,
// Matches of the query, and the rank of the current one from 1; a null count hides the counter
matchCount: Int? = null,
matchPosition: Int? = null,
// Meaning-based find; null hides its toggle where it isn't available
smartModeEnabled: Boolean = false,
onToggleSmartMode: () -> Unit = {},
onToggleSmartMode: (() -> Unit)? = null,
) {
val panelColor = JewelTheme.globalColors.panelBackground
val borderColor = JewelTheme.globalColors.borders.normal
val shape = RoundedCornerShape(8.dp)
// Focus management: request focus when shown so typing works immediately
val focusRequester = androidx.compose.runtime.remember { FocusRequester() }
LaunchedEffect(autoFocus) {
val focusRequester = remember { FocusRequester() }
LaunchedEffect(autoFocus, focusRequest) {
if (autoFocus) {
// Small delay ensures the node is placed before requesting focus
delay(10)
focusRequester.requestFocus()
// As in Chromium, typing replaces the previous query
state.edit { selection = TextRange(0, length) }
}
}

Row(
modifier =
modifier
.background(panelColor, shape)
.border(1.dp, borderColor, shape)
.padding(8.dp),
.shadow(6.dp, shape)
.background(JewelTheme.globalColors.panelBackground, shape)
.border(1.dp, JewelTheme.globalColors.borders.normal, shape)
.height(36.dp)
.padding(start = 10.dp, end = 4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
JewelTextField(
Icon(
key = AllIconsKeys.Actions.Find,
contentDescription = null,
modifier = Modifier.size(16.dp),
)
Spacer(Modifier.width(8.dp))
TextField(
state = state,
undecorated = true,
modifier =
Modifier
.widthIn(min = 220.dp, max = 380.dp)
.height(36.dp)
.width(200.dp)
.focusRequester(focusRequester)
.onPreviewKeyEvent { ev ->
if (ev.type == KeyEventType.KeyUp && (ev.key == Key.Enter || ev.key == Key.NumPadEnter)) {
if (ev.isShiftPressed) onEnterPrev() else onEnterNext()
true
} else if (ev.type == KeyEventType.KeyUp && ev.key == Key.Escape) {
onClose()
true
} else {
false
// On key press as in Chromium's FindBarView, so holding Enter keeps stepping
if (ev.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
when (ev.key) {
Key.Enter, Key.NumPadEnter -> {
when {
ev.isCtrlPressed -> onActivate()
ev.isShiftPressed -> onEnterPrev()
else -> onEnterNext()
}
true
}

Key.Escape -> {
onClose()
true
}

else -> {
false
}
}
},
placeholder = { Text(stringResource(Res.string.search_in_page)) },
leadingIcon = {
Icon(key = AllIconsKeys.Actions.Find, contentDescription = stringResource(Res.string.search_in_page))
},
textStyle =
androidx.compose.ui.text
.TextStyle(fontSize = 13.sp),
textStyle = JewelTheme.defaultTextStyle.copy(fontSize = 13.sp),
)
Spacer(Modifier.width(4.dp))
// Smart mode toggle (magic button icon)
IconButton(onClick = onToggleSmartMode) {
Icon(
imageVector = MaterialSymbolsMagicButton,
contentDescription = "Smart search mode",
tint =
if (smartModeEnabled) {
JewelTheme.globalColors.outlines.focused
} else {
Color.Gray
},
if (matchCount != null) {
Spacer(Modifier.width(8.dp))
// "3/861" once a match is current, like a browser's find bar; red when nothing matches
Text(
text = matchPosition?.let { "$it/$matchCount" } ?: matchCount.toString(),
color = if (matchCount == 0) JewelTheme.globalColors.text.error else JewelTheme.globalColors.text.info,
fontSize = 12.sp,
)
}
Spacer(Modifier.width(4.dp))
IconButton(onClick = onEnterPrev) {
Icon(key = AllIconsKeys.General.ChevronRight, contentDescription = stringResource(Res.string.chevron_icon_description))
}
IconButton(onClick = onEnterNext) {
Icon(key = AllIconsKeys.General.ChevronLeft, contentDescription = stringResource(Res.string.chevron_icon_description))
}
Spacer(Modifier.width(8.dp))
IconButton(onClick = onClose) {
Icon(key = AllIconsKeys.Windows.Close, contentDescription = stringResource(Res.string.search_in_page))
Divider(
orientation = Orientation.Vertical,
modifier = Modifier.padding(horizontal = 8.dp).height(20.dp),
)
Row(horizontalArrangement = Arrangement.spacedBy(2.dp), verticalAlignment = Alignment.CenterVertically) {
if (onToggleSmartMode != null) {
val smartLabel = stringResource(Res.string.find_smart_mode)
ShortcutTooltip(smartLabel) {
ToggleableIconButton(
value = smartModeEnabled,
onValueChange = { onToggleSmartMode() },
focusable = false,
) {
Icon(
imageVector = MaterialSymbolsMagicButton,
contentDescription = smartLabel,
modifier = Modifier.size(16.dp),
tint =
if (smartModeEnabled) {
JewelTheme.globalColors.outlines.focused
} else {
JewelTheme.globalColors.text.info
},
)
}
}
}
// Up/down, not left/right: unambiguous in a right-to-left layout
val hasMatches = matchCount != 0
FindActionButton(
key = AllIconsKeys.Actions.PreviousOccurence,
label = stringResource(Res.string.find_previous_match),
shortcut = "Shift+Enter",
enabled = hasMatches,
onClick = onEnterPrev,
)
FindActionButton(
key = AllIconsKeys.Actions.NextOccurence,
label = stringResource(Res.string.find_next_match),
shortcut = "Enter",
enabled = hasMatches,
onClick = onEnterNext,
)
FindActionButton(
key = AllIconsKeys.Actions.Close,
label = stringResource(Res.string.find_close),
shortcut = "Esc",
onClick = onClose,
)
}
}
}

@Composable
private fun FindActionButton(
key: org.jetbrains.jewel.ui.icon.IconKey,
label: String,
shortcut: String,
onClick: () -> Unit,
enabled: Boolean = true,
) {
ShortcutTooltip(label, shortcut) {
// Not focusable: the keyboard stays in the field, as in Chromium
IconActionButton(key = key, contentDescription = label, onClick = onClick, enabled = enabled, focusable = false)
}
}

/** Tooltip with the action's name and, dimmed, its shortcut, as the title bar's buttons. */
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun ShortcutTooltip(
label: String,
shortcut: String? = null,
content: @Composable () -> Unit,
) {
Tooltip(
tooltip = {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Text(label)
if (shortcut != null) Text(shortcut, color = JewelTheme.globalColors.text.disabled)
}
},
content = content,
)
}

/**
* Copies the [persisted] query into the find field, selected as Chromium does with a prefilled
* query, so typing replaces it. The field's own short text (under 2 letters, persisted as "")
* isn't wiped: that's the user still typing.
*/
fun syncFindField(
field: TextFieldState,
persisted: String,
) {
val current = field.text.toString()
if (current == persisted || (persisted.isEmpty() && current.trim().length < 2)) return
field.edit {
replace(0, length, persisted)
selection = TextRange(0, length)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.kdroidfilter.seforim.tabs.TabsDestination
import io.github.kdroidfilter.seforim.tabs.TabsViewModel
import io.github.kdroidfilter.seforimapp.core.presentation.theme.IntUiThemes
import io.github.kdroidfilter.seforimapp.core.presentation.utils.LocalWindowViewModelStoreOwner
import io.github.kdroidfilter.seforimapp.core.presentation.utils.hasFindInPage
import io.github.kdroidfilter.seforimapp.features.settings.SettingsWindowEvents
import io.github.kdroidfilter.seforimapp.features.settings.SettingsWindowViewModel
import io.github.kdroidfilter.seforimapp.framework.desktop.LocalOpenWindow
Expand Down Expand Up @@ -47,19 +48,7 @@ fun TitleBarActionsButtonsView() {
val tabsViewModel: TabsViewModel = LocalOpenWindow.current.tabsViewModel
val tabsState = tabsViewModel.state.collectAsState().value
val currentTab = tabsState.tabs.getOrNull(tabsState.selectedTabIndex)
val findEnabled =
when (val dest = currentTab?.destination) {
is TabsDestination.Search -> true
is TabsDestination.BookContent -> {
(
appGraph.tabPersistedStateStore
.get(dest.tabId)
?.bookContent
?.selectedBookId ?: -1L
) > 0L
}
else -> false
}
val findEnabled = currentTab?.destination?.hasFindInPage(appGraph.tabPersistedStateStore) == true

val iconDescription =
when (theme) {
Expand Down Expand Up @@ -130,7 +119,7 @@ fun TitleBarActionsButtonsView() {
val tabId = tabs.getOrNull(selectedIndex)?.destination?.tabId ?: return@TitleBarActionButton
// Toggle the Find-in-Page bar for the current tab only
val isOpen = appSettings.findBarOpenFlow(tabId).value
if (isOpen) appSettings.closeFindBar(tabId) else appSettings.openFindBar(tabId)
if (isOpen) appSettings.closeFindBar(tabId) else appSettings.showFindBar(tabId, appGraph.selectionContext.selectedTextIn(tabId))
},
tooltipText =
if (findEnabled) {
Expand Down
Loading
Loading