Added the ViewIndex function to the ViewsContainer interface

This commit is contained in:
anoshenko 2023-04-24 21:13:45 +03:00
parent f3c9bf7f56
commit ac8bb47677
4 changed files with 39 additions and 1 deletions

View File

@ -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. Для его создания используется функция

View File

@ -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

View File

@ -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)

View File

@ -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`)