@@ -82,7 +82,6 @@ class TextEditorActivity : AppCompatActivity() {
8282 private var isSaving = false
8383 private var noWrapContainer: HorizontalScrollView ? = null
8484 private var mainListBaseBottomMargin = 0
85- private var fabBaseBottomMargin = 0
8685 private val progressBarDialog by lazy { ProgressBarDialog (this ) }
8786
8887 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -116,7 +115,6 @@ class TextEditorActivity : AppCompatActivity() {
116115 title = extraTitle.ifEmpty { File (absoluteFilePath).name }
117116
118117 applyWrapState()
119- setupFab()
120118 setupCursorAutoScroll()
121119 loadFileContent()
122120 }
@@ -128,16 +126,14 @@ class TextEditorActivity : AppCompatActivity() {
128126 *
129127 * Dùng translationY (chỉ dịch hình ảnh) từng gây lỗi không kéo lên đầu được, vì vùng chạm/
130128 * cuộn vẫn được tính theo kích thước View cũ (translation không đổi kích thước/measurement
131- * thật). Ở đây đổi sang tăng bottomMargin thật của vùng nội dung và FAB — tương đương
132- * cách adjustResize thật sự hoạt động — nên kích thước, vùng cuộn và vùng chạm đều được
133- * tính lại chính xác theo không gian còn trống phía trên bàn phím. Có thêm một khoảng đệm
134- * dư ra để văn bản/con trỏ không dính sát vào mép bàn phím.
129+ * thật). Ở đây đổi sang tăng bottomMargin thật của vùng nội dung — tương đương cách
130+ * adjustResize thật sự hoạt động — nên kích thước, vùng cuộn và vùng chạm đều được tính lại
131+ * chính xác theo không gian còn trống phía trên bàn phím. Có thêm một khoảng đệm dư ra để
132+ * văn bản/con trỏ không dính sát vào mép bàn phím.
135133 */
136134 private fun setupKeyboardInsets () {
137135 val mainListParams = binding.mainList.layoutParams as ViewGroup .MarginLayoutParams
138136 mainListBaseBottomMargin = mainListParams.bottomMargin
139- val fabParams = binding.editorFabRun.layoutParams as ViewGroup .MarginLayoutParams
140- fabBaseBottomMargin = fabParams.bottomMargin
141137 val extraGapPx = (24 * resources.displayMetrics.density).toInt()
142138
143139 ViewCompat .setOnApplyWindowInsetsListener(binding.root) { _, insets ->
@@ -151,10 +147,6 @@ class TextEditorActivity : AppCompatActivity() {
151147 mlp.bottomMargin = mainListBaseBottomMargin + keyboardHeight + extraGap
152148 binding.mainList.layoutParams = mlp
153149
154- val flp = binding.editorFabRun.layoutParams as ViewGroup .MarginLayoutParams
155- flp.bottomMargin = fabBaseBottomMargin + keyboardHeight + extraGap
156- binding.editorFabRun.layoutParams = flp
157-
158150 if (keyboardVisible) {
159151 binding.mainList.post { scrollToCursor() }
160152 }
@@ -206,19 +198,7 @@ class TextEditorActivity : AppCompatActivity() {
206198 return if (dotIndex >= 0 && dotIndex < name.length - 1 ) name.substring(dotIndex + 1 ).lowercase() else " "
207199 }
208200
209- private fun setupFab () {
210- val interpreter = RUNNABLE_EXTENSIONS [fileExtension()]
211- binding.editorFabRun.visibility = View .VISIBLE
212- if (interpreter != null ) {
213- binding.editorFabRun.contentDescription = getString(R .string.editor_run_test)
214- binding.editorFabRun.setImageResource(R .drawable.kr_run)
215- binding.editorFabRun.setOnClickListener { saveAndRun(interpreter) }
216- } else {
217- binding.editorFabRun.contentDescription = getString(R .string.editor_save)
218- binding.editorFabRun.setImageResource(android.R .drawable.ic_menu_save)
219- binding.editorFabRun.setOnClickListener { saveFile() }
220- }
221- }
201+ private fun runnableInterpreter (): String? = RUNNABLE_EXTENSIONS [fileExtension()]
222202
223203 private fun loadFileContent () {
224204 progressBarDialog.showDialog(getString(R .string.please_wait))
@@ -321,11 +301,16 @@ class TextEditorActivity : AppCompatActivity() {
321301 override fun onCreateOptionsMenu (menu : Menu ): Boolean {
322302 menuInflater.inflate(R .menu.menu_text_editor, menu)
323303 menu.findItem(R .id.editor_menu_wrap)?.isChecked = wrapEnabled
304+ menu.findItem(R .id.editor_menu_run)?.isVisible = runnableInterpreter() != null
324305 return true
325306 }
326307
327308 override fun onOptionsItemSelected (item : MenuItem ): Boolean {
328309 return when (item.itemId) {
310+ R .id.editor_menu_run -> {
311+ runnableInterpreter()?.let { saveAndRun(it) }
312+ true
313+ }
329314 R .id.editor_menu_save -> {
330315 saveFile()
331316 true
@@ -441,4 +426,4 @@ class TextEditorActivity : AppCompatActivity() {
441426 dialog.isCancelable = false
442427 dialog.show(supportFragmentManager, " editor-run-test" )
443428 }
444- }
429+ }
0 commit comments