1// Aseprite
2// Copyright (C) 2019-2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/ui/file_list.h"
13
14#include "app/modules/gfx.h"
15#include "app/thumbnail_generator.h"
16#include "app/ui/skin/skin_theme.h"
17#include "base/string.h"
18#include "base/time.h"
19#include "os/font.h"
20#include "os/surface.h"
21#include "ui/ui.h"
22
23#include <algorithm>
24#include <cctype>
25#include <cstring>
26
27#define ISEARCH_KEYPRESS_INTERVAL_MSECS 500
28
29namespace app {
30
31using namespace app::skin;
32using namespace gfx;
33using namespace ui;
34
35FileList::FileList()
36 : Widget(kGenericWidget)
37 , m_currentFolder(FileSystemModule::instance()->getRootFileItem())
38 , m_req_valid(false)
39 , m_selected(nullptr)
40 , m_isearchClock(0)
41 , m_generateThumbnailTimer(200, this)
42 , m_monitoringTimer(50, this)
43 , m_itemToGenerateThumbnail(nullptr)
44 , m_multiselect(false)
45 , m_zoom(1.0)
46 , m_itemsPerRow(0)
47{
48 setFocusStop(true);
49 setDoubleBuffered(true);
50
51 m_generateThumbnailTimer.Tick.connect(&FileList::onGenerateThumbnailTick, this);
52 m_monitoringTimer.Tick.connect(&FileList::onMonitoringTick, this);
53 m_monitoringTimer.start();
54
55 regenerateList();
56}
57
58FileList::~FileList()
59{
60 // Stop timers.
61 m_generateThumbnailTimer.stop();
62 m_monitoringTimer.stop();
63
64 // Stop workers creating thumbnails.
65 ThumbnailGenerator::instance()->stopAllWorkers();
66}
67
68void FileList::setExtensions(const base::paths& extensions)
69{
70 m_exts = extensions;
71
72 // Refresh
73 if (isVisible())
74 setCurrentFolder(m_currentFolder);
75}
76
77void FileList::setCurrentFolder(IFileItem* folder)
78{
79 ASSERT(folder != NULL);
80 ASSERT(folder->isBrowsable());
81
82 m_currentFolder = folder;
83 m_req_valid = false;
84 m_selected = nullptr;
85
86 regenerateList();
87
88 // As now we are in other folder, we can stop the generation of all
89 // thumbnails.
90 ThumbnailGenerator::instance()->stopAllWorkers();
91
92 // select first folder
93 if (!m_list.empty() && m_list.front()->isBrowsable())
94 selectIndex(0);
95
96 // Emit "CurrentFolderChanged" event.
97 onCurrentFolderChanged();
98
99 invalidate();
100 View::getView(this)->updateView();
101}
102
103FileItemList FileList::selectedFileItems() const
104{
105 ASSERT(m_selectedItems.size() == m_list.size());
106
107 FileItemList result;
108 for (int i=0; i<int(m_list.size()); ++i) {
109 if (m_selectedItems[i])
110 result.push_back(m_list[i]);
111 }
112 return result;
113}
114
115void FileList::deselectedFileItems()
116{
117 if (!m_multiselect)
118 return;
119
120 bool redraw = false;
121
122 for (int i=0; i<int(m_list.size()); ++i) {
123 if (m_selected == m_list[i]) {
124 if (!m_selectedItems[i]) {
125 m_selectedItems[i] = true;
126 redraw = true;
127 }
128 }
129 else if (m_selectedItems[i]) {
130 m_selectedItems[i] = false;
131 redraw = true;
132 }
133 }
134
135 if (redraw)
136 invalidate();
137}
138
139void FileList::setMultipleSelection(bool multiple)
140{
141 m_multiselect = multiple;
142}
143
144void FileList::goUp()
145{
146 IFileItem* folder = m_currentFolder;
147 IFileItem* parent = folder->parent();
148 if (parent) {
149 setCurrentFolder(parent);
150
151 m_selected = folder;
152 deselectedFileItems();
153
154 // Make the selected item visible.
155 makeSelectedFileitemVisible();
156 }
157}
158
159void FileList::setZoom(const double zoom)
160{
161 m_zoom = std::clamp(zoom, 1.0, 8.0);
162 m_req_valid = false;
163
164 // if (auto view = View::getView(this))
165 recalcAllFileItemInfo();
166}
167
168void FileList::animateToZoom(const double zoom)
169{
170 m_fromZoom = m_zoom;
171 m_toZoom = zoom;
172 startAnimation(ANI_ZOOM, 10);
173}
174
175bool FileList::onProcessMessage(Message* msg)
176{
177 switch (msg->type()) {
178
179 case kMouseDownMessage:
180 captureMouse();
181 [[fallthrough]];
182
183 case kMouseMoveMessage:
184 if (hasCapture()) {
185 MouseMessage* mouseMsg = static_cast<MouseMessage*>(msg);
186 IFileItem* oldSelected = m_selected;
187 m_selected = nullptr;
188
189 // Mouse position in client coordinates
190 gfx::Point mousePos = mouseMsg->position() - bounds().origin();
191
192 // rows
193 int i = 0;
194 for (auto begin=m_list.begin(), end=m_list.end(), it=begin;
195 it!=end; ++it, ++i) {
196 IFileItem* fi = *it;
197 ItemInfo info = getFileItemInfo(i);
198
199 if ((info.bounds.contains(mousePos)) ||
200 (isListView() &&
201 ((it == begin && mousePos.y < info.bounds.y) ||
202 (it == end-1 && mousePos.y >= info.bounds.y2())))) {
203 m_selected = fi;
204
205 if (m_multiselect &&
206 oldSelected != fi &&
207 m_selectedItems.size() == m_list.size()) {
208 if (msg->shiftPressed() ||
209 msg->ctrlPressed() ||
210 msg->cmdPressed()) {
211 m_selectedItems[i] = !m_selectedItems[i];
212 }
213 else {
214 std::fill(m_selectedItems.begin(),
215 m_selectedItems.end(), false);
216 m_selectedItems[i] = true;
217 }
218 invalidate();
219 }
220
221 makeSelectedFileitemVisible();
222 break;
223 }
224 }
225
226 if (oldSelected != m_selected) {
227 if (hasThumbnailsPerItem())
228 generateThumbnailForFileItem(m_selected);
229 else
230 delayThumbnailGenerationForSelectedItem();
231
232 invalidate();
233
234 // Emit "FileSelected" event.
235 onFileSelected();
236 }
237 }
238 break;
239
240 case kMouseUpMessage:
241 if (hasCapture()) {
242 releaseMouse();
243 }
244 break;
245
246 case kKeyDownMessage:
247 if (hasFocus()) {
248 KeyMessage* keyMsg = static_cast<KeyMessage*>(msg);
249 KeyScancode scancode = keyMsg->scancode();
250 int unicodeChar = keyMsg->unicodeChar();
251 int select = selectedIndex();
252 View* view = View::getView(this);
253 int bottom = m_list.size();
254
255 switch (scancode) {
256
257 case kKeyUp:
258 if (select >= 0)
259 select -= m_itemsPerRow;
260 else
261 select = 0;
262 break;
263
264 case kKeyDown:
265 if (select >= 0)
266 select += m_itemsPerRow;
267 else
268 select = 0;
269 break;
270
271 case kKeyHome:
272 select = 0;
273 break;
274
275 case kKeyEnd:
276 select = bottom-1;
277 break;
278
279 case kKeyPageUp:
280 case kKeyPageDown: {
281 int sgn = (scancode == kKeyPageUp) ? -1: 1;
282 gfx::Rect vp = view->viewportBounds();
283 if (select < 0)
284 select = 0;
285 select += sgn * vp.h / (textHeight()+4*guiscale());
286 break;
287 }
288
289 case kKeyLeft:
290 case kKeyRight: {
291 const int delta = (scancode == kKeyLeft ? -1: 1);
292 if (isIconView()) {
293 if (select >= 0)
294 select += delta;
295 else
296 select = 0;
297 }
298 else if (select >= 0) {
299 gfx::Rect vp = view->viewportBounds();
300 gfx::Point scroll = view->viewScroll();
301 scroll.x += vp.w/2*delta;
302 view->setViewScroll(scroll);
303 }
304 break;
305 }
306
307 case kKeyEnter:
308 case kKeyEnterPad:
309 if (m_selected) {
310 if (m_selected->isBrowsable()) {
311 setCurrentFolder(m_selected);
312 return true;
313 }
314 if (m_selected->isFolder()) {
315 // Do nothing (is a folder but not browseable).
316 return true;
317 }
318 else {
319 // Emit "FileAccepted" event.
320 onFileAccepted();
321 return true;
322 }
323 }
324 else
325 return Widget::onProcessMessage(msg);
326
327 case kKeyBackspace:
328 goUp();
329 return true;
330
331 default:
332 if (unicodeChar == ' ' ||
333 (std::tolower(unicodeChar) >= 'a' &&
334 std::tolower(unicodeChar) <= 'z') ||
335 (unicodeChar >= '0' &&
336 unicodeChar <= '9')) {
337 if ((base::current_tick() - m_isearchClock) > ISEARCH_KEYPRESS_INTERVAL_MSECS)
338 m_isearch.clear();
339
340 m_isearch.push_back(unicodeChar);
341
342 int i, chrs = m_isearch.size();
343 FileItemList::iterator
344 link = m_list.begin() + ((select >= 0) ? select: 0);
345
346 for (i=std::max(select, 0); i<bottom; ++i, ++link) {
347 IFileItem* fi = *link;
348 if (base::utf8_icmp(fi->displayName(), m_isearch, chrs) == 0) {
349 select = i;
350 break;
351 }
352 }
353 m_isearchClock = base::current_tick();
354 // Go to selectIndex...
355 }
356 else
357 return Widget::onProcessMessage(msg);
358 }
359
360 if (bottom > 0)
361 selectIndex(std::clamp(select, 0, bottom-1));
362
363 return true;
364 }
365 break;
366
367 case kMouseWheelMessage: {
368 View* view = View::getView(this);
369 if (view) {
370 // Zoom
371 if (msg->ctrlPressed() || // TODO configurable
372 msg->cmdPressed()) {
373 gfx::Point delta = static_cast<MouseMessage*>(msg)->wheelDelta();
374 const bool precise = static_cast<MouseMessage*>(msg)->preciseWheel();
375 double dz = delta.x + delta.y;
376
377 if (precise) {
378 dz /= 1.5;
379 if (dz < -1.0) dz = -1.0;
380 else if (dz > 1.0) dz = 1.0;
381 }
382
383 setZoom(zoom() - dz);
384 break;
385 }
386 else {
387 gfx::Point scroll = view->viewScroll();
388
389 if (static_cast<MouseMessage*>(msg)->preciseWheel())
390 scroll += static_cast<MouseMessage*>(msg)->wheelDelta();
391 else
392 scroll += static_cast<MouseMessage*>(msg)->wheelDelta() * 3*(textHeight()+4*guiscale());
393
394 view->setViewScroll(scroll);
395 }
396 }
397 break;
398 }
399
400 case kDoubleClickMessage:
401 if (m_selected) {
402 if (m_selected->isBrowsable()) {
403 setCurrentFolder(m_selected);
404 return true;
405 }
406 else {
407 onFileAccepted(); // Emit "FileAccepted" event.
408 return true;
409 }
410 }
411 break;
412
413 case kTouchMagnifyMessage: {
414 setZoom(zoom() + 4.0*static_cast<ui::TouchMessage*>(msg)->magnification());
415 break;
416 }
417
418 }
419
420 return Widget::onProcessMessage(msg);
421}
422
423void FileList::onPaint(ui::PaintEvent& ev)
424{
425 Graphics* g = ev.graphics();
426 auto theme = SkinTheme::get(this);
427 gfx::Rect bounds = clientBounds();
428
429 g->fillRect(theme->colors.background(), bounds);
430 // g->fillRect(bgcolor, gfx::Rect(bounds.x, y, bounds.w, itemSize.h));
431
432 int i = 0, selectedIndex = -1;
433 for (IFileItem* fi : m_list) {
434 if (m_selected != fi) {
435 paintItem(g, fi, i);
436 }
437 else {
438 selectedIndex = i;
439 }
440 ++i;
441 }
442
443 // Paint main selected index (so if the filename label is bigger it
444 // will appear over other items).
445 if (m_selected) {
446 ASSERT(selectedIndex >= 0);
447 if (selectedIndex >= 0)
448 paintItem(g, m_selected, selectedIndex);
449 else {
450 // Strange run-time state where the "m_selected" is not in the
451 // list. The previous assert should fail on Debug so this is
452 // here only for Release mode.
453 return;
454 }
455 }
456
457 // Draw main thumbnail for the selected item when there are no
458 // thumbnails per item.
459 if (isListView() &&
460 m_selected &&
461 m_selected->getThumbnail()) {
462 gfx::Rect tbounds = mainThumbnailBounds();
463 tbounds.enlarge(1);
464 g->drawRect(gfx::rgba(0, 0, 0, 64), tbounds);
465 tbounds.shrink(1);
466
467 os::SurfaceRef thumbnail = m_selected->getThumbnail();
468
469 ui::Paint paint;
470 paint.blendMode(os::BlendMode::SrcOver);
471
472 os::Sampling sampling;
473 if (thumbnail->width() > tbounds.w &&
474 thumbnail->height() > tbounds.h) {
475 sampling = os::Sampling(os::Sampling::Filter::Linear,
476 os::Sampling::Mipmap::Nearest);
477 }
478
479 g->drawSurface(thumbnail.get(),
480 gfx::Rect(0, 0, thumbnail->width(), thumbnail->height()),
481 tbounds,
482 sampling,
483 &paint);
484 }
485}
486
487void FileList::paintItem(ui::Graphics* g, IFileItem* fi, const int i)
488{
489 ItemInfo info = getFileItemInfo(i);
490 if ((g->getClipBounds() & info.bounds).isEmpty())
491 return;
492
493 auto theme = SkinTheme::get(this);
494 const bool evenRow = ((i & 1) == 0);
495 gfx::Rect tbounds = info.thumbnail;
496
497 gfx::Color bgcolor;
498 gfx::Color fgcolor;
499 if ((!m_multiselect && fi == m_selected) ||
500 (m_multiselect &&
501 m_selectedItems.size() == m_list.size() &&
502 m_selectedItems[i])) {
503 fgcolor = theme->colors.filelistSelectedRowText();
504 bgcolor = theme->colors.filelistSelectedRowFace();
505 }
506 else {
507 bgcolor = evenRow ? theme->colors.filelistEvenRowFace():
508 theme->colors.filelistOddRowFace();
509
510 if (fi->isFolder() && !fi->isBrowsable())
511 fgcolor = theme->colors.filelistDisabledRowText();
512 else
513 fgcolor = evenRow ? theme->colors.filelistEvenRowText():
514 theme->colors.filelistOddRowText();
515 }
516
517 // Item background
518 g->fillRect(bgcolor, info.bounds);
519
520 gfx::Rect textBounds = info.text;
521
522 // Folder icon or thumbnail
523 os::SurfaceRef thumbnail = nullptr;
524 if (fi->isFolder()) {
525 if (isListView()) {
526 thumbnail = theme->parts.folderIconSmall()->bitmapRef(0);
527 tbounds = textBounds;
528 tbounds.x += 2*guiscale();
529 tbounds.w = tbounds.h;
530 textBounds.x += tbounds.x2();
531 }
532 else {
533 thumbnail =
534 (m_zoom < 4.0 ?
535 theme->parts.folderIconMedium()->bitmapRef(0):
536 theme->parts.folderIconBig()->bitmapRef(0));
537 }
538 }
539 else {
540 thumbnail = fi->getThumbnail();
541 }
542
543 // item name
544 if (isIconView() && textBounds.w > info.bounds.w) {
545 g->drawAlignedUIText(
546 fi->displayName().c_str(),
547 fgcolor, bgcolor,
548 (textBounds & gfx::Rect(info.bounds).shrink(2*guiscale())),
549 ui::CENTER | ui::TOP | ui::CHARWRAP);
550 }
551 else {
552 g->drawText(
553 fi->displayName().c_str(),
554 fgcolor, bgcolor,
555 gfx::Point(textBounds.x+2*guiscale(),
556 textBounds.y+2*guiscale()));
557 }
558
559 // Draw thumbnail progress bar
560 double progress = fi->getThumbnailProgress();
561 if (isIconView() && !thumbnail) {
562 if (progress == 0.0)
563 generateThumbnailForFileItem(fi);
564 }
565
566 if (!tbounds.isEmpty()) {
567 if (thumbnail) {
568 tbounds =
569 gfx::Rect(0, 0, thumbnail->width(), thumbnail->height())
570 .fitIn(tbounds);
571
572 if (!fi->isFolder()) {
573 g->drawRect(gfx::rgba(0, 0, 0, 64), tbounds);
574 tbounds.shrink(1);
575 }
576
577 ui::Paint paint;
578 paint.blendMode(os::BlendMode::SrcOver);
579
580 os::Sampling sampling;
581 if (thumbnail->width() > tbounds.w &&
582 thumbnail->height() > tbounds.h) {
583 sampling = os::Sampling(os::Sampling::Filter::Linear,
584 os::Sampling::Mipmap::Nearest);
585 }
586
587 g->drawSurface(thumbnail.get(),
588 gfx::Rect(0, 0, thumbnail->width(), thumbnail->height()),
589 tbounds,
590 sampling,
591 &paint);
592 }
593 else {
594 tbounds = gfx::Rect(0, 0, 20*guiscale(), 2+4*(8.0-m_zoom)/8.0*guiscale())
595 .fitIn(tbounds);
596
597 // Start thumbnail generation for this item
598 generateThumbnailForFileItem(fi);
599 theme->paintProgressBar(g, tbounds, progress);
600 }
601 }
602}
603
604gfx::Rect FileList::mainThumbnailBounds()
605{
606 gfx::Rect result;
607
608 if (!m_selected)
609 return result;
610
611 os::SurfaceRef thumbnail = m_selected->getThumbnail();
612 if (!thumbnail)
613 return result;
614
615 ItemInfo info = getFileItemInfo(selectedIndex());
616 if (!info.thumbnail.isEmpty()) // There is thumbnail per item
617 return result;
618
619 View* view = View::getView(this);
620 gfx::Rect vp = view->viewportBounds();
621 int x = vp.x+vp.w - 2*guiscale() - thumbnail->width();
622 int y = info.bounds.center().y - thumbnail->height()/2 + bounds().y;
623 y = std::clamp(y, vp.y+2*guiscale(), vp.y+vp.h-3*guiscale()-thumbnail->height());
624 x -= bounds().x;
625 y -= bounds().y;
626 return gfx::Rect(x, y, thumbnail->width(), thumbnail->height());
627}
628
629void FileList::onSizeHint(SizeHintEvent& ev)
630{
631 if (!m_req_valid) {
632 gfx::Rect req;
633
634 // rows
635 for (int i=0; i<int(m_list.size()); ++i) {
636 ItemInfo info = getFileItemInfo(i);
637 req |= info.bounds;
638 }
639
640 m_req_valid = true;
641 m_req_w = req.w;
642 m_req_h = req.h;
643 }
644 ev.setSizeHint(Size(m_req_w, m_req_h));
645}
646
647void FileList::onFileSelected()
648{
649 FileSelected();
650}
651
652void FileList::onFileAccepted()
653{
654 FileAccepted();
655}
656
657void FileList::onCurrentFolderChanged()
658{
659 CurrentFolderChanged();
660}
661
662void FileList::onMonitoringTick()
663{
664 auto start = base::current_tick();
665 while (!m_generateThumbnailsForTheseItems.empty() &&
666 // No more than 200ms launching thumbnail generators
667 base::current_tick() - start < 200) {
668 auto fi = m_generateThumbnailsForTheseItems.front();
669 m_generateThumbnailsForTheseItems.pop_front();
670 ThumbnailGenerator::instance()->generateThumbnail(fi);
671 }
672
673 if (ThumbnailGenerator::instance()->checkWorkers())
674 invalidate();
675}
676
677void FileList::onGenerateThumbnailTick()
678{
679 m_generateThumbnailTimer.stop();
680
681 auto fi = m_itemToGenerateThumbnail;
682 if (fi)
683 generateThumbnailForFileItem(fi);
684}
685
686void FileList::recalcAllFileItemInfo()
687{
688 View* view = View::getView(this);
689 if (view)
690 view->setScrollableSize(gfx::Size(1, view->bounds().h), false);
691
692 m_info.resize(m_list.size());
693 if (m_info.empty()) {
694 m_itemsPerRow = 0;
695 return;
696 }
697
698 for (int i=0; i<int(m_info.size()); ++i)
699 m_info[i] = calcFileItemInfo(i);
700
701 m_itemsPerRow = 1;
702
703 // Add the vertical scrollbar space
704 if (isListView()) {
705 if (view) {
706 view->updateView();
707 if (!view->verticalBar()->isVisible()) {
708 gfx::Rect vp = view->viewportBounds();
709 for (auto& info : m_info) {
710 info.bounds.w = vp.w;
711 }
712 }
713 }
714 }
715 // Redistribute items in X axis
716 else if (isIconView()) {
717 int maxWidth = 0;
718 int maxTextWidth = 0;
719 for (const auto& info : m_info) {
720 int w = std::min(info.bounds.w, info.thumbnail.w*2);
721 maxWidth = std::max(maxWidth, w);
722 maxTextWidth = std::max(maxTextWidth, info.text.w);
723 }
724 if (maxWidth == 0)
725 return;
726
727 gfx::Size vp = (view ? view->viewportBounds().size(): size());
728
729 int itemsPerRow = vp.w / maxWidth;
730 if (itemsPerRow < 3) itemsPerRow = 3;
731 int itemWidth = vp.w / itemsPerRow;
732
733 int i = 0;
734 for (int y=0; i<int(m_info.size()); ) {
735 int h = 0;
736 int j = 0;
737 int x = 0;
738 for (; j<itemsPerRow && i<int(m_info.size()); ++j, ++i, x += itemWidth) {
739 auto& info = m_info[i];
740 int deltax = x - info.bounds.x;
741 int deltay = y - info.bounds.y;
742 info.bounds.x += deltax;
743 info.bounds.y += deltay;
744 info.bounds.w = itemWidth;
745
746 if (maxTextWidth > itemWidth) {
747 info.bounds.h += info.text.h;
748 info.text.h += info.text.h;
749 }
750
751 info.text.x = info.bounds.x + info.bounds.w/2 - info.text.w/2;
752 info.text.y += deltay;
753 info.thumbnail.x = info.bounds.x + info.bounds.w/2 - info.thumbnail.w/2;
754 info.thumbnail.y += deltay;
755 h = info.bounds.h;
756 }
757 if (h)
758 y += h;
759 else
760 break;
761 }
762
763 m_itemsPerRow = itemsPerRow;
764 }
765
766 invalidate();
767 if (view) {
768 view->updateView();
769 makeSelectedFileitemVisible();
770 }
771}
772
773FileList::ItemInfo FileList::calcFileItemInfo(int i) const
774{
775 ASSERT(i >= 0 && i < int(m_list.size()));
776
777 const bool withThumbnails = hasThumbnailsPerItem();
778 IFileItem* fi = m_list[i];
779 int len = 0;
780
781 if (fi->isFolder() && isListView()) {
782 auto theme = SkinTheme::get(this);
783 len += theme->parts.folderIconSmall()->bitmap(0)->width() + 2*guiscale();
784 }
785
786 len += font()->textLength(fi->displayName().c_str());
787
788 int textHeight = this->textHeight() + 4*guiscale();
789 int rowHeight = textHeight + (withThumbnails ? 8*m_zoom+2*guiscale(): 0);
790
791 ItemInfo info;
792 info.text = gfx::Rect(0, 0+rowHeight*i, len+4*guiscale(), textHeight);
793 if (withThumbnails) {
794 info.thumbnail = gfx::Rect(0, info.text.y,
795 8*m_zoom*guiscale(),
796 8*m_zoom*guiscale());
797 info.text.y += info.thumbnail.h + 2*guiscale();
798 }
799
800 info.bounds = info.text | info.thumbnail;
801 if (withThumbnails) {
802 info.text.x = info.bounds.x + info.bounds.w/2 - info.text.w/2;
803 info.thumbnail.x = info.bounds.x + info.bounds.w/2 - info.thumbnail.w/2;
804 }
805 else {
806 info.bounds.x = 0;
807 if (View* view = View::getView(this))
808 info.bounds.w = view->viewportBounds().w;
809 else
810 info.bounds.w = bounds().w;
811 }
812 return info;
813}
814
815FileList::ItemInfo FileList::getFileItemInfo(int i) const
816{
817 ASSERT(i >= 0 && i < int(m_info.size()));
818
819 if (i >= 0 && i < int(m_info.size()))
820 return m_info[i];
821 else
822 return ItemInfo();
823}
824
825void FileList::makeSelectedFileitemVisible()
826{
827 int i = selectedIndex();
828 if (i < 0)
829 return;
830
831 View* view = View::getView(this);
832 gfx::Rect vp = view->viewportBounds();
833 gfx::Point scroll = view->viewScroll();
834 ItemInfo info = getFileItemInfo(i);
835
836 if (info.bounds.x+bounds().x <= vp.x)
837 scroll.x = info.bounds.x;
838 else if (info.bounds.x+bounds().x > vp.x2() - info.bounds.w)
839 scroll.x = info.bounds.x - vp.w + info.bounds.w;
840
841 if (info.bounds.y+bounds().y < vp.y)
842 scroll.y = info.bounds.y;
843 else if (info.bounds.y+bounds().y > vp.y2() - info.bounds.h)
844 scroll.y = info.bounds.y - vp.h + info.bounds.h;
845
846 view->setViewScroll(scroll);
847}
848
849void FileList::regenerateList()
850{
851 // get the children of the current folder
852 m_list = m_currentFolder->children();
853
854 // filter the list by the available extensions
855 if (!m_exts.empty()) {
856 for (FileItemList::iterator
857 it=m_list.begin();
858 it!=m_list.end(); ) {
859 IFileItem* fileitem = *it;
860
861 if (fileitem->isHidden())
862 it = m_list.erase(it);
863 else if (!fileitem->isFolder() &&
864 !fileitem->hasExtension(m_exts)) {
865 it = m_list.erase(it);
866 }
867 else
868 ++it;
869 }
870 }
871
872 recalcAllFileItemInfo();
873
874 if (m_multiselect && !m_list.empty()) {
875 m_selectedItems.resize(m_list.size());
876 deselectedFileItems();
877 }
878 else
879 m_selectedItems.clear();
880}
881
882int FileList::selectedIndex() const
883{
884 for (auto it = m_list.begin(), end = m_list.end();
885 it != end; ++it) {
886 if (*it == m_selected)
887 return it - m_list.begin();
888 }
889 return -1;
890}
891
892void FileList::selectIndex(int index)
893{
894 IFileItem* oldSelected = m_selected;
895
896 m_selected = m_list.at(index);
897 deselectedFileItems();
898
899 if (oldSelected != m_selected) {
900 makeSelectedFileitemVisible();
901
902 invalidate();
903
904 // Emit "FileSelected" event.
905 onFileSelected();
906 }
907
908 delayThumbnailGenerationForSelectedItem();
909}
910
911void FileList::generateThumbnailForFileItem(IFileItem* fi)
912{
913 if (fi && fi->needThumbnail() && animation() == ANI_NONE) {
914 auto it = std::find(m_generateThumbnailsForTheseItems.begin(),
915 m_generateThumbnailsForTheseItems.end(), fi);
916 if (it != m_generateThumbnailsForTheseItems.end())
917 m_generateThumbnailsForTheseItems.erase(it);
918 m_generateThumbnailsForTheseItems.push_front(fi);
919 }
920}
921
922void FileList::delayThumbnailGenerationForSelectedItem()
923{
924 if (m_selected &&
925 !m_selected->isFolder() &&
926 !m_selected->getThumbnail()) {
927 m_itemToGenerateThumbnail = m_selected;
928 m_generateThumbnailTimer.start();
929 }
930}
931
932void FileList::onAnimationStop(int animation)
933{
934 if (animation == ANI_ZOOM)
935 setZoom(m_toZoom);
936}
937
938void FileList::onAnimationFrame()
939{
940 if (animation() == ANI_ZOOM)
941 setZoom(inbetween(m_fromZoom, m_toZoom, animationTime()));
942}
943
944} // namespace app
945