Skip to content

Commit

Permalink
Separated MessageContextMenu into a different file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Apr 11, 2023
1 parent d0b6320 commit 5c2392f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package io.minchat.client.ui.chat

import arc.Core
import arc.scene.style.Drawable
import arc.scene.ui.Dialog
import arc.scene.ui.layout.Table
import arc.util.*
import arc.util.Align
import com.github.mnemotechnician.mkui.extensions.dsl.*
import io.minchat.client.Minchat
import io.minchat.client.ui.dialog.UserDialog
import io.minchat.client.ui.dialog.*
import io.minchat.rest.entity.MinchatMessage
import kotlinx.coroutines.*
import mindustry.Vars
import mindustry.gen.Icon
import kotlinx.coroutines.CoroutineScope
import io.minchat.client.misc.MinchatStyle as Style

/**
Expand Down Expand Up @@ -61,77 +55,7 @@ class NormalMinchatMessageElement(
}

override fun onRightClick() {
MessageContextMenu().show()
MessageContextMenu(chat, message).show()
}

inner class MessageContextMenu : Dialog() {
val messageElementCopy = NormalMinchatMessageElement(chat, message, false)

lateinit var actionTable: Table

init {
closeOnBack()

titleTable.remove()
buttons.remove()

cont.apply {
addTable(Style.surfaceBackground) {
margin(Style.layoutMargin)

add(messageElementCopy).minWidth(400f)
}.fill().pad(Style.layoutPad).row()

hsplitter(Style.foreground)

addTable {
actionTable = this
}.fill()
}

action(Icon.copy, "Copy text") {
Core.app.clipboardText = messageElementCopy.message.content
}

if (Minchat.client.canEditMessage(messageElementCopy.message)) {
action(Icon.pencil, "Edit message") {
// TODO
Vars.ui.showInfo("TODO: Edit message")
}

action(Icon.trash.tint(Style.red), "Delete message") {
// TODO: should there be a confirmation dialog?
launch {
runCatching {
messageElementCopy.message.delete()
}.onFailure {
Log.err("Failed to delete message", it)
}
}
hide()
}
}

action(Icon.exit, "Close") {
hide()
}
}

inline fun action(icon: Drawable?, text: String, crossinline listener: () -> Unit) {
actionTable.customButton({
margin(Style.buttonMargin)

addLabel(text, Style.Label).growX()

if (icon != null) {
addImage(icon, Scaling.fit)
.minSize(30f).fill()
.padLeft(20f)
}
}, Style.ActionButton) {
listener()
}.pad(Style.layoutPad).growX()
.row()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.minchat.client.ui.dialog

import arc.Core
import arc.scene.style.Drawable
import arc.scene.ui.Dialog
import arc.scene.ui.layout.Table
import arc.util.*
import com.github.mnemotechnician.mkui.extensions.dsl.*
import io.minchat.client.Minchat
import io.minchat.client.misc.MinchatStyle
import io.minchat.client.ui.chat.*
import io.minchat.rest.entity.MinchatMessage
import kotlinx.coroutines.*
import mindustry.Vars
import mindustry.gen.Icon

/**
* A context menu dialog that allows the user to
* perform various actions with the message.
*/
class MessageContextMenu(
val chat: ChatFragment,
val message: MinchatMessage,
parentScope: CoroutineScope
) : Dialog(), CoroutineScope by parentScope {
val messageElement = NormalMinchatMessageElement(chat, message, false)
lateinit var actionTable: Table

init {
closeOnBack()

titleTable.remove()
buttons.remove()

cont.apply {
addTable(MinchatStyle.surfaceBackground) {
margin(MinchatStyle.layoutMargin)

add(messageElement).minWidth(400f)
}.fill().pad(MinchatStyle.layoutPad).row()

hsplitter(MinchatStyle.foreground)

addTable {
actionTable = this
}.fill()
}

action(Icon.copy, "Copy text") {
Core.app.clipboardText = messageElement.message.content
}

if (Minchat.client.canEditMessage(messageElement.message)) {
action(Icon.pencil, "Edit message") {
// TODO
Vars.ui.showInfo("TODO: Edit message")
}

action(Icon.trash.tint(MinchatStyle.red), "Delete message") {
// TODO: should there be a confirmation dialog?
launch {
runCatching {
messageElement.message.delete()
}.onFailure {
Log.err("Failed to delete message", it)
}
}
hide()
}
}

action(Icon.exit, "Close") {
hide()
}
}

inline fun action(icon: Drawable?, text: String, crossinline listener: () -> Unit) {
actionTable.customButton({
margin(MinchatStyle.buttonMargin)

addLabel(text, MinchatStyle.Label).growX()

if (icon != null) {
addImage(icon, Scaling.fit)
.minSize(30f).fill()
.padLeft(20f)
}
}, MinchatStyle.ActionButton) {
listener()
}.pad(MinchatStyle.layoutPad).growX()
.row()
}
}

fun CoroutineScope.MessageContextMenu(chat: ChatFragment, message: MinchatMessage) =
MessageContextMenu(chat, message, this)

0 comments on commit 5c2392f

Please sign in to comment.