Make back gesture close modals and similar
This commit is contained in:
parent
924ba2b74b
commit
2f35908d0c
5 changed files with 58 additions and 9 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
25
app/src/main/java/dev/vendicated/vencord/VencordNative.java
Normal file
25
app/src/main/java/dev/vendicated/vencord/VencordNative.java
Normal 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
12
app/src/main/res/raw/vencord_mobile.js
Normal file
12
app/src/main/res/raw/vencord_mobile.js
Normal 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});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
Loading…
Reference in a new issue