@@ -17,6 +17,8 @@ import android.widget.FrameLayout
1717import android.widget.TextView
1818import androidx.annotation.ColorInt
1919import androidx.annotation.StringRes
20+ import androidx.core.content.withStyledAttributes
21+ import androidx.core.view.isVisible
2022import com.otaliastudios.zoom.*
2123import de.markusressel.kodeeditor.library.R
2224import de.markusressel.kodeeditor.library.extensions.createSnapshot
@@ -43,9 +45,9 @@ import kotlin.math.roundToInt
4345open class CodeEditorLayout
4446@JvmOverloads
4547constructor (
46- context: Context ,
47- attrs: AttributeSet ? = null ,
48- defStyleAttr: Int = 0 )
48+ context: Context ,
49+ attrs: AttributeSet ? = null ,
50+ defStyleAttr: Int = 0 )
4951 : FrameLayout (context, attrs, defStyleAttr) {
5052
5153 /* *
@@ -58,7 +60,7 @@ constructor(
5860 **/
5961 private val isEngineInitialized: Boolean
6062 get() = codeEditorView.engine.computeVerticalScrollRange() > 0
61- && codeEditorView.engine.computeHorizontalScrollRange() > 0
63+ && codeEditorView.engine.computeHorizontalScrollRange() > 0
6264
6365 /* *
6466 * The view displaying line numbers.
@@ -159,7 +161,7 @@ constructor(
159161 set(value) {
160162 dividerView.visibility = if (value) View .VISIBLE else View .GONE
161163 }
162- get() = dividerView.visibility == View . VISIBLE
164+ get() = dividerView.isVisible
163165
164166 /* *
165167 * Indicates if the minimap should be shown or not.
@@ -266,57 +268,57 @@ constructor(
266268 }
267269
268270 private fun readParameters (attrs : AttributeSet ? , defStyleAttr : Int ) {
269- val a = context.obtainStyledAttributes (attrs, R .styleable.CodeEditorLayout , defStyleAttr, 0 )
271+ context.withStyledAttributes (attrs, R .styleable.CodeEditorLayout , defStyleAttr, 0 ) {
270272
271- val lineNumberTextColor = a. getColor(context,
273+ val lineNumberTextColor = getColor(context,
272274 defaultColor = Color .BLACK ,
273275 styleableRes = R .styleable.CodeEditorLayout_ke_lineNumbers_textColor ,
274276 attr = intArrayOf(R .attr.ke_lineNumbers_textColor,
275- android.R .attr.textColorPrimary))
276- lineNumberTextView.setTextColor(lineNumberTextColor)
277+ android.R .attr.textColorPrimary))
278+ lineNumberTextView.setTextColor(lineNumberTextColor)
277279
278- val lineNumberBackgroundColor = a. getColor(context,
280+ val lineNumberBackgroundColor = getColor(context,
279281 defaultColor = Color .WHITE ,
280282 styleableRes = R .styleable.CodeEditorLayout_ke_lineNumbers_backgroundColor ,
281283 attr = intArrayOf(R .attr.ke_lineNumbers_backgroundColor,
282- android.R .attr.windowBackground))
283- lineNumberZoomLayout.setBackgroundColor(lineNumberBackgroundColor)
284+ android.R .attr.windowBackground))
285+ lineNumberZoomLayout.setBackgroundColor(lineNumberBackgroundColor)
284286
285287
286- showDivider = a. getBoolean(R .styleable.CodeEditorLayout_ke_divider_enabled , DEFAULT_SHOW_DIVIDER )
288+ showDivider = getBoolean(R .styleable.CodeEditorLayout_ke_divider_enabled , DEFAULT_SHOW_DIVIDER )
287289
288- val dividerColor = a. getColor(context,
290+ val dividerColor = getColor(context,
289291 defaultColor = Color .BLACK ,
290292 styleableRes = R .styleable.CodeEditorLayout_ke_divider_color ,
291293 attr = intArrayOf(R .attr.ke_divider_color,
292- android.R .attr.textColorPrimary))
293- dividerView.setBackgroundColor(dividerColor)
294+ android.R .attr.textColorPrimary))
295+ dividerView.setBackgroundColor(dividerColor)
294296
295- editorBackgroundColor = a. getColor(context,
297+ editorBackgroundColor = getColor(context,
296298 defaultColor = Color .WHITE ,
297299 styleableRes = R .styleable.CodeEditorLayout_ke_editor_backgroundColor ,
298300 attr = intArrayOf(R .attr.ke_editor_backgroundColor,
299- android.R .attr.windowBackground))
300- codeEditorView.setBackgroundColor(editorBackgroundColor)
301- isMoveWithCursorEnabled = a. getBoolean(R .styleable.CodeEditorLayout_ke_editor_followCursor , true )
301+ android.R .attr.windowBackground))
302+ codeEditorView.setBackgroundColor(editorBackgroundColor)
303+ isMoveWithCursorEnabled = getBoolean(R .styleable.CodeEditorLayout_ke_editor_followCursor , true )
302304
303- val codeEditorMaxZoom = a. getFloat(R .styleable.CodeEditorLayout_ke_editor_maxZoom , CodeEditorView .DEFAULT_MAX_ZOOM )
304- lineNumberZoomLayout.setMaxZoom(codeEditorMaxZoom, ZoomApi .TYPE_REAL_ZOOM )
305- codeEditorView.setMaxZoom(codeEditorMaxZoom, ZoomApi .TYPE_REAL_ZOOM )
305+ val codeEditorMaxZoom = getFloat(R .styleable.CodeEditorLayout_ke_editor_maxZoom , CodeEditorView .DEFAULT_MAX_ZOOM )
306+ lineNumberZoomLayout.setMaxZoom(codeEditorMaxZoom, ZoomApi .TYPE_REAL_ZOOM )
307+ codeEditorView.setMaxZoom(codeEditorMaxZoom, ZoomApi .TYPE_REAL_ZOOM )
306308
307- showMinimap = a. getBoolean(R .styleable.CodeEditorLayout_ke_minimap_enabled , DEFAULT_SHOW_MINIMAP )
308- minimapMaxDimension = a. getDimensionPixelSize(R .styleable.CodeEditorLayout_ke_minimap_maxDimension , DEFAULT_MINIMAP_MAX_DIMENSION_DP ).toFloat()
309- minimapBorderColor = a. getColor(context,
309+ showMinimap = getBoolean(R .styleable.CodeEditorLayout_ke_minimap_enabled , DEFAULT_SHOW_MINIMAP )
310+ minimapMaxDimension = getDimensionPixelSize(R .styleable.CodeEditorLayout_ke_minimap_maxDimension , DEFAULT_MINIMAP_MAX_DIMENSION_DP ).toFloat()
311+ minimapBorderColor = getColor(context,
310312 defaultColor = Color .BLACK ,
311313 styleableRes = R .styleable.CodeEditorLayout_ke_minimap_borderColor ,
312314 attr = intArrayOf(R .attr.ke_minimap_borderColor))
313315
314- minimapIndicatorColor = a. getColor(context,
316+ minimapIndicatorColor = getColor(context,
315317 defaultColor = Color .RED ,
316318 styleableRes = R .styleable.CodeEditorLayout_ke_minimap_indicatorColor ,
317319 attr = intArrayOf(R .attr.ke_minimap_indicatorColor))
318320
319- a.recycle()
321+ }
320322 }
321323
322324 private fun setListeners () {
@@ -357,6 +359,7 @@ constructor(
357359 moveEditorToPercentage(percentageX, percentageY)
358360 true
359361 }
362+
360363 else -> false
361364 }
362365 }
@@ -388,17 +391,17 @@ constructor(
388391 }
389392
390393 codeEditorView.codeEditText.textChanges()
391- .debounce(50 )
392- .onEach {
393- try {
394- updateLineNumbers()
395- } catch (e: Throwable ) {
396- Log .e(CodeEditorView .TAG , " Error updating line numbers" , e)
397- }
394+ .debounce(50 )
395+ .onEach {
396+ try {
397+ updateLineNumbers()
398+ } catch (e: Throwable ) {
399+ Log .e(CodeEditorView .TAG , " Error updating line numbers" , e)
398400 }
399- .catch {
400- Log .e(CodeEditorView .TAG , " Unrecoverable error while updating line numbers" , it)
401- }.launchIn(CoroutineScope (Job () + Dispatchers .Main ))
401+ }
402+ .catch {
403+ Log .e(CodeEditorView .TAG , " Unrecoverable error while updating line numbers" , it)
404+ }.launchIn(CoroutineScope (Job () + Dispatchers .Main ))
402405 }
403406
404407 /* *
@@ -435,8 +438,8 @@ constructor(
435438 targetView.apply {
436439 post {
437440 createSnapshot(
438- dimensionLimit = minimapMaxDimension,
439- backgroundColor = editorBackgroundColor
441+ dimensionLimit = minimapMaxDimension,
442+ backgroundColor = editorBackgroundColor
440443 )?.let {
441444 minimapZoomLayout.setImageBitmap(it)
442445 }
@@ -475,9 +478,9 @@ constructor(
475478 // update minimap indicator position and size
476479 (minimapIndicator.layoutParams as MarginLayoutParams ).apply {
477480 topMargin = ((minimapZoomLayout.height *
478- (engine.computeVerticalScrollOffset().toFloat() / engine.computeVerticalScrollRange()))).roundToInt()
481+ (engine.computeVerticalScrollOffset().toFloat() / engine.computeVerticalScrollRange()))).roundToInt()
479482 leftMargin = ((minimapZoomLayout.width *
480- (engine.computeHorizontalScrollOffset().toFloat() / engine.computeHorizontalScrollRange()))).roundToInt()
483+ (engine.computeHorizontalScrollOffset().toFloat() / engine.computeHorizontalScrollRange()))).roundToInt()
481484
482485 width = (minimapZoomLayout.width * (editorRect.width().toFloat() / engine.computeHorizontalScrollRange())).roundToInt()
483486 height = (minimapZoomLayout.height * (editorRect.height().toFloat() / engine.computeVerticalScrollRange())).roundToInt()
@@ -510,10 +513,10 @@ constructor(
510513
511514 // synchronize zoom and vertical pan to match code editor
512515 lineNumberZoomLayout.moveTo(
513- engine.zoom,
514- - engine.computeHorizontalScrollRange().toFloat(),
515- engine.panY,
516- false )
516+ engine.zoom,
517+ - engine.computeHorizontalScrollRange().toFloat(),
518+ engine.panY,
519+ false )
517520 }
518521
519522 /* *
@@ -568,19 +571,23 @@ constructor(
568571 cursorPosition.x < targetArea.left -> {
569572 codeEditorView.panX + (targetArea.left - cursorPosition.x) / codeEditorView.realZoom
570573 }
574+
571575 cursorPosition.x > targetArea.right -> {
572576 codeEditorView.panX + (targetArea.right - cursorPosition.x) / codeEditorView.realZoom
573577 }
578+
574579 else -> codeEditorView.panX
575580 }
576581
577582 val newY = when {
578583 cursorPosition.y < targetArea.top -> {
579584 codeEditorView.panY + (targetArea.top - cursorPosition.y) / codeEditorView.realZoom
580585 }
586+
581587 cursorPosition.y > targetArea.bottom -> {
582588 codeEditorView.panY + (targetArea.bottom - cursorPosition.y) / codeEditorView.realZoom
583589 }
590+
584591 else -> codeEditorView.panY
585592 }
586593
@@ -608,7 +615,7 @@ constructor(
608615 val y = (baseline + ascent).toFloat()
609616
610617 return PointF ((x + codeEditorView.panX) * codeEditorView.realZoom,
611- (y + codeEditorView.panY) * codeEditorView.realZoom)
618+ (y + codeEditorView.panY) * codeEditorView.realZoom)
612619 }
613620
614621 companion object {
0 commit comments