1// Copyright (C) 2024 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16import {Trace} from './trace'; 17 18/** 19 * Allows to register custom page endpoints that response to given routes, e.g. 20 * /viewer, /record etc. 21 */ 22export interface PageManager { 23 /** 24 * Example usage: 25 * registerPage({route: '/foo', page: FooPage}) 26 * class FooPage implements m.ClassComponent<PageWithTrace> { 27 * view({attrs}: m.CVnode<PageWithTrace>) { 28 * return m('div', ... 29 * onclick: () => attrs.trace.timeline.zoom(...); 30 * ) 31 * } 32 * } 33 */ 34 registerPage(pageHandler: PageHandler): Disposable; 35} 36 37/** 38 * Mithril attrs for pages that don't require a Trace object. These pages are 39 * always accessible, even before a trace is loaded. 40 */ 41export interface PageAttrs { 42 subpage?: string; 43 trace?: Trace; 44} 45 46/** 47 * Mithril attrs for pages that require a Trace object. These pages are 48 * reachable only after a trace is loaded. Trying to access the route without a 49 * trace loaded results in the HomePage (route: '/') to be displayed instead. 50 */ 51export interface PageWithTraceAttrs extends PageAttrs { 52 trace: Trace; 53} 54 55export type PageHandler<PWT = m.ComponentTypes<PageWithTraceAttrs>> = { 56 route: string; // e.g. '/' (default route), '/viewer' 57 pluginId?: string; // Not needed, the internal impl will fill it. 58} & ( 59 | { 60 // If true, the route will be available even when there is no trace 61 // loaded. The component needs to deal with a possibly undefined attr. 62 traceless: true; 63 page: m.ComponentTypes<PageAttrs>; 64 } 65 | { 66 // If is omitted, the route will be available only when a trace is loaded. 67 // The component is guarranteed to get a defined Trace in its attrs. 68 traceless?: false; 69 page: PWT; 70 } 71); 72