-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitPanel.cpp
More file actions
501 lines (434 loc) · 15.7 KB
/
Copy pathSplitPanel.cpp
File metadata and controls
501 lines (434 loc) · 15.7 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "SplitPanel.h"
#include "Backplate.h"
#include <algorithm>
#include <cmath>
namespace FD2D
{
SplitPanel::SplitPanel()
: Panel()
{
m_splitter = std::make_shared<Splitter>(L"splitter", SplitterOrientation::Horizontal);
m_splitter->OnSplitChanged([this](float ratio)
{
OnSplitRatioChanged(ratio);
});
AddChild(m_splitter);
}
SplitPanel::SplitPanel(const std::wstring& name, SplitterOrientation orientation)
: Panel(name)
, m_orientation(orientation)
{
m_splitter = std::make_shared<Splitter>(L"splitter", orientation);
m_splitter->OnSplitChanged([this](float ratio)
{
OnSplitRatioChanged(ratio);
});
AddChild(m_splitter);
}
void SplitPanel::SetOrientation(SplitterOrientation orientation)
{
m_orientation = orientation;
if (m_splitter)
{
m_splitter->SetOrientation(orientation);
}
Invalidate();
}
void SplitPanel::SetFirstChild(const std::shared_ptr<Wnd>& child)
{
m_firstChild = child;
if (m_firstChild && !m_firstChild->Name().empty())
{
// Add if not already added
if (Children().find(m_firstChild->Name()) == Children().end())
{
AddChild(m_firstChild);
}
}
}
void SplitPanel::SetSecondChild(const std::shared_ptr<Wnd>& child)
{
m_secondChild = child;
if (m_secondChild && !m_secondChild->Name().empty())
{
// Add if not already added
if (Children().find(m_secondChild->Name()) == Children().end())
{
AddChild(m_secondChild);
}
}
}
void SplitPanel::SetSplitRatio(float ratio)
{
const float newRatio = (std::max)(0.0f, (std::min)(1.0f, ratio));
// Only invalidate if the requested ratio actually changed (prevents
// unnecessary re-renders). Compared against m_requestedSplitRatio (the
// user's intent), not the effective/clamped m_splitRatio, since those
// two can legitimately differ once pane extent constraints kick in.
if (std::abs(m_requestedSplitRatio - newRatio) < 0.001f)
{
return;
}
m_requestedSplitRatio = newRatio;
m_splitRatio = newRatio; // tentative; Arrange() re-clamps from m_requestedSplitRatio
Invalidate();
}
void SplitPanel::SetFirstPaneMinExtent(float extent)
{
m_firstPaneMinExtent = (std::max)(0.0f, extent);
Invalidate();
}
void SplitPanel::SetFirstPaneMaxExtent(float extent)
{
m_firstPaneMaxExtent = (std::max)(0.0f, extent);
Invalidate();
}
void SplitPanel::SetSecondPaneMinExtent(float extent)
{
m_secondPaneMinExtent = (std::max)(0.0f, extent);
Invalidate();
}
void SplitPanel::SetSecondPaneMaxExtent(float extent)
{
m_secondPaneMaxExtent = (std::max)(0.0f, extent);
Invalidate();
}
void SplitPanel::SetConstraintPropagation(ConstraintPropagation policy)
{
m_propagation = policy;
Invalidate();
}
void SplitPanel::OnSplitChanged(std::function<void(float ratio)> handler)
{
m_splitChanged = std::move(handler);
}
bool SplitPanel::IsSplitterDragging() const
{
return (m_splitter != nullptr) && m_splitter->IsDragging();
}
float SplitPanel::ClampRatioForPaneConstraints(const Rect& childArea, float splitterExtent, float ratio) const
{
float availableExtent = 0.0f;
if (m_orientation == SplitterOrientation::Horizontal)
{
availableExtent = childArea.w - splitterExtent;
}
else
{
availableExtent = childArea.h - splitterExtent;
}
if (availableExtent <= 0.0f)
{
return (std::max)(0.0f, (std::min)(1.0f, ratio));
}
// Convert constraints into a ratio range.
// firstExtent = availableExtent * ratio
// secondExtent = availableExtent * (1 - ratio)
float minRatio = 0.0f;
float maxRatio = 1.0f;
const float firstMin = m_firstPaneMinExtent;
const float firstMax = m_firstPaneMaxExtent;
const float secondMin = m_secondPaneMinExtent;
const float secondMax = m_secondPaneMaxExtent;
if (firstMin > 0.0f)
{
minRatio = (std::max)(minRatio, firstMin / availableExtent);
}
if (firstMax > 0.0f)
{
maxRatio = (std::min)(maxRatio, firstMax / availableExtent);
}
if (secondMin > 0.0f)
{
maxRatio = (std::min)(maxRatio, 1.0f - (secondMin / availableExtent));
}
if (secondMax > 0.0f)
{
minRatio = (std::max)(minRatio, 1.0f - (secondMax / availableExtent));
}
minRatio = (std::max)(0.0f, (std::min)(1.0f, minRatio));
maxRatio = (std::max)(0.0f, (std::min)(1.0f, maxRatio));
float clamped = ratio;
// If constraints are feasible, clamp directly.
if (minRatio <= maxRatio)
{
clamped = (std::max)(minRatio, (std::min)(maxRatio, clamped));
return clamped;
}
// Best-effort when constraints conflict: treat mins as hard, relax maxes.
clamped = (std::max)(0.0f, (std::min)(1.0f, clamped));
if (firstMin > 0.0f)
{
clamped = (std::max)(clamped, firstMin / availableExtent);
}
if (secondMin > 0.0f)
{
clamped = (std::min)(clamped, 1.0f - (secondMin / availableExtent));
}
clamped = (std::max)(0.0f, (std::min)(1.0f, clamped));
return clamped;
}
void SplitPanel::OnSplitRatioChanged(float ratio)
{
// Clamp ratio based on current bounds and second-pane constraints
Rect inset = Inset(m_bounds, m_margin);
Rect childArea = Inset(inset, m_padding);
float splitterExtent = 0.0f;
if (m_splitter)
{
Size s = m_splitter->Measure({ childArea.w, childArea.h });
splitterExtent = (m_orientation == SplitterOrientation::Horizontal) ? s.w : s.h;
}
float clamped = ClampRatioForPaneConstraints(childArea, splitterExtent, ratio);
m_splitRatio = clamped;
// The user just dragged the splitter to this position, so it becomes
// the new sticky "requested" ratio too - future resizes should grow
// or shrink relative to *this* position, not silently revert to
// whatever ratio was passed to the last SetSplitRatio() call.
m_requestedSplitRatio = clamped;
if (m_splitter)
{
m_splitter->SetRatio(clamped);
}
// Call Measure/Arrange to recalculate layout
if (BackplateRef() && m_bounds.w > 0.0f && m_bounds.h > 0.0f)
{
// Recalculate layout using current bounds
Measure({ m_bounds.w, m_bounds.h });
Arrange(m_bounds);
}
Invalidate();
if (m_splitChanged)
{
m_splitChanged(m_splitRatio);
}
}
void SplitPanel::OnRender(ID2D1RenderTarget* target)
{
// Draw children first.
Wnd::OnRender(target);
if (target == nullptr || !m_splitter || !m_splitter->IsDragging())
{
return;
}
// While dragging: add subtle feedback on both panes (dim + outline).
if (!m_dragDimBrush)
{
(void)target->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.06f), &m_dragDimBrush);
}
if (!m_dragOutlineBrush)
{
(void)target->CreateSolidColorBrush(D2D1::ColorF(1.0f, 0.60f, 0.24f, 0.30f), &m_dragOutlineBrush);
}
auto drawPaneFeedback = [&](const std::shared_ptr<Wnd>& pane)
{
if (!pane)
{
return;
}
const D2D1_RECT_F r = pane->LayoutRect();
if (!(r.right > r.left && r.bottom > r.top))
{
return;
}
if (m_dragDimBrush)
{
target->FillRectangle(r, m_dragDimBrush.Get());
}
if (m_dragOutlineBrush)
{
// Slight inflate so the stroke is visible even if content is edge-to-edge.
D2D1_RECT_F o = r;
o.left += 1.0f;
o.top += 1.0f;
o.right -= 1.0f;
o.bottom -= 1.0f;
target->DrawRectangle(o, m_dragOutlineBrush.Get(), 1.5f);
}
};
drawPaneFeedback(m_firstChild);
drawPaneFeedback(m_secondChild);
}
Size SplitPanel::Measure(Size available)
{
// Calculate desired size of children
Size firstSize {};
Size secondSize {};
Size splitterSize {};
if (m_firstChild)
{
firstSize = m_firstChild->Measure(available);
}
if (m_secondChild)
{
secondSize = m_secondChild->Measure(available);
}
if (m_splitter)
{
splitterSize = m_splitter->Measure(available);
}
if (m_orientation == SplitterOrientation::Horizontal)
{
// Left-right split
float totalWidth = firstSize.w + splitterSize.w + secondSize.w;
float maxHeight = (std::max)(firstSize.h, (std::max)(secondSize.h, splitterSize.h));
// Upward constraint propagation (min width)
if (m_propagation != ConstraintPropagation::None)
{
float minFirst = m_firstPaneMinExtent;
float minSecond = m_secondPaneMinExtent;
float minTotal = minFirst + splitterSize.w + minSecond;
totalWidth = (std::max)(totalWidth, minTotal);
}
m_desired = { totalWidth, maxHeight };
}
else
{
// Top-bottom split
float totalHeight = firstSize.h + splitterSize.h + secondSize.h;
float maxWidth = (std::max)(firstSize.w, (std::max)(secondSize.w, splitterSize.w));
// Upward constraint propagation (min height)
if (m_propagation != ConstraintPropagation::None)
{
float minFirst = m_firstPaneMinExtent;
float minSecond = m_secondPaneMinExtent;
float minTotal = minFirst + splitterSize.h + minSecond;
totalHeight = (std::max)(totalHeight, minTotal);
}
m_desired = { maxWidth, totalHeight };
}
return m_desired;
}
Size SplitPanel::MinSize() const
{
// Child intrinsic mins (may be 0 for many leaf controls)
Size firstMin {};
Size secondMin {};
if (m_firstChild)
{
firstMin = m_firstChild->MinSize();
}
if (m_secondChild)
{
secondMin = m_secondChild->MinSize();
}
const float splitterExtent = m_splitter ? m_splitter->HitAreaThickness() : 0.0f;
// Apply SplitPanel constraints only when propagation is enabled.
const bool propagate = (m_propagation != ConstraintPropagation::None);
float minW = 0.0f;
float minH = 0.0f;
if (m_orientation == SplitterOrientation::Horizontal)
{
float a = firstMin.w;
float b = secondMin.w;
if (propagate)
{
a = (std::max)(a, m_firstPaneMinExtent);
b = (std::max)(b, m_secondPaneMinExtent);
}
minW = a + splitterExtent + b;
minH = (std::max)(firstMin.h, secondMin.h);
}
else
{
float a = firstMin.h;
float b = secondMin.h;
if (propagate)
{
a = (std::max)(a, m_firstPaneMinExtent);
b = (std::max)(b, m_secondPaneMinExtent);
}
minH = a + splitterExtent + b;
minW = (std::max)(firstMin.w, secondMin.w);
}
// Include this node's margin/padding (same convention as Wnd::Arrange)
minW += 2.0f * m_margin + 2.0f * m_padding;
minH += 2.0f * m_margin + 2.0f * m_padding;
return { minW, minH };
}
void SplitPanel::Arrange(Rect finalRect)
{
Rect inset = Inset(finalRect, m_margin);
Rect childArea = Inset(inset, m_padding);
if (m_orientation == SplitterOrientation::Horizontal)
{
// Left-right split
float totalWidth = childArea.w;
float splitterWidth = m_splitter ? m_splitter->Measure({ childArea.w, childArea.h }).w : 0.0f;
float availableWidth = totalWidth - splitterWidth;
// Re-derive the effective ratio from the *requested* ratio every
// time (not from last frame's already-clamped m_splitRatio) so the
// result only depends on the current size, not the resize history
// (see m_requestedSplitRatio's doc comment in SplitPanel.h).
m_splitRatio = ClampRatioForPaneConstraints(childArea, splitterWidth, m_requestedSplitRatio);
if (m_splitter)
{
m_splitter->SetRatio(m_splitRatio);
}
float firstWidth = availableWidth * m_splitRatio;
float secondWidth = availableWidth * (1.0f - m_splitRatio);
float x = childArea.x;
// First child
if (m_firstChild)
{
Rect firstRect { x, childArea.y, firstWidth, childArea.h };
m_firstChild->Arrange(firstRect);
x += firstWidth;
}
// Splitter
if (m_splitter)
{
Rect splitterRect { x, childArea.y, splitterWidth, childArea.h };
m_splitter->SetParentBounds(childArea);
m_splitter->Arrange(splitterRect);
x += splitterWidth;
}
// Second child
if (m_secondChild)
{
Rect secondRect { x, childArea.y, secondWidth, childArea.h };
m_secondChild->Arrange(secondRect);
}
}
else
{
// Top-bottom split
float totalHeight = childArea.h;
float splitterHeight = m_splitter ? m_splitter->Measure({ childArea.w, childArea.h }).h : 0.0f;
float availableHeight = totalHeight - splitterHeight;
// See the Horizontal branch above: always clamp from the
// requested ratio, not the previous effective one.
m_splitRatio = ClampRatioForPaneConstraints(childArea, splitterHeight, m_requestedSplitRatio);
if (m_splitter)
{
m_splitter->SetRatio(m_splitRatio);
}
float firstHeight = availableHeight * m_splitRatio;
float secondHeight = availableHeight * (1.0f - m_splitRatio);
float y = childArea.y;
// First child
if (m_firstChild)
{
Rect firstRect { childArea.x, y, childArea.w, firstHeight };
m_firstChild->Arrange(firstRect);
y += firstHeight;
}
// Splitter
if (m_splitter)
{
Rect splitterRect { childArea.x, y, childArea.w, splitterHeight };
m_splitter->SetParentBounds(childArea);
m_splitter->Arrange(splitterRect);
y += splitterHeight;
}
// Second child
if (m_secondChild)
{
Rect secondRect { childArea.x, y, childArea.w, secondHeight };
m_secondChild->Arrange(secondRect);
}
}
m_bounds = finalRect;
m_layoutRect = ToD2D(finalRect);
}
}