mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
fix(editor): update tab width for long file names, and enable tab icons (#1736)
- use single MutableIntObjectMap for tab names and icons - code cleanup - add icons for editor tabs, tab names are truncated when icons are present - do not limit the width of tabs in TabLayout to allow long tab names
This commit is contained in:
@@ -24,8 +24,10 @@ import android.text.TextUtils
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.ViewGroup.LayoutParams
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.appcompat.view.menu.MenuBuilder
|
||||
import androidx.collection.MutableIntObjectMap
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import androidx.core.view.GravityCompat
|
||||
import com.blankj.utilcode.util.ImageUtils
|
||||
import com.itsaky.androidide.R.string
|
||||
@@ -44,6 +46,7 @@ import com.itsaky.androidide.editor.ui.IDEEditor
|
||||
import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent
|
||||
import com.itsaky.androidide.eventbus.events.file.FileRenameEvent
|
||||
import com.itsaky.androidide.interfaces.IEditorHandler
|
||||
import com.itsaky.androidide.models.FileExtension
|
||||
import com.itsaky.androidide.models.OpenedFile
|
||||
import com.itsaky.androidide.models.OpenedFilesCache
|
||||
import com.itsaky.androidide.models.Range
|
||||
@@ -669,7 +672,7 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
|
||||
editorActivityScope.launch {
|
||||
val files = editorViewModel.getOpenedFiles()
|
||||
val dupliCount = mutableMapOf<String, Int>()
|
||||
val names = MutableIntObjectMap<String>()
|
||||
val names = MutableIntObjectMap<Pair<String, @DrawableRes Int>>()
|
||||
val nameBuilder = UniqueNameBuilder<File>("", File.separator)
|
||||
|
||||
files.forEach {
|
||||
@@ -681,18 +684,21 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler {
|
||||
for (index in 0 until binding.tabs.tabCount) {
|
||||
val file = files.getOrNull(index) ?: continue
|
||||
val count = dupliCount[file.name] ?: 0
|
||||
|
||||
val isModified = getEditorAtIndex(index)?.isModified ?: false
|
||||
var name = if (count > 1) nameBuilder.getShortPath(file) else file.name
|
||||
if (isModified) {
|
||||
name = "*${name}"
|
||||
}
|
||||
|
||||
names[index] = name
|
||||
names[index] = name to FileExtension.Factory.forFile(file).icon
|
||||
}
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
names.forEach { index, name ->
|
||||
binding.tabs.getTabAt(index)?.text = name
|
||||
names.forEach { index, (name, iconId) ->
|
||||
val tab = binding.tabs.getTabAt(index) ?: return@forEach
|
||||
tab.icon = ResourcesCompat.getDrawable(resources, iconId, theme)
|
||||
tab.text = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import android.widget.LinearLayout;
|
||||
import androidx.transition.ChangeImageTransform;
|
||||
import androidx.transition.TransitionManager;
|
||||
|
||||
import com.blankj.utilcode.util.ImageUtils;
|
||||
import com.blankj.utilcode.util.SizeUtils;
|
||||
import com.itsaky.androidide.databinding.LayoutFiletreeItemBinding;
|
||||
import com.itsaky.androidide.models.FileExtension;
|
||||
@@ -91,19 +90,6 @@ public class FileTreeViewHolder extends TreeNode.BaseNodeViewHolder<File> {
|
||||
}
|
||||
|
||||
protected int getIconForFile(final File file) {
|
||||
|
||||
if (file.isDirectory()) {
|
||||
return R.drawable.ic_folder;
|
||||
}
|
||||
|
||||
if (ImageUtils.isImage(file)) {
|
||||
return R.drawable.ic_file_image;
|
||||
}
|
||||
|
||||
if ("gradlew".equals(file.getName()) || "gradlew.bat".equals(file.getName())) {
|
||||
return R.drawable.ic_terminal;
|
||||
}
|
||||
|
||||
return FileExtension.Factory.forFile(file).getIcon();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package com.itsaky.androidide.models
|
||||
|
||||
import androidx.annotation.DrawableRes
|
||||
import com.blankj.utilcode.util.ImageUtils
|
||||
import com.itsaky.androidide.resources.R
|
||||
import java.io.File
|
||||
|
||||
@@ -40,6 +41,10 @@ enum class FileExtension(val extension: String, @DrawableRes val icon: Int) {
|
||||
LOG("log", R.drawable.ic_file_txt),
|
||||
CPP("cpp", R.drawable.ic_language_cpp),
|
||||
H("h", R.drawable.ic_language_cpp),
|
||||
BAT("bat", R.drawable.ic_terminal),
|
||||
DIRECTORY("", R.drawable.ic_folder),
|
||||
IMAGE("", R.drawable.ic_file_image),
|
||||
GRADLEW("", R.drawable.ic_terminal),
|
||||
UNKNOWN("", R.drawable.ic_file_unknown);
|
||||
|
||||
/** Factory class for getting [FileExtension] instances. */
|
||||
@@ -49,17 +54,22 @@ enum class FileExtension(val extension: String, @DrawableRes val icon: Int) {
|
||||
/** Get [FileExtension] for the given file. */
|
||||
@JvmStatic
|
||||
fun forFile(file: File?): FileExtension {
|
||||
return forExtension(file?.extension)
|
||||
return if (file?.isDirectory == true) DIRECTORY
|
||||
else if (ImageUtils.isImage(file)) IMAGE
|
||||
else if ("gradlew" == file?.name) GRADLEW
|
||||
else forExtension(file?.extension)
|
||||
}
|
||||
|
||||
/** Get [FileExtension] for the given extension. */
|
||||
@JvmStatic
|
||||
fun forExtension(extension: String?): FileExtension {
|
||||
if (extension == null) {
|
||||
// To not assign IMAGE, GRADLEW and DIRECTORY in case of an empty extension,
|
||||
// we check if an extension is empty here
|
||||
if (extension.isNullOrEmpty()) {
|
||||
return UNKNOWN
|
||||
}
|
||||
|
||||
for (value in values()) {
|
||||
for (value in entries) {
|
||||
if (value.extension == extension) {
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -80,6 +80,10 @@
|
||||
<item name="tabIndicatorGravity">bottom</item>
|
||||
<item name="tabIndicatorHeight">3dp</item>
|
||||
<item name="tabMode">scrollable</item>
|
||||
<!-- Increase tab's width according to its title -->
|
||||
<item name="tabMaxWidth">0px</item>
|
||||
<!-- Make icons appear inline with tab's title -->
|
||||
<item name="tabInlineLabel">true</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.ShapeOverlay.Large" parent="">
|
||||
|
||||
Reference in New Issue
Block a user