2022-06-26 09:16:35 +02:00
|
|
|
package org.mian.gitnex.helpers.codeeditor;
|
|
|
|
|
|
|
|
import android.text.Layout;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.accessibility.AccessibilityEvent;
|
|
|
|
import android.widget.EditText;
|
2024-03-28 13:02:17 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2022-06-26 09:16:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author AmrDeveloper
|
|
|
|
* @author M M Arif
|
|
|
|
*/
|
|
|
|
public class SourcePositionListener {
|
|
|
|
|
|
|
|
private OnPositionChanged onPositionChanged;
|
2022-08-15 16:26:02 +02:00
|
|
|
|
|
|
|
public SourcePositionListener(EditText editText) {
|
2022-09-21 07:43:00 +02:00
|
|
|
View.AccessibilityDelegate viewAccessibility =
|
|
|
|
new View.AccessibilityDelegate() {
|
|
|
|
|
|
|
|
@Override
|
2024-03-28 13:02:17 +01:00
|
|
|
public void sendAccessibilityEvent(@NonNull View host, int eventType) {
|
2022-09-21 07:43:00 +02:00
|
|
|
super.sendAccessibilityEvent(host, eventType);
|
|
|
|
if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
|
|
|
|
&& onPositionChanged != null) {
|
|
|
|
int selectionStart = editText.getSelectionStart();
|
|
|
|
Layout layout = editText.getLayout();
|
|
|
|
if (layout == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int line = editText.getLayout().getLineForOffset(selectionStart);
|
|
|
|
int column = selectionStart - editText.getLayout().getLineStart(line);
|
|
|
|
onPositionChanged.onPositionChange(line + 1, column + 1);
|
|
|
|
}
|
2022-08-26 19:00:08 +02:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
};
|
2022-08-15 16:26:02 +02:00
|
|
|
editText.setAccessibilityDelegate(viewAccessibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnPositionChanged(OnPositionChanged listener) {
|
|
|
|
onPositionChanged = listener;
|
|
|
|
}
|
2022-09-30 05:04:01 +02:00
|
|
|
|
|
|
|
@FunctionalInterface
|
|
|
|
public interface OnPositionChanged {
|
|
|
|
|
|
|
|
void onPositionChange(int line, int column);
|
|
|
|
}
|
2022-06-26 09:16:35 +02:00
|
|
|
}
|