From cfbe853f69825c7bec986e74831911b2c3cf534b Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 22 Jul 2025 12:15:00 +0100 Subject: [PATCH] Projucer: Enforce that Icons instances are created from files --- .../juce_build_tools/utils/juce_Icons.cpp | 52 +++++++++++-------- .../Build/juce_build_tools/utils/juce_Icons.h | 15 ++++-- extras/Build/juceaide/Main.cpp | 11 ++-- .../Projucer/Source/Project/jucer_Project.cpp | 13 ----- .../Projucer/Source/Project/jucer_Project.h | 3 -- .../jucer_ProjectExport_Android.h | 8 +-- .../ProjectSaving/jucer_ProjectExporter.cpp | 12 +++-- 7 files changed, 59 insertions(+), 55 deletions(-) diff --git a/extras/Build/juce_build_tools/utils/juce_Icons.cpp b/extras/Build/juce_build_tools/utils/juce_Icons.cpp index 6ae5c59e01..6ae6cf533a 100644 --- a/extras/Build/juce_build_tools/utils/juce_Icons.cpp +++ b/extras/Build/juce_build_tools/utils/juce_Icons.cpp @@ -35,22 +35,28 @@ namespace juce::build_tools { - Array asArray (const Icons& icons) + Icons Icons::fromFilesSmallAndBig (const File& small, const File& big) { - Array result; + Icons result; + result.small = Drawable::createFromImageFile (small); + result.big = Drawable::createFromImageFile (big); + return result; + } - if (icons.small != nullptr) - result.add (icons.small.get()); + Array asArray (const Icons& icons) + { + Array result; - if (icons.big != nullptr) - result.add (icons.big.get()); + for (auto getter : { &Icons::getSmall, &Icons::getBig }) + if (auto* got = (icons.*getter)()) + result.add (got); return result; } namespace mac { - static Image fixIconImageSize (Drawable& image) + static Image fixIconImageSize (const Drawable& image) { const int validSizes[] = { 16, 32, 64, 128, 256, 512, 1024 }; @@ -90,7 +96,7 @@ namespace juce::build_tools { MemoryOutputStream data; auto smallest = std::numeric_limits::max(); - Drawable* smallestImage = nullptr; + const Drawable* smallestImage = nullptr; const auto images = asArray (icons); @@ -134,25 +140,25 @@ namespace juce::build_tools int size, bool returnNullIfNothingBigEnough) { - auto* const im = [&]() -> Drawable* + auto* const im = std::invoke ([&]() -> const Drawable* { - if ((icons.small != nullptr) != (icons.big != nullptr)) - return icons.small != nullptr ? icons.small.get() : icons.big.get(); + if ((icons.getSmall() != nullptr) != (icons.getBig() != nullptr)) + return icons.getSmall() != nullptr ? icons.getSmall() : icons.getBig(); - if (icons.small != nullptr && icons.big != nullptr) + if (icons.getSmall() != nullptr && icons.getBig() != nullptr) { - if (icons.small->getWidth() >= size && icons.big->getWidth() >= size) - return icons.small->getWidth() < icons.big->getWidth() ? icons.small.get() : icons.big.get(); + if (icons.getSmall()->getWidth() >= size && icons.getBig()->getWidth() >= size) + return icons.getSmall()->getWidth() < icons.getBig()->getWidth() ? icons.getSmall() : icons.getBig(); - if (icons.small->getWidth() >= size) - return icons.small.get(); + if (icons.getSmall()->getWidth() >= size) + return icons.getSmall(); - if (icons.big->getWidth() >= size) - return icons.big.get(); + if (icons.getBig()->getWidth() >= size) + return icons.getBig(); } return nullptr; - }(); + }); if (im == nullptr) return {}; @@ -304,9 +310,9 @@ namespace juce::build_tools writeStreamToFile (file, [&] (MemoryOutputStream& mo) { writeWinIcon (icons, mo); }); } - Image rescaleImageForIcon (Drawable& d, const int size) + Image rescaleImageForIcon (const Drawable& d, const int size) { - if (auto* drawableImage = dynamic_cast (&d)) + if (auto* drawableImage = dynamic_cast (&d)) { auto im = SoftwareImageType().convert (drawableImage->getImage()); @@ -365,8 +371,8 @@ namespace juce::build_tools static void createiOSIconFiles (const Icons& icons, File appIconSet) { - auto* imageToUse = icons.big != nullptr ? icons.big.get() - : icons.small.get(); + auto* imageToUse = icons.getBig() != nullptr ? icons.getBig() + : icons.getSmall(); if (imageToUse != nullptr) { diff --git a/extras/Build/juce_build_tools/utils/juce_Icons.h b/extras/Build/juce_build_tools/utils/juce_Icons.h index 708ecfba94..57bab078da 100644 --- a/extras/Build/juce_build_tools/utils/juce_Icons.h +++ b/extras/Build/juce_build_tools/utils/juce_Icons.h @@ -35,20 +35,29 @@ namespace juce::build_tools { - struct Icons + class Icons { + public: + Icons() = default; + + static Icons fromFilesSmallAndBig (const File& small, const File& big); + + const Drawable* getSmall() const { return small.get(); } + const Drawable* getBig() const { return big.get(); } + + private: std::unique_ptr small; std::unique_ptr big; }; - Array asArray (const Icons&); + Array asArray (const Icons&); void writeMacIcon (const Icons&, const File&); void writeWinIcon (const Icons&, const File&); Image getBestIconForSize (const Icons& icons, int size, bool returnNullIfNothingBigEnough); - Image rescaleImageForIcon (Drawable& d, int size); + Image rescaleImageForIcon (const Drawable& d, int size); RelativePath createXcassetsFolderFromIcons (const Icons& icons, const File& targetFolder, diff --git a/extras/Build/juceaide/Main.cpp b/extras/Build/juceaide/Main.cpp index e7eef131be..34e9847294 100644 --- a/extras/Build/juceaide/Main.cpp +++ b/extras/Build/juceaide/Main.cpp @@ -124,19 +124,18 @@ IconParseResults parseIconArguments (juce::ArgumentList&& args) args.checkMinNumArguments (2); const auto output = args.arguments.removeAndReturn (0); - const auto popDrawable = [&args]() -> std::unique_ptr + const auto popFile = [&args]() -> juce::File { if (args.size() == 0) return {}; - const auto firstArgText = args.arguments.removeAndReturn (0).text; - return juce::Drawable::createFromImageFile (firstArgText); + return args.arguments.removeAndReturn (0).text; }; - auto smallIcon = popDrawable(); - auto bigIcon = popDrawable(); + const auto smallIcon = popFile(); + const auto bigIcon = popFile(); - return { { std::move (smallIcon), std::move (bigIcon) }, output.text }; + return { juce::build_tools::Icons::fromFilesSmallAndBig (smallIcon, bigIcon), output.text }; } int writeMacIcon (juce::ArgumentList&& argumentList) diff --git a/extras/Projucer/Source/Project/jucer_Project.cpp b/extras/Projucer/Source/Project/jucer_Project.cpp index 2e9bd40d3e..24c93b7e84 100644 --- a/extras/Projucer/Source/Project/jucer_Project.cpp +++ b/extras/Projucer/Source/Project/jucer_Project.cpp @@ -1632,19 +1632,6 @@ Project::Item Project::Item::createCopy() { Item i (*this); i.state = i. String Project::Item::getID() const { return state [Ids::ID]; } void Project::Item::setID (const String& newID) { state.setProperty (Ids::ID, newID, nullptr); } -std::unique_ptr Project::Item::loadAsImageFile() const -{ - const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob()); - - if (! mml.lockWasGained()) - return nullptr; - - if (isValid()) - return Drawable::createFromImageFile (getFile()); - - return {}; -} - Project::Item Project::Item::createGroup (Project& project, const String& name, const String& uid, bool isModuleCode) { Item group (project, ValueTree (Ids::GROUP), isModuleCode); diff --git a/extras/Projucer/Source/Project/jucer_Project.h b/extras/Projucer/Source/Project/jucer_Project.h index 96d60a31de..d1d7f1e8f1 100644 --- a/extras/Projucer/Source/Project/jucer_Project.h +++ b/extras/Projucer/Source/Project/jucer_Project.h @@ -453,9 +453,6 @@ public: void setID (const String& newID); Item findItemWithID (const String& targetId) const; // (recursive search) - String getImageFileID() const; - std::unique_ptr loadAsImageFile() const; - //============================================================================== Value getNameValue(); String getName() const; diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h index 841a8df94f..753f140523 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h @@ -1424,15 +1424,15 @@ private: { const auto icons = getIcons(); - if (icons.big != nullptr && icons.small != nullptr) + if (icons.getBig() != nullptr && icons.getSmall() != nullptr) { - auto step = jmax (icons.big->getWidth(), icons.big->getHeight()) / 8; + auto step = jmax (icons.getBig()->getWidth(), icons.getBig()->getHeight()) / 8; writeIcon (folder.getChildFile ("drawable-xhdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 8, false)); writeIcon (folder.getChildFile ("drawable-hdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 6, false)); writeIcon (folder.getChildFile ("drawable-mdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 4, false)); writeIcon (folder.getChildFile ("drawable-ldpi/icon.png"), build_tools::getBestIconForSize (icons, step * 3, false)); } - else if (auto* icon = (icons.big != nullptr ? icons.big.get() : icons.small.get())) + else if (auto* icon = (icons.getBig() != nullptr ? icons.getBig() : icons.getSmall())) { writeIcon (folder.getChildFile ("drawable-mdpi/icon.png"), build_tools::rescaleImageForIcon (*icon, icon->getWidth())); } @@ -1829,7 +1829,7 @@ private: { const auto icons = getIcons(); - if (icons.big != nullptr || icons.small != nullptr) + if (icons.getBig() != nullptr || icons.getSmall() != nullptr) app->setAttribute ("android:icon", "@drawable/icon"); } diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp index 5cfb1c21c4..a6d9091cdf 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExporter.cpp @@ -869,12 +869,18 @@ void ProjectExporter::createDefaultConfigs() build_tools::Icons ProjectExporter::getIcons() const { - const auto loadIcon = [this] (auto id) + const MessageManagerLock mml (ThreadPoolJob::getCurrentThreadPoolJob()); + + if (! mml.lockWasGained()) + return {}; + + const auto getFile = [this] (auto id) { - return project.getMainGroup().findItemWithID (settings[id]).loadAsImageFile(); + return project.getMainGroup().findItemWithID (settings[id]).getFile(); }; - return { loadIcon (Ids::smallIcon), loadIcon (Ids::bigIcon) }; + return build_tools::Icons::fromFilesSmallAndBig (getFile (Ids::smallIcon), + getFile (Ids::bigIcon)); } //==============================================================================