-
-
Notifications
You must be signed in to change notification settings - Fork 29
ADFA-4419: Remote peer editor decoration API #1459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daniel-ADFA
wants to merge
9
commits into
stage
Choose a base branch
from
feat/ADFA-4419-remote-peer-editor-decoration
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c693756
feat(plugin-api): add remote-peer editor decoration API
Daniel-ADFA 58fcf5c
WIP: Add cursor markers to plugin
Daniel-ADFA f318743
WIP: remote pair decoration
Daniel-ADFA 2d887f0
Implement pair programming plugin
Daniel-ADFA 136ea3a
fix(ADFA-4419): remove stray token breaking PluginManager compilation
Daniel-ADFA 78ee0cb
Merge remote-tracking branch 'origin/stage' into feat/ADFA-4419-remot…
Daniel-ADFA 7788b79
Merge remote-tracking branch 'origin/stage' into feat/ADFA-4419-remot…
alome007 ea2293a
refactor(ADFA-4419): distinguish peer-presence overlay from #1448 dec…
alome007 90dbab1
fix(ADFA-4419): address CodeRabbit review + drop dev-trace logging
alome007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
app/src/main/java/com/itsaky/androidide/activities/editor/PeerPresenceOverlayManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.itsaky.androidide.activities.editor | ||
|
|
||
| import android.graphics.Color | ||
| import android.graphics.drawable.GradientDrawable | ||
| import android.view.Gravity | ||
| import android.view.View | ||
| import android.widget.TextView | ||
| import io.github.rosemoe.sora.widget.CodeEditor | ||
| import io.github.rosemoe.sora.widget.base.EditorPopupWindow | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * Renders remote-collaborator presence as small named caret badges floating in the editor, | ||
| * one per (file, peerId). Markers are positioned in content coordinates and track scrolling | ||
| * via [EditorPopupWindow.FEATURE_SCROLL_AS_CONTENT]; the plugin repositions a marker by | ||
| * calling [addMarker] again with a new line/column on each cursor move. | ||
| * | ||
| * All methods must be called on the main thread (the editor view is touched directly). | ||
| */ | ||
| class PeerPresenceOverlayManager( | ||
| private val editorForFile: (File) -> CodeEditor?, | ||
| ) { | ||
|
|
||
| private val markers: HashMap<String, HashMap<String, PeerCursorWindow>> = HashMap() | ||
|
|
||
| fun addMarker( | ||
| file: File, | ||
| line: Int, | ||
| column: Int, | ||
| peerId: String, | ||
| peerName: String, | ||
| peerColor: Int, | ||
| ): Boolean { | ||
| val editor = editorForFile(file) ?: return false | ||
| val content = editor.text | ||
| if (line !in 0 until content.lineCount) return false | ||
| val safeColumn = column.coerceIn(0, content.getColumnCount(line)) | ||
| val byPeer = markers.getOrPut(file.absolutePath) { HashMap() } | ||
| val existing = byPeer[peerId] | ||
| val window = if (existing != null && existing.boundEditor === editor) { | ||
| existing | ||
| } else { | ||
| existing?.dismiss() | ||
| PeerCursorWindow(editor).also { byPeer[peerId] = it } | ||
| } | ||
| window.update(peerName, peerColor, line, safeColumn) | ||
| return true | ||
| } | ||
|
|
||
| fun removeMarker(file: File, peerId: String): Boolean { | ||
| val removed = markers[file.absolutePath]?.remove(peerId) ?: return false | ||
| removed.dismiss() | ||
| return true | ||
| } | ||
|
|
||
| fun clear(file: File) { | ||
| markers.remove(file.absolutePath)?.values?.forEach { it.dismiss() } | ||
| } | ||
|
|
||
| fun clearAll() { | ||
| markers.values.forEach { byPeer -> byPeer.values.forEach { it.dismiss() } } | ||
| markers.clear() | ||
| } | ||
| } | ||
|
|
||
| class PeerCursorWindow( | ||
| val boundEditor: CodeEditor, | ||
| ) : EditorPopupWindow( | ||
| boundEditor, | ||
| FEATURE_SCROLL_AS_CONTENT or FEATURE_SHOW_OUTSIDE_VIEW_ALLOWED, | ||
| ) { | ||
|
|
||
| private val density = boundEditor.context.resources.displayMetrics.density | ||
|
|
||
| private val label = TextView(boundEditor.context).apply { | ||
| textSize = 11f | ||
| gravity = Gravity.CENTER | ||
| maxLines = 1 | ||
| includeFontPadding = false | ||
| val padH = (8 * density).toInt() | ||
| val padV = (3 * density).toInt() | ||
| setPadding(padH, padV, padH, padV) | ||
| } | ||
|
|
||
| init { | ||
| popup.isClippingEnabled = false | ||
| setContentView(label) | ||
| } | ||
|
|
||
| fun update(peerName: String, peerColor: Int, line: Int, column: Int) { | ||
| // getOffset returns the on-screen x. If the caret is past the visible width, add a | ||
| // direction arrow so the badge (clamped to the edge below) signals where the peer is. | ||
| val rawX = boundEditor.getOffset(line, column).toInt() | ||
| label.text = when { | ||
| rawX > boundEditor.width -> "$peerName →" | ||
| rawX < 0 -> "← $peerName" | ||
| else -> peerName | ||
| } | ||
| label.setTextColor(contrastingTextColor(peerColor)) | ||
| label.background = GradientDrawable().apply { | ||
| setColor(peerColor) | ||
| cornerRadius = 4 * density | ||
| } | ||
|
|
||
| label.measure( | ||
| View.MeasureSpec.makeMeasureSpec(boundEditor.width, View.MeasureSpec.AT_MOST), | ||
| View.MeasureSpec.makeMeasureSpec(boundEditor.height, View.MeasureSpec.AT_MOST), | ||
| ) | ||
| val width = label.measuredWidth | ||
| val height = label.measuredHeight | ||
| setSize(width, height) | ||
|
|
||
| // Clamp into the visible width so a caret scrolled off to the right pins at the edge | ||
| // instead of vanishing. Only clamp when the editor has a known width. | ||
| val maxX = boundEditor.width - width | ||
| val x = if (maxX >= 0) rawX.coerceIn(0, maxX) else rawX | ||
| val y = (boundEditor.rowHeight * line) - boundEditor.offsetY - height | ||
| setLocationAbsolutely(x, y) | ||
| if (!isShowing) show() | ||
| } | ||
|
|
||
| private fun contrastingTextColor(background: Int): Int { | ||
| val r = Color.red(background) / 255.0 | ||
| val g = Color.green(background) / 255.0 | ||
| val b = Color.blue(background) / 255.0 | ||
| val luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b | ||
| return if (luminance > 0.6) Color.parseColor("#0A0A0A") else Color.WHITE | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.