| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/* eslint-disable @typescript-eslint/explicit-function-return-type */ |
| 24 |
|
| 25 |
/* --------------------------------------------------------------------------*/ |
| 26 |
/* --- Ivette Extensions ---*/ |
| 27 |
/* --------------------------------------------------------------------------*/ |
| 28 |
|
| 29 |
import React from 'react'; |
| 30 |
import * as Dome from 'dome'; |
| 31 |
|
| 32 |
/* --------------------------------------------------------------------------*/ |
| 33 |
/* --- Extension Elements ---*/ |
| 34 |
/* --------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
const UPDATED = new Dome.Event('ivette.updated'); |
| 37 |
|
| 38 |
export interface ElementProps { |
| 39 |
id: string; |
| 40 |
rank?: number; |
| 41 |
children?: React.ReactNode; |
| 42 |
} |
| 43 |
|
| 44 |
function byPanel(p: ElementProps, q: ElementProps) { |
| 45 |
const rp = p.rank ?? 0; |
| 46 |
const rq = q.rank ?? 0; |
| 47 |
if (rp < rq) return -1; |
| 48 |
if (rp > rq) return +1; |
| 49 |
const ip = p.id; |
| 50 |
const iq = q.id; |
| 51 |
if (ip < iq) return -1; |
| 52 |
if (ip > iq) return +1; |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
export class ElementRack { |
| 57 |
|
| 58 |
private rank = 1; |
| 59 |
private readonly items = new Map<string, ElementProps>(); |
| 60 |
|
| 61 |
register(elt: ElementProps) { |
| 62 |
if (elt.rank === undefined) elt.rank = this.rank; |
| 63 |
this.rank++; |
| 64 |
this.items.set(elt.id, elt); |
| 65 |
UPDATED.emit(); |
| 66 |
} |
| 67 |
|
| 68 |
render() { |
| 69 |
const panels: ElementProps[] = []; |
| 70 |
this.items.forEach((p) => { if (p.children) { panels.push(p); } }); |
| 71 |
const contents = panels.sort(byPanel).map((p) => p.children); |
| 72 |
return <>{React.Children.toArray(contents)}</>; |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
export function useRack(E: ElementRack) { |
| 78 |
Dome.useUpdate(UPDATED); |
| 79 |
return E.render(); |
| 80 |
} |
| 81 |
|
| 82 |
export const SIDEBAR = new ElementRack(); |
| 83 |
export const TOOLBAR = new ElementRack(); |
| 84 |
export const STATUSBAR = new ElementRack(); |
| 85 |
|
| 86 |
export function Sidebar() { return useRack(SIDEBAR); } |
| 87 |
export function Toolbar() { return useRack(TOOLBAR); } |
| 88 |
export function Statusbar() { return useRack(STATUSBAR); } |
| 89 |
|
| 90 |
/* --------------------------------------------------------------------------*/ |