Interface for defining custom actions.Source: core/actions/src/main/java/com/tom/rv2ide/actions/ActionItem.kt:35
package com.tom.rv2ide.actionsimport android.graphics.ColorFilterimport android.graphics.drawable.Drawableimport android.view.Viewinterface ActionItem { // Identity val id: String val itemId: Int get() = id.hashCode() // Display properties var label: String var subtitle: String? var icon: Drawable? var visible: Boolean var enabled: Boolean // Behavior var requiresUIThread: Boolean var location: Location val order: Int get() = -1 // Lifecycle methods fun prepare(data: ActionData) suspend fun execAction(data: ActionData): Any fun postExec(data: ActionData, result: Any) fun destroy() // UI customization fun getShowAsActionFlags(data: ActionData): Int fun createActionView(data: ActionData): View? fun createColorFilter(data: ActionData): ColorFilter? enum class Location(val id: String) { EDITOR_TOOLBAR("ide.editor.toolbar"), EDITOR_SIDEBAR("ide.editor.sidebar"), EDITOR_SIDEBAR_DEFAULT_ITEMS("ide.editor.sidebar.defaultItems"), EDITOR_TEXT_ACTIONS("ide.editor.textActions"), EDITOR_CODE_ACTIONS("ide.editor.codeActions"), EDITOR_FILE_TABS("ide.editor.fileTabs"), EDITOR_FILE_TREE("ide.editor.fileTree"), UI_DESIGNER_TOOLBAR("ide.uidesigner.toolbar") }}
Container for passing data to actions.Source: core/actions/src/main/java/com/tom/rv2ide/actions/ActionData.java:30
package com.tom.rv2ide.actions;import java.util.Map;public class ActionData { public <T> void put(Class<T> type, T object); public <T> T get(Class<T> type); public void clear();}
import com.tom.rv2ide.actions.ActionItemimport com.tom.rv2ide.actions.ActionDataimport android.graphics.drawable.Drawableclass MyCustomAction : ActionItem { override val id = "my.custom.action" override var label = "My Action" override var subtitle: String? = "Performs custom operation" override var icon: Drawable? = null override var visible = true override var enabled = true override var requiresUIThread = false override var location = ActionItem.Location.EDITOR_TOOLBAR override fun prepare(data: ActionData) { // Update action state before showing enabled = isActionAvailable(data) visible = shouldShowAction(data) } override suspend fun execAction(data: ActionData): Any { // Perform the action println("Executing my custom action") // Access context data val editor = data.get(IEditor::class.java) val file = editor?.getFile() if (file != null) { // Do something with the file processFile(file) return true } return false } override fun postExec(data: ActionData, result: Any) { // Run on UI thread after execution if (result == true) { showSuccessMessage("Action completed successfully") } } private fun isActionAvailable(data: ActionData): Boolean { // Check if action should be enabled return data.get(IEditor::class.java) != null } private fun shouldShowAction(data: ActionData): Boolean { // Check if action should be visible return true }}
override suspend fun execAction(data: ActionData): Any { // Get editor instance val editor = data.get(IEditor::class.java) // Get LSP editor val lspEditor = data.get(ILspEditor::class.java) // Get activity val activity = data.get(Activity::class.java) // Get project manager val projectManager = data.get(IProjectManager::class.java) // Get selected file val file = data.get(File::class.java) // Use the data if (editor != null) { val position = editor.getCursorLSPPosition() // ... } return true}
override fun prepare(data: ActionData) { val editor = data.get(IEditor::class.java) val file = editor?.getFile() // Enable only for Kotlin files enabled = file?.extension == "kt" // Show only when text is selected val selection = editor?.getCursorLSPRange() visible = selection != null && !selection.isSamePosition() // Update label based on context label = if (editor?.isModified() == true) { "Save*" } else { "Save" }}
override fun prepare(data: ActionData) { val projectManager = data.get(IProjectManager::class.java) val workspace = projectManager?.getWorkspace() // Only show if workspace is configured visible = workspace != null // Only enable if no build is running enabled = !isBuildRunning()}
override var requiresUIThread = falseoverride suspend fun execAction(data: ActionData): Any { // This runs on a background thread val result = performLongRunningOperation() return result}
override suspend fun execAction(data: ActionData): Any { // Background work val result = analyzeCode() return result}override fun postExec(data: ActionData, result: Any) { // Always runs on UI thread if (result is AnalysisResult) { showResults(result) }}