DrawBoxController

class DrawBoxController(reducer: Reducer, initialState: State = State()) : ViewModel

ViewModel orchestrating drawing state and side effects.

DrawBoxController is the main entry point for state management in DrawBox. It manages three primary responsibilities:

  1. State Management: Holds and exposes the current drawing state via state flow

  2. Intent Processing: Accepts user intents and passes them through the reducer

  3. Side Effects: Emits events and handles persistence operations

MVI Loop:

UI (DrawBox) → Intent → onIntent()
↓
Reducer
↓
New State
↓
Update Flow
↓
UI Recomposes

Usage:

@Composable
fun MyDrawingScreen() {
val viewModel = rememberDrawBoxController()
val state by viewModel.state.collectAsState()

DrawBox(
state = state,
onIntent = viewModel::onIntent,
)
}

See also

Constructors

Link copied to clipboard
constructor(reducer: Reducer, initialState: State = State())

Properties

Link copied to clipboard
val canRedo: StateFlow<Boolean>

True when redo is available (previously undone actions exist). Use this to enable/disable redo buttons.

Link copied to clipboard
val canUndo: StateFlow<Boolean>

True when undo is available (elements exist on canvas). Use this to enable/disable undo buttons:

Link copied to clipboard
val events: SharedFlow<Event>

Side effect events like drawing saved, drawing loaded, or errors. Use LaunchedEffect to collect and respond to events:

Link copied to clipboard
val intents: SharedFlow<Intent>

Reactive stream of intents that have been processed by this controller.

Link copied to clipboard
val state: StateFlow<State>

Current drawing state as a reactive flow. Collect this in your UI to receive state updates:

Functions

Link copied to clipboard
expect open fun addCloseable(closeable: AutoCloseable)
expect fun addCloseable(key: String, closeable: AutoCloseable)
Link copied to clipboard

Bring selected elements to the top of the z-order.

Link copied to clipboard

Clear the current selection.

Link copied to clipboard

Delete every selected element.

Link copied to clipboard
fun eraseAt(point: Offset, radius: Float)

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.

Link copied to clipboard

Export the current drawing as JSON.

Link copied to clipboard
fun exportPng(scale: Float = 1.0f, background: Color? = null, textMeasurer: TextMeasurer? = null)

Export the current drawing as a rasterized PNG.

Link copied to clipboard
fun exportSvg()

Export the current drawing as an SVG string.

Link copied to clipboard
expect fun <T : AutoCloseable> getCloseable(key: String): T?
Link copied to clipboard
fun importPath(jsonString: String)

Import a previously exported drawing from JSON string.

Link copied to clipboard
fun insertImage(bytes: ByteArray, intrinsicSize: Size, position: Offset = Offset.Zero)

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.

Link copied to clipboard
fun insertText(text: String, position: Offset = Offset.Zero, fontSize: Float = 24.0f, fontFamilyKey: String = io.ak1.drawbox.domain.model.DEFAULT_FONT_FAMILY_KEY, alignment: TextAlignment = io.ak1.drawbox.domain.model.TextAlignment.LEFT, color: Color = Color.Black)

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.

Link copied to clipboard
fun onIntent(intent: Intent)

Process an intent and update state accordingly.

Link copied to clipboard
fun panBy(delta: Offset)

Translate the camera by delta screen pixels.

Link copied to clipboard
fun redo()

Redo the last undone action

Link copied to clipboard
fun registerFont(key: String, family: FontFamily)

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.

Link copied to clipboard
fun reset()

Clear the canvas and reset to empty state

Link copied to clipboard

Reset viewport to identity.

Link copied to clipboard
fun selectAt(offset: Offset, tolerance: Float = 8.0f)

Pick the topmost element at the given canvas offset (or clear selection on miss).

Link copied to clipboard

Send selected elements to the bottom of the z-order.

Link copied to clipboard
fun setBackgroundPattern(painter: Painter?, tint: Color? = null)

Set a repeating image — typically an SVG vector drawable — painted above the solid setBgColor background and below all strokes/shapes.

Link copied to clipboard
fun setBgColor(color: Color)

Change the canvas background color

Link copied to clipboard
fun setColor(color: Color)

Change the stroke color for new drawings

Link copied to clipboard
fun setCornerRadius(radius: Float)

Default corner radius applied to new RECTANGLE / TRIANGLE shapes.

Link copied to clipboard
fun setEraserSize(size: Float)

Change the world-space radius used by Mode.ERASER.

Link copied to clipboard
fun setFillColor(color: Color?)

Default fill color applied to new shapes. null = stroke-only.

Link copied to clipboard
fun setFontFamily(fontFamilyKey: String)

Default font family key for the next Intent.InsertText.

Link copied to clipboard
fun setFontSize(size: Float)

Default font size for the next Intent.InsertText.

Link copied to clipboard
fun setMode(mode: Mode)

Switch to a different drawing mode

Link copied to clipboard
fun setOpacity(opacity: Float)

Change the opacity for new drawings (0.0 to 1.0)

Link copied to clipboard

Recolor every selected element's stroke.

Link copied to clipboard

Update the corner radius of every selected RECTANGLE / TRIANGLE shape.

Link copied to clipboard

Set the fill color of every selected shape. Pass null to clear fill so the shape renders stroke-only.

Link copied to clipboard
fun setSelectionFontFamily(fontFamilyKey: String)

Set the font family key of every selected text element.

Link copied to clipboard

Set the font size of every selected text element.

Link copied to clipboard

Toggle the stroke pass on every selected shape. false makes the shape fill-only; true restores its outline.

Link copied to clipboard

Update the stroke pattern of every selected shape.

Link copied to clipboard

Re-stroke every selected element.

Link copied to clipboard

Set the alignment of every selected text element.

Link copied to clipboard

Whether new shapes render an outline pass. false = fill-only.

Link copied to clipboard

Default stroke pattern applied to new shapes.

Link copied to clipboard
fun setStrokeWidth(width: Float)

Change the stroke width for new drawings

Link copied to clipboard

Default alignment for the next Intent.InsertText.

Link copied to clipboard
fun undo()

Undo the last drawing action

Link copied to clipboard
fun updateText(id: String, text: String)

Replace the text content of an existing io.ak1.drawbox.domain.model.Element.Text.

Link copied to clipboard
fun zoomBy(factor: Float, focalScreen: Offset)

Multiplicatively zoom anchored at focalScreen.

Link copied to clipboard
fun zoomTo(targetScale: Float, focalScreen: Offset)

Set absolute scale anchored at focalScreen.