diff --git a/packages/day-card/README.md b/packages/day-card/README.md index 76a9622..ede3d9f 100644 --- a/packages/day-card/README.md +++ b/packages/day-card/README.md @@ -1,8 +1,8 @@ # @magpie-community/page-day-card -A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. **Background** picks what the paper lies on: teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. +A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. **Background** picks what the paper lies on: nothing (the paper alone, a transparent PNG), teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. -Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. Pick an earlier day with the arrows or the date's menu. +Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. Pick an earlier day with the arrows or the date's menu. The card is shown as large as fits under the toolbar, so a whole day is on one screen; the picture you copy or save is full size. It needs a magpie with the `usage:day` read and `magpie.image` (see `engines.magpie`). An older magpie refuses the read, and the page says to update. diff --git a/packages/day-card/day-card.test.mjs b/packages/day-card/day-card.test.mjs index ac84cc4..772d4f7 100644 --- a/packages/day-card/day-card.test.mjs +++ b/packages/day-card/day-card.test.mjs @@ -236,6 +236,24 @@ test("every backdrop has a name in every language, and its picture comes with th expect(html.indexOf('src="backgrounds.js"')).toBeLessThan(html.indexOf('src="app.js"')) }) +test("no background is the first choice, and the card fits the room it has", () => { + expect(B.list[0].id).toBe("none") + expect(B.norm({ bg: "none" }).bg).toBe("none") + expect(B.meta("none").photo).toBe(false) + // nothing is drawn under the paper, so what was there is cleared + const calls = [] + const ctx = { save() {}, restore() {}, clearRect: (...a) => calls.push(a), fillRect() { throw new Error("drew") } } + B.paint(ctx, 440, 900, 2, B.norm({ bg: "none" }), null) + expect(calls).toEqual([[0, 0, 440, 900]]) + // fit: wide room, short screen -> the height rules; narrow room -> the width + expect(B.fit(900, 500, 592, 1100, 760, 160)).toBe(269) + expect(B.fit(300, 2000, 592, 1100, 760, 160)).toBe(300) + expect(B.fit(2000, 2000, 592, 1100, 760, 160)).toBe(760) + expect(B.fit(900, 10, 592, 1100, 760, 160)).toBe(160) + expect(B.fit(100, 10, 592, 1100, 760, 160)).toBe(100) + expect(read("app.js")).toContain("fitCard") +}) + test("a saved backdrop is made sound before it is drawn", () => { expect(B.norm(null)).toEqual({ bg: "teal", pad: 76, blur: 0, dim: 0, image: "" }) expect(B.norm({ bg: "seigaiha", pad: 500, blur: -3, dim: "40" })).toEqual({ bg: "seigaiha", pad: 160, blur: 0, dim: 40, image: "" }) diff --git a/packages/day-card/package.json b/packages/day-card/package.json index 3cccf6a..a373716 100644 --- a/packages/day-card/package.json +++ b/packages/day-card/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/page-day-card", - "version": "0.2.1", + "version": "0.2.2", "description": "A page in magpie's window that draws a day of your AI usage as a card: tokens, cost, each agent and its models, your streak and your average day. Copy it as a picture or save it as a PNG", "files": ["page", "README.md"], "keywords": ["magpie", "magpie-page", "usage", "tokens", "cost", "card"], diff --git a/packages/day-card/page/app.js b/packages/day-card/page/app.js index e6c7b21..bb7d577 100644 --- a/packages/day-card/page/app.js +++ b/packages/day-card/page/app.js @@ -333,7 +333,8 @@ // measure, then draw const ph = slip(document.createElement("canvas").getContext("2d"), c, new Map(), 1) + TOOTH; - const side = bg.pad; + const none = bg.bg === "none"; + const side = none ? 0 : bg.pad; const W = PW + side * 2; const H = ph + side * 2; const top = side; @@ -361,7 +362,7 @@ paper.lineTo(i * tw, ph - TOOTH); } paper.closePath(); - ctx.shadowColor = B.meta(bg.bg).shadow; + ctx.shadowColor = none ? "transparent" : B.meta(bg.bg).shadow; ctx.shadowBlur = 36; ctx.shadowOffsetY = 12; ctx.fillStyle = PAPER; @@ -370,8 +371,26 @@ slip(ctx, c, icons, scale); ctx.restore(); $("copy").disabled = $("save").disabled = !magpie.image; + state.size = [W, H]; + fitCard(); } + // The card is drawn as large as the page allows, but no taller than the + // room under the bar, so a whole day is on one screen. The bitmap (and + // the picture copied or saved) is unchanged: only its size on the page. + function fitCard() { + if (!state.size) return; + const [W, H] = state.size; + const m = $("main"); + const cs = getComputedStyle(document.body); + const availW = m.clientWidth; + const availH = window.innerHeight - m.getBoundingClientRect().top - (parseFloat(cs.paddingBottom) || 0); + $("card").style.width = `${B.fit(availW, availH, W, H, 760, 160)}px`; + } + addEventListener("resize", fitCard); + new ResizeObserver(fitCard).observe(document.querySelector("header")); + new ResizeObserver(fitCard).observe($("status")); + // ---- reading a day ---- async function load(date) { const n = ++state.loading; @@ -525,6 +544,12 @@ cv.width = cv.height = size * s; const ctx = cv.getContext("2d"); ctx.setTransform(s, 0, 0, s, 0, 0); + if (id === "none") { + // nothing, shown the way a picture editor shows it: a chequerboard + const q = size / 6; + for (let y = 0; y < 6; y++) for (let x = 0; x < 6; x++) { ctx.fillStyle = (x + y) % 2 ? "#cfcfd4" : "#fafafa"; ctx.fillRect(x * q, y * q, q, q); } + return cv; + } B.paint(ctx, size, size, s, { ...state.bg, bg: id }, null); return cv; } @@ -575,6 +600,7 @@ $("blur").value = bg.blur; $("dim").value = bg.dim; pop.classList.toggle("photo", B.meta(bg.bg).photo === true); + pop.classList.toggle("none", bg.bg === "none"); paintChip(); } async function paintChip() { @@ -586,7 +612,8 @@ const bg = state.bg; const img = await backdrop(bg); if (bg !== state.bg) return; - B.paint(ctx, 14, 14, s, bg, img); + if (bg.bg === "none") ctx.drawImage(patternCanvas("none", 14), 0, 0, 14, 14); + else B.paint(ctx, 14, 14, s, bg, img); } function openPop() { diff --git a/packages/day-card/page/backgrounds.js b/packages/day-card/page/backgrounds.js index e72917a..f4c9725 100644 --- a/packages/day-card/page/backgrounds.js +++ b/packages/day-card/page/backgrounds.js @@ -4,7 +4,9 @@ // test can check it; paint draws on a canvas. (() => { // shadow: the paper's shadow on that ground + // none: the paper alone, on nothing (a transparent PNG), with no margin const LIST = [ + { id: "none", shadow: "transparent" }, { id: "teal", shadow: "rgba(0, 60, 55, .28)" }, { id: "gingham", shadow: "rgba(90, 30, 20, .22)" }, { id: "grid", shadow: "rgba(30, 50, 90, .2)" }, @@ -219,10 +221,19 @@ function paint(ctx, W, H, scale, o, img) { ctx.save(); const m = meta(o.bg); - if (m.photo && img) photo(ctx, W, H, scale, img, o.blur, o.dim); + if (o.bg === "none") ctx.clearRect(0, 0, W, H); + else if (m.photo && img) photo(ctx, W, H, scale, img, o.blur, o.dim); else (PATTERNS[o.bg] || PATTERNS.teal)(ctx, W, H, scale); ctx.restore(); } - globalThis.DayBackgrounds = { list: LIST, meta, norm, paint, PAD, BLUR, DIM }; + // the size the card is drawn at on the page: as wide as the page allows + // (up to max), and no taller than the room it has, so a day's card is on + // one screen; never below min + function fit(availW, availH, W, H, max, min) { + const w = Math.min(max, availW, availH > 0 ? availH * W / H : Infinity); + return Math.max(Math.min(min, availW), Math.floor(Number.isFinite(w) ? w : max)); + } + + globalThis.DayBackgrounds = { list: LIST, meta, norm, paint, fit, PAD, BLUR, DIM }; })(); diff --git a/packages/day-card/page/index.html b/packages/day-card/page/index.html index 9dd697b..041b145 100644 --- a/packages/day-card/page/index.html +++ b/packages/day-card/page/index.html @@ -27,7 +27,7 @@