CustomEmojiHelper: add a way to disable big emojis

main
Alibek Omarov 5 years ago
parent 978bc5ec44
commit beb9dd69e4
  1. 41
      app/src/main/java/com/keylesspalace/tusky/util/CustomEmojiHelper.kt
  2. 1
      app/src/main/res/values/husky.xml
  3. 6
      app/src/main/res/xml/preferences.xml

@ -43,7 +43,11 @@ import androidx.preference.PreferenceManager
* @param view a reference to the a view the emojis will be shown in (should be the TextView, but parents of the TextView are also acceptable) * @param view a reference to the a view the emojis will be shown in (should be the TextView, but parents of the TextView are also acceptable)
* @return the text with the shortcodes replaced by EmojiSpans * @return the text with the shortcodes replaced by EmojiSpans
*/ */
fun emojifyText(text: Spanned, emojis: List<Emoji>?, view: View) : Spanned { fun emojifyText(text: Spanned, emojis: List<Emoji>?, view: View, forceSmallEmoji: Boolean) : Spanned {
val pm = PreferenceManager.getDefaultSharedPreferences(view.context)
val smallEmojis = forceSmallEmoji || !pm.getBoolean("bigEmojis", true)
// val animatedEmojis = pm.getBoolean("animateEmojis", false)
if (emojis != null && emojis.isNotEmpty()) { if (emojis != null && emojis.isNotEmpty()) {
val builder = SpannableStringBuilder(text) val builder = SpannableStringBuilder(text)
for (emoji in emojis) { for (emoji in emojis) {
@ -51,7 +55,11 @@ fun emojifyText(text: Spanned, emojis: List<Emoji>?, view: View) : Spanned {
val matcher = Pattern.compile(pattern.toString(), Pattern.LITERAL) val matcher = Pattern.compile(pattern.toString(), Pattern.LITERAL)
.matcher(text) .matcher(text)
while(matcher.find()) { while(matcher.find()) {
val span = EmojiSpan(WeakReference<View>(view)) val span = if(smallEmojis) {
SmallEmojiSpan(WeakReference<View>(view))
} else {
EmojiSpan(WeakReference<View>(view))
}
builder.setSpan(span, matcher.start(), matcher.end(), 0); builder.setSpan(span, matcher.start(), matcher.end(), 0);
Glide.with(view) Glide.with(view)
.asBitmap() .asBitmap()
@ -66,11 +74,19 @@ fun emojifyText(text: Spanned, emojis: List<Emoji>?, view: View) : Spanned {
return text return text
} }
fun emojifyText(text: Spanned, emojis: List<Emoji>?, view: View) : Spanned {
return emojifyText(text, emojis, view, false)
}
fun emojifyString(string: String, emojis: List<Emoji>?, view: View, forceSmallEmoji: Boolean) : Spanned {
return emojifyText(SpannedString(string), emojis, view, forceSmallEmoji)
}
fun emojifyString(string: String, emojis: List<Emoji>?, view: View) : Spanned { fun emojifyString(string: String, emojis: List<Emoji>?, view: View) : Spanned {
return emojifyText(SpannedString(string), emojis, view) return emojifyString(string, emojis, view, false)
} }
public class EmojiSpan(val viewWeakReference: WeakReference<View>) : ReplacementSpan() { public open class EmojiSpan(val viewWeakReference: WeakReference<View>) : ReplacementSpan() {
var imageDrawable: Drawable? = null var imageDrawable: Drawable? = null
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?) : Int { override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?) : Int {
@ -118,3 +134,20 @@ public class EmojiSpan(val viewWeakReference: WeakReference<View>) : Replacement
} }
} }
} }
public class SmallEmojiSpan(viewWeakReference: WeakReference<View>)
: EmojiSpan(viewWeakReference) {
override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
if (fm != null) {
/* update FontMetricsInt or otherwise span does not get drawn when
* it covers the whole text */
val metrics = paint.fontMetricsInt
fm.top = metrics.top
fm.ascent = metrics.ascent
fm.descent = metrics.descent
fm.bottom = metrics.bottom
}
return paint.textSize.toInt()
}
}

@ -26,5 +26,6 @@
<string name="pref_title_default_formatting">Default formatting syntax(if supported by instance)</string> <string name="pref_title_default_formatting">Default formatting syntax(if supported by instance)</string>
<string name="pref_title_notification_filter_emoji">my posts are reacted with emojis</string> <string name="pref_title_notification_filter_emoji">my posts are reacted with emojis</string>
<string name="pref_title_hide_muted_users">Hide muted users</string> <string name="pref_title_hide_muted_users">Hide muted users</string>
<string name="pref_title_enable_big_emojis">Enable bigger custom emojis</string>
</resources> </resources>

@ -96,6 +96,12 @@
android:title="@string/pref_title_enable_swipe_for_tabs" android:title="@string/pref_title_enable_swipe_for_tabs"
app:singleLineTitle="false" /> app:singleLineTitle="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="bigEmojis"
android:title="@string/pref_title_enable_big_emojis"
app:singleLineTitle="false" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_title_browser_settings"> <PreferenceCategory android:title="@string/pref_title_browser_settings">

Loading…
Cancel
Save