Khunndev Cursor commited on
Commit
ffdf130
·
1 Parent(s): 0758854

Smaller spatial circle labels and settings update-app button.

Browse files

Number pipes top-left to bottom-right, tighten label sizing, and reload home with a cache-busting timestamp from settings.

Co-authored-by: Cursor <cursoragent@cursor.com>

frontend/src/components/ResultCanvas.tsx CHANGED
@@ -36,20 +36,37 @@ function easeOut(t: number): number {
36
  return 1 - (1 - t) * (1 - t)
37
  }
38
 
39
- interface SortedDet {
40
  d: Detection
41
- origIdx: number
42
  }
43
 
44
- function sortSpatially(detections: Detection[]): SortedDet[] {
45
- return [...detections]
46
- .map((d, i) => ({ d, origIdx: i }))
 
 
 
 
 
 
 
 
 
 
47
  .sort((a, b) => {
48
- const rowA = Math.round(((a.d.ymin + a.d.ymax) / 2) / 40)
49
- const rowB = Math.round(((b.d.ymin + b.d.ymax) / 2) / 40)
50
- if (rowA !== rowB) return rowA - rowB
51
- return (a.d.xmin + a.d.xmax) / 2 - (b.d.xmin + b.d.xmax) / 2
52
  })
 
 
 
 
 
 
 
 
 
53
  }
54
 
55
  function computeStagger(count: number): { staggerMs: number; durationMs: number } {
@@ -82,19 +99,18 @@ function fitLabelFontSize(
82
  radius: number,
83
  displayPx: number,
84
  ): number {
85
- const minFs = 6 / displayPx
86
- // upper bound: text should not exceed ~78% of the inner diameter
87
- let fs = Math.min(radius * 1.2, 20 / displayPx)
88
- const maxW = radius * 2 * 0.78
89
- const maxH = radius * 2 * 0.62
90
-
91
- for (let i = 0; i < 12; i++) {
92
- ctx.font = `bold ${fs}px sans-serif`
93
  const m = ctx.measureText(label)
94
  const w = m.width
95
  const h = (m.actualBoundingBoxAscent ?? fs * 0.8) + (m.actualBoundingBoxDescent ?? fs * 0.2)
96
  if (w <= maxW && h <= maxH) break
97
- fs = Math.max(minFs, fs * 0.88)
98
  }
99
  return Math.max(minFs, fs)
100
  }
@@ -157,7 +173,7 @@ function drawCircle(
157
  ctx.arc(cx, cy, Math.max(1, radius - lw * 0.55), 0, Math.PI * 2)
158
  ctx.clip()
159
 
160
- const outline = Math.max(0.5 / displayPx, fs * 0.1)
161
  ctx.lineJoin = 'round'
162
  ctx.lineWidth = outline
163
  ctx.strokeStyle = `rgba(0,0,0,${0.6 * alpha})`
@@ -301,7 +317,7 @@ export function ResultCanvas({
301
 
302
  let visibleCount = 0
303
 
304
- sorted.forEach(({ d, origIdx }, animIdx) => {
305
  const startAt = animIdx * staggerMs
306
  if (elapsed < startAt) return
307
  const progress = (elapsed - startAt) / durationMs
@@ -317,7 +333,7 @@ export function ResultCanvas({
317
  const cy = d.ymin + ph / 2
318
  const radius = fitRadius(pw, ph, minR)
319
 
320
- drawCircle(ctx!, cx, cy, radius, rgb, progress, String(origIdx + 1), showLabels, displayPx)
321
  visibleCount++
322
  })
323
 
@@ -332,7 +348,7 @@ export function ResultCanvas({
332
  // final static redraw with all circles fully complete
333
  ctx!.clearRect(0, 0, natW, natH)
334
  ctx!.drawImage(image, 0, 0, natW, natH)
335
- sorted.forEach(({ d, origIdx }) => {
336
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
337
  const color = group?.color ?? '#ef4444'
338
  const rgb = hexToRgb(color)
@@ -341,7 +357,7 @@ export function ResultCanvas({
341
  const cx = d.xmin + pw / 2
342
  const cy = d.ymin + ph / 2
343
  const radius = fitRadius(pw, ph, minR)
344
- drawCircle(ctx!, cx, cy, radius, rgb, 1, String(origIdx + 1), showLabels, displayPx)
345
  })
346
  if (countDisplayRef?.current) {
347
  countDisplayRef.current.textContent = String(detections.length)
@@ -411,7 +427,9 @@ export function ResultCanvas({
411
  const showLabels = detections.length <= 3000
412
  const minR = (dense ? 5 : 7) / displayPx
413
 
414
- detections.forEach((d, idx) => {
 
 
415
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
416
  const color = group?.color ?? '#ef4444'
417
  const rgb = hexToRgb(color)
@@ -420,7 +438,8 @@ export function ResultCanvas({
420
  const cx = d.xmin + pw / 2
421
  const cy = d.ymin + ph / 2
422
  const radius = fitRadius(pw, ph, minR)
423
- drawCircle(ctx, cx, cy, radius, rgb, 1, String(idx + 1), showLabels, displayPx)
 
424
  })
425
 
426
  const area = areaRef.current
@@ -460,12 +479,25 @@ export function ResultCanvas({
460
  ctx.clearRect(0, 0, natW, natH)
461
  ctx.drawImage(image, 0, 0, natW, natH)
462
 
463
- detections.forEach((d, idx) => {
 
 
464
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
465
  const rgb = hexToRgb(group?.color ?? '#ef4444')
466
  const pw = d.xmax - d.xmin
467
  const ph = d.ymax - d.ymin
468
- drawCircle(ctx, d.xmin + pw / 2, d.ymin + ph / 2, fitRadius(pw, ph, minR), rgb, 1, String(idx + 1), showLabels, displayPx)
 
 
 
 
 
 
 
 
 
 
 
469
  })
470
 
471
  const area = areaRef.current
 
36
  return 1 - (1 - t) * (1 - t)
37
  }
38
 
39
+ interface RankedDet {
40
  d: Detection
41
+ label: number
42
  }
43
 
44
+ function sortSpatially(detections: Detection[]): RankedDet[] {
45
+ const items = detections.map((d) => ({
46
+ d,
47
+ cy: (d.ymin + d.ymax) / 2,
48
+ cx: (d.xmin + d.xmax) / 2,
49
+ h: d.ymax - d.ymin,
50
+ }))
51
+
52
+ const heights = items.map((x) => x.h).sort((a, b) => a - b)
53
+ const medianH = heights[Math.floor(heights.length / 2)] ?? 40
54
+ const rowTol = Math.max(14, medianH * 0.45)
55
+
56
+ return items
57
  .sort((a, b) => {
58
+ if (Math.abs(a.cy - b.cy) > rowTol) return a.cy - b.cy
59
+ return a.cx - b.cx
 
 
60
  })
61
+ .map((item, i) => ({ d: item.d, label: i + 1 }))
62
+ }
63
+
64
+ function spatialLabelMap(detections: Detection[]): Map<string, number> {
65
+ const map = new Map<string, number>()
66
+ for (const { d, label } of sortSpatially(detections)) {
67
+ map.set(d.id, label)
68
+ }
69
+ return map
70
  }
71
 
72
  function computeStagger(count: number): { staggerMs: number; durationMs: number } {
 
99
  radius: number,
100
  displayPx: number,
101
  ): number {
102
+ const minFs = 5 / displayPx
103
+ let fs = Math.min(radius * 0.82, 13 / displayPx)
104
+ const maxW = radius * 2 * 0.66
105
+ const maxH = radius * 2 * 0.5
106
+
107
+ for (let i = 0; i < 16; i++) {
108
+ ctx.font = `bold ${fs}px system-ui, -apple-system, sans-serif`
 
109
  const m = ctx.measureText(label)
110
  const w = m.width
111
  const h = (m.actualBoundingBoxAscent ?? fs * 0.8) + (m.actualBoundingBoxDescent ?? fs * 0.2)
112
  if (w <= maxW && h <= maxH) break
113
+ fs = Math.max(minFs, fs * 0.84)
114
  }
115
  return Math.max(minFs, fs)
116
  }
 
173
  ctx.arc(cx, cy, Math.max(1, radius - lw * 0.55), 0, Math.PI * 2)
174
  ctx.clip()
175
 
176
+ const outline = Math.max(0.35 / displayPx, fs * 0.08)
177
  ctx.lineJoin = 'round'
178
  ctx.lineWidth = outline
179
  ctx.strokeStyle = `rgba(0,0,0,${0.6 * alpha})`
 
317
 
318
  let visibleCount = 0
319
 
320
+ sorted.forEach(({ d, label }, animIdx) => {
321
  const startAt = animIdx * staggerMs
322
  if (elapsed < startAt) return
323
  const progress = (elapsed - startAt) / durationMs
 
333
  const cy = d.ymin + ph / 2
334
  const radius = fitRadius(pw, ph, minR)
335
 
336
+ drawCircle(ctx!, cx, cy, radius, rgb, progress, String(label), showLabels, displayPx)
337
  visibleCount++
338
  })
339
 
 
348
  // final static redraw with all circles fully complete
349
  ctx!.clearRect(0, 0, natW, natH)
350
  ctx!.drawImage(image, 0, 0, natW, natH)
351
+ sorted.forEach(({ d, label }) => {
352
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
353
  const color = group?.color ?? '#ef4444'
354
  const rgb = hexToRgb(color)
 
357
  const cx = d.xmin + pw / 2
358
  const cy = d.ymin + ph / 2
359
  const radius = fitRadius(pw, ph, minR)
360
+ drawCircle(ctx!, cx, cy, radius, rgb, 1, String(label), showLabels, displayPx)
361
  })
362
  if (countDisplayRef?.current) {
363
  countDisplayRef.current.textContent = String(detections.length)
 
427
  const showLabels = detections.length <= 3000
428
  const minR = (dense ? 5 : 7) / displayPx
429
 
430
+ const labels = spatialLabelMap(detections)
431
+
432
+ detections.forEach((d) => {
433
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
434
  const color = group?.color ?? '#ef4444'
435
  const rgb = hexToRgb(color)
 
438
  const cx = d.xmin + pw / 2
439
  const cy = d.ymin + ph / 2
440
  const radius = fitRadius(pw, ph, minR)
441
+ const label = String(labels.get(d.id) ?? '')
442
+ drawCircle(ctx, cx, cy, radius, rgb, 1, label, showLabels && Boolean(label), displayPx)
443
  })
444
 
445
  const area = areaRef.current
 
479
  ctx.clearRect(0, 0, natW, natH)
480
  ctx.drawImage(image, 0, 0, natW, natH)
481
 
482
+ const labels = spatialLabelMap(detections)
483
+
484
+ detections.forEach((d) => {
485
  const group = groups.find((g) => g.id === d.groupId) ?? groups[0]
486
  const rgb = hexToRgb(group?.color ?? '#ef4444')
487
  const pw = d.xmax - d.xmin
488
  const ph = d.ymax - d.ymin
489
+ const label = String(labels.get(d.id) ?? '')
490
+ drawCircle(
491
+ ctx,
492
+ d.xmin + pw / 2,
493
+ d.ymin + ph / 2,
494
+ fitRadius(pw, ph, minR),
495
+ rgb,
496
+ 1,
497
+ label,
498
+ showLabels && Boolean(label),
499
+ displayPx,
500
+ )
501
  })
502
 
503
  const area = areaRef.current
frontend/src/components/SettingsPanel.tsx CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import { t } from '../lib/i18n'
2
  import type { InferenceMode, Lang } from '../lib/types'
3
 
@@ -161,6 +163,23 @@ export function SettingsPanel({
161
  />
162
  </div>
163
  </section>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  </div>
165
  )
166
  }
 
1
+ import { RefreshCw } from 'lucide-react'
2
+ import { reloadAppForUpdate } from '../lib/appUpdate'
3
  import { t } from '../lib/i18n'
4
  import type { InferenceMode, Lang } from '../lib/types'
5
 
 
163
  />
164
  </div>
165
  </section>
166
+
167
+ <section>
168
+ <p className="ct-section-label mb-2 px-1">{t(lang, 'updateApp')}</p>
169
+ <div className="ct-card overflow-hidden p-3">
170
+ <button
171
+ type="button"
172
+ className="flex w-full items-center justify-center gap-2 rounded-xl border border-[var(--border)] bg-[var(--surface-2)] px-4 py-3 text-sm font-semibold text-[var(--text)] transition-all active:scale-[0.98] active:bg-[var(--surface-active)]"
173
+ onClick={reloadAppForUpdate}
174
+ >
175
+ <RefreshCw size={16} />
176
+ {t(lang, 'updateApp')}
177
+ </button>
178
+ <p className="mt-2 px-1 text-center text-[11px] text-[var(--text-faint)]">
179
+ {t(lang, 'updateAppHint')}
180
+ </p>
181
+ </div>
182
+ </section>
183
  </div>
184
  )
185
  }
frontend/src/lib/appUpdate.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ /** Force-reload the PWA home route with a cache-busting query string. */
2
+ export function reloadAppForUpdate(): void {
3
+ window.location.assign(`/?${Date.now()}`)
4
+ }
frontend/src/lib/i18n.ts CHANGED
@@ -61,6 +61,8 @@ const messages = {
61
  creditFooter: 'Digital Manufacturing · Nawaplastic',
62
  settings: 'Settings',
63
  apply: 'Apply',
 
 
64
  },
65
  th: {
66
  title: 'NPI นับท่อ',
@@ -122,6 +124,8 @@ const messages = {
122
  creditFooter: 'Digital Manufacturing · Nawaplastic',
123
  settings: 'ตั้งค่า',
124
  apply: 'ใช้',
 
 
125
  },
126
  } as const
127
 
 
61
  creditFooter: 'Digital Manufacturing · Nawaplastic',
62
  settings: 'Settings',
63
  apply: 'Apply',
64
+ updateApp: 'Update app',
65
+ updateAppHint: 'Reload the latest version from the server',
66
  },
67
  th: {
68
  title: 'NPI นับท่อ',
 
124
  creditFooter: 'Digital Manufacturing · Nawaplastic',
125
  settings: 'ตั้งค่า',
126
  apply: 'ใช้',
127
+ updateApp: 'อัปเดตแอป',
128
+ updateAppHint: 'โหลดเวอร์ชันล่าสุดจากเซิร์ฟเวอร์',
129
  },
130
  } as const
131