@@ -3,6 +3,8 @@ import { useMemo } from "react";
33import { cn } from "@/lib/utils" ;
44import type { AppSnapshot } from "@/types" ;
55
6+ type LayoutOutput = AppSnapshot [ "layout" ] [ "outputs" ] [ number ] ;
7+
68function previewOutputs ( snapshot : AppSnapshot | null ) {
79 if ( ! snapshot ) {
810 return [ ] ;
@@ -14,32 +16,44 @@ function previewOutputs(snapshot: AppSnapshot | null) {
1416 ) ;
1517}
1618
17- function layoutBounds ( snapshot : AppSnapshot | null ) {
18- const outputs = previewOutputs ( snapshot ) ;
19+ function getOutputResolution (
20+ output : LayoutOutput ,
21+ displayByKey : Map < string , AppSnapshot [ "displays" ] [ number ] > ,
22+ ) {
23+ const display = displayByKey . get ( output . display_key ) ;
24+ if ( ! display ?. is_active ) {
25+ return output . resolution ;
26+ }
27+
28+ return display . resolution ;
29+ }
30+
31+ function layoutBounds ( snapshot : AppSnapshot | null , outputs : LayoutOutput [ ] ) {
1932 if ( ! snapshot || outputs . length === 0 ) {
2033 return null ;
2134 }
2235
2336 const displayByKey = new Map ( snapshot . displays . map ( ( display ) => [ display . id_key , display ] ) ) ;
24- const outputWidth = ( output : AppSnapshot [ "layout" ] [ "outputs" ] [ number ] ) =>
25- displayByKey . get ( output . display_key ) ?. is_active
26- ? ( displayByKey . get ( output . display_key ) ?. resolution . width ?? output . resolution . width )
27- : output . resolution . width ;
28- const outputHeight = ( output : AppSnapshot [ "layout" ] [ "outputs" ] [ number ] ) =>
29- displayByKey . get ( output . display_key ) ?. is_active
30- ? ( displayByKey . get ( output . display_key ) ?. resolution . height ?? output . resolution . height )
31- : output . resolution . height ;
37+
3238 const left = Math . min ( ...outputs . map ( ( o ) => o . position . x ) ) ;
3339 const top = Math . min ( ...outputs . map ( ( o ) => o . position . y ) ) ;
34- const right = Math . max ( ...outputs . map ( ( o ) => o . position . x + outputWidth ( o ) ) ) ;
35- const bottom = Math . max ( ...outputs . map ( ( o ) => o . position . y + outputHeight ( o ) ) ) ;
40+ const right = Math . max (
41+ ...outputs . map ( ( output ) => output . position . x + getOutputResolution ( output , displayByKey ) . width ) ,
42+ ) ;
43+ const bottom = Math . max (
44+ ...outputs . map ( ( output ) => output . position . y + getOutputResolution ( output , displayByKey ) . height ) ,
45+ ) ;
3646
3747 return { left, top, right, bottom, width : right - left , height : bottom - top } ;
3848}
3949
4050export function LayoutPreview ( { snapshot } : { snapshot : AppSnapshot | null } ) {
41- const bounds = useMemo ( ( ) => layoutBounds ( snapshot ) , [ snapshot ] ) ;
4251 const outputs = useMemo ( ( ) => previewOutputs ( snapshot ) , [ snapshot ] ) ;
52+ const bounds = useMemo ( ( ) => layoutBounds ( snapshot , outputs ) , [ snapshot , outputs ] ) ;
53+ const displayByKey = useMemo (
54+ ( ) => new Map ( ( snapshot ?. displays ?? [ ] ) . map ( ( display ) => [ display . id_key , display ] ) ) ,
55+ [ snapshot ] ,
56+ ) ;
4357 const monitorNumberByDisplayKey = useMemo (
4458 ( ) =>
4559 new Map (
@@ -71,7 +85,7 @@ export function LayoutPreview({ snapshot }: { snapshot: AppSnapshot | null }) {
7185 } }
7286 >
7387 { outputs . map ( ( output ) => {
74- const display = snapshot . displays . find ( ( d ) => d . id_key === output . display_key ) ;
88+ const display = displayByKey . get ( output . display_key ) ;
7589 const monitorNumber = monitorNumberByDisplayKey . get ( output . display_key ) ;
7690 const active = output . enabled ;
7791 const previewResolution =
0 commit comments