-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculate_intersect.ts
More file actions
189 lines (173 loc) · 6.13 KB
/
calculate_intersect.ts
File metadata and controls
189 lines (173 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░░░▄▀░█▀▄░█▀▀░█▀▀░█░█░█░░░█▀█░█▀▄░░░░░█░░░█▀█░█░█░█▀█░█░█░▀█▀░▀▄░░░░░░░░
// ░░░░░░░▀▄░░█▀▄░█▀▀░█░█░█░█░█░░░█▀█░█▀▄░▀▀▀░█░░░█▀█░░█░░█░█░█░█░░█░░░▄▀░░░░░░░
// ░░░░░░░░░▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀░▀░░░░░▀▀▀░▀░▀░░▀░░▀▀▀░▀▀▀░░▀░░▀░░░░░░░░░
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ * Copyright (c) 2026, the Regular Layout Authors. This file is part * ┃
// ┃ * of the Regular Layout library, distributed under the terms of the * ┃
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import type {
LayoutPath,
LayoutDivider,
Layout,
ViewWindow,
} from "./layout_config.ts";
/**
* Determines which panel or divider is located at a given position in the
* layout.
*
* @param row - Vertical position as a fraction (0-1) of the container
* height
* @param column - Horizontal position as a fraction (0-1) of the
* container width
* @param layout - The layout tree to search
* @param check_dividers - Whether `LayoutDivider` intersection should be
* checked, which oyu may not want for e.g. `drop` actions.
* @returns The panel path if over a panel, a divider if over a resizable
* boundary, or null if outside all panels
*/
export function calculate_intersection(
column: number,
row: number,
layout: Layout,
check_dividers: false,
): LayoutPath;
export function calculate_intersection(
column: number,
row: number,
layout: Layout,
check_dividers: true,
): LayoutPath | null | LayoutDivider;
export function calculate_intersection(
column: number,
row: number,
layout: Layout,
check_dividers?: boolean,
): LayoutPath | null | LayoutDivider;
export function calculate_intersection(
column: number,
row: number,
layout: Layout,
check_dividers: boolean = true,
): LayoutPath | null | LayoutDivider {
return calculate_intersection_recursive(column, row, layout, check_dividers);
}
const VIEW_WINDOW = {
row_start: 0,
row_end: 1,
col_start: 0,
col_end: 1,
};
function calculate_intersection_recursive(
column: number,
row: number,
panel: Layout,
check_dividers: boolean,
parent_orientation: "horizontal" | "vertical" | null = null,
view_window: ViewWindow = structuredClone(VIEW_WINDOW),
path: number[] = [],
): LayoutPath | null | LayoutDivider {
if (column < 0 || row < 0 || column > 1 || row > 1) {
return null;
}
// Base case: if this is a child panel, return its name
if (panel.type === "child-panel") {
const selected = panel.selected ?? 0;
const column_offset =
(column - view_window.col_start) /
(view_window.col_end - view_window.col_start);
const row_offset =
(row - view_window.row_start) /
(view_window.row_end - view_window.row_start);
return {
type: "layout-path",
box: undefined,
slot: panel.child[selected],
panel: structuredClone(panel),
path: path,
view_window: view_window,
is_edge: false,
column,
row,
column_offset,
row_offset,
orientation: parent_orientation || "horizontal",
};
}
// For split panels, determine which child was hit
if (panel.orientation === "vertical") {
// Vertical orientation = rows
let current_row = view_window.row_start;
for (let i = 0; i < panel.children.length; i++) {
const total_size = view_window.row_end - view_window.row_start;
const next_row = total_size * panel.sizes[i] + current_row;
if (check_dividers && Math.abs(row - next_row) < 0.01) {
return {
path: [...path, i],
type: "vertical",
view_window: {
...view_window,
row_start: current_row,
row_end: next_row,
},
};
}
if (row >= current_row && row < next_row) {
return calculate_intersection_recursive(
column,
row,
panel.children[i],
check_dividers,
"vertical",
{
...view_window,
row_start: current_row,
row_end: next_row,
},
[...path, i],
);
}
current_row = next_row;
}
} else {
// Horizontal orientation = columns
let current_col = view_window.col_start;
for (let i = 0; i < panel.children.length; i++) {
const total_size = view_window.col_end - view_window.col_start;
const next_col = current_col + total_size * panel.sizes[i];
if (check_dividers && Math.abs(column - next_col) < 0.01) {
return {
path: [...path, i],
type: "horizontal",
view_window: {
...view_window,
col_start: current_col,
col_end: next_col,
},
};
}
// Check if the column falls within this child's bounds.
if (column >= current_col && column < next_col) {
return calculate_intersection_recursive(
column,
row,
panel.children[i],
check_dividers,
"horizontal",
{
...view_window,
col_start: current_col,
col_end: next_col,
},
[...path, i],
);
}
current_col = next_col;
}
}
// If we get here, the hit was outside all children (possibly in a gap or
// boundary).
return null;
}