added basis for lifelines

This commit is contained in:
Louis Heredero 2024-06-18 20:32:39 +02:00
parent 0e0be4e76a
commit 94d0eb286e
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 174 additions and 7 deletions

BIN
gallery/example2.pdf Normal file

Binary file not shown.

33
gallery/example2.typ Normal file
View File

@ -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)
})

View File

@ -8,7 +8,12 @@
dashed: false, dashed: false,
tip: "default", tip: "default",
color: black, color: black,
flip: false flip: false,
enable-dst: false,
disable-dst: false,
destroy-dst: false,
disable-src: false,
destroy-src: false,
) = { ) = {
return (( return ((
type: "seq", type: "seq",
@ -18,7 +23,12 @@
dashed: dashed, dashed: dashed,
tip: tip, tip: tip,
color: color, 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,
),) ),)
} }

View File

@ -5,9 +5,15 @@
#let PAR-PAD = (5pt, 3pt) #let PAR-PAD = (5pt, 3pt)
#let PAR-SPACE = 10 #let PAR-SPACE = 10
#let COMMENT-PAD = 8 #let COMMENT-PAD = 8
#let LIFELINE-W = 10
#let get-columns-width(participants, elements) = { #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 pars-i = get-participants-i(participants)
let cells = () let cells = ()
for elmt in elements { for elmt in elements {
@ -23,6 +29,23 @@
cell: box(com, inset: 3pt) 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() 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 return widths
} }
@ -119,12 +151,68 @@
let y = -Y-SPACE let y = -Y-SPACE
let groups = () let groups = ()
let lifelines = participants.map(_ => (
level: 0,
lines: ()
))
// Draw sequences // Draw sequences
for elmt in elements { for elmt in elements {
if elmt.type == "seq" { if elmt.type == "seq" {
let x1 = x-pos.at(pars-i.at(elmt.p1)) let i1 = pars-i.at(elmt.p1)
let x2 = x-pos.at(pars-i.at(elmt.p2)) 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 = ( let style = (
mark: (end: "straight"), mark: (end: "straight"),
stroke: ( stroke: (
@ -134,10 +222,11 @@
) )
if elmt.p1 == elmt.p2 { if elmt.p1 == elmt.p2 {
let x3 = x1 - ll-lvl1 + ll-lvl2
x2 = if elmt.flip {x1 - 20} else {x1 + 20} x2 = if elmt.flip {x1 - 20} else {x1 + 20}
if elmt.comment != none { if elmt.comment != none {
y -= measure(box(elmt.comment)).height / 1pt + 6
draw.content( draw.content(
(x1, y), (x1, y),
elmt.comment, elmt.comment,
@ -150,7 +239,7 @@
(x1, y), (x1, y),
(x2, y), (x2, y),
(x2, y - 10), (x2, y - 10),
(x1, y - 10), (x3, y - 10),
..style ..style
) )
y -= 10 y -= 10
@ -161,7 +250,6 @@
if x2 < x1 { if x2 < x1 {
x += COMMENT-PAD x += COMMENT-PAD
} }
y -= measure(box(elmt.comment)).height / 1pt + 6
draw.content( draw.content(
(x, y), (x, y),
elmt.comment, elmt.comment,
@ -176,6 +264,11 @@
..style ..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 y -= Y-SPACE
} else if elmt.type == "grp" { } else if elmt.type == "grp" {
@ -244,6 +337,37 @@
(x, y), (x, y),
stroke: (dash: "dashed", paint: gray.darken(40%)) 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( draw.content(
(x, y), (x, y),
p.display-name, p.display-name,