Documentation
¶
Overview ¶
Package selection provides text selection functionality for Compose Foundation.
This package contains the SelectionContainer composable and related types for enabling text selection across multiple text composables.
Index ¶
- Constants
- Variables
- func HasSelection(registrar SelectionRegistrar, selectableId int64) bool
- type AnchorInfo
- type Composable
- type Handle
- type HapticFeedback
- type HapticFeedbackType
- type SectionContainerOption
- type SectionContainerOptions
- type Selectable
- type Selection
- type SelectionAdjustment
- type SelectionLayout
- type SelectionLayoutBuilder
- type SelectionManager
- func (m *SelectionManager) Copy()
- func (m *SelectionManager) DraggingHandle() Handle
- func (m *SelectionManager) EndHandleLineHeight() float32
- func (m *SelectionManager) EndHandlePosition() *geometry.Offset
- func (m *SelectionManager) GetSelectedText() *text.AnnotatedString
- func (m *SelectionManager) HasFocus() bool
- func (m *SelectionManager) IsInTouchMode() bool
- func (m *SelectionManager) IsNonEmptySelection() bool
- func (m *SelectionManager) IsTriviallyCollapsedSelection() bool
- func (m *SelectionManager) Modifier() ui.Modifier
- func (m *SelectionManager) OnRelease()
- func (m *SelectionManager) SelectAll()
- func (m *SelectionManager) Selection() *Selection
- func (m *SelectionManager) SetContainerLayoutCoordinates(coords layout.LayoutCoordinates)
- func (m *SelectionManager) SetHasFocus(value bool)
- func (m *SelectionManager) SetIsInTouchMode(value bool)
- func (m *SelectionManager) SetOnSelectionChange(callback func(*Selection))
- func (m *SelectionManager) SetSelection(selection *Selection)
- func (m *SelectionManager) StartHandleLineHeight() float32
- func (m *SelectionManager) StartHandlePosition() *geometry.Offset
- type SelectionRegistrar
- type SelectionRegistrarImpl
- func (r *SelectionRegistrarImpl) GetIncrementId() int64
- func (r *SelectionRegistrarImpl) NextSelectableId() int64
- func (r *SelectionRegistrarImpl) NotifyPositionChange(selectableId int64)
- func (r *SelectionRegistrarImpl) NotifySelectableChange(selectableId int64)
- func (r *SelectionRegistrarImpl) NotifySelectionUpdate(layoutCoordinates layout.LayoutCoordinates, newPosition geometry.Offset, ...) bool
- func (r *SelectionRegistrarImpl) NotifySelectionUpdateEnd()
- func (r *SelectionRegistrarImpl) NotifySelectionUpdateSelectAll(selectableId int64, isInTouchMode bool)
- func (r *SelectionRegistrarImpl) NotifySelectionUpdateStart(layoutCoordinates layout.LayoutCoordinates, startPosition geometry.Offset, ...)
- func (r *SelectionRegistrarImpl) SelectableMap() map[int64]Selectable
- func (r *SelectionRegistrarImpl) Selectables() []Selectable
- func (r *SelectionRegistrarImpl) SetSubselections(subs map[int64]*Selection)
- func (r *SelectionRegistrarImpl) Sort(containerLayoutCoordinates layout.LayoutCoordinates) []Selectable
- func (r *SelectionRegistrarImpl) Subscribe(selectable Selectable) Selectable
- func (r *SelectionRegistrarImpl) Subselections() map[int64]*Selection
- func (r *SelectionRegistrarImpl) Unsubscribe(selectable Selectable)
- type TextSelectionColors
- type TextToolbar
- type TextToolbarStatus
Constants ¶
const InvalidSelectableId int64 = 0
InvalidSelectableId represents an invalid ID for Selectable.
Variables ¶
var LocalSelectionRegistrar = compose.CompositionLocalOf[SelectionRegistrar](func() SelectionRegistrar { return nil })
LocalSelectionRegistrar is a CompositionLocal for SelectionRegistrar. Composables that implement selection logic can use this to get a SelectionRegistrar in order to subscribe and unsubscribe to SelectionRegistrar.
The default value is nil, meaning selection is not enabled.
var LocalTextSelectionColors = compose.CompositionLocalOf(DefaultTextSelectionColors)
LocalTextSelectionColors is a CompositionLocal for TextSelectionColors. Provides selection colors to text composables within the composition.
Functions ¶
func HasSelection ¶
func HasSelection(registrar SelectionRegistrar, selectableId int64) bool
HasSelection checks if there is a selection on the CoreText with the given selectableId.
Types ¶
type AnchorInfo ¶
type AnchorInfo struct {
// Direction is the text direction of the character at selection edge.
Direction style.ResolvedTextDirection
// Offset is the character offset for the selection edge.
// This offset is within the individual child text composable.
Offset int
// SelectableId is the id of the Selectable which contains this anchor.
SelectableId int64
}
AnchorInfo contains information about a selection anchor (start/end).
type Composable ¶
type Composable = compose.Composable
Composable represents a composable function type.
func DisableSelection ¶
func DisableSelection(content Composable) Composable
DisableSelection disables text selection for its direct or indirect children. To use this, simply add this to wrap one or more text composables.
func SelectionContainer ¶
func SelectionContainer(content Composable, options ...SectionContainerOption) Composable
SelectionContainer enables text selection for its direct or indirect children.
Use of a lazy layout, such as LazyRow or LazyColumn, within a SelectionContainer has undefined behavior on text items that aren't composed. For example, texts that aren't composed will not be included in copy operations and select all will not expand the selection to include them.
type HapticFeedback ¶
type HapticFeedback interface {
PerformHapticFeedback(feedbackType HapticFeedbackType)
}
HapticFeedback provides haptic feedback functionality. This is a platform-specific interface that should be implemented per platform.
type HapticFeedbackType ¶
type HapticFeedbackType int
HapticFeedbackType represents the type of haptic feedback.
const ( // HapticFeedbackTextHandleMove is the feedback for text handle movement. HapticFeedbackTextHandleMove HapticFeedbackType = iota )
type SectionContainerOption ¶
type SectionContainerOption func(*SectionContainerOptions)
func WithModifier ¶
func WithModifier(modifier ui.Modifier) SectionContainerOption
type SectionContainerOptions ¶
type Selectable ¶
type Selectable interface {
// SelectableId returns an ID used by SelectionRegistrar to identify this Selectable.
// This value should not be InvalidSelectableId.
SelectableId() int64
// AppendSelectableInfoToBuilder adds SelectableInfo representing this Selectable
// to the SelectionLayoutBuilder.
AppendSelectableInfoToBuilder(builder SelectionLayoutBuilder)
// GetSelectAllSelection returns selectAll Selection information for this selectable.
// Returns nil if no selection can be provided.
GetSelectAllSelection() *Selection
// GetHandlePosition returns the Offset of a SelectionHandle.
// isStartHandle is true for the start handle, false for the end handle.
GetHandlePosition(selection Selection, isStartHandle bool) geometry.Offset
// GetLayoutCoordinates returns the LayoutCoordinates of the Selectable.
// This could be nil if called before composing.
GetLayoutCoordinates() layout.LayoutCoordinates
// TextLayoutResult returns the TextLayoutResult of the selectable.
// This could be nil if called before composing.
TextLayoutResult() *text.TextLayoutResult
// GetText returns the text content as AnnotatedString of the Selectable.
GetText() text.AnnotatedString
// GetBoundingBox returns the bounding box of the character for given offset.
// Returns Rect.Zero if the selectable is empty.
GetBoundingBox(offset int) geometry.Rect
// GetLineLeft returns the left x coordinate of the line for the given offset.
GetLineLeft(offset int) float32
// GetLineRight returns the right x coordinate of the line for the given offset.
GetLineRight(offset int) float32
// GetCenterYForOffset returns the center y coordinate of the line
// on which the specified text offset appears.
GetCenterYForOffset(offset int) float32
// GetRangeOfLineContaining returns the offsets of the start and end of the line
// containing the given offset, or TextRange.Zero if the selectable is empty.
GetRangeOfLineContaining(offset int) text.TextRange
// GetLastVisibleOffset returns the last visible character's offset.
// Some lines can be hidden due to maxLines or Constraints.maxHeight.
GetLastVisibleOffset() int
// GetLineHeight returns the text line height for the given offset.
GetLineHeight(offset int) float32
}
Selectable provides Selection information for a composable to SelectionContainer. Composables that can be selected should subscribe to SelectionRegistrar using this interface.
type Selection ¶
type Selection struct {
// Start contains information about the start of the selection.
Start AnchorInfo
// End contains information about the end of the selection.
End AnchorInfo
// HandlesCrossed is true when the user drags one handle to cross the other handle.
// When selection happens in a single widget, checking TextRange.start > TextRange.end
// is enough. But when selection happens across multiple widgets, this value needs
// more complicated calculation.
HandlesCrossed bool
}
Selection represents the current selection state.
func NewSelection ¶
func NewSelection(start, end AnchorInfo, handlesCrossed bool) Selection
NewSelection creates a new Selection with the given parameters.
func (Selection) IsCollapsed ¶
IsCollapsed returns true if the selection start and end are at the same position.
func (Selection) Merge ¶
Merge merges this selection with another selection. If other is nil, returns this selection unchanged.
func (Selection) ToTextRange ¶
ToTextRange returns the selection offset information as a TextRange.
type SelectionAdjustment ¶
type SelectionAdjustment int
SelectionAdjustment defines how a selection should be adjusted.
const ( // SelectionAdjustmentNone makes no adjustment to the selection. SelectionAdjustmentNone SelectionAdjustment = iota // SelectionAdjustmentCharacter adjusts selection to the character level. SelectionAdjustmentCharacter // SelectionAdjustmentWord adjusts selection to the word level. SelectionAdjustmentWord // SelectionAdjustmentParagraph adjusts selection to the paragraph level. SelectionAdjustmentParagraph // SelectionAdjustmentCharacterWithWordAccelerate starts with character adjustment // and accelerates to word level when dragging fast. SelectionAdjustmentCharacterWithWordAccelerate )
func (SelectionAdjustment) Adjust ¶
func (adj SelectionAdjustment) Adjust(layout SelectionLayout) *Selection
Adjust applies this adjustment to the selection layout and returns the adjusted selection. This is a placeholder - full implementation requires SelectionLayout.
type SelectionLayout ¶
type SelectionLayout interface {
// ShouldRecomputeSelection returns true if selection should be recomputed
// based on the previous layout.
ShouldRecomputeSelection(previousLayout SelectionLayout) bool
}
SelectionLayout contains layout information for computing selection adjustments. This is a placeholder interface that will be expanded as needed.
type SelectionLayoutBuilder ¶
type SelectionLayoutBuilder interface {
}
SelectionLayoutBuilder is used to build selection layout information. This is a placeholder interface that will be expanded as needed.
type SelectionManager ¶
type SelectionManager struct {
// hapticFeedback provides haptic feedback.
HapticFeedback HapticFeedback
// onCopyHandler handles copy operations.
OnCopyHandler func(text.AnnotatedString)
// textToolbar shows floating toolbar.
TextToolbar TextToolbar
// contains filtered or unexported fields
}
SelectionManager is a bridge class between user interaction and text composables for selection.
func NewSelectionManager ¶
func NewSelectionManager(registrar *SelectionRegistrarImpl) *SelectionManager
NewSelectionManager creates a new SelectionManager with the given registrar.
func (*SelectionManager) DraggingHandle ¶
func (m *SelectionManager) DraggingHandle() Handle
DraggingHandle returns the currently dragging handle.
func (*SelectionManager) EndHandleLineHeight ¶
func (m *SelectionManager) EndHandleLineHeight() float32
EndHandleLineHeight returns the line height at the end handle.
func (*SelectionManager) EndHandlePosition ¶
func (m *SelectionManager) EndHandlePosition() *geometry.Offset
EndHandlePosition returns the end handle position.
func (*SelectionManager) GetSelectedText ¶
func (m *SelectionManager) GetSelectedText() *text.AnnotatedString
GetSelectedText returns the selected text as an AnnotatedString.
func (*SelectionManager) HasFocus ¶
func (m *SelectionManager) HasFocus() bool
HasFocus returns whether the container has focus.
func (*SelectionManager) IsInTouchMode ¶
func (m *SelectionManager) IsInTouchMode() bool
IsInTouchMode returns whether touch mode is active.
func (*SelectionManager) IsNonEmptySelection ¶
func (m *SelectionManager) IsNonEmptySelection() bool
IsNonEmptySelection returns whether the selection selects any characters.
func (*SelectionManager) IsTriviallyCollapsedSelection ¶
func (m *SelectionManager) IsTriviallyCollapsedSelection() bool
IsTriviallyCollapsedSelection returns whether start and end anchors are equal.
func (*SelectionManager) Modifier ¶
func (m *SelectionManager) Modifier() ui.Modifier
Modifier returns the modifier for the selection container.
func (*SelectionManager) OnRelease ¶
func (m *SelectionManager) OnRelease()
OnRelease clears the selection.
func (*SelectionManager) SelectAll ¶
func (m *SelectionManager) SelectAll()
SelectAll selects all text in the container.
func (*SelectionManager) Selection ¶
func (m *SelectionManager) Selection() *Selection
Selection returns the current selection.
func (*SelectionManager) SetContainerLayoutCoordinates ¶
func (m *SelectionManager) SetContainerLayoutCoordinates(coords layout.LayoutCoordinates)
SetContainerLayoutCoordinates sets the container's layout coordinates.
func (*SelectionManager) SetHasFocus ¶
func (m *SelectionManager) SetHasFocus(value bool)
SetHasFocus sets the focus state.
func (*SelectionManager) SetIsInTouchMode ¶
func (m *SelectionManager) SetIsInTouchMode(value bool)
SetIsInTouchMode sets the touch mode state.
func (*SelectionManager) SetOnSelectionChange ¶
func (m *SelectionManager) SetOnSelectionChange(callback func(*Selection))
SetOnSelectionChange sets the selection change callback.
func (*SelectionManager) SetSelection ¶
func (m *SelectionManager) SetSelection(selection *Selection)
SetSelection sets the current selection.
func (*SelectionManager) StartHandleLineHeight ¶
func (m *SelectionManager) StartHandleLineHeight() float32
StartHandleLineHeight returns the line height at the start handle.
func (*SelectionManager) StartHandlePosition ¶
func (m *SelectionManager) StartHandlePosition() *geometry.Offset
StartHandlePosition returns the start handle position.
type SelectionRegistrar ¶
type SelectionRegistrar interface {
// Subselections returns the map storing current selection information on each Selectable.
// A selectable can query its selected range using its selectableId.
Subselections() map[int64]*Selection
// Subscribe registers a Selectable to this SelectionRegistrar.
// Returns the Selectable for use with Unsubscribe.
Subscribe(selectable Selectable) Selectable
// Unsubscribe removes a Selectable from this SelectionRegistrar.
Unsubscribe(selectable Selectable)
// NextSelectableId returns a unique ID for a Selectable.
NextSelectableId() int64
// NotifyPositionChange is called when the global position of a subscribed Selectable changes.
NotifyPositionChange(selectableId int64)
// NotifySelectionUpdateStart is called when selection has been initiated.
// layoutCoordinates: LayoutCoordinates of the Selectable.
// startPosition: coordinates where selection is initiated.
// adjustment: how selection should be adjusted.
// isInTouchMode: whether the update is from a touch pointer.
NotifySelectionUpdateStart(
layoutCoordinates layout.LayoutCoordinates,
startPosition geometry.Offset,
adjustment SelectionAdjustment,
isInTouchMode bool,
)
// NotifySelectionUpdateSelectAll is called when selection is initiated with selectAll.
// selectableId: the selectableId of the Selectable.
// isInTouchMode: whether the update is from a touch pointer.
NotifySelectionUpdateSelectAll(selectableId int64, isInTouchMode bool)
// NotifySelectionUpdate is called when a selection handle has moved.
// Returns true if the selection handle movement is consumed.
NotifySelectionUpdate(
layoutCoordinates layout.LayoutCoordinates,
newPosition geometry.Offset,
previousPosition geometry.Offset,
isStartHandle bool,
adjustment SelectionAdjustment,
isInTouchMode bool,
) bool
// NotifySelectionUpdateEnd is called when selection update has stopped.
NotifySelectionUpdateEnd()
// NotifySelectableChange is called when the content of a selectable has changed.
NotifySelectableChange(selectableId int64)
}
SelectionRegistrar allows a composable to subscribe and unsubscribe to selection changes.
type SelectionRegistrarImpl ¶
type SelectionRegistrarImpl struct {
// OnPositionChangeCallback is called when a position change was triggered.
OnPositionChangeCallback func(selectableId int64)
// OnSelectionUpdateStartCallback is called when selection is initiated.
OnSelectionUpdateStartCallback func(isInTouchMode bool, layoutCoordinates layout.LayoutCoordinates, position geometry.Offset, adjustment SelectionAdjustment)
// OnSelectionUpdateSelectAllCallback is called when selection is initiated with selectAll.
OnSelectionUpdateSelectAllCallback func(isInTouchMode bool, selectableId int64)
// OnSelectionUpdateCallback is called when selection is updated.
OnSelectionUpdateCallback func(isInTouchMode bool, layoutCoordinates layout.LayoutCoordinates, newPosition, previousPosition geometry.Offset, isStartHandle bool, adjustment SelectionAdjustment) bool
// OnSelectionUpdateEndCallback is called when selection update finished.
OnSelectionUpdateEndCallback func()
// OnSelectableChangeCallback is called when one of the selectable has changed.
OnSelectableChangeCallback func(selectableId int64)
// AfterSelectableUnsubscribe is called after a selectable is unsubscribed.
AfterSelectableUnsubscribe func(selectableId int64)
// contains filtered or unexported fields
}
SelectionRegistrarImpl is the implementation of SelectionRegistrar.
func NewSelectionRegistrarImpl ¶
func NewSelectionRegistrarImpl() *SelectionRegistrarImpl
NewSelectionRegistrarImpl creates a new SelectionRegistrarImpl.
func NewSelectionRegistrarImplWithInitialId ¶
func NewSelectionRegistrarImplWithInitialId(initialId int64) *SelectionRegistrarImpl
NewSelectionRegistrarImplWithInitialId creates a new SelectionRegistrarImpl with an initial ID. This is used for state restoration.
func (*SelectionRegistrarImpl) GetIncrementId ¶
func (r *SelectionRegistrarImpl) GetIncrementId() int64
GetIncrementId returns the current increment ID value. This is useful for state saving.
func (*SelectionRegistrarImpl) NextSelectableId ¶
func (r *SelectionRegistrarImpl) NextSelectableId() int64
NextSelectableId implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifyPositionChange ¶
func (r *SelectionRegistrarImpl) NotifyPositionChange(selectableId int64)
NotifyPositionChange implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifySelectableChange ¶
func (r *SelectionRegistrarImpl) NotifySelectableChange(selectableId int64)
NotifySelectableChange implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifySelectionUpdate ¶
func (r *SelectionRegistrarImpl) NotifySelectionUpdate( layoutCoordinates layout.LayoutCoordinates, newPosition geometry.Offset, previousPosition geometry.Offset, isStartHandle bool, adjustment SelectionAdjustment, isInTouchMode bool, ) bool
NotifySelectionUpdate implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifySelectionUpdateEnd ¶
func (r *SelectionRegistrarImpl) NotifySelectionUpdateEnd()
NotifySelectionUpdateEnd implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifySelectionUpdateSelectAll ¶
func (r *SelectionRegistrarImpl) NotifySelectionUpdateSelectAll(selectableId int64, isInTouchMode bool)
NotifySelectionUpdateSelectAll implements SelectionRegistrar.
func (*SelectionRegistrarImpl) NotifySelectionUpdateStart ¶
func (r *SelectionRegistrarImpl) NotifySelectionUpdateStart( layoutCoordinates layout.LayoutCoordinates, startPosition geometry.Offset, adjustment SelectionAdjustment, isInTouchMode bool, )
NotifySelectionUpdateStart implements SelectionRegistrar.
func (*SelectionRegistrarImpl) SelectableMap ¶
func (r *SelectionRegistrarImpl) SelectableMap() map[int64]Selectable
SelectableMap returns the map of selectables.
func (*SelectionRegistrarImpl) Selectables ¶
func (r *SelectionRegistrarImpl) Selectables() []Selectable
Selectables returns the list of registered selectables.
func (*SelectionRegistrarImpl) SetSubselections ¶
func (r *SelectionRegistrarImpl) SetSubselections(subs map[int64]*Selection)
SetSubselections updates the subselections map.
func (*SelectionRegistrarImpl) Sort ¶
func (r *SelectionRegistrarImpl) Sort(containerLayoutCoordinates layout.LayoutCoordinates) []Selectable
Sort sorts the list of registered Selectables. Currently the order is geometric-based (y-coordinate first, then x-coordinate).
func (*SelectionRegistrarImpl) Subscribe ¶
func (r *SelectionRegistrarImpl) Subscribe(selectable Selectable) Selectable
Subscribe implements SelectionRegistrar.
func (*SelectionRegistrarImpl) Subselections ¶
func (r *SelectionRegistrarImpl) Subselections() map[int64]*Selection
Subselections implements SelectionRegistrar.
func (*SelectionRegistrarImpl) Unsubscribe ¶
func (r *SelectionRegistrarImpl) Unsubscribe(selectable Selectable)
Unsubscribe implements SelectionRegistrar.
type TextSelectionColors ¶
type TextSelectionColors struct {
// HandleColor is the color used for the selection handles.
HandleColor graphics.Color
// BackgroundColor is the color used for the selection background highlight.
BackgroundColor graphics.Color
}
TextSelectionColors represents the colors used for text selection.
func DefaultTextSelectionColors ¶
func DefaultTextSelectionColors() TextSelectionColors
DefaultTextSelectionColors returns the default text selection colors. Uses a light blue for selection background similar to Android defaults.
func NewTextSelectionColors ¶
func NewTextSelectionColors(handleColor, backgroundColor graphics.Color) TextSelectionColors
NewTextSelectionColors creates new TextSelectionColors with the given colors.
func (TextSelectionColors) Copy ¶
func (c TextSelectionColors) Copy() TextSelectionColors
Copy returns a copy of the TextSelectionColors.
type TextToolbar ¶
type TextToolbar interface {
ShowMenu(rect geometry.Rect, onCopy func(), onSelectAll func())
Hide()
Status() TextToolbarStatus
}
TextToolbar provides text toolbar functionality (copy, paste, etc). This is a platform-specific interface that should be implemented per platform.
type TextToolbarStatus ¶
type TextToolbarStatus int
TextToolbarStatus represents the status of the text toolbar.
const ( // TextToolbarStatusHidden means the toolbar is hidden. TextToolbarStatusHidden TextToolbarStatus = iota // TextToolbarStatusShown means the toolbar is shown. TextToolbarStatusShown )