Cells/cells/ui/src/net/okennedy/cells/widgets/DragFrame.scala

44 lines
1.2 KiB
Scala

package net.okennedy.cells.widgets
import com.raquo.laminar.api.L._
import com.raquo.airstream.ownership.OneTimeOwner
class DragFrame(initX: Int, initY: Int, width: Int, height: Int, cursorX: Double, cursorY: Double, callback: (Int, Int) => Unit)
extends Widget
{
implicit val tableOwner: Owner =
new OneTimeOwner( () => println(s"Accessing owner of DragFrame after killed") )
val curr = Var[(Int, Int)](initial = (initX, initY))
val root = div(
className("dragFrame"),
styleAttr <--
curr.signal.map { case (x, y) =>
s"left: ${x-2}px; top: ${y-2}px; height: ${height-4}px; width: ${width-4}px"
},
documentEvents.onMouseUp --> {
(evt) =>
val (x, y) = curr.now()
callback( x, y )
Widgets.remove(this)
},
documentEvents.onMouseMove --> {
(evt) =>
curr.set( (
initX + (evt.pageX - cursorX).toInt,
initY + (evt.pageY - cursorY).toInt
) )
}
)
}
object DragFrame
{
def start(x: Int, y: Int, width: Int, height: Int, evt: org.scalajs.dom.MouseEvent)(callback: (Int, Int) => Unit): Unit =
{
val frame = new DragFrame(x, y, width, height, evt.pageX, evt.pageY, callback)
Widgets.register(frame)
}
}