Skip to content

Commit

Permalink
Make back gesture close modals and similar
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendicated committed Nov 25, 2022
1 parent 924ba2b commit 2f35908
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
17 changes: 13 additions & 4 deletions app/src/main/java/dev/vendicated/vencord/HttpClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.vendicated.vencord;

import android.app.Activity;
import android.content.res.Resources;

import androidx.annotation.NonNull;

import java.io.*;
Expand Down Expand Up @@ -39,14 +42,20 @@ public String getMessage() {
}
}

public static String vencord;
public static String VencordRuntime;
public static String VencordMobileRuntime;

public static void fetchVencord(Activity activity) throws IOException {
if (VencordRuntime != null) return;

public static void fetchVencord() throws IOException {
if (vencord != 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);
}
}

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/dev/vendicated/vencord/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ protected void onCreate(Bundle savedInstanceState) {
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;
Expand All @@ -52,8 +54,8 @@ protected void onCreate(Bundle savedInstanceState) {

@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);
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/dev/vendicated/vencord/VWebviewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {

@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
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/dev/vendicated/vencord/VencordNative.java
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/raw/vencord_mobile.js
Original file line number Diff line number Diff line change
@@ -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});
}
}
})();

0 comments on commit 2f35908

Please sign in to comment.