diff --git a/widget/diagramwidget/diagram.go b/widget/diagramwidget/diagram.go index b0f3a1ff..1e3038f6 100644 --- a/widget/diagramwidget/diagram.go +++ b/widget/diagramwidget/diagram.go @@ -114,6 +114,7 @@ func (dw *DiagramWidget) addElementToSelection(de DiagramElement) { if !dw.IsSelected(de) { if dw.primarySelection == nil { dw.primarySelection = de + dw.BringToFront(de.GetDiagramElementID()) if dw.PrimaryDiagramElementSelectionChangedCallback != nil { dw.PrimaryDiagramElementSelectionChangedCallback(de.GetDiagramElementID()) } @@ -126,9 +127,7 @@ func (dw *DiagramWidget) addElementToSelection(de DiagramElement) { // addLink adds a link to the diagram func (dw *DiagramWidget) addLink(link DiagramLink) { dw.DiagramElements.PushBack(link) - // dw.Links[link.GetDiagramElementID()] = link link.Refresh() - // TODO add logic to rezise diagram if necessary } func (dw *DiagramWidget) addLinkDependency(diagramElement DiagramElement, link *BaseDiagramLink, pad ConnectionPad) { @@ -150,9 +149,8 @@ func (dw *DiagramWidget) addLinkDependency(diagramElement DiagramElement, link * // addNode adds a node to the diagram func (dw *DiagramWidget) addNode(node DiagramNode) { dw.DiagramElements.PushBack(node) - // dw.Nodes[node.GetDiagramElementID()] = node + dw.adjustBounds() node.Refresh() - // TODO add logic to rezise diagram if necessary } // adjustBounds calculates the bounds of the diagram elements and adjusts the size of the drawing area accordingly @@ -206,6 +204,30 @@ func (dw *DiagramWidget) adjustBounds() { dw.scrollingContainer.Refresh() } +// BringToFront moves the diagram element to the top of the display list (which is the back of the DiagramElements list) +func (dw *DiagramWidget) BringToFront(elementID string) { + for listElement := dw.DiagramElements.Front(); listElement != nil; listElement = listElement.Next() { + value := listElement.Value + diagramElement := value.(DiagramElement) + if diagramElement.GetDiagramElementID() == elementID { + dw.DiagramElements.MoveToBack(listElement) + dw.drawingArea.Refresh() + } + } +} + +// BringForward moves the diagram element on top of the next element of the display list +func (dw *DiagramWidget) BringForward(elementID string) { + for listElement := dw.DiagramElements.Front(); listElement != nil; listElement = listElement.Next() { + value := listElement.Value + diagramElement := value.(DiagramElement) + if diagramElement.GetDiagramElementID() == elementID { + dw.DiagramElements.MoveAfter(listElement, listElement.Next()) + dw.drawingArea.Refresh() + } + } +} + // CreateRenderer creates the renderer for the diagram func (dw *DiagramWidget) CreateRenderer() fyne.WidgetRenderer { r := diagramWidgetRenderer{ @@ -445,7 +467,7 @@ func (dw *DiagramWidget) RemoveElement(elementID string) { if element.IsLink() { dw.removeDependenciesInvolvingLink(elementID) } - dw.Refresh() + dw.drawingArea.Refresh() } // SelectDiagramElement clears the selection, makes the indicated element the primary selection, and invokes @@ -467,6 +489,30 @@ func (dw *DiagramWidget) SelectDiagramElementNoCallback(id string) { } } +// SendToBack moves the diagram element to the top of the display list (which is the front of the DiagramElements list) +func (dw *DiagramWidget) SendToBack(elementID string) { + for listElement := dw.DiagramElements.Front(); listElement != nil; listElement = listElement.Next() { + value := listElement.Value + diagramElement := value.(DiagramElement) + if diagramElement.GetDiagramElementID() == elementID { + dw.DiagramElements.MoveToFront(listElement) + dw.drawingArea.Refresh() + } + } +} + +// SendBackward moves the diagram element on top of the next element of the display list +func (dw *DiagramWidget) SendBackward(elementID string) { + for listElement := dw.DiagramElements.Front(); listElement != nil; listElement = listElement.Next() { + value := listElement.Value + diagramElement := value.(DiagramElement) + if diagramElement.GetDiagramElementID() == elementID { + dw.DiagramElements.MoveBefore(listElement, listElement.Prev()) + dw.drawingArea.Refresh() + } + } +} + // showAllPads is a work-around for fyne Issue #3906 in which a child's Hoverable interface // (i.e. the pad) masks the parent's Tappable interface. This function (and all references to // it) should be removed when this issue has been resolved @@ -508,7 +554,7 @@ func (r *diagramWidgetRenderer) Objects() []fyne.CanvasObject { } func (r *diagramWidgetRenderer) Refresh() { - r.diagramWidget.scrollingContainer.Refresh() + r.diagramWidget.drawingArea.Refresh() } type drawingArea struct { diff --git a/widget/diagramwidget/diagramElement.go b/widget/diagramwidget/diagramElement.go index 7bcc64c5..7145645c 100644 --- a/widget/diagramwidget/diagramElement.go +++ b/widget/diagramwidget/diagramElement.go @@ -153,5 +153,6 @@ func (de *diagramElement) SetProperties(properties DiagramElementProperties) { func (de *diagramElement) ShowHandles() { for _, handle := range de.handles { handle.Show() + de.Refresh() } } diff --git a/widget/diagramwidget/link.go b/widget/diagramwidget/link.go index 9f0e412f..8439f9ff 100644 --- a/widget/diagramwidget/link.go +++ b/widget/diagramwidget/link.go @@ -9,7 +9,7 @@ import ( "fyne.io/fyne/v2/driver/desktop" ) -var _ fyne.Tappable = (*BaseDiagramLink)(nil) +// var _ fyne.Tappable = (*BaseDiagramLink)(nil) var _ desktop.Hoverable = (*BaseDiagramLink)(nil) var _ DiagramElement = (*BaseDiagramLink)(nil) @@ -68,10 +68,8 @@ type DiagramLink interface { // Link can connect to another Link using this ConnectionPad. type BaseDiagramLink struct { diagramElement - linkPoints []*LinkPoint - linkSegments []*LinkSegment - // LinkColor color.Color - // strokeWidth float32 + linkPoints []*LinkPoint + linkSegments []*LinkSegment sourcePad ConnectionPad targetPad ConnectionPad SourceDecorations []Decoration @@ -442,9 +440,9 @@ func (bdl *BaseDiagramLink) SetTargetPad(pad ConnectionPad) { } } -// Tapped handles tap events -func (bdl *BaseDiagramLink) Tapped(event *fyne.PointEvent) { -} +// // Tapped handles tap events +// func (bdl *BaseDiagramLink) Tapped(event *fyne.PointEvent) { +// } // diagramLinkRenderer type diagramLinkRenderer struct { diff --git a/widget/diagramwidget/linksegment.go b/widget/diagramwidget/linksegment.go index 1ca39a97..32bf75bc 100644 --- a/widget/diagramwidget/linksegment.go +++ b/widget/diagramwidget/linksegment.go @@ -60,7 +60,7 @@ func (ls *LinkSegment) MouseUp(event *desktop.MouseEvent) { clickPoint := geom.Coord{float64(event.Position.X), float64(event.Position.Y)} p1 := geom.Coord{float64(ls.p1.X), float64(ls.p1.Y)} p2 := geom.Coord{float64(ls.p2.X), float64(ls.p2.Y)} - if xy.DistanceFromPointToLine(clickPoint, p1, p2) <= float64(ls.link.properties.StrokeWidth/2)+1 { + if xy.DistanceFromPointToLine(clickPoint, p1, p2) <= float64(ls.link.properties.StrokeWidth/2)+3 { ls.link.diagram.DiagramElementTapped(ls.link) } } else if ls.link.diagram.LinkSegmentMouseUpCallback != nil {