Draw Box Controller
ViewModel orchestrating drawing state and side effects.
DrawBoxController is the main entry point for state management in DrawBox. It manages three primary responsibilities:
State Management: Holds and exposes the current drawing state via state flow
Intent Processing: Accepts user intents and passes them through the reducer
Side Effects: Emits events and handles persistence operations
MVI Loop:
UI (DrawBox) → Intent → onIntent()
↓
Reducer
↓
New State
↓
Update Flow
↓
UI RecomposesUsage:
@Composable
fun MyDrawingScreen() {
val viewModel = rememberDrawBoxController()
val state by viewModel.state.collectAsState()
DrawBox(
state = state,
onIntent = viewModel::onIntent,
)
}See also
Functions
Bring selected elements to the top of the z-order.
Clear the current selection.
Delete every selected element.
Erase every element whose body intersects a disk of radius world pixels around point. Snapshots history at most once and only if an element is actually removed, so calls that hit nothing leave the undo stack untouched. Useful for programmatic eraser passes; for user-driven erasing the gesture layer in io.ak1.drawbox.DrawBox already drives this via Intent.EraseAt in Mode.ERASER.
Export the current drawing as JSON.
Export the current drawing as a rasterized PNG.
Import a previously exported drawing from JSON string.
Place a bitmap on the canvas. bytes is the raw encoded image (PNG / JPEG / WebP — whatever the renderer's decoder accepts); the SDK owns decoding from there. intrinsicSize is the source image's pixel dimensions, used to preserve aspect ratio when sizing the placed element. position is the world-space anchor that lands at the center of the placed image — drop / paste at the cursor naturally puts the image under the cursor.
Place a plain-text block on the canvas. text may be empty — the sample-app modal-editor flow inserts an empty block first and then dispatches updateText when the user commits content.
Register a custom font family the SDK can resolve by key when rendering io.ak1.drawbox.domain.model.Element.Text elements. Built-in keys (sans, serif, mono) ship with the SDK and don't need registration; calling this is for adding host-provided typefaces.
Reset viewport to identity.
Send selected elements to the bottom of the z-order.
Set a repeating image — typically an SVG vector drawable — painted above the solid setBgColor background and below all strokes/shapes.
Change the canvas background color
Default corner radius applied to new RECTANGLE / TRIANGLE shapes.
Change the world-space radius used by Mode.ERASER.
Default fill color applied to new shapes. null = stroke-only.
Default font family key for the next Intent.InsertText.
Default font size for the next Intent.InsertText.
Change the opacity for new drawings (0.0 to 1.0)
Recolor every selected element's stroke.
Update the corner radius of every selected RECTANGLE / TRIANGLE shape.
Set the fill color of every selected shape. Pass null to clear fill so the shape renders stroke-only.
Set the font family key of every selected text element.
Set the font size of every selected text element.
Toggle the stroke pass on every selected shape. false makes the shape fill-only; true restores its outline.
Update the stroke pattern of every selected shape.
Re-stroke every selected element.
Set the alignment of every selected text element.
Whether new shapes render an outline pass. false = fill-only.
Default stroke pattern applied to new shapes.
Change the stroke width for new drawings
Default alignment for the next Intent.InsertText.
Replace the text content of an existing io.ak1.drawbox.domain.model.Element.Text.
Multiplicatively zoom anchored at focalScreen.
Set absolute scale anchored at focalScreen.