@@ -10,10 +10,62 @@ class ImageDemo final : public DemoBase,
1010 {
1111 imageFormatManager.registerBasicFormats ();
1212
13+ SafePointer sp (this );
14+
15+ open.onClick = [this , sp]()
16+ {
17+ if (sp == nullptr )
18+ return ;
19+
20+ if (fileChooser != nullptr )
21+ {
22+ return ;
23+ }
24+
25+ fileChooser = std::make_unique<FileChooser> (TRANS (" Find an image to load." ),
26+ File::getSpecialLocation (File::userPicturesDirectory),
27+ imageFormatManager.getWildcardForAllFormats ());
28+
29+ fileChooser->launchAsync (FileBrowserComponent::openMode, [sp] (const FileChooser& chooser)
30+ {
31+ if (sp == nullptr )
32+ return ;
33+
34+ sp->setImage (chooser.getURLResult ());
35+ sp->fileChooser .reset ();
36+ });
37+ };
38+
1339 addAndMakeVisible (imageComponent);
1440 addAndMakeVisible (open);
1541 }
1642
43+ // ==============================================================================
44+ /* * Changes the currently displayed with the given one.
45+ If the given image is null, the underlying image component will be cleared.
46+ */
47+ void setImage (const Image& image)
48+ {
49+ imageComponent.setImage (image);
50+ repaint ();
51+ }
52+
53+ /* * Changes the currently displayed with one from the given file.
54+ If the given image fails to load, the underlying image component will be cleared.
55+ */
56+ void setImage (const File& file)
57+ {
58+ setImage (imageFormatManager.loadFrom (file));
59+ }
60+
61+ /* * Changes the currently displayed with one from the given URL.
62+ If the given image fails to load, the underlying image component will be cleared.
63+ */
64+ void setImage (const URL& url)
65+ {
66+ setImage (imageFormatManager.loadFrom (url));
67+ }
68+
1769 // ==============================================================================
1870 /* * @internal */
1971 void paint (Graphics& g) override
@@ -37,7 +89,7 @@ class ImageDemo final : public DemoBase,
3789 }
3890
3991 /* * @internal */
40- bool isInterestedInTextDrag (const String& text) override { return isBase64 (text); }
92+ bool isInterestedInTextDrag (const String& text) override { return isPossiblyBase64 (text); }
4193
4294 /* * @internal */
4395 bool isInterestedInFileDrag (const StringArray& files) override
@@ -58,7 +110,7 @@ class ImageDemo final : public DemoBase,
58110 const File file (path);
59111 if (file.existsAsFile () && imageFormatManager.findFormatForFile (file) != nullptr )
60112 {
61- setImage (imageFormatManager. loadFrom ( file) );
113+ setImage (file);
62114 return ;
63115 }
64116 }
@@ -67,7 +119,7 @@ class ImageDemo final : public DemoBase,
67119 /* * @internal */
68120 void textDropped (const String& text, int , int ) override
69121 {
70- if (isBase64 (text))
122+ if (isPossiblyBase64 (text))
71123 setImage (imageFormatManager.fromBase64 (text));
72124 }
73125
@@ -82,15 +134,10 @@ class ImageDemo final : public DemoBase,
82134 ImageFormatManager imageFormatManager;
83135 HighQualityImageComponent imageComponent;
84136 TextButton open;
137+ std::unique_ptr<FileChooser> fileChooser;
85138
86139 // ==============================================================================
87- void setImage (const Image& image)
88- {
89- imageComponent.setImage (image);
90- repaint ();
91- }
92-
93- static bool isBase64 (const String& text)
140+ static bool isPossiblyBase64 (const String& text)
94141 {
95142 const String b64 = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
96143
0 commit comments