Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.itsaky.androidide.idetooltips.TooltipTag

class ShowTooltipAction(private val context: Context, override val order: Int) :
EditorRelatedAction() {
override val id: String = "ide.editor.code.text.format"
override val id: String = "ide.editor.code.text.show_tooltip"
override var location: ActionItem.Location = ActionItem.Location.EDITOR_TEXT_ACTIONS
private var htmlString: String = ""

Expand All @@ -39,6 +39,18 @@ class ShowTooltipAction(private val context: Context, override val order: Int) :
icon = ContextCompat.getDrawable(context, R.drawable.ic_action_help)
}

override fun prepare(data: ActionData) {
super.prepare(data)
val editor = data.getEditor()
if (editor == null) {
visible = false
enabled = false
return
}
visible = editor.cursor?.isSelected == true
enabled = visible
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

override suspend fun execAction(data: ActionData): Any {
val editor = data.getEditor()!!
val cursor = editor.text.cursor
Expand All @@ -50,10 +62,11 @@ class ShowTooltipAction(private val context: Context, override val order: Int) :
}
val word = editor.text.substring(cursor.left, cursor.right)
if (cursor.isSelected) {
val tag = if (category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute()) {
word.substringAfterLast(":")
} else {
word
val useEditorTag = editor.tag != null
val tag = when {
useEditorTag -> editor.tag.toString()
category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute() -> word.substringAfterLast(":")
else -> word
}
TooltipManager.showTooltip(
context = context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.viewbinding.ViewBinding
import com.itsaky.androidide.databinding.FragmentEmptyStateBinding
import com.itsaky.androidide.editor.ui.EditorLongPressEvent
import com.itsaky.androidide.editor.ui.IDEEditor
import com.itsaky.androidide.idetooltips.TooltipManager
import com.itsaky.androidide.utils.viewLifecycleScope
import com.itsaky.androidide.viewmodel.EmptyStateFragmentViewModel
Expand Down Expand Up @@ -40,16 +41,31 @@ abstract class EmptyStateFragment<T : ViewBinding> : FragmentWithBinding<T> {
@Volatile
private var cachedIsEmpty: Boolean = true

open val currentEditor: IDEEditor? get() = null

/**
* Called when a long press is detected on the fragment's root view.
* Subclasses must implement this to define the action (e.g., show a tooltip).
*/
protected abstract fun onFragmentLongPressed()
open fun onFragmentLongPressed(x: Float = -1f, y: Float = -1f) {
currentEditor?.let { editor ->
if (x >= 0 && y >= 0) {
editor.setSelectionFromPoint(x, y)
}
}
onFragmentLongPressed()
}
Comment thread
jatezzz marked this conversation as resolved.

open fun onFragmentLongPressed() {
val currentEditor = currentEditor ?: return
currentEditor.selectCurrentWord()
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private val gestureListener =
object : GestureDetector.SimpleOnGestureListener() {
override fun onLongPress(e: MotionEvent) {
onFragmentLongPressed()
if (currentEditor?.isReadOnlyContext == true) return
onFragmentLongPressed(e.x, e.y)
}
}

Expand Down Expand Up @@ -160,6 +176,7 @@ abstract class EmptyStateFragment<T : ViewBinding> : FragmentWithBinding<T> {
// This method will be called when an EditorLongPressEvent is posted
@Subscribe(threadMode = ThreadMode.MAIN)
fun onEditorLongPressed(event: EditorLongPressEvent) {
onFragmentLongPressed()
val motionEvent = event.motionEvent
onFragmentLongPressed(motionEvent.x, motionEvent.y)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ThreadUtils
import com.itsaky.androidide.R
import com.itsaky.androidide.editor.ui.IDEEditor
import com.itsaky.androidide.idetooltips.TooltipTag

class BuildOutputFragment : NonEditableEditorFragment() {
override fun onFragmentLongPressed() {
showTooltipDialog(TooltipTag.PROJECT_BUILD_OUTPUT)
}
override val currentEditor: IDEEditor? get() = editor

private val unsavedLines: MutableList<String?> = ArrayList()

Expand All @@ -35,6 +34,7 @@ class BuildOutputFragment : NonEditableEditorFragment() {
) {
super.onViewCreated(view, savedInstanceState)
editor?.includeDebugInfoOnCopy = true
editor?.tag = TooltipTag.PROJECT_BUILD_OUTPUT
emptyStateViewModel.setEmptyMessage(getString(R.string.msg_emptyview_buildoutput))
if (unsavedLines.isNotEmpty()) {
for (line in unsavedLines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.itsaky.androidide.editor.language.treesitter.LogLanguage
import com.itsaky.androidide.editor.language.treesitter.TreeSitterLanguageProvider
import com.itsaky.androidide.editor.schemes.IDEColorScheme
import com.itsaky.androidide.editor.schemes.IDEColorSchemeProvider
import com.itsaky.androidide.editor.ui.IDEEditor
import com.itsaky.androidide.fragments.EmptyStateFragment
import com.itsaky.androidide.fragments.output.LogViewFragment.Companion.LOG_FREQUENCY
import com.itsaky.androidide.fragments.output.LogViewFragment.Companion.MAX_CHUNK_SIZE
Expand Down Expand Up @@ -126,6 +127,8 @@ abstract class LogViewFragment :
}
}

override val currentEditor: IDEEditor? get() = _binding?.editor

fun appendLog(line: LogLine) {
val lineString =
if (isSimpleFormattingEnabled()) {
Expand Down Expand Up @@ -222,6 +225,7 @@ abstract class LogViewFragment :
editor.typefaceText = jetbrainsMono()
editor.isEnsurePosAnimEnabled = false
editor.includeDebugInfoOnCopy = true
editor.tag = tooltipTag
editor.cursorAnimator =
object : CursorAnimator {
override fun markStartPos() {}
Expand Down Expand Up @@ -280,8 +284,4 @@ abstract class LogViewFragment :
emptyStateViewModel.setEmpty(true)
}
}

override fun onFragmentLongPressed() {
showTooltipDialog(tooltipTag)
}
}
16 changes: 16 additions & 0 deletions editor/src/main/java/com/itsaky/androidide/editor/ui/IDEEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1131,4 +1131,20 @@ constructor(
) {
editorFeatures.setSelectionAround(line, column)
}

fun setSelectionFromPoint(x: Float, y: Float) {
if (isReleased) return

try {
val packedPos = getPointPosition(x, y)

val line = (packedPos ushr 32).toInt()
val column = (packedPos and 0xffffffffL).toInt()
if (line < 0 || column < 0) return

setSelection(line, column)
} catch (e: Exception) {
log.error("Error setting selection from point", e)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}