Sudha247 / ocaml-joy

MIT License
21 stars 14 forks source link

Context API: support multiple types of contexts #119

Open nikochiko opened 4 months ago

nikochiko commented 4 months ago

We should be able to support multiple types of context.

So far, the API is restricted to the Cairo backend and assumes that it will be the only one.

I suggest that we:

e.g. the context type that is as of now a record that immediately stores the cairo context and surface, will change to a sum type:

type context =
    CairoContext of Backends.Cairo.context
  | SVGContext of Backends.SVG.context

The global context will be of context type, then we change the rendering logic to do a match-case on the context before we pass it to the actual backend.

The interface for each backend should be something like:

type context = anything_at_all

val show: (?ctx: context) -> Joy.shape list -> unit  (* ctx defaults to the global context *)
nikochiko commented 4 months ago

We can also do a LazyContext that only stores the list of shapes that are show-n. At the end, depending on the function that the user calls (writePNG or writeSVG or whatever), and from introspecting the environment, we can smartly guess the context and give the user what they probably want.