portico/Sources/Portico/Core/SequentialContainer.swift

34 lines
1.5 KiB
Swift

import Gtk
/// SPI protocol for containers that manage an ordered list of children.
///
/// Method names `appendChild`/`removeChild` avoid collision with the
/// wrapper types' own `append`/`remove` methods.
@_spi(Portico) @MainActor public protocol SequentialContainer {
/// Appends a child widget to the end of the container.
func appendChild(_ child: Gtk.Widget)
/// Removes a previously added child widget.
func removeChild(_ child: Gtk.Widget)
}
/// SPI capability for containers that support indexed insertion and movement.
///
/// `index` and `sibling` describe the same target position: `index` is the child's
/// absolute zero-based position after the operation, while `sibling` is the child
/// that immediately precedes it (`nil` means the first position). Implementations
/// use whichever representation their native widget API requires.
@_spi(Portico) @MainActor public protocol DynamicChildHost: SequentialContainer {
/// Inserts a child at the target position.
func insertChild(_ child: Gtk.Widget, at index: Int, after sibling: Gtk.Widget?)
/// Moves a child to the target position.
func moveChild(_ child: Gtk.Widget, to index: Int, after sibling: Gtk.Widget?)
}
@_spi(Portico) extension DynamicChildHost {
/// Moves a child using the universally available remove-then-insert operation.
@_spi(Portico) public func moveChild(_ child: Gtk.Widget, to index: Int, after sibling: Gtk.Widget?) {
removeChild(child)
insertChild(child, at: index, after: sibling)
}
}