1818package com.itsaky.androidide.activities.editor
1919
2020import android.content.ComponentName
21+ import android.content.res.Configuration
2122import android.content.Intent
2223import android.content.ServiceConnection
2324import android.graphics.Color
@@ -40,6 +41,8 @@ import android.view.MotionEvent
4041import android.view.View
4142import android.view.ViewGroup
4243import android.view.ViewTreeObserver.OnGlobalLayoutListener
44+ import android.widget.LinearLayout
45+ import android.widget.TextView
4346import androidx.activity.OnBackPressedCallback
4447import androidx.activity.result.ActivityResult
4548import androidx.activity.result.ActivityResultLauncher
@@ -596,6 +599,7 @@ abstract class BaseEditorActivity :
596599 }
597600
598601 setupToolbar()
602+ syncProjectToolbarRowForOrientation(resources.configuration.orientation)
599603 setupDrawers()
600604 content.tabs.addOnTabSelectedListener(this )
601605
@@ -631,6 +635,11 @@ abstract class BaseEditorActivity :
631635 setupGestureDetector()
632636 }
633637
638+ override fun onConfigurationChanged (newConfig : Configuration ) {
639+ super .onConfigurationChanged(newConfig)
640+ syncProjectToolbarRowForOrientation(newConfig.orientation)
641+ }
642+
634643 private fun setupToolbar () {
635644 // Set the project name in the title TextView
636645 content.root.findViewById< android.widget.TextView > (R .id.title_text)?.apply {
@@ -682,6 +691,88 @@ abstract class BaseEditorActivity :
682691 }
683692 }
684693
694+ private fun syncProjectToolbarRowForOrientation (currentOrientation : Int ) {
695+ val appBar = content.editorAppBarLayout
696+ val titleToolbar = content.titleToolbar
697+ val actionsToolbar = content.projectActionsToolbar
698+
699+ val titleParent = titleToolbar.parent as ? ViewGroup ? : return
700+ val actionsParent = actionsToolbar.parent as ? ViewGroup ? : return
701+ if (titleParent != actionsParent) return
702+
703+ val isLandscape = currentOrientation == Configuration .ORIENTATION_LANDSCAPE
704+
705+ if (isLandscape && titleParent == = appBar) {
706+ val insertAt =
707+ minOf(
708+ appBar.indexOfChild(titleToolbar),
709+ appBar.indexOfChild(actionsToolbar),
710+ ).coerceAtLeast(0 )
711+ val row =
712+ LinearLayout (this ).apply {
713+ orientation = LinearLayout .HORIZONTAL
714+ gravity = Gravity .CENTER_VERTICAL
715+ layoutParams =
716+ com.google.android.material.appbar.AppBarLayout .LayoutParams (
717+ ViewGroup .LayoutParams .MATCH_PARENT ,
718+ ViewGroup .LayoutParams .WRAP_CONTENT ,
719+ )
720+ }
721+
722+ appBar.removeView(titleToolbar)
723+ appBar.removeView(actionsToolbar)
724+
725+ titleToolbar.layoutParams =
726+ LinearLayout .LayoutParams (
727+ 0 ,
728+ ViewGroup .LayoutParams .WRAP_CONTENT ,
729+ 1f ,
730+ )
731+ actionsToolbar.layoutParams =
732+ LinearLayout .LayoutParams (
733+ ViewGroup .LayoutParams .WRAP_CONTENT ,
734+ ViewGroup .LayoutParams .WRAP_CONTENT ,
735+ ).apply { marginEnd = SizeUtils .dp2px(8f ) }
736+
737+ content.root.findViewById<TextView >(R .id.title_text)?.updateLayoutParams<ViewGroup .MarginLayoutParams > {
738+ marginEnd = SizeUtils .dp2px(8f )
739+ }
740+
741+ row.addView(titleToolbar)
742+ row.addView(actionsToolbar)
743+ appBar.addView(row, insertAt)
744+ return
745+ }
746+
747+ if (! isLandscape && titleParent is LinearLayout && titleParent.parent == = appBar) {
748+ val row = titleParent
749+ val insertAt = appBar.indexOfChild(row).coerceAtLeast(0 )
750+ row.removeView(titleToolbar)
751+ row.removeView(actionsToolbar)
752+ appBar.removeView(row)
753+
754+ titleToolbar.layoutParams =
755+ com.google.android.material.appbar.AppBarLayout .LayoutParams (
756+ ViewGroup .LayoutParams .MATCH_PARENT ,
757+ ViewGroup .LayoutParams .WRAP_CONTENT ,
758+ )
759+ actionsToolbar.layoutParams =
760+ com.google.android.material.appbar.AppBarLayout .LayoutParams (
761+ ViewGroup .LayoutParams .MATCH_PARENT ,
762+ ViewGroup .LayoutParams .WRAP_CONTENT ,
763+ ).apply {
764+ topMargin = SizeUtils .dp2px(4f )
765+ }
766+
767+ content.root.findViewById<TextView >(R .id.title_text)?.updateLayoutParams<ViewGroup .MarginLayoutParams > {
768+ marginEnd = SizeUtils .dp2px(16f )
769+ }
770+
771+ appBar.addView(titleToolbar, insertAt)
772+ appBar.addView(actionsToolbar, insertAt + 1 )
773+ }
774+ }
775+
685776 private fun onSwipeRevealDragProgress (progress : Float ) {
686777 _binding ?.apply {
687778 contentCard.progress = progress
0 commit comments