π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
fix latex fallback
Prateek Sunalff593f70033b
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/MathViewCompat.java b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/MathViewCompat.javaindex b875eb78ae..29ab110c4a 100644--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/MathViewCompat.java+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/MathViewCompat.java@@ -1,12 +1,72 @@package io.ente.ensu.chat;import com.agog.mathdisplay.MTMathView;+import java.lang.reflect.Field;+import java.lang.reflect.Method;final class MathViewCompat {+ private static final Method DISPLAY_ERROR_INLINE_METHOD = lookupMethod("setDisplayErrorInline", boolean.class);+ private static final Method GET_ERROR_METHOD = lookupMethod("getError");+ private static final Method GET_ERROR_MESSAGE_METHOD = lookupMethod("getErrorMessage");+ private static final Field ERROR_FIELD = lookupField("error");+private MathViewCompat() {}static void setMathTextAlignment(MTMathView view, MTMathView.MTTextAlignment alignment) {view.setTextAlignment(alignment);}++ static void setDisplayErrorInline(MTMathView view, boolean displayInline) {+ if (DISPLAY_ERROR_INLINE_METHOD == null) {+ return;+ }+ try {+ DISPLAY_ERROR_INLINE_METHOD.invoke(view, displayInline);+ } catch (Exception ignored) {+ }+ }++ static String getError(MTMathView view) {+ Object error = invokeIfPresent(GET_ERROR_METHOD, view);+ if (error == null) {+ error = invokeIfPresent(GET_ERROR_MESSAGE_METHOD, view);+ }+ if (error == null && ERROR_FIELD != null) {+ try {+ error = ERROR_FIELD.get(view);+ } catch (Exception ignored) {+ }+ }+ return error == null ? null : error.toString();+ }++ private static Object invokeIfPresent(Method method, MTMathView view) {+ if (method == null) {+ return null;+ }+ try {+ return method.invoke(view);+ } catch (Exception ignored) {+ return null;+ }+ }++ private static Method lookupMethod(String name, Class<?>... params) {+ try {+ return MTMathView.class.getMethod(name, params);+ } catch (Exception ignored) {+ return null;+ }+ }++ private static Field lookupField(String name) {+ try {+ Field field = MTMathView.class.getDeclaredField(name);+ field.setAccessible(true);+ return field;+ } catch (Exception ignored) {+ return null;+ }+ }}diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/LaTeXView.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/LaTeXView.swiftindex 932dfd78ef..31095097c0 100644--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/LaTeXView.swift+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/LaTeXView.swift@@ -11,6 +11,7 @@ struct LaTeXView: View {let sanitizedLatex = latex.replacingOccurrences(of: "\\boxed", with: "")MathLabelView(latex: sanitizedLatex,+ rawLatex: latex,textColor: colorScheme == .dark? PlatformColor(hex: "#E8E4DF"): PlatformColor(hex: "#1A1A1A")@@ -21,41 +22,129 @@ struct LaTeXView: View {#if os(iOS)private struct MathLabelView: UIViewRepresentable {let latex: String+ let rawLatex: Stringlet textColor: PlatformColor- func makeUIView(context: Context) -> MTMathUILabel {- let label = MTMathUILabel()- label.fontSize = 16- label.textColor = textColor- label.contentInsets = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)- label.latex = latex- return label+ func makeUIView(context: Context) -> MathLabelContainerView {+ MathLabelContainerView()}- func updateUIView(_ label: MTMathUILabel, context: Context) {- label.latex = latex- label.textColor = textColor- label.contentInsets = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)+ func updateUIView(_ view: MathLabelContainerView, context: Context) {+ view.update(latex: latex, rawLatex: rawLatex, textColor: textColor)}}++private final class MathLabelContainerView: UIView {+ private let mathLabel = MTMathUILabel()+ private let fallbackLabel = UILabel()+ private let insets = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)++ override init(frame: CGRect) {+ super.init(frame: frame)++ mathLabel.translatesAutoresizingMaskIntoConstraints = false+ fallbackLabel.translatesAutoresizingMaskIntoConstraints = false+ fallbackLabel.numberOfLines = 0++ addSubview(mathLabel)+ addSubview(fallbackLabel)++ NSLayoutConstraint.activate([+ mathLabel.topAnchor.constraint(equalTo: topAnchor, constant: insets.top),+ mathLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left),+ mathLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -insets.right),+ mathLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -insets.bottom),+ fallbackLabel.topAnchor.constraint(equalTo: topAnchor, constant: insets.top),+ fallbackLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left),+ fallbackLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -insets.right),+ fallbackLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -insets.bottom),+ ])+ }++ required init?(coder: NSCoder) {+ fatalError("init(coder:) has not been implemented")+ }++ func update(latex: String, rawLatex: String, textColor: PlatformColor) {+ mathLabel.fontSize = 16+ mathLabel.textColor = textColor+ mathLabel.contentInsets = insets+ mathLabel.displayErrorInline = false+ mathLabel.latex = latex++ fallbackLabel.font = UIFont.systemFont(ofSize: 16)+ fallbackLabel.textColor = textColor+ fallbackLabel.text = rawLatex++ let trimmedLatex = latex.trimmingCharacters(in: .whitespacesAndNewlines)+ let shouldFallback = trimmedLatex.isEmpty || mathLabel.error != nil+ mathLabel.isHidden = shouldFallback+ fallbackLabel.isHidden = !shouldFallback+ }+}+#elseif os(macOS)private struct MathLabelView: NSViewRepresentable {let latex: String+ let rawLatex: Stringlet textColor: PlatformColor- func makeNSView(context: Context) -> MTMathUILabel {- let label = MTMathUILabel()- label.fontSize = 16- label.textColor = textColor- label.contentInsets = NSEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)- label.latex = latex- return label+ func makeNSView(context: Context) -> MathLabelContainerView {+ MathLabelContainerView()}- func updateNSView(_ label: MTMathUILabel, context: Context) {- label.latex = latex- label.textColor = textColor- label.contentInsets = NSEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)+ func updateNSView(_ view: MathLabelContainerView, context: Context) {+ view.update(latex: latex, rawLatex: rawLatex, textColor: textColor)+ }+}++private final class MathLabelContainerView: NSView {+ private let mathLabel = MTMathUILabel()+ private let fallbackLabel = NSTextField(labelWithString: "")+ private let insets = NSEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)++ override init(frame frameRect: NSRect) {+ super.init(frame: frameRect)++ mathLabel.translatesAutoresizingMaskIntoConstraints = false+ fallbackLabel.translatesAutoresizingMaskIntoConstraints = false+ fallbackLabel.lineBreakMode = .byWordWrapping+ fallbackLabel.maximumNumberOfLines = 0++ addSubview(mathLabel)+ addSubview(fallbackLabel)++ NSLayoutConstraint.activate([+ mathLabel.topAnchor.constraint(equalTo: topAnchor, constant: insets.top),+ mathLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left),+ mathLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -insets.right),+ mathLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -insets.bottom),+ fallbackLabel.topAnchor.constraint(equalTo: topAnchor, constant: insets.top),+ fallbackLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left),+ fallbackLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -insets.right),+ fallbackLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -insets.bottom),+ ])+ }++ required init?(coder: NSCoder) {+ fatalError("init(coder:) has not been implemented")+ }++ func update(latex: String, rawLatex: String, textColor: PlatformColor) {+ mathLabel.fontSize = 16+ mathLabel.textColor = textColor+ mathLabel.contentInsets = insets+ mathLabel.displayErrorInline = false+ mathLabel.latex = latex++ fallbackLabel.font = NSFont.systemFont(ofSize: 16)+ fallbackLabel.textColor = textColor+ fallbackLabel.stringValue = rawLatex++ let trimmedLatex = latex.trimmingCharacters(in: .whitespacesAndNewlines)+ let shouldFallback = trimmedLatex.isEmpty || mathLabel.error != nil+ mathLabel.isHidden = shouldFallback+ fallbackLabel.isHidden = !shouldFallback}}#endif