Skip to content

Commit

Permalink
Merge pull request #1300 from khalid-nasralla/PAINTROID-241
Browse files Browse the repository at this point in the history
PAINTROID-241 Layout errors in right-to-left languages
  • Loading branch information
Lenkomotive authored Mar 20, 2024
2 parents 5c64bdc + e472cbc commit 2c8a5fb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
12 changes: 12 additions & 0 deletions Paintroid/src/main/java/org/catrobat/paintroid/LanguageHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.catrobat.paintroid

import android.text.TextUtils
import android.view.View
import java.util.*

object LanguageHelper {
fun isCurrentLanguageRTL(): Boolean {
val layoutDirection = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())
return layoutDirection == View.LAYOUT_DIRECTION_RTL
}
}
26 changes: 25 additions & 1 deletion Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.core.widget.ContentLoadingProgressBar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -549,7 +550,7 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
private fun setLayoutDirection() {
var visibilityBtn = findViewById<ImageButton>(R.id.pocketpaint_layer_side_nav_button_visibility)
var layerNavigationView = findViewById<NavigationView>(R.id.pocketpaint_nav_view_layer)
if (resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
if (LanguageHelper.isCurrentLanguageRTL()) {
visibilityBtn.setBackgroundResource(R.drawable.rounded_corner_top_rtl)
layerNavigationView.setBackgroundResource(R.drawable.layer_nav_view_background_rtl)
} else {
Expand Down Expand Up @@ -587,9 +588,32 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
TooltipCompat.setTooltipText(topBar.redoButton, context.getString(R.string.button_redo))
}

private fun mirrorUndoAndRedoButtonsForRtlLanguage() {
val undoButton: ImageButton = findViewById(R.id.pocketpaint_btn_top_undo)
val undoDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pocketpaint_undo)

val redoButton: ImageButton = findViewById(R.id.pocketpaint_btn_top_redo)
val redoDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pocketpaint_redo)

undoDrawable?.let {
it.isAutoMirrored = true
undoButton.setImageDrawable(it)
}

redoDrawable?.let {
it.isAutoMirrored = true
redoButton.setImageDrawable(it)
}
}

private fun setTopBarListeners(topBar: TopBarViewHolder) {
topBar.undoButton.setOnClickListener { presenterMain.undoClicked() }
topBar.redoButton.setOnClickListener { presenterMain.redoClicked() }

if (LanguageHelper.isCurrentLanguageRTL()) {
mirrorUndoAndRedoButtonsForRtlLanguage()
}

topBar.checkmarkButton.setOnClickListener {
if (toolReference.tool?.toolType?.name.equals(ToolType.TRANSFORM.name)) {
(toolReference.tool as TransformTool).checkMarkClicked = true
Expand Down
56 changes: 29 additions & 27 deletions Paintroid/src/main/java/org/catrobat/paintroid/WelcomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.catrobat.paintroid

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
Expand All @@ -39,7 +38,6 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
import org.catrobat.paintroid.common.RESULT_INTRO_MW_NOT_SUPPORTED
import org.catrobat.paintroid.intro.IntroPageViewAdapter
import org.catrobat.paintroid.tools.ToolType
import java.util.Locale

private const val DEFAULT_TEXT_SIZE = 30f

Expand Down Expand Up @@ -106,17 +104,35 @@ class WelcomeActivity : AppCompatActivity() {
}
}

private fun mirrorUndoAndRedoButtonsForRtlLanguage(undoButton: AppCompatImageButton, redoButton: AppCompatImageButton) {
val undoDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pocketpaint_undo)
val redoDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pocketpaint_redo)

undoDrawable?.let {
it.isAutoMirrored = true
undoButton.setImageDrawable(it)
}

redoDrawable?.let {
it.isAutoMirrored = true
redoButton.setImageDrawable(it)
}
}

private fun setUpUndoAndRedoButtons(head: AppCompatTextView, description: AppCompatTextView) {
val topBar: AppBarLayout =
findViewById(R.id.pocketpaint_intro_possibilities_topbar)
val undo: AppCompatImageButton =
topBar.findViewById(R.id.pocketpaint_btn_top_undo)
val redo: AppCompatImageButton =
topBar.findViewById(R.id.pocketpaint_btn_top_redo)
val topBar: AppBarLayout = findViewById(R.id.pocketpaint_intro_possibilities_topbar)
val undo: AppCompatImageButton = topBar.findViewById(R.id.pocketpaint_btn_top_undo)
val redo: AppCompatImageButton = topBar.findViewById(R.id.pocketpaint_btn_top_redo)

if (LanguageHelper.isCurrentLanguageRTL()) {
mirrorUndoAndRedoButtonsForRtlLanguage(undo, redo)
}

undo.setOnClickListener {
head.setText(ToolType.UNDO.nameResource)
description.setText(ToolType.UNDO.helpTextResource)
}

redo.setOnClickListener {
head.setText(ToolType.REDO.nameResource)
description.setText(ToolType.REDO.helpTextResource)
Expand Down Expand Up @@ -158,6 +174,7 @@ class WelcomeActivity : AppCompatActivity() {
finish()
return
}

setContentView(R.layout.activity_pocketpaint_welcome)
viewPager = findViewById(R.id.pocketpaint_view_pager)
dotsLayout = findViewById(R.id.pocketpaint_layout_dots)
Expand All @@ -179,7 +196,7 @@ class WelcomeActivity : AppCompatActivity() {
var finished: Boolean
var current = getItem(1)
finished = current > layouts.size - 1
if (isRTL(this@WelcomeActivity)) {
if (LanguageHelper.isCurrentLanguageRTL()) {
current = getItem(-1)
finished = current < 0
}
Expand All @@ -192,12 +209,12 @@ class WelcomeActivity : AppCompatActivity() {
}

private fun initViewPager() {
if (isRTL(this)) {
if (LanguageHelper.isCurrentLanguageRTL()) {
layouts.reverse()
}
viewPager.adapter = IntroPageViewAdapter(layouts)
viewPager.addOnPageChangeListener(viewPagerPageChangeListener)
if (isRTL(this)) {
if (LanguageHelper.isCurrentLanguageRTL()) {
val pos = layouts.size
viewPager.currentItem = pos
addBottomDots(layouts.size - 1)
Expand Down Expand Up @@ -232,23 +249,8 @@ class WelcomeActivity : AppCompatActivity() {
}
}

private fun defaultLocaleIsRTL(): Boolean {
val locale = Locale.getDefault()
if (locale.toString().isEmpty()) {
return false
}
val directionality = Character.getDirectionality(locale.displayName[0]).toInt()
return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT.toInt() || directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC.toInt()
}

private fun isRTL(context: Context): Boolean {
val layoutDirection = context.resources.configuration.layoutDirection
val layoutDirectionIsRTL = layoutDirection == View.LAYOUT_DIRECTION_RTL
return layoutDirectionIsRTL || defaultLocaleIsRTL()
}

private fun getDotsIndex(position: Int): Int =
if (isRTL(this)) layouts.size - position - 1 else position
if (LanguageHelper.isCurrentLanguageRTL()) layouts.size - position - 1 else position

override fun onBackPressed() {
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.catrobat.paintroid.LanguageHelper
import org.catrobat.paintroid.MainActivity
import org.catrobat.paintroid.R
import org.catrobat.paintroid.contract.LayerContracts
Expand Down Expand Up @@ -264,21 +265,20 @@ class LayerAdapter(
}

private fun getRadius(backgroundType: BackgroundType): FloatArray {
val isRTL = mainActivity?.resources?.configuration?.layoutDirection == View.LAYOUT_DIRECTION_RTL
val cornerRadius = mainActivity?.let { CORNER_RADIUS * it.resources.displayMetrics.density } ?: 0f

return when (backgroundType) {
BackgroundType.TOP -> if (isRTL) {
BackgroundType.TOP -> if (LanguageHelper.isCurrentLanguageRTL()) {
floatArrayOf(0f, 0f, cornerRadius, cornerRadius, 0f, 0f, 0f, 0f)
} else {
floatArrayOf(cornerRadius, cornerRadius, 0f, 0f, 0f, 0f, 0f, 0f)
}
BackgroundType.BOTTOM -> if (isRTL) {
BackgroundType.BOTTOM -> if (LanguageHelper.isCurrentLanguageRTL()) {
floatArrayOf(0f, 0f, 0f, 0f, cornerRadius, cornerRadius, 0f, 0f)
} else {
floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, cornerRadius, cornerRadius)
}
BackgroundType.SINGLE -> if (isRTL) {
BackgroundType.SINGLE -> if (LanguageHelper.isCurrentLanguageRTL()) {
floatArrayOf(0f, 0f, cornerRadius, cornerRadius, cornerRadius, cornerRadius, 0f, 0f)
} else {
floatArrayOf(cornerRadius, cornerRadius, 0f, 0f, 0f, 0f, cornerRadius, cornerRadius)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_margin="10dp"
android:layout_marginTop="@dimen/toolbar_height"
tools:ignore="RelativeOverlap,RtlHardcoded">
Expand Down
4 changes: 2 additions & 2 deletions Paintroid/src/main/res/layout/pocketpaint_layout_top_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
android:contentDescription="@string/button_checkmark"
android:src="@drawable/ic_pocketpaint_plus_enabled"
android:tint="@android:color/white"
android:visibility="gone"/>
android:visibility="gone" />

<ImageButton
android:id="@+id/pocketpaint_btn_top_checkmark"
Expand All @@ -77,7 +77,7 @@
android:contentDescription="@string/button_checkmark"
android:src="@drawable/ic_pocketpaint_checkmark"
android:tint="@android:color/white"
android:visibility="gone"/>
android:visibility="gone" />

</LinearLayout>
</androidx.appcompat.widget.Toolbar>
Expand Down

0 comments on commit 2c8a5fb

Please sign in to comment.