Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 61 additions & 54 deletions widget/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ func (o ContainerOptions) Layout(layout Layouter) ContainerOpt {
}
}

func (c *Container) addChildInit(child PreferredSizeLocateableWidget) {
child.GetWidget().parent = c.widget
child.GetWidget().self = child

if c.validated {
child.Validate()
}

child.GetWidget().ContextMenuEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetContextMenuEventArgs); ok {
c.GetWidget().FireContextMenuEvent(a.Widget, a.Location)
}
})
child.GetWidget().FocusEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetFocusEventArgs); ok {
c.GetWidget().FireFocusEvent(a.Widget, a.Focused, a.Location)
}
})
child.GetWidget().ToolTipEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetToolTipEventArgs); ok {
c.GetWidget().FireToolTipEvent(a.Window, a.Show)
}
})
child.GetWidget().DragAndDropEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetDragAndDropEventArgs); ok {
c.GetWidget().FireDragAndDropEvent(a.Window, a.Show, a.DnD)
}
})
}

func (c *Container) AddChild(children ...PreferredSizeLocateableWidget) RemoveChildFunc {
c.init.Do()

Expand All @@ -91,35 +121,8 @@ func (c *Container) AddChild(children ...PreferredSizeLocateableWidget) RemoveCh
panic("cannot add nil child")
}

child.GetWidget().parent = c.widget
child.GetWidget().self = child

if c.validated {
child.Validate()
}

c.addChildInit(child)
c.children = append(c.children, child)

child.GetWidget().ContextMenuEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetContextMenuEventArgs); ok {
c.GetWidget().FireContextMenuEvent(a.Widget, a.Location)
}
})
child.GetWidget().FocusEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetFocusEventArgs); ok {
c.GetWidget().FireFocusEvent(a.Widget, a.Focused, a.Location)
}
})
child.GetWidget().ToolTipEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetToolTipEventArgs); ok {
c.GetWidget().FireToolTipEvent(a.Window, a.Show)
}
})
child.GetWidget().DragAndDropEvent.AddHandler(func(args interface{}) {
if a, ok := args.(*WidgetDragAndDropEventArgs); ok {
c.GetWidget().FireDragAndDropEvent(a.Window, a.Show, a.DnD)
}
})
}
c.RequestRelayout()
c.relayoutParent = true
Expand All @@ -130,6 +133,34 @@ func (c *Container) AddChild(children ...PreferredSizeLocateableWidget) RemoveCh
}
}

func (c *Container) ReplaceChild(remove PreferredSizeLocateableWidget, add PreferredSizeLocateableWidget) {
for i, ch := range c.children {
if ch == remove {
c.addChildInit(add)
c.children[i] = add
closeWidget(remove.GetWidget())
c.RequestRelayout()
return
}
}
}

func closeWidget(w *Widget) {
w.parent = nil

if w.ToolTip != nil && w.ToolTip.window != nil {
w.ToolTip.window.Close()
}

if w.DragAndDrop != nil && w.DragAndDrop.window != nil {
w.DragAndDrop.window.Close()
}

if w.ContextMenuWindow != nil {
w.ContextMenuWindow.Close()
}
}

func (c *Container) RemoveChild(child PreferredSizeLocateableWidget) {
index := -1
for i, ch := range c.children {
Expand All @@ -145,39 +176,15 @@ func (c *Container) RemoveChild(child PreferredSizeLocateableWidget) {

c.children = append(c.children[:index], c.children[index+1:]...)

child.GetWidget().parent = nil

if child.GetWidget().ToolTip != nil && child.GetWidget().ToolTip.window != nil {
child.GetWidget().ToolTip.window.Close()
}

if child.GetWidget().DragAndDrop != nil && child.GetWidget().DragAndDrop.window != nil {
child.GetWidget().DragAndDrop.window.Close()
}
closeWidget(child.GetWidget())

if child.GetWidget().ContextMenuWindow != nil {
child.GetWidget().ContextMenuWindow.Close()
}
c.RequestRelayout()
c.relayoutParent = true
}

func (c *Container) RemoveChildren() {
for i := range c.children {
childWidget := c.children[i].GetWidget()
childWidget.parent = nil

if childWidget.ToolTip != nil && childWidget.ToolTip.window != nil {
childWidget.ToolTip.window.Close()
}

if childWidget.DragAndDrop != nil && childWidget.DragAndDrop.window != nil {
childWidget.DragAndDrop.window.Close()
}

if childWidget.ContextMenuWindow != nil {
childWidget.ContextMenuWindow.Close()
}
closeWidget(c.children[i].GetWidget())
}
c.children = nil

Expand Down
38 changes: 38 additions & 0 deletions widget/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,44 @@ func TestContainer_SetupInputLayer(t *testing.T) {
m.AssertExpectations(t)
}

func TestContainer_Replace(t *testing.T) {
c := newContainer(t)
m1 := controlMock{}
m2 := controlMock{}
m3 := controlMock{}

m1.On("GetWidget").Maybe().Return(NewWidget())
m2.On("GetWidget").Maybe().Return(NewWidget())
m3.On("GetWidget").Maybe().Return(NewWidget())

c.AddChild(&m1, &m2, &m3)

if len(c.children) != 3 {
t.Fatalf("expected 3 children, got %d", len(c.children))
}

replaced := controlMock{}
replaced.On("GetWidget").Maybe().Return(NewWidget())

c.ReplaceChild(&m2, &replaced)

if len(c.children) != 3 {
t.Fatalf("expected 3 children, got %d", len(c.children))
}

if c.children[0] != &m1 {
t.Fatalf("expected original child at index 0")
}

if c.children[1] != &replaced {
t.Fatalf("expected replaced child at index 1")
}

if c.children[2] != &m3 {
t.Fatalf("expected original child at index 2")
}
}

func (c *controlMock) GetWidget() *Widget {
args := c.Called()
if arg, ok := args.Get(0).(*Widget); ok {
Expand Down