[Global] Fix for 204201+ (#185)

* Fix for 204201+

* Hooki-ify safeMode styles

* Re-add theme fixer

* def.d.ts

* Alternate `createThemedStyleSheet` approach

* verbatim import aa

* make backwards compatible

---------

Co-authored-by: redstonekasi <kasi@hanneskann.de>
This commit is contained in:
Amsyar Rasyiq 2023-11-14 07:24:48 +08:00 committed by GitHub
parent 289ffa49f2
commit 0a78aa4d60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 5 deletions

1
src/def.d.ts vendored
View file

@ -356,6 +356,7 @@ interface LoaderIdentity {
interface DiscordStyleSheet {
[index: string]: any,
createStyles: <T extends _RN.StyleSheet.NamedStyles<T>>(sheet: T | (() => T)) => () => T;
createThemedStyleSheet: typeof import("react-native").StyleSheet.create;
}

View file

@ -11,8 +11,10 @@ function onDispatch({ locale }: { locale: string }) {
// Theming
// Based on https://github.com/Aliucord/AliucordRN/blob/main/src/ui/patchTheme.ts
try {
if (ThemeManager) {
ThemeManager.overrideTheme(ThemeStore?.theme ?? "dark");
if (AMOLEDThemeManager && UnsyncedUserSettingsStore.useAMOLEDTheme === 2) AMOLEDThemeManager.setAMOLEDThemeEnabled(true);
}
} catch(e) {
logger.error("Failed to fix theme...", e);
}

View file

@ -1,5 +1,28 @@
import { find, findByProps, findByStoreName } from "@metro/filters";
import { DiscordStyleSheet } from "@types";
import { find, findByProps } from "@metro/filters";
import { ReactNative as RN } from "@lib/preinit";
import type { StyleSheet } from "react-native";
const ThemeStore = findByStoreName("ThemeStore");
const colorResolver = findByProps("colors", "meta").meta;
// Reimplementation of Discord's createThemedStyleSheet, which was removed since 204201
// Not exactly a 1:1 reimplementation, but sufficient to keep compatibility with existing plugins
function createThemedStyleSheet<T extends StyleSheet.NamedStyles<T>>(sheet: T) {
for (const key in sheet) {
// @ts-ignore
sheet[key] = new Proxy(RN.StyleSheet.flatten(sheet[key]), {
get(target, prop, receiver) {
const res = Reflect.get(target, prop, receiver);
return colorResolver.isSemanticColor(res)
? colorResolver.resolveSemanticColor(ThemeStore.theme, res)
: res
}
});
}
return sheet;
}
// Discord
export const constants = findByProps("Fonts", "Permissions");
@ -7,7 +30,14 @@ export const channels = findByProps("getVoiceChannelId");
export const i18n = findByProps("Messages");
export const url = findByProps("openURL", "openDeeplink");
export const toasts = find(m => m.open && m.close && !m.startDrag && !m.init && !m.openReplay && !m.setAlwaysOnTop);
export const stylesheet = findByProps("createThemedStyleSheet") as DiscordStyleSheet;
// Compatible with pre-204201 versions since createThemedStyleSheet is undefined.
export const stylesheet = {
...find(m => m.createStyles && !m.ActionSheet),
createThemedStyleSheet,
...findByProps("createThemedStyleSheet") as {},
} as DiscordStyleSheet;
export const clipboard = findByProps("setString", "getString", "hasString") as typeof import("@react-native-clipboard/clipboard").default;
export const assets = findByProps("registerAsset");
export const invites = findByProps("acceptInviteAndTransitionToInviteChannel");