diff --git a/README-ru.md b/README-ru.md index f8a3914..d303853 100644 --- a/README-ru.md +++ b/README-ru.md @@ -2296,6 +2296,15 @@ radius необходимо передать nil Данная функция удаляет View из заданной позиции и возвращает его. Если index указывает за границы списка, то ничего не удаляется, а функция возвращает nil. + ViewIndex(view View) int + +Данная функция возвращает индекс дочернего View или -1 если такого View нет в контейнере. +Она часто используется в паре с RemoveView если индекс дочернего View неизвестен: + + if index := container.ViewIndex(view); index >= 0 { + container.RemoveView(index) + } + ## ListLayout ListLayout является контейнером, реализующим интерфейс ViewsContainer. Для его создания используется функция diff --git a/README.md b/README.md index c534ee9..3881b10 100644 --- a/README.md +++ b/README.md @@ -2270,6 +2270,15 @@ If index is less than 0, then to the beginning of the list. This function removes the View from the given position and returns it. If index points outside the bounds of the list, then nothing is removed, and the function returns nil. + ViewIndex(view View) int + +This function returns the index of the child View, or -1 if there is no such View in the container. +It is often used in conjunction with RemoveView if the index of the child View is unknown: + + if index := container.ViewIndex(view); index >= 0 { + container.RemoveView(index) + } + ## ListLayout ListLayout is a container that implements the ViewsContainer interface. To create it, use the function diff --git a/customView.go b/customView.go index e88df6c..fa2cba2 100644 --- a/customView.go +++ b/customView.go @@ -243,10 +243,19 @@ func (customView *CustomViewData) RemoveView(index int) View { return container.RemoveView(index) } } - return nil } +// Remove removes a view from the list of a view children and return it +func (customView *CustomViewData) ViewIndex(view View) int { + if customView.superView != nil { + if container, ok := customView.superView.(ViewsContainer); ok { + return container.ViewIndex(view) + } + } + return -1 +} + func (customView *CustomViewData) String() string { if customView.superView != nil { return getViewString(customView) diff --git a/viewsContainer.go b/viewsContainer.go index 9811e2e..1ef429a 100644 --- a/viewsContainer.go +++ b/viewsContainer.go @@ -17,6 +17,8 @@ type ViewsContainer interface { Insert(view View, index int) // Remove removes a view from the list of a view children and return it RemoveView(index int) View + // ViewIndex returns the index of view, -1 overwise + ViewIndex(view View) int } type viewsContainerData struct { @@ -117,6 +119,15 @@ func (container *viewsContainerData) RemoveView(index int) View { return view } +func (container *viewsContainerData) ViewIndex(view View) int { + for index, v := range container.views { + if v == view { + return index + } + } + return -1 +} + func (container *viewsContainerData) cssStyle(self View, builder cssBuilder) { container.viewData.cssStyle(self, builder) builder.add(`overflow`, `auto`)