1
0
Fork 0

Make back gesture close modals and similar

This commit is contained in:
Vendicated 2022-11-25 13:05:02 +01:00
parent 924ba2b74b
commit 2f35908d0c
No known key found for this signature in database
GPG key ID: EC781ADFB93EFFA3
5 changed files with 58 additions and 9 deletions

View file

@ -1,5 +1,8 @@
package dev.vendicated.vencord;
import android.app.Activity;
import android.content.res.Resources;
import androidx.annotation.NonNull;
import java.io.*;
@ -39,14 +42,20 @@ public class HttpClient {
}
}
public static String vencord;
public static String VencordRuntime;
public static String VencordMobileRuntime;
public static void fetchVencord() throws IOException {
if (vencord != null) return;
public static void fetchVencord(Activity activity) throws IOException {
if (VencordRuntime != null) return;
var res = activity.getResources();
try (var is = res.openRawResource(R.raw.vencord_mobile)) {
VencordMobileRuntime = readAsText(is);
}
var conn = fetch(VENCORD_BUNDLE_URL);
try (var is = conn.getInputStream()) {
vencord = readAsText(is);
VencordRuntime = readAsText(is);
}
}

View file

@ -39,9 +39,11 @@ public class MainActivity extends Activity {
s.setJavaScriptEnabled(true);
s.setDomStorageEnabled(true);
s.setAllowFileAccess(true);
wv.addJavascriptInterface(new VencordNative(this, wv), "VencordMobileNative");
try {
HttpClient.fetchVencord();
HttpClient.fetchVencord(this);
} catch (IOException ex) {
Logger.e("Failed to fetch Vencord", ex);
return;
@ -52,8 +54,8 @@ public class MainActivity extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && wv != null && wv.canGoBack()) {
wv.goBack();
if (keyCode == KeyEvent.KEYCODE_BACK && wv != null) {
runOnUiThread(() -> wv.evaluateJavascript("VencordMobile.onBackPress()", null));
return true;
}
return super.onKeyDown(keyCode, event);

View file

@ -18,7 +18,8 @@ public class VWebviewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.evaluateJavascript(HttpClient.vencord, null);
view.evaluateJavascript(HttpClient.VencordRuntime, null);
view.evaluateJavascript(HttpClient.VencordMobileRuntime, null);
}
@Nullable

View file

@ -0,0 +1,25 @@
package dev.vendicated.vencord;
import android.app.Activity;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
public class VencordNative {
private final WebView wv;
private final Activity activity;
public VencordNative(Activity activity, WebView wv) {
this.activity = activity;
this.wv = wv;
}
@JavascriptInterface
public void goBack() {
activity.runOnUiThread(() -> {
if (wv.canGoBack())
wv.goBack();
else
activity.getActionBar();
});
}
}

View file

@ -0,0 +1,12 @@
!(() => {
const ModalEscapeHandler = Vencord.Util.lazyWebpack(m => m.binds?.[0] === "esc" && m.binds.length === 1);
const EscapeHandler = Vencord.Util.lazyWebpack(m => m.binds?.[0] === "esc" && m.binds[1] === "shift+pagedown");
window.VencordMobile = {
onBackPress() {
// false implies modal closed
if (ModalEscapeHandler.action() === false) return;
EscapeHandler.action({target:document.activeElement});
}
}
})();