import { UIExpression } from "../../miscellaneous/UIExpression"; import { UIPriority } from "../../miscellaneous/UIPriority"; import { UIRelation } from "../../miscellaneous/UIRelation"; import type { UISolverWrapperView } from "./UISolverWrapper.Internal"; /** * Wrapper around the Kiwi constraint solver providing enhanced UI-specific functionality. * * UISolverWrapper abstracts the Kiwi (Cassowary) constraint solver to provide a more * UI-friendly interface with additional features like dynamic constraint modification, * required variables (P0 priority), partial constraint updates, and automatic solver * recovery. It manages variables and constraints through index-based references, * allowing for easy tracking and modification of layout constraints. * * Key features: * - Index-based variable and constraint management * - Required variables with P0 priority (always satisfied) * - Dynamic constraint modification without full rebuilds * - Automatic solver recovery on constraint conflicts * - Lazy evaluation with recalculation flags * * @see {@link UIExpression} - Expression system for constraint definitions * @see {@link UIPriority} - Priority levels for variables and constraints * @see {@link UIRelation} - Relational operators for constraints */ export declare class UISolverWrapper implements UISolverWrapperView { /** The underlying Kiwi solver instance, undefined when solver needs rebuilding. */ private solver?; /** Flag indicating whether variable values need recalculation. */ private recalculationRequired; /** Map of variable indices to their descriptions. */ private readonly variables; /** Counter for generating unique variable indices. */ private lastVariableIndex; /** Map of constraint indices to their descriptions. */ private readonly constraints; /** Counter for generating unique constraint indices. */ private lastConstraintIndex; /** * Creates a new solver variable with the specified value and priority. * * Variables with P0 priority are treated as required (must be satisfied exactly) * and are implemented using equality constraints. Other priorities use edit * variables that can be adjusted by the solver. * * @param value - Initial value for the variable * @param priority - Priority level for constraint solving * @returns Unique index identifier for the created variable */ createVariable(value: number, priority: UIPriority): number; /** * Removes a variable from the solver system. * * The variable must not be referenced by any existing constraints. * Cleans up both the variable itself and any associated required constraint. * * @param index - Index of the variable to remove * @throws Will throw an error if the variable doesn't exist or is still in use */ removeVariable(index: number): void; /** * Suggests a new value for an existing variable. * * For P0 priority variables, rebuilds the associated constraint. * For other priorities, suggests the value to the solver's edit system. * Only triggers updates if the value actually changes. * * @param index - Index of the variable to update * @param value - New suggested value * @throws Will throw an error if the variable doesn't exist */ suggestVariableValue(index: number, value: number): void; /** * Changes the priority of an existing variable. * * Switching to/from P0 priority changes how the variable is handled: * - P0: Creates required constraint (must be satisfied exactly) * - Other: Uses edit variable system (can be adjusted by solver) * * @param index - Index of the variable to modify * @param priority - New priority level * @throws Will throw an error if the variable doesn't exist */ setVariablePriority(index: number, priority: UIPriority): void; /** * Reads the current solved value of a variable. * * Triggers solver recalculation if needed and rebuilds the solver * if it was invalidated due to constraint conflicts. * * @param index - Index of the variable to read * @returns Current solved value of the variable * @throws Will throw an error if the variable doesn't exist */ readVariableValue(index: number): number; /** * Creates a new constraint between two expressions. * * Constraints can be enabled or disabled, and all properties can be * modified later without recreating the constraint. * * @param lhs - Left-hand side expression * @param rhs - Right-hand side expression * @param relation - Relational operator (equal, less than, greater than) * @param priority - Priority level for constraint solving * @param enabled - Whether the constraint is initially active * @returns Unique index identifier for the created constraint */ createConstraint(lhs: UIExpression, rhs: UIExpression, relation: UIRelation, priority: UIPriority, enabled: boolean): number; /** * Removes a constraint from the solver system. * * If the constraint is currently enabled, it will be removed from * the active solver before deletion. * * @param index - Index of the constraint to remove * @throws Will throw an error if the constraint doesn't exist */ removeConstraint(index: number): void; /** * Updates the left-hand side expression of an existing constraint. * * Rebuilds the underlying constraint with the new expression while * preserving all other properties. * * @param index - Index of the constraint to modify * @param lhs - New left-hand side expression * @throws Will throw an error if the constraint doesn't exist */ setConstraintLHS(index: number, lhs: UIExpression): void; /** * Updates the right-hand side expression of an existing constraint. * * Rebuilds the underlying constraint with the new expression while * preserving all other properties. * * @param index - Index of the constraint to modify * @param rhs - New right-hand side expression * @throws Will throw an error if the constraint doesn't exist */ setConstraintRHS(index: number, rhs: UIExpression): void; /** * Updates the relational operator of an existing constraint. * * Changes how the left and right expressions are compared * (equal, less than, greater than). * * @param index - Index of the constraint to modify * @param relation - New relational operator * @throws Will throw an error if the constraint doesn't exist */ setConstraintRelation(index: number, relation: UIRelation): void; /** * Updates the priority level of an existing constraint. * * Higher priority constraints are more likely to be satisfied * when conflicts arise. * * @param index - Index of the constraint to modify * @param priority - New priority level * @throws Will throw an error if the constraint doesn't exist */ setConstraintPriority(index: number, priority: UIPriority): void; /** * Enables or disables an existing constraint. * * Disabled constraints are not considered during solving but * remain in the system for potential re-enabling. * * @param index - Index of the constraint to modify * @param enabled - Whether the constraint should be active * @throws Will throw an error if the constraint doesn't exist */ setConstraintEnabled(index: number, enabled: boolean): void; /** * Rebuilds a variable in the solver system with new properties. * * Temporarily removes all constraints that reference the variable, * updates the variable configuration, then rebuilds and re-adds * the dependent constraints. */ private rebuildVariable; /** * Rebuilds a constraint based on its current description. * * Uses different rebuild strategies depending on whether the * constraint is currently enabled. */ private rebuildConstraintByDescription; /** * Rebuilds a constraint by removing the old one and creating a new one. * * Safely handles solver state transitions and constraint replacement. */ private rebuildConstraint; /** * Creates a new Kiwi constraint from UI expressions and parameters. * * Converts UI-level expressions and enums to Kiwi-compatible formats. */ private buildConstraint; /** * Converts a UIExpression to a Kiwi Expression. * * Maps variable indices to their corresponding Kiwi Variable instances * and preserves coefficients and constants. */ private convertExpression; /** * Completely rebuilds the solver from scratch. * * Re-adds all variables and constraints to a fresh solver instance. * Used for recovery when the solver enters an invalid state. * * The solver is normally dropped because some constraint was rejected by kiwi, * and that same constraint is still registered here. Replaying it would throw * again, so every replay is guarded individually: a constraint kiwi refuses is * skipped and stays registered but unapplied, which is exactly the state it was * already in. The fresh solver is only published once the replay is complete, * so a rejection can never leave a half-built solver in place. */ private rebuildSolver; }