|
| 1 | +import { CoreTypes, EventData, Frame, GridLayout, Observable, Page } from '@nativescript/core'; |
| 2 | +import { readEdges, readWindowInsets } from './shared'; |
| 3 | + |
| 4 | +interface Case { |
| 5 | + edge: CoreTypes.AndroidOverflow; |
| 6 | + description: string; |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * One entry per shape androidOverflowEdge can take. Walk them with prev/next and the |
| 11 | + * green ring tells you which edges the probe padded. |
| 12 | + */ |
| 13 | +const CASES: Case[] = [ |
| 14 | + { edge: 'none', description: 'pads every edge and consumes them - expect a green ring all the way round' }, |
| 15 | + { edge: 'ignore', description: 'takes no part at all - expect no green ring, and no ring left over from the previous case' }, |
| 16 | + { edge: 'top', description: 'overflows the top - blue should reach up under the status bar, green on the other three' }, |
| 17 | + { edge: 'bottom', description: 'overflows the bottom - blue should reach down under the navigation bar' }, |
| 18 | + { edge: 'left', description: 'overflows the left - only visible when there is a left inset (landscape / gesture nav)' }, |
| 19 | + { edge: 'right', description: 'overflows the right - only visible when there is a right inset' }, |
| 20 | + { edge: 'top,bottom', description: 'overflows both - green only on left/right, which are usually 0 in portrait' }, |
| 21 | + { edge: 'left,right', description: 'overflows the sides - green top and bottom' }, |
| 22 | + { edge: 'all-but-top', description: 'only the top is padded' }, |
| 23 | + { edge: 'all-but-bottom', description: 'only the bottom is padded' }, |
| 24 | + { edge: 'top-dont-consume', description: 'pads the top AND passes the top inset on to children' }, |
| 25 | + { edge: 'bottom-dont-consume', description: 'pads the bottom AND passes the bottom inset on to children' }, |
| 26 | + { edge: 'dont-apply', description: 'hands the insets to JS and pads nothing - expect no green ring' }, |
| 27 | + { edge: 'none,none', description: 'a stacked value that resolves to none - must behave exactly like the first case' }, |
| 28 | + { edge: 'ignore,bottom', description: 'ignore wins over anything after it - must behave exactly like ignore' }, |
| 29 | + { edge: 'none,cutout', description: 'like none, but the display cutout counts too - rotate to landscape, where the camera sits on an edge with no system bar' }, |
| 30 | + { edge: 'top,cutout', description: 'overflows the top while still keeping clear of the cutout on the other edges' }, |
| 31 | +]; |
| 32 | + |
| 33 | +class EdgeMatrix extends Observable { |
| 34 | + private index = 0; |
| 35 | + |
| 36 | + constructor(private page: Page) { |
| 37 | + super(); |
| 38 | + this.apply(); |
| 39 | + } |
| 40 | + |
| 41 | + get probe(): GridLayout { |
| 42 | + return this.page.getViewById<GridLayout>('probe'); |
| 43 | + } |
| 44 | + |
| 45 | + next = () => { |
| 46 | + this.index = (this.index + 1) % CASES.length; |
| 47 | + this.apply(); |
| 48 | + }; |
| 49 | + |
| 50 | + prev = () => { |
| 51 | + this.index = (this.index - 1 + CASES.length) % CASES.length; |
| 52 | + this.apply(); |
| 53 | + }; |
| 54 | + |
| 55 | + back = () => { |
| 56 | + Frame.topmost().goBack(); |
| 57 | + }; |
| 58 | + |
| 59 | + private apply() { |
| 60 | + const current = CASES[this.index]; |
| 61 | + this.set('edge', current.edge); |
| 62 | + this.set('heading', `androidOverflowEdge = "${current.edge}"`); |
| 63 | + this.set('description', current.description); |
| 64 | + this.set('hint', `green ring = padded edge, blue to the screen edge = overflowing (${this.index + 1} of ${CASES.length})`); |
| 65 | + // The insets land on the next layout pass, so read the probe after it. |
| 66 | + setTimeout(() => this.refresh(), 80); |
| 67 | + } |
| 68 | + |
| 69 | + refresh() { |
| 70 | + const readout = readEdges(this.probe); |
| 71 | + this.set('padding', readout.padding); |
| 72 | + this.set('edgeInsets', readout.edgeInsets); |
| 73 | + this.set('imeInsets', readout.imeInsets); |
| 74 | + this.set('windowInsets', readWindowInsets(this.page)); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export function navigatingTo(args: EventData) { |
| 79 | + const page = <Page>args.object; |
| 80 | + page.bindingContext = new EdgeMatrix(page); |
| 81 | +} |
0 commit comments