WIP baseline: Termux open-file bridge + word-wrap-aware viewport/scroll
- JNI: open_file_in_termux via ACTION_SEND intent (text/plain + file:// uri), global context ref kept from registerFragment - impl_android.go: OpenFile(path) attaches current thread if needed - NewLogic takes openfunc; State.open + ui.OpenFile now func(string) - ChunkedBuffer.VisibleByteRange: word-wrap path using GlyphLayout.VisualLineStarts (+byteOffset, lineHeight, visual index param) - GlyphLayout gains LineHeight; drawWrappedText records VisualLineStarts - WordWrap default true; State.ByteOffset tracks first visible line - types: VisualLineIndex - scroll_fix_test.go (new) - debug prints left in place (WIP; cleanup in later phase)
This commit is contained in:
@@ -65,3 +65,28 @@ func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
f(env)
|
||||
}
|
||||
|
||||
func OpenFile(path string) {
|
||||
log.Printf("OpenFile(%s)",path)
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("OpenFile: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
}
|
||||
|
||||
if detach {
|
||||
defer func() {
|
||||
C.DetachCurrentThread(theJVM)
|
||||
}()
|
||||
}
|
||||
cpath := C.CString(path)
|
||||
log.Printf("OpenFile: calling open_file_in_termux",path)
|
||||
C.open_file_in_termux(env, cpath)
|
||||
C.free(unsafe.Pointer(cpath))
|
||||
}
|
||||
|
||||
|
||||
@@ -12,3 +12,4 @@ var (
|
||||
|
||||
func handleEvent(e event.Event) { }
|
||||
|
||||
func OpenFile(path string) { }
|
||||
|
||||
+80
-2
@@ -1,15 +1,20 @@
|
||||
|
||||
//#include <stdlib.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
#include "jni_android.h"
|
||||
#include "_cgo_export.h"
|
||||
|
||||
// Global context
|
||||
static jobject ctx = NULL;
|
||||
|
||||
void
|
||||
registerFragment(JNIEnv *env, jobject view) {
|
||||
jclass cls = (*env)->GetObjectClass(env, view);
|
||||
jmethodID mid = (*env)->GetMethodID(env, cls, "getContext", "()Landroid/content/Context;");
|
||||
jobject ctx = (*env)->CallObjectMethod(env, view, mid);
|
||||
jobject l_ctx = (*env)->CallObjectMethod(env, view, mid);
|
||||
ctx = (*env)->NewGlobalRef(env, l_ctx);
|
||||
cls = (*env)->GetObjectClass(env, ctx);
|
||||
mid = (*env)->GetMethodID(env, cls, "getClassLoader", "()Ljava/lang/ClassLoader;");
|
||||
jobject loader = (*env)->CallObjectMethod(env, ctx, mid);
|
||||
@@ -41,3 +46,76 @@ jobject
|
||||
NewGlobalRef(JNIEnv *env, jobject o) {
|
||||
return (*env)->NewGlobalRef(env, o);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Open file in Termux's file editor via ACTION_SEND share intent.
|
||||
// env: JNIEnv* from your JNI callback
|
||||
// path: absolute filesystem path to the file (e.g. "/storage/emulated/0/foo.txt")
|
||||
void open_file_in_termux(JNIEnv *env, const char *path) {
|
||||
if (!ctx) return;
|
||||
|
||||
jclass intentClass = (*env)->FindClass(env, "android/content/Intent");
|
||||
if (!intentClass) return;
|
||||
|
||||
// new Intent("android.intent.action.SEND")
|
||||
jmethodID intentInit = (*env)->GetMethodID(env, intentClass, "<init>", "(Ljava/lang/String;)V");
|
||||
if (!intentInit) return;
|
||||
|
||||
jstring actionSend = (*env)->NewStringUTF(env, "android.intent.action.SEND");
|
||||
jobject intent = (*env)->NewObject(env, intentClass, intentInit, actionSend);
|
||||
if (!intent) return;
|
||||
|
||||
// intent.setType("text/plain");
|
||||
jmethodID setType = (*env)->GetMethodID(env, intentClass,
|
||||
"setType", "(Ljava/lang/String;)Landroid/content/Intent;");
|
||||
if (!setType) return;
|
||||
|
||||
jstring type = (*env)->NewStringUTF(env, "text/plain");
|
||||
(*env)->CallObjectMethod(env, intent, setType, type);
|
||||
|
||||
// Uri uri = Uri.parse("file://" + path);
|
||||
jclass uriClass = (*env)->FindClass(env, "android/net/Uri");
|
||||
if (!uriClass) return;
|
||||
|
||||
jmethodID uriParse = (*env)->GetStaticMethodID(env, uriClass,
|
||||
"parse", "(Ljava/lang/String;)Landroid/net/Uri;");
|
||||
if (!uriParse) return;
|
||||
|
||||
// Build "file://" + path
|
||||
size_t uriLen = strlen("file://") + strlen(path) + 1;
|
||||
char *uriStr = (char *)malloc(uriLen);
|
||||
if (!uriStr) return;
|
||||
strcpy(uriStr, "file://");
|
||||
strcat(uriStr, path);
|
||||
|
||||
jstring uriJstr = (*env)->NewStringUTF(env, uriStr);
|
||||
jobject uri = (*env)->CallStaticObjectMethod(env, uriClass, uriParse, uriJstr);
|
||||
free(uriStr);
|
||||
|
||||
if (!uri) return;
|
||||
|
||||
// intent.setData(uri);
|
||||
jmethodID setData = (*env)->GetMethodID(env, intentClass,
|
||||
"setData", "(Landroid/net/Uri;)Landroid/content/Intent;");
|
||||
if (!setData) return;
|
||||
|
||||
(*env)->CallObjectMethod(env, intent, setData, uri);
|
||||
|
||||
// intent.putExtra("android.intent.extra.STREAM", uri.toString());
|
||||
// Use putExtra(String, String)
|
||||
jmethodID putExtra = (*env)->GetMethodID(env, intentClass,
|
||||
"putExtra", "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;");
|
||||
if (!putExtra) return;
|
||||
|
||||
jstring extraKey = (*env)->NewStringUTF(env, "android.intent.extra.STREAM");
|
||||
(*env)->CallObjectMethod(env, intent, putExtra, extraKey, uriJstr);
|
||||
|
||||
// ctx.startActivity(intent);
|
||||
jclass contextClass = (*env)->GetObjectClass(env, ctx);
|
||||
jmethodID startActivity = (*env)->GetMethodID(env, contextClass,
|
||||
"startActivity", "(Landroid/content/Intent;)V");
|
||||
if (!startActivity) return;
|
||||
|
||||
(*env)->CallVoidMethod(env, ctx, startActivity, intent);
|
||||
}
|
||||
|
||||
@@ -11,3 +11,4 @@ jint GetEnv(JavaVM *vm, JNIEnv **env, jint version);
|
||||
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args);
|
||||
jint DetachCurrentThread(JavaVM *vm);
|
||||
jobject NewGlobalRef(JNIEnv *env, jobject o);
|
||||
void open_file_in_termux(JNIEnv *env, const char *path);
|
||||
|
||||
+7
-10
@@ -52,7 +52,7 @@ func run(w *app.Window) error {
|
||||
}
|
||||
log.Printf("using filesystem at / (startup directory: %s)", startAbs)
|
||||
|
||||
logic := editor.NewLogic(fs, startAbs)
|
||||
logic := editor.NewLogic(fs, startAbs, OpenFile)
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
|
||||
var mu sync.Mutex
|
||||
var elems []ui.Element
|
||||
@@ -75,7 +75,6 @@ func run(w *app.Window) error {
|
||||
PixelHeight: e.Config.Size.Y,
|
||||
}
|
||||
case app.FrameEvent:
|
||||
log.Printf("run: FrameEvent")
|
||||
gtx := app.NewContext(&ops, e)
|
||||
newScale = gtx.Metric.PxPerDp
|
||||
curScale = logic.State().Scale()
|
||||
@@ -95,10 +94,8 @@ func run(w *app.Window) error {
|
||||
|
||||
// Gather key events
|
||||
focusedID := logic.State().FocusedElementID
|
||||
log.Printf("focusedID = %s", focusedID)
|
||||
if focusedID != "" {
|
||||
if reg, ok := renderer.Keys[focusedID]; ok {
|
||||
log.Printf("looking for events")
|
||||
// Use key.Filter to only receive events destined for the focused element.
|
||||
// We need both Key events (for arrow keys) and Edit events (for text input).
|
||||
// In Gio, key.Filter covers key presses, while key.FocusFilter covers
|
||||
@@ -109,10 +106,10 @@ func run(w *app.Window) error {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
log.Printf("found an event: %T", evt)
|
||||
//log.Printf("found an event: %T", evt)
|
||||
switch k := evt.(type) {
|
||||
case key.Event:
|
||||
log.Printf("key event: %v state=%v", k.Name, k.State)
|
||||
//log.Printf("key event: %v state=%v", k.Name, k.State)
|
||||
if k.State == key.Press {
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
@@ -120,13 +117,13 @@ func run(w *app.Window) error {
|
||||
})
|
||||
}
|
||||
case key.EditEvent:
|
||||
log.Printf("edit event text: %q", k.Text)
|
||||
//log.Printf("edit event text: %q", k.Text)
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
Data: k,
|
||||
})
|
||||
case key.SnippetEvent:
|
||||
log.Printf("snippet event: %v", k)
|
||||
//log.Printf("snippet event: %v", k)
|
||||
// Handle snippet event if necessary, or ignore
|
||||
default:
|
||||
log.Printf("unexpected event type: %T", k)
|
||||
@@ -147,7 +144,7 @@ func run(w *app.Window) error {
|
||||
}
|
||||
logic.LayoutChan() <- glyphLayout
|
||||
default:
|
||||
log.Printf("pad: default event")
|
||||
//log.Printf("pad: default event")
|
||||
handleEvent(e)
|
||||
}
|
||||
}
|
||||
@@ -157,7 +154,7 @@ func frameReceiver(w *app.Window, mu *sync.Mutex, elems *[]ui.Element, frameChan
|
||||
log.Printf("frameReceiver: loop starting")
|
||||
for {
|
||||
frame := <-frameChan
|
||||
log.Printf("frameReceiver: received frame")
|
||||
//log.Printf("frameReceiver: received frame")
|
||||
mu.Lock()
|
||||
*elems = frame
|
||||
w.Invalidate()
|
||||
|
||||
Reference in New Issue
Block a user