Fix how LogWidget is laid out

This commit is contained in:
Fingercomp 2025-09-03 18:52:40 +03:00
parent 2c77f8cba9
commit d676a4a5bf
No known key found for this signature in database
GPG Key ID: BBC71CEE45D86E37

View File

@ -5,23 +5,28 @@ import ocelot.desktop.color.RGBAColorNorm
import ocelot.desktop.geometry.{Padding2D, Rect2D, Size2D, Vector2D}
import ocelot.desktop.graphics.{Graphics, IconSource}
import ocelot.desktop.ui.UiHandler
import ocelot.desktop.ui.event.{ClickEvent, MouseEvent}
import ocelot.desktop.ui.event.handlers.MouseHandler
import ocelot.desktop.ui.layout.{Layout, LinearLayout}
import ocelot.desktop.ui.event.{ClickEvent, MouseEvent}
import ocelot.desktop.ui.layout.{CopyLayout, Layout}
import ocelot.desktop.ui.widget.LogWidget.{BorderThickness, EntryMargin, EntryPadding, LogEntry}
import ocelot.desktop.ui.widget.contextmenu.{ContextMenu, ContextMenuEntry}
import ocelot.desktop.util.{DrawUtils, Orientation}
import ocelot.desktop.util.DrawUtils
import scala.annotation.tailrec
import scala.collection.mutable
abstract class LogWidget extends Widget {
override protected val layout: Layout = new LinearLayout(this, orientation = Orientation.Vertical)
override protected val layout: Layout = new CopyLayout(this)
private object MessageListWidget extends Widget with MouseHandler { messageList =>
override protected val layout: Layout = new Layout(this)
override protected val layout: Layout = new Layout(this) {
override def recalculateBounds(): Unit = {
super.recalculateBounds()
}
}
private val entries = mutable.ArrayDeque.empty[Entry]
private var needRelayout = false
val dummyEntry: Entry = new RxEntry("", nextMessageY)
@ -62,6 +67,8 @@ abstract class LogWidget extends Widget {
nextMessageY - removedOffset,
)
override def maximumSize: Size2D = minimumSize
private def nextMessageY: Float = {
entries.lastOption.map(_.maxY).getOrElse(0f) + EntryMargin
}
@ -72,7 +79,7 @@ abstract class LogWidget extends Widget {
case LogEntry.Tx(message) => new TxEntry(message, nextMessageY)
})
parent.get.recalculateBoundsAndRelayout()
needRelayout = true
}
def removeFirst(count: Int): Unit = {
@ -82,10 +89,7 @@ abstract class LogWidget extends Widget {
entries.dropInPlace(count)
}
// this is a fix for log widget not scaling down after being cleaned
// TODO: implement it nicely
size = minimumSize
parent.get.recalculateBoundsAndRelayout()
needRelayout = true
}
@tailrec
@ -106,6 +110,15 @@ abstract class LogWidget extends Widget {
entries.iterator.drop(firstVisibleIdx).takeWhile(_.absoluteBounds.y <= clippedBounds.max.y)
}
override def update(): Unit = {
if (needRelayout) {
recalculateBoundsAndRelayout()
needRelayout = false
}
super.update()
}
override def draw(g: Graphics): Unit = {
applyRemovedOffset()
for (entry <- visibleEntries) {