-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageBrowserMainPane.cpp
More file actions
602 lines (523 loc) · 15.5 KB
/
Copy pathImageBrowserMainPane.cpp
File metadata and controls
602 lines (523 loc) · 15.5 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include "ImageBrowserMainPane.h"
#include "FD2D/Util.h"
#include "framework.h"
#include <wrl/client.h>
#include <vector>
class InfoBar : public FD2D::Wnd
{
public:
InfoBar()
: Wnd(L"infoBar")
{
SetPadding(6.0f);
m_host = std::make_shared<FD2D::DockPanel>(L"infoDock");
m_leftText = std::make_shared<FD2D::Text>(L"infoLeft");
m_leftText->SetFont(L"Segoe UI", 11.0f);
m_leftText->SetColor(D2D1::ColorF(0.85f, 0.85f, 0.85f, 1.0f));
m_leftText->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
m_leftText->SetEllipsisTrimmingEnabled(true);
m_leftText->SetTooltipOnTruncation(true);
m_leftText->SetCopyTextOnRightClick(true);
m_rightText = std::make_shared<FD2D::Text>(L"infoRight");
m_rightText->SetFont(L"Segoe UI", 11.0f);
m_rightText->SetColor(D2D1::ColorF(0.85f, 0.85f, 0.85f, 1.0f));
m_rightText->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
m_rightText->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING);
// Wide enough for "100% 270°" (zoom% plus an optional rotation angle).
m_rightText->SetFixedWidth(96.0f);
// DockPanel: Fill stops subsequent docking, so add Right first, then Fill.
m_host->AddChild(m_rightText);
m_host->SetChildDock(m_rightText, FD2D::Dock::Right);
m_host->AddChild(m_leftText);
m_host->SetChildDock(m_leftText, FD2D::Dock::Fill);
AddChild(m_host);
}
void SetLeftValue(const std::wstring& v)
{
if (m_leftText)
{
m_leftText->SetText(v);
}
}
void SetRightValue(const std::wstring& v)
{
if (m_rightText)
{
m_rightText->SetText(v);
}
}
FD2D::Size Measure(FD2D::Size available) override
{
// Must account for our own padding (top/bottom) + text line height to avoid clipping.
// Text::Measure uses (fontSize * 1.2) as line height.
const float fontSize = 12.0f;
const float lineH = fontSize * 1.2f;
const float h = (lineH + (2.0f * m_padding) + 2.0f);
const float w = (available.w > 0.0f) ? available.w : 0.0f;
m_desired = { w, h };
return m_desired;
}
void OnRender(ID2D1RenderTarget* target) override
{
UNREFERENCED_PARAMETER(target);
Wnd::OnRender(target);
}
private:
std::shared_ptr<FD2D::DockPanel> m_host {};
std::shared_ptr<FD2D::Text> m_leftText {};
std::shared_ptr<FD2D::Text> m_rightText {};
};
class PathBar : public FD2D::Wnd
{
public:
PathBar()
: Wnd(L"pathBar")
{
SetPadding(6.0f);
m_text = std::make_shared<FD2D::Text>(L"pathText");
m_text->SetFont(L"Segoe UI", 11.0f);
m_text->SetColor(D2D1::ColorF(0.90f, 0.90f, 0.90f, 1.0f));
m_text->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
m_text->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
m_text->SetEllipsisTrimmingEnabled(true);
m_text->SetCopyTextOnRightClick(true);
AddChild(m_text);
}
void SetRawValue(const std::wstring& v)
{
if (v == m_rawValue)
{
return;
}
m_rawValue = v;
UpdateFittedText();
}
FD2D::Size Measure(FD2D::Size available) override
{
const float fontSize = 12.0f;
const float lineH = fontSize * 1.2f;
const float h = (lineH + (2.0f * m_padding) + 2.0f);
const float w = (available.w > 0.0f) ? available.w : 0.0f;
m_desired = { w, h };
return m_desired;
}
void Arrange(FD2D::Rect finalRect) override
{
Wnd::Arrange(finalRect);
UpdateFittedText();
}
void OnRender(ID2D1RenderTarget* target) override
{
UNREFERENCED_PARAMETER(target);
Wnd::OnRender(target);
}
private:
void EnsureFormat()
{
if (m_format)
{
return;
}
IDWriteFactory* factory = FD2D::Core::DWriteFactory();
if (!factory)
{
return;
}
(void)factory->CreateTextFormat(
L"Segoe UI",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
11.0f,
L"",
&m_format);
if (m_format)
{
(void)m_format->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
}
}
bool FitsWidth(const std::wstring& s, float maxWidth)
{
EnsureFormat();
if (!m_format)
{
return true;
}
IDWriteFactory* factory = FD2D::Core::DWriteFactory();
if (!factory)
{
return true;
}
Microsoft::WRL::ComPtr<IDWriteTextLayout> layout {};
(void)factory->CreateTextLayout(
s.c_str(),
static_cast<UINT32>(s.size()),
m_format.Get(),
100000.0f,
1000.0f,
&layout);
if (!layout)
{
return true;
}
DWRITE_TEXT_METRICS metrics {};
(void)layout->GetMetrics(&metrics);
return metrics.widthIncludingTrailingWhitespace <= maxWidth;
}
static std::wstring NormalizeSeparators(std::wstring s)
{
for (auto& ch : s)
{
if (ch == L'/')
{
ch = L'\\';
}
}
return s;
}
static void SplitPathRemainder(const std::wstring& s, std::vector<std::wstring>& outParts)
{
outParts.clear();
size_t start = 0;
while (start < s.size())
{
size_t sep = s.find(L'\\', start);
if (sep == std::wstring::npos)
{
sep = s.size();
}
if (sep > start)
{
outParts.push_back(s.substr(start, sep - start));
}
start = sep + 1;
}
}
static void EnsureTrailingSlash(std::wstring& s)
{
if (!s.empty() && s.back() != L'\\')
{
s.push_back(L'\\');
}
}
static std::wstring JoinTail(const std::vector<std::wstring>& parts, size_t tailCount)
{
if (tailCount == 0 || parts.empty())
{
return L"";
}
const size_t start = (parts.size() > tailCount) ? (parts.size() - tailCount) : 0;
std::wstring out;
for (size_t i = start; i < parts.size(); ++i)
{
if (!out.empty())
{
out.push_back(L'\\');
}
out += parts[i];
}
return out;
}
std::wstring FolderEllipsize(const std::wstring& raw, float maxWidth)
{
if (raw.empty())
{
return raw;
}
const std::wstring s = NormalizeSeparators(raw);
// If it already fits, keep as-is.
if (FitsWidth(s, maxWidth))
{
return s;
}
std::wstring prefix;
std::wstring remainder;
// UNC path: \\server\share\...
if (s.size() >= 2 && s[0] == L'\\' && s[1] == L'\\')
{
size_t p = 2;
const size_t serverEnd = s.find(L'\\', p);
if (serverEnd == std::wstring::npos)
{
return s;
}
const std::wstring server = s.substr(p, serverEnd - p);
p = serverEnd + 1;
const size_t shareEnd = s.find(L'\\', p);
if (shareEnd == std::wstring::npos)
{
return s;
}
const std::wstring share = s.substr(p, shareEnd - p);
prefix = L"\\\\";
prefix += server;
prefix.push_back(L'\\');
prefix += share;
remainder = s.substr(shareEnd + 1);
}
// Drive path: C:\...
else if (s.size() >= 2 && s[1] == L':')
{
if (s.size() >= 3 && s[2] == L'\\')
{
prefix = s.substr(0, 3); // "C:\"
remainder = s.substr(3);
}
else
{
prefix = s.substr(0, 2); // "C:"
remainder = s.substr(2);
EnsureTrailingSlash(prefix);
if (!remainder.empty() && remainder.front() == L'\\')
{
remainder.erase(remainder.begin());
}
}
}
else
{
remainder = s;
}
std::vector<std::wstring> parts;
SplitPathRemainder(remainder, parts);
if (parts.empty())
{
return s;
}
// If even the filename doesn't fit, just return the filename and let Text do char-level ellipsis.
const std::wstring fileOnly = parts.back();
if (!FitsWidth(fileOnly, maxWidth))
{
return fileOnly;
}
std::wstring base = prefix;
EnsureTrailingSlash(base);
// Try keeping as many tail folders as possible: prefix + "...\\" + last N parts
for (size_t tailCount = (parts.size() >= 2) ? (parts.size() - 1) : 1; tailCount >= 1; --tailCount)
{
std::wstring candidate = base;
candidate += L"...\\";
candidate += JoinTail(parts, tailCount);
if (FitsWidth(candidate, maxWidth))
{
return candidate;
}
if (tailCount == 1)
{
break;
}
}
// Fallback: prefix + "...\\" + filename
std::wstring fallback = base;
fallback += L"...\\";
fallback += parts.back();
return fallback;
}
void UpdateFittedText()
{
if (!m_text)
{
return;
}
const D2D1_RECT_F r = LayoutRect();
float maxWidth = (r.right - r.left) - (2.0f * m_padding);
if (maxWidth < 8.0f)
{
maxWidth = 8.0f;
}
const std::wstring fitted = FolderEllipsize(m_rawValue, maxWidth);
if (fitted != m_lastFittedValue)
{
m_lastFittedValue = fitted;
m_text->SetText(fitted);
}
// Full path for hover tooltip + clipboard, even when the displayed
// label is middle-ellipsized by FolderEllipsize (not DWrite trimming).
m_text->SetTooltipText(m_rawValue);
m_text->SetCopyText(m_rawValue);
}
std::shared_ptr<FD2D::Text> m_text {};
Microsoft::WRL::ComPtr<IDWriteTextFormat> m_format {};
std::wstring m_rawValue {};
std::wstring m_lastFittedValue {};
};
ImageBrowserMainPane::ImageBrowserMainPane()
: FD2D::Wnd(L"mainPane")
{
}
void ImageBrowserMainPane::Build(
const std::shared_ptr<FD2D::SplitPanel>& rootSplit,
const std::wstring& initialFile,
const std::function<void(const ImageViewTransform&)>& onViewChanged,
const std::function<void()>& onClick,
const std::function<void(ImageBrowserMainImage&)>& applyIni,
const std::function<bool(const POINT&)>& onContextMenuRequest,
const std::function<void()>& onMainImageWheelFocus)
{
if (!rootSplit)
{
return;
}
m_onContextMenuRequest = onContextMenuRequest;
m_onMainImageWheelFocus = onMainImageWheelFocus;
m_mainDock.reset();
m_mainImage.reset();
auto mainImage = std::make_shared<ImageBrowserMainImage>(L"mainImage0");
if (!initialFile.empty())
{
mainImage->SetSourceFile(initialFile);
}
if (onViewChanged)
{
mainImage->SetOnViewChanged(onViewChanged);
}
if (onClick)
{
mainImage->SetOnClick(onClick);
}
if (applyIni)
{
applyIni(*mainImage);
}
m_mainImage = mainImage;
if (!m_infoBar)
{
m_infoBar = std::make_shared<InfoBar>();
}
if (!m_pathBar)
{
m_pathBar = std::make_shared<PathBar>();
}
// Main area layout: DockPanel where bottom is info bar and fill is main image/grid.
auto dock = std::make_shared<FD2D::DockPanel>(L"mainDock");
dock->AddChild(m_pathBar);
dock->SetChildDock(m_pathBar, FD2D::Dock::Top);
dock->AddChild(m_infoBar);
dock->SetChildDock(m_infoBar, FD2D::Dock::Bottom);
dock->AddChild(m_mainImage);
dock->SetChildDock(m_mainImage, FD2D::Dock::Fill);
m_mainDock = dock;
ClearChildren();
AddChild(dock);
rootSplit->SetFirstChild(shared_from_this());
}
void ImageBrowserMainPane::UpdateInfo(
const std::wstring& pathText,
const std::wstring& infoText,
const std::wstring& zoomText)
{
if (m_pathBar)
{
if (pathText != m_lastPathText)
{
m_lastPathText = pathText;
m_pathBar->SetRawValue(pathText);
}
}
if (m_infoBar)
{
if (infoText != m_lastInfoText)
{
m_lastInfoText = infoText;
m_infoBar->SetLeftValue(infoText);
}
if (zoomText != m_lastZoomText)
{
m_lastZoomText = zoomText;
m_infoBar->SetRightValue(zoomText);
}
}
}
void ImageBrowserMainPane::ResetInfoCache()
{
m_lastPathText.clear();
m_lastInfoText.clear();
m_lastZoomText.clear();
}
bool ImageBrowserMainPane::TryGetMainImageRect(D2D1_RECT_F& outRect) const
{
if (m_mainImage == nullptr)
{
return false;
}
outRect = m_mainImage->LayoutRect();
return outRect.right > outRect.left && outRect.bottom > outRect.top;
}
void ImageBrowserMainPane::PauseMainImageViewAnimation()
{
if (m_mainImage == nullptr)
{
return;
}
auto vt = m_mainImage->GetViewTransform();
vt.targetZoomScale = vt.zoomScale;
vt.zoomVelocity = 0.0f;
m_mainImage->SetViewTransform(vt, false /*notify*/);
}
void ImageBrowserMainPane::RenderCenteredMainOverlayBitmap(
ID2D1RenderTarget* target,
ID2D1Bitmap* bitmap,
float sizeFactor)
{
if (target == nullptr || bitmap == nullptr)
{
return;
}
D2D1_RECT_F mainRect {};
if (!TryGetMainImageRect(mainRect))
{
return;
}
const float w = mainRect.right - mainRect.left;
const float h = mainRect.bottom - mainRect.top;
if (w <= 0.0f || h <= 0.0f)
{
return;
}
const float clampedFactor = (std::max)(0.01f, (std::min)(1.0f, sizeFactor));
const float iconSize = (std::min)(w, h) * clampedFactor;
const float iconX = mainRect.left + (w - iconSize) * 0.5f;
const float iconY = mainRect.top + (h - iconSize) * 0.5f;
const D2D1_RECT_F dst = D2D1::RectF(iconX, iconY, iconX + iconSize, iconY + iconSize);
target->DrawBitmap(
bitmap,
dst,
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(0.0f, 0.0f, bitmap->GetSize().width, bitmap->GetSize().height));
}
void ImageBrowserMainPane::RenderOnMainRect(const std::function<void(const D2D1_RECT_F&)>& renderer)
{
if (!renderer)
{
return;
}
D2D1_RECT_F mainRect {};
if (!TryGetMainImageRect(mainRect))
{
return;
}
renderer(mainRect);
}
bool ImageBrowserMainPane::OnInputEvent(const FD2D::InputEvent& event)
{
D2D1_RECT_F mainRect {};
const bool pointInMain = event.hasPoint &&
TryGetMainImageRect(mainRect) &&
FD2D::Util::RectContainsPoint(mainRect, event.point);
if (pointInMain)
{
if (event.type == FD2D::InputEventType::MouseUp &&
event.button == FD2D::MouseButton::Right &&
m_onContextMenuRequest)
{
return m_onContextMenuRequest(event.point);
}
if (event.type == FD2D::InputEventType::MouseWheel && m_onMainImageWheelFocus)
{
m_onMainImageWheelFocus();
}
}
return Wnd::OnInputEvent(event);
}