1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 13:57:10 +09:00

Editor | Enhancement | Default container for decorations (#2352)

This commit is contained in:
Evgenii Kozlov 2022-06-16 12:21:28 +03:00 committed by GitHub
parent d0aaf1345e
commit b69bb21f0d
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 1226 additions and 3 deletions

View file

@ -0,0 +1,40 @@
package com.anytypeio.anytype.core_ui.features.editor.decoration
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import com.anytypeio.anytype.core_ui.R
interface DecorationWidget {
class Background @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
background: Int
) : View(context, attrs), DecorationWidget {
init {
setBackgroundColor(background)
}
}
class Highlight @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : FrameLayout(context, attrs), DecorationWidget {
init {
val lp = LayoutParams(
resources.getDimensionPixelSize(R.dimen.highlight_line_width),
LayoutParams.MATCH_PARENT
).apply {
gravity = Gravity.CENTER
}
val line = View(context).apply {
setBackgroundResource(R.color.block_higlight_divider)
}
addView(line, lp)
}
}
}

View file

@ -0,0 +1,160 @@
package com.anytypeio.anytype.core_ui.features.editor.decoration
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.updateLayoutParams
import com.anytypeio.anytype.core_ui.R
import com.anytypeio.anytype.core_ui.extensions.veryLight
import com.anytypeio.anytype.presentation.editor.editor.ThemeColor
import com.anytypeio.anytype.presentation.editor.editor.model.BlockView
class EditorDecorationContainer @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
private val graphicOffsetValue =
resources.getDimension(R.dimen.default_graphic_container_width).toInt()
private val graphicOffsetValueExtra =
resources.getDimension(R.dimen.default_graphic_container_right_offset).toInt()
private val defaultIndentOffset = resources.getDimension(R.dimen.default_indent).toInt()
private val highlightBottomOffset =
resources.getDimension(R.dimen.default_highlight_content_margin_top).toInt()
private val totalGraphicOffset =
(defaultIndentOffset + graphicOffsetValue + graphicOffsetValueExtra)
private val defaultHeaderOneExtraSpaceTop =
resources.getDimension(R.dimen.default_header_one_extra_space_top)
.toInt()
private val defaultHeaderOneExtraSpaceBottom =
resources.getDimension(R.dimen.default_header_one_extra_space_bottom)
.toInt()
private val defaultHeaderTwoExtraSpaceTop =
resources.getDimension(R.dimen.default_header_two_extra_space_top)
.toInt()
private val defaultHeaderTwoExtraSpaceBottom =
resources.getDimension(R.dimen.default_header_two_extra_space_bottom)
.toInt()
private val defaultHeaderThreeExtraSpaceTop =
resources.getDimension(R.dimen.default_header_three_extra_space_top)
.toInt()
private val defaultHeaderThreeExtraSpaceBottom =
resources.getDimension(R.dimen.default_header_three_extra_space_bottom)
.toInt()
private val defaultGraphicContainerWidth = resources.getDimensionPixelSize(R.dimen.default_graphic_container_width)
fun decorate(
decorations: List<BlockView.Decoration>,
content: View? = null
) {
if (childCount > 0) removeAllViews()
var bottomOffset = 0
var topOffset = 0
var isPreviousHighlight = false
var previousOffset = 0
decorations.forEachIndexed { indent, decor ->
when (indent) {
0 -> {
previousOffset = 0
}
else -> {
previousOffset += if (isPreviousHighlight) {
totalGraphicOffset
} else {
defaultIndentOffset
}
}
}
// Drawing background
if (decor.background != null) {
val bg = ThemeColor.values().find { it.code == decor.background }
if (bg != null && bg != ThemeColor.DEFAULT) {
val lp = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
).apply {
marginStart = previousOffset
// Offsets for extra space related to certain styles.
if (indent == decorations.lastIndex) {
when (decor.style) {
BlockView.Decoration.Style.Header.H1 -> {
topMargin = defaultHeaderOneExtraSpaceTop
bottomOffset += defaultHeaderOneExtraSpaceBottom
}
BlockView.Decoration.Style.Header.H2 -> {
topMargin = defaultHeaderTwoExtraSpaceTop
bottomOffset += defaultHeaderTwoExtraSpaceBottom
}
BlockView.Decoration.Style.Header.H3 -> {
topMargin = defaultHeaderThreeExtraSpaceTop
bottomOffset += defaultHeaderThreeExtraSpaceBottom
}
else -> {
// Do nothing
}
}
}
bottomMargin = bottomOffset
}
addView(
DecorationWidget.Background(
context = context,
background = resources.veryLight(bg, 0)
),
lp
)
}
}
// Drawing highlight line inside box
if (decor.style is BlockView.Decoration.Style.Highlight) {
if (decor.style is BlockView.Decoration.Style.Highlight.End) {
bottomOffset += highlightBottomOffset
}
val highlight = DecorationWidget.Highlight(context = context)
val lm = LayoutParams(
defaultGraphicContainerWidth,
LayoutParams.MATCH_PARENT
).apply {
marginStart = when (indent) {
0 -> {
defaultIndentOffset
}
else -> {
previousOffset + defaultIndentOffset
}
}
bottomMargin = bottomOffset
}
addView(
highlight,
lm
)
isPreviousHighlight = true
} else {
isPreviousHighlight = false
}
// Optional offset for content
// TODO this code will be removed
// Instead of applying margin here, return offset. ViewGroup, containing content, should handle this offset.
if (indent == decorations.lastIndex) {
content?.updateLayoutParams<LayoutParams> {
bottomMargin = bottomOffset
}
}
}
}
}

View file

@ -28,7 +28,7 @@
<View
android:id="@+id/divider"
android:layout_width="2dp"
android:layout_width="@dimen/highlight_line_width"
android:layout_height="match_parent"
android:layout_marginStart="11dp"
android:background="@color/block_higlight_divider" />

View file

@ -105,8 +105,6 @@
<dimen name="default_dashboard_item_spacing">20dp</dimen>
<dimen name="cover_gallery_item_spacing">8dp</dimen>
<dimen name="indent">28dp</dimen>
<dimen name="default_block_type_width">24dp</dimen>
<dimen name="block_style_toolbar_circle_size">52dp</dimen>
<dimen name="default_document_content_padding_start">12dp</dimen>
@ -253,5 +251,24 @@
<dimen name="drag_shadow_padding_width_other">10dp</dimen>
<dimen name="drag_shadow_max_size">150dp</dimen>
<!-- Editor, block: headers -->
<dimen name="default_header_one_extra_space_top">16dp</dimen>
<dimen name="default_header_one_extra_space_bottom">2dp</dimen>
<dimen name="default_header_two_extra_space_top">16dp</dimen>
<dimen name="default_header_two_extra_space_bottom">2dp</dimen>
<dimen name="default_header_three_extra_space_top">16dp</dimen>
<dimen name="default_header_three_extra_space_bottom">2dp</dimen>
<!-- Editor, block: highlight -->
<dimen name="default_highlight_content_margin_top">6dp</dimen>
<dimen name="highlight_line_width">2dp</dimen>
<!-- Editor, indentation -->
<dimen name="indent">28dp</dimen>
<dimen name="default_indent">20dp</dimen>
<dimen name="default_graphic_container_width">24dp</dimen>
<dimen name="default_graphic_container_height">24dp</dimen>
<dimen name="default_graphic_container_right_offset">4dp</dimen>
</resources>