github.com/jmigpin/editor@v1.6.0/util/uiutil/widget/separatorhandle.go (about)

     1  package widget
     2  
     3  import (
     4  	"image"
     5  
     6  	"github.com/jmigpin/editor/util/uiutil/event"
     7  	"github.com/jmigpin/editor/util/uiutil/mousefilter"
     8  )
     9  
    10  // A transparent widget added to a top layer (usually multilayer) to facilitate dragging.
    11  // Calculations are made on top of the reference node (usually a thin separator that otherwise would not be easy to put the pointer over for dragging).
    12  type SeparatorHandle struct {
    13  	ENode
    14  	Top, Bottom, Left, Right int
    15  	DragPad                  image.Point
    16  	ref                      Node // reference node for calc bounds
    17  }
    18  
    19  func NewSeparatorHandle(ref Node) *SeparatorHandle {
    20  	sh := &SeparatorHandle{ref: ref}
    21  	sh.AddMarks(MarkNotPaintable)
    22  	return sh
    23  }
    24  
    25  func (sh *SeparatorHandle) Measure(hint image.Point) image.Point {
    26  	panic("calling measure on thin separator handle")
    27  }
    28  
    29  func (sh *SeparatorHandle) Layout() {
    30  	// calc own bounds based on reference node
    31  	b := sh.ref.Embed().Bounds
    32  	b.Min.X -= sh.Left
    33  	b.Max.X += sh.Right
    34  	b.Min.Y -= sh.Top
    35  	b.Max.Y += sh.Bottom
    36  
    37  	// limit with parents bounds (might be wider/thiner)
    38  	pb := sh.Parent.Bounds
    39  	b = b.Intersect(pb)
    40  
    41  	// set own bounds
    42  	sh.Bounds = b
    43  }
    44  
    45  func (sh *SeparatorHandle) OnInputEvent(ev0 interface{}, p image.Point) event.Handled {
    46  	switch ev := ev0.(type) {
    47  	case *event.MouseDragStart:
    48  		u := sh.ref.Embed().Bounds.Min
    49  		sh.DragPad = mousefilter.DetectMovePad(ev.Point2, ev.Point, u)
    50  	}
    51  	return sh.ref.Embed().Wrapper.OnInputEvent(ev0, p)
    52  }