diff --git a/gallery/example2.pdf b/gallery/example2.pdf new file mode 100644 index 0000000..364a2ee Binary files /dev/null and b/gallery/example2.pdf differ diff --git a/gallery/example2.typ b/gallery/example2.typ new file mode 100644 index 0000000..8f0e5fb --- /dev/null +++ b/gallery/example2.typ @@ -0,0 +1,33 @@ +#import "/src/lib.typ" as chronos + + +#chronos.diagram({ + import "/src/diagram.typ": * + _seq("User", "A", comment: "DoWork", enable-dst: true) + _seq("A", "B", comment: [#sym.quote.angle.l createRequest #sym.quote.angle.r], enable-dst: true) + _seq("B", "C", comment: "DoWork", enable-dst: true) + _seq("C", "B", comment: "WorkDone", destroy-src: true, dashed: true) + _seq("B", "A", comment: "RequestCreated", disable-src: true, dashed: true) + _seq("A", "User", comment: "Done", disable-src: true) +}) + +#chronos.diagram({ + import "/src/diagram.typ": * + _seq("User", "A", comment: "DoWork", enable-dst: true) + _seq("A", "A", comment: "Internal call", enable-dst: true) + _seq("A", "B", comment: [#sym.quote.angle.l createRequest #sym.quote.angle.r], enable-dst: true) + _seq("B", "A", comment: "RequestCreated", disable-src: true, disable-dst: true, dashed: true) + _seq("A", "User", comment: "Done", disable-src: true) +}) + +#chronos.diagram({ + import "/src/diagram.typ": * + _seq("alice", "bob", comment: "hello", enable-dst: true) + _seq("bob", "bob", comment: "self call", enable-dst: true) + _seq("bill", "bob", comment: "hello from thread 2", enable-dst: true) + _seq("bob", "george", comment: "create", enable-dst: true) + _seq("bob", "bill", comment: "done in thread 2", disable-src: true, dashed: true) + _seq("bob", "bob", comment: "rc", disable-src: true, dashed: true) + _seq("bob", "george", comment: "delete", destroy-dst: true) + _seq("bob", "alice", comment: "success", disable-src: true, dashed: true) +}) \ No newline at end of file diff --git a/src/diagram.typ b/src/diagram.typ index 31312c9..cd4c168 100644 --- a/src/diagram.typ +++ b/src/diagram.typ @@ -8,7 +8,12 @@ dashed: false, tip: "default", color: black, - flip: false + flip: false, + enable-dst: false, + disable-dst: false, + destroy-dst: false, + disable-src: false, + destroy-src: false, ) = { return (( type: "seq", @@ -18,7 +23,12 @@ dashed: dashed, tip: tip, color: color, - flip: flip + flip: flip, + enable-dst: enable-dst, + disable-dst: disable-dst, + destroy-dst: destroy-dst, + disable-src: disable-src, + destroy-src: destroy-src, ),) } diff --git a/src/renderer.typ b/src/renderer.typ index 0c4a00c..7bf0f10 100644 --- a/src/renderer.typ +++ b/src/renderer.typ @@ -5,9 +5,15 @@ #let PAR-PAD = (5pt, 3pt) #let PAR-SPACE = 10 #let COMMENT-PAD = 8 +#let LIFELINE-W = 10 #let get-columns-width(participants, elements) = { + participants = participants.map(p => { + p.insert("lifeline-lvl", 0) + p.insert("max-lifelines", 0) + p + }) let pars-i = get-participants-i(participants) let cells = () for elmt in elements { @@ -23,6 +29,23 @@ cell: box(com, inset: 3pt) ) ) + + if elmt.disable-src or elmt.destroy-src { + let p = participants.at(i1) + p.lifeline-lvl -= 1 + participants.at(i1) = p + } + if elmt.disable-dst { + let p = participants.at(i2) + p.lifeline-lvl -= 1 + participants.at(i2) = p + } + if elmt.enable-dst { + let p = participants.at(i2) + p.lifeline-lvl += 1 + p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl) + participants.at(i2) = p + } } } @@ -54,6 +77,15 @@ m.width / 1pt - widths.slice(0, cell.i2 - 1).sum() ) } + for (i, w) in widths.enumerate() { + let p1 = participants.at(i) + let p2 = participants.at(i + 1) + let w = w + p1.max-lifelines * LIFELINE-W / 2 + if p2.max-lifelines != 0 { + w += LIFELINE-W / 2 + } + widths.at(i) = w + } return widths } @@ -119,12 +151,68 @@ let y = -Y-SPACE let groups = () + let lifelines = participants.map(_ => ( + level: 0, + lines: () + )) // Draw sequences for elmt in elements { if elmt.type == "seq" { - let x1 = x-pos.at(pars-i.at(elmt.p1)) - let x2 = x-pos.at(pars-i.at(elmt.p2)) + let i1 = pars-i.at(elmt.p1) + let i2 = pars-i.at(elmt.p2) + + if elmt.comment != none { + y -= measure(box(elmt.comment)).height / 1pt + 6 + } + + if elmt.disable-src { + let src-line = lifelines.at(i1) + src-line.level -= 1 + src-line.lines.push(("disable", y, auto)) + lifelines.at(i1) = src-line + } + if elmt.destroy-src { + let src-line = lifelines.at(i1) + src-line.level -= 1 + src-line.lines.push(("destroy", y, auto)) + lifelines.at(i1) = src-line + } + + let ll-lvl1 = lifelines.at(i1).level * LIFELINE-W / 2 + + if elmt.disable-dst { + let dst-line = lifelines.at(i2) + dst-line.level -= 1 + dst-line.lines.push(("disable", y, auto)) + lifelines.at(i2) = dst-line + } + if elmt.destroy-dst { + let dst-line = lifelines.at(i2) + dst-line.level -= 1 + dst-line.lines.push(("destroy", y, auto)) + lifelines.at(i2) = dst-line + } + if elmt.enable-dst { + let dst-line = lifelines.at(i2) + dst-line.level += 1 + lifelines.at(i2) = dst-line + } + + let x1 = x-pos.at(i1) + let x2 = x-pos.at(i2) + + let ll-lvl2 = lifelines.at(i2).level * LIFELINE-W / 2 + + let f = if elmt.flip {-1} else {1} + if i1 <= i2 { + x1 += ll-lvl1 * f + x2 -= ll-lvl2 * f + } else { + x1 -= ll-lvl1 * f + x2 += ll-lvl2 * f + } + let style = ( mark: (end: "straight"), stroke: ( @@ -134,10 +222,11 @@ ) if elmt.p1 == elmt.p2 { + let x3 = x1 - ll-lvl1 + ll-lvl2 + x2 = if elmt.flip {x1 - 20} else {x1 + 20} if elmt.comment != none { - y -= measure(box(elmt.comment)).height / 1pt + 6 draw.content( (x1, y), elmt.comment, @@ -150,7 +239,7 @@ (x1, y), (x2, y), (x2, y - 10), - (x1, y - 10), + (x3, y - 10), ..style ) y -= 10 @@ -161,7 +250,6 @@ if x2 < x1 { x += COMMENT-PAD } - y -= measure(box(elmt.comment)).height / 1pt + 6 draw.content( (x, y), elmt.comment, @@ -176,6 +264,11 @@ ..style ) } + if elmt.enable-dst { + let dst-line = lifelines.at(i2) + dst-line.lines.push(("enable", y, auto)) + lifelines.at(i2) = dst-line + } y -= Y-SPACE } else if elmt.type == "grp" { @@ -244,6 +337,37 @@ (x, y), stroke: (dash: "dashed", paint: gray.darken(40%)) ) + + let rects = () + let destructions = () + let lines = () + for line in lifelines.at(i).lines { + let event = line.first() + if event == "enable" { + lines.push(line) + } else if event == "disable" or event == "destroy" { + let l = lines.pop() + let lvl = lines.len() + rects.push((x + lvl * LIFELINE-W / 2, l.at(1), line.at(1))) + if event == "destroy" { + destructions.push((x + lvl * LIFELINE-W / 2, line.at(1))) + } + } + } + + for rect in rects.rev() { + let (cx, y0, y1) = rect + draw.rect( + (cx - LIFELINE-W / 2, y0), + (cx + LIFELINE-W / 2, y1), + ) + } + for dest in destructions { + let (cx, cy) = dest + draw.line((cx - 8, cy - 8), (cx + 8, cy + 8), stroke: red + 2pt) + draw.line((cx - 8, cy + 8), (cx + 8, cy - 8), stroke: red + 2pt) + } + draw.content( (x, y), p.display-name,