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; package dev.vendicated.vencord;
import android.app.Activity;
import android.content.res.Resources;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.io.*; 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 { public static void fetchVencord(Activity activity) throws IOException {
if (vencord != null) return; 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); var conn = fetch(VENCORD_BUNDLE_URL);
try (var is = conn.getInputStream()) { try (var is = conn.getInputStream()) {
vencord = readAsText(is); VencordRuntime = readAsText(is);
} }
} }

View file

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

View file

@ -18,7 +18,8 @@ public class VWebviewClient extends WebViewClient {
@Override @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) { 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 @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});
}
}
})();