feat(java): glue for workspace events

This commit is contained in:
zaaarf 2024-08-09 14:11:13 +02:00
parent 264dd319d3
commit 53503ae117
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
3 changed files with 91 additions and 18 deletions

View file

@ -67,6 +67,11 @@ public class Workspace {
delete_buffer(this.ptr, path); delete_buffer(this.ptr, path);
} }
private static native Event event(long self) throws CodeMPException;
public Event event() throws CodeMPException {
return event(this.ptr);
}
private static native BufferController select_buffer(long self, long timeout) throws CodeMPException; private static native BufferController select_buffer(long self, long timeout) throws CodeMPException;
public Optional<BufferController> selectBuffer(long timeout) throws CodeMPException { public Optional<BufferController> selectBuffer(long timeout) throws CodeMPException {
return Optional.ofNullable(select_buffer(this.ptr, timeout)); return Optional.ofNullable(select_buffer(this.ptr, timeout));
@ -78,4 +83,36 @@ public class Workspace {
protected void finalize() { protected void finalize() {
free(this.ptr); free(this.ptr);
} }
public static class Event {
private final Type type;
private final String argument;
Event(Type type, String argument) {
this.type = type;
this.argument = argument;
}
public Optional<String> getUserJoined() {
if(this.type == Type.USER_JOIN) {
return Optional.of(this.argument);
} else return Optional.empty();
}
public Optional<String> getUserLeft() {
if(this.type == Type.USER_LEAVE) {
return Optional.of(this.argument);
} else return Optional.empty();
}
public boolean hasFileTreeUpdated() {
return type == Type.FILE_TREE_UPDATED;
}
private enum Type {
USER_JOIN,
USER_LEAVE,
FILE_TREE_UPDATED
}
}
} }

View file

@ -113,9 +113,9 @@ pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>(
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let path = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") }; let path = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
let name = match workspace.detach(path.to_str().expect("Not UTF-8")) { let name = match workspace.detach(path.to_str().expect("Not UTF-8")) {
crate::workspace::DetachResult::NotAttached => "NOT_ATTACHED", crate::workspace::worker::DetachResult::NotAttached => "NOT_ATTACHED",
crate::workspace::DetachResult::Detaching => "DETACHED", crate::workspace::worker::DetachResult::Detaching => "DETACHED",
crate::workspace::DetachResult::AlreadyDetached => "ALREADY_DETACHED" crate::workspace::worker::DetachResult::AlreadyDetached => "ALREADY_DETACHED"
}; };
let class = env.find_class("mp/code/data/DetachResult").expect("Failed to find class!"); let class = env.find_class("mp/code/data/DetachResult").expect("Failed to find class!");
@ -187,6 +187,42 @@ pub extern "system" fn Java_mp_code_Workspace_delete_1buffer<'local>(
RT.block_on(workspace.delete(buffer.to_str().expect("Not UTF-8!"))).jexcept(&mut env); RT.block_on(workspace.delete(buffer.to_str().expect("Not UTF-8!"))).jexcept(&mut env);
} }
/// Receives a workspace event if present.
#[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_event(
mut env: JNIEnv,
_class: JClass,
self_ptr: jlong
) -> jobject {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
RT.block_on(workspace.event())
.map(|event| {
let type_class = env.find_class("mp/code/Workspace$Event$Type").expect("Failed to find class!");
let (name, arg) = match event {
crate::api::Event::FileTreeUpdated => ("FILE_TREE_UPDATED", None),
crate::api::Event::UserJoin(arg) => ("USER_JOIN", Some(arg)),
crate::api::Event::UserLeave(arg) => ("USER_LEAVE", Some(arg)),
};
let event_type = env.get_static_field(type_class, name, "Lmp/code/Workspace/Event/Type;")
.expect("Failed to get field!")
.l()
.expect("Field was not of expected type!");
let arg = arg.map(|s| env.new_string(s).expect("Failed to create String!"))
.unwrap_or_default();
let event_class = env.find_class("mp/code/Workspace$Event").expect("Failed to find class!");
env.new_object(
event_class,
"(Lmp/code/Workspace/Event/Type;Ljava/lang/String;)V",
&[
JValueGen::Object(&event_type),
JValueGen::Object(&arg)
]
).expect("Failed to create object!")
}).jexcept(&mut env).as_raw()
}
/// Polls a list of buffers, returning the first ready one. /// Polls a list of buffers, returning the first ready one.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_select_1buffer( pub extern "system" fn Java_mp_code_Workspace_select_1buffer(