Skip to content

Commit

Permalink
ui: make history context menu dialog show up corresponding to its pos…
Browse files Browse the repository at this point in the history
…ition
  • Loading branch information
plateaukao committed Nov 16, 2024
1 parent fcc2cf9 commit 2abca12
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fun AutoCompleteTextField(
shouldReverse = shouldReverse,
shouldShowTwoColumns = isWideLayout,
onClick = onRecordClick,
onLongClick = {}
onLongClick = { _, _ -> }
)
HorizontalSeparator()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package info.plateaukao.einkbro.view.compose

import android.graphics.Bitmap
import android.graphics.Point
import android.text.TextUtils
import android.widget.TextView
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -21,10 +22,16 @@ import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionOnScreen
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -36,6 +43,7 @@ import info.plateaukao.einkbro.database.BookmarkManager
import info.plateaukao.einkbro.database.Record
import info.plateaukao.einkbro.database.RecordType
import info.plateaukao.einkbro.unit.ViewUnit
import info.plateaukao.einkbro.view.dialog.compose.toScreenPoint
import java.text.SimpleDateFormat
import java.util.Locale

Expand All @@ -48,21 +56,30 @@ fun BrowseHistoryList(
shouldShowTwoColumns: Boolean,
bookmarkManager: BookmarkManager? = null,
onClick: (Record) -> Unit,
onLongClick: (Record) -> Unit,
onLongClick: (Record, Point) -> Unit,
) {
LazyVerticalGrid(
modifier = modifier,
columns = GridCells.Fixed(if (shouldShowTwoColumns) 2 else 1),
reverseLayout = shouldReverse
) {

itemsIndexed(records) { index, record ->
var boxPosition = remember { mutableStateOf(Offset.Zero) }

RecordItem(
record = record,
bitmap = bookmarkManager?.findFaviconBy(record.url)?.getBitmap(),
modifier = Modifier.combinedClickable(
onClick = { onClick(record) },
onLongClick = { onLongClick(record) }
)
modifier = Modifier
.pointerInput(Unit) {
detectTapGestures(
onTap = { _ -> onClick(record) },
onLongPress = { it ->
onLongClick(record, it.toScreenPoint(boxPosition.value))
}
)
}
.onGloballyPositioned { boxPosition.value = it.positionOnScreen() }
)
}
}
Expand Down Expand Up @@ -204,6 +221,6 @@ private fun previewHistoryList() {
shouldReverse = true,
shouldShowTwoColumns = true,
onClick = {},
onLongClick = {})
onLongClick = { _, _ -> })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package info.plateaukao.einkbro.view.compose
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Point
import android.util.AttributeSet
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
Expand Down Expand Up @@ -83,7 +84,7 @@ class HistoryAndTabsView @JvmOverloads constructor(
var recordList: List<Record> by mutableStateOf(emptyList())
var onHistoryIconClick by mutableStateOf({})
var onHistoryItemClick by mutableStateOf<(Record) -> Unit>({})
var onHistoryItemLongClick by mutableStateOf<(Record) -> Unit>({})
var onHistoryItemLongClick by mutableStateOf<(Record, Point) -> Unit>({ _, _ -> })

var addIncognitoTab by mutableStateOf({})
var addTab by mutableStateOf({})
Expand Down Expand Up @@ -137,7 +138,7 @@ fun HistoryAndTabs(
records: List<Record>,
onHistoryIconClick: () -> Unit,
onHistoryItemClick: (Record) -> Unit,
onHistoryItemLongClick: (Record) -> Unit,
onHistoryItemLongClick: (Record, Point) -> Unit,

addIncognitoTab: () -> Unit,
addTab: () -> Unit,
Expand Down Expand Up @@ -222,7 +223,7 @@ private fun MainContent(
bookmarkManager: BookmarkManager?,
records: List<Record>,
onHistoryItemClick: (Record) -> Unit,
onHistoryItemLongClick: (Record) -> Unit,
onHistoryItemLongClick: (Record, Point) -> Unit,
) {
if (!isHistoryOpen) {
PreviewTabs(
Expand Down Expand Up @@ -512,7 +513,7 @@ fun PreviewHistoryAndTabs() {
records = recordList,
onHistoryIconClick = {},
onHistoryItemClick = {},
onHistoryItemLongClick = {},
onHistoryItemLongClick = { _, _ -> },

addIncognitoTab = {},
addTab = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ToolbarActionHandler(
private val browserController = activity as BrowserController

fun handleLongClick(toolbarAction: ToolbarAction) = when (toolbarAction) {
ToolbarAction.Back -> browserController.openHistoryPage(5)
ToolbarAction.Back -> browserController.openHistoryPage(6)
ToolbarAction.BoldFont -> browserController.showFontBoldnessDialog()
ToolbarAction.Bookmark -> browserController.saveBookmark()
ToolbarAction.Font -> browserController.toggleReaderMode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package info.plateaukao.einkbro.view.viewControllers

import android.app.Activity
import android.content.Context
import android.graphics.Point
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.compose.runtime.MutableState
Expand Down Expand Up @@ -61,14 +62,14 @@ class OverviewDialogController(
shouldShowTwoColumns = isWideLayout()
albumList = currentAlbumList
albumFocusIndex = albumFocusIndex
onTabIconClick = { openHomePage() }
onTabIconClick = this@OverviewDialogController::openHomePage
onTabClick = { hide(); it.showOrJumpToTop() }
onTabLongClick = { it.remove() }

recordList = currentRecordList
onHistoryIconClick = { openHistoryPage() }
onHistoryItemClick = { clickHistoryItem(it) }
onHistoryItemLongClick = { longClickHistoryItem(it) }
onHistoryIconClick = this@OverviewDialogController::openHistoryPage
onHistoryItemClick = this@OverviewDialogController::clickHistoryItem
onHistoryItemLongClick = this@OverviewDialogController::showHistoryContextMenu
addIncognitoTab = addIncognitoTabAction
addTab = { hide(); addEmptyTabAction() }
closePanel = { hide() }
Expand All @@ -92,10 +93,6 @@ class OverviewDialogController(
hide()
}

private fun longClickHistoryItem(record: Record) {
showHistoryContextMenu(record)
}

fun hide() {
composeView.visibility = GONE
}
Expand Down Expand Up @@ -143,11 +140,12 @@ class OverviewDialogController(
}
}

private fun showHistoryContextMenu(record: Record) {
private fun showHistoryContextMenu(record: Record, position: Point) {
val parentFragmentManager = (context as FragmentActivity).supportFragmentManager
BookmarkContextMenuDlgFragment(
Bookmark(record.title ?: "no title", record.url),
allowEdit = false,
anchorPoint = position,
) {
when (it) {
ContextMenuItemType.Delete -> deleteHistory(record)
Expand Down

0 comments on commit 2abca12

Please sign in to comment.