Skip to content

Commit 6cf317a

Browse files
authored
Loading images from resources (#508)
1 parent 3bbc635 commit 6cf317a

File tree

11 files changed

+63
-486
lines changed

11 files changed

+63
-486
lines changed

ReactQt/application/src/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
1111
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
1212
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
1313
set(FONTS_RESOURCE "")
14+
set(IMAGES_RESOURCE "")
1415

1516
find_package(Qt5Core REQUIRED)
1617
find_package(Qt5Qml REQUIRED)
@@ -31,6 +32,15 @@ foreach(FONT_PATH ${DESKTOP_FONTS})
3132
endforeach(FONT_PATH)
3233

3334

35+
# Add all external desktop images to resources. Images will be outside of directory with qrc,
36+
# so we are using aliases to load them
37+
foreach(IMAGE_PATH ${DESKTOP_IMAGES})
38+
get_filename_component(IMAGE_FILENAME ${IMAGE_PATH} NAME_WE)
39+
STRING(CONCAT TMP ${IMAGES_RESOURCE} "<file alias=\"${IMAGE_FILENAME}\">${IMAGE_PATH}</file>")
40+
SET(IMAGES_RESOURCE ${TMP})
41+
endforeach(IMAGE_PATH)
42+
43+
3444
# Format EXTERNAL_MODULES to contain array of external modules type names defined as strings
3545
string (REPLACE ";" "," EXTERNAL_MODULES "${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES}")
3646

ReactQt/application/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ReactNativeProperties : public QObject {
127127
QString m_packagerTemplate = "http://%1:%2/index.desktop.bundle?platform=desktop&dev=true";
128128
QUrl m_codeLocation;
129129
QString m_pluginsPath;
130-
QString m_executor = "LocalServerConnection";
130+
QString m_executor = "RemoteServerConnection";
131131
QVariantMap m_initialProps;
132132
};
133133

ReactQt/application/src/main.qrc.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<file>main.qml</file>
1414
${JS_BUNDLE_RESOURCE}
1515
${FONTS_RESOURCE}
16+
${IMAGES_RESOURCE}
1617
${ICON_PNG_RESOURCE}
1718
</qresource>
1819
</RCC>

ReactQt/runtime/src/componentmanagers/imagemanager.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ImageManagerPrivate {
4343
public:
4444
bool isBase64ImageUrl(const QUrl& url) const;
4545
void setSource(QObject* image, const QUrl& url);
46+
bool isStoredInResources(const QString& fileName);
4647
};
4748

4849
ImageManager::ImageManager(QObject* parent) : ViewManager(parent), d_ptr(new ImageManagerPrivate) {}
@@ -70,26 +71,28 @@ void ImageManager::manageSource(const QVariantMap& imageSource, QQuickItem* imag
7071

7172
auto imageLoader = bridge()->imageLoader();
7273

73-
QUrl source = imageSource[URI_KEY].toUrl();
74+
QUrl imageSourceUrl = imageSource[URI_KEY].toUrl();
75+
QString imageSourceStr = imageSource[URI_KEY].toString();
7476

75-
if (d->isBase64ImageUrl(source)) {
76-
d->setSource(image, source);
77+
if (d->isBase64ImageUrl(imageSourceUrl)) {
78+
d->setSource(image, imageSourceUrl);
7779
return;
7880
}
7981

80-
image->setProperty("isSVG", source.toString(QUrl::RemoveQuery).endsWith("svg"));
82+
image->setProperty("isSVG", imageSourceUrl.toString(QUrl::RemoveQuery).endsWith("svg"));
8183

82-
if (source.scheme() == "file" && imageSource[URI_KEY].toString().startsWith(FILE_SCHEME)) {
83-
source = QUrl(imageSource[URI_KEY].toString().replace(FILE_SCHEME, ""));
84+
if (imageSourceUrl.scheme() == "file" && imageSource[URI_KEY].toString().startsWith(FILE_SCHEME)) {
85+
imageSourceUrl = QUrl(imageSourceStr.replace(FILE_SCHEME, ""));
8486
}
85-
86-
if (source.isRelative()) {
87-
source = QUrl::fromLocalFile(QFileInfo(source.toString()).absoluteFilePath());
87+
if (d->isStoredInResources(imageSourceStr)) {
88+
imageSourceUrl = QUrl(QString("qrc:/") + imageSourceStr);
89+
} else if (imageSourceUrl.isRelative()) {
90+
imageSourceUrl = QUrl::fromLocalFile(QFileInfo(imageSourceUrl.toString()).absoluteFilePath());
8891
}
8992

90-
imageLoader->loadImage(source, [=](ImageLoader::Event event, const QVariantMap& data) {
93+
imageLoader->loadImage(imageSourceUrl, [=](ImageLoader::Event event, const QVariantMap& data) {
9194
if (event == ImageLoader::Event_LoadSuccess) {
92-
d->setSource(image, source);
95+
d->setSource(image, imageSourceUrl);
9396
}
9497
bool eventHandlerSet =
9598
image->property(QString(QML_PROPERTY_PREFIX + eventNames[event]).toStdString().c_str()).toBool();
@@ -117,4 +120,10 @@ void ImageManagerPrivate::setSource(QObject* image, const QUrl& url) {
117120
image->setProperty("managedSource", url);
118121
}
119122

123+
bool ImageManagerPrivate::isStoredInResources(const QString& fileName) {
124+
125+
QFile file(QString(":/") + fileName);
126+
return file.exists();
127+
}
128+
120129
#include "imagemanager.moc"

local-cli/desktop/build.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function build(args, dependencies) {
2626
console.log("Found desktop JS bundle path: " + desktopJSBundlePath);
2727
var desktopFonts = _findDesktopFonts(args);
2828
console.log("Found desktop fonts: " + desktopFonts);
29+
var desktopImages = _findDesktopImages(args);
30+
console.log("Found desktop images: " + desktopImages);
2931

3032
return _findModules(args).then((dependencies) => {
3133
return new Promise((resolve, reject) => {
@@ -40,7 +42,7 @@ function build(args, dependencies) {
4042
}
4143
});
4244
}).then(() => {
43-
return _buildApplication(args, desktopExternalModules, desktopJSBundlePath, desktopFonts);
45+
return _buildApplication(args, desktopExternalModules, desktopJSBundlePath, desktopFonts, desktopImages);
4446
});
4547
}
4648

@@ -59,6 +61,11 @@ function _findDesktopFonts(args) {
5961
return JSON.parse(data).desktopFonts;
6062
}
6163

64+
function _findDesktopImages(args) {
65+
var data = fs.readFileSync(path.join(args.root, 'package.json'));
66+
return JSON.parse(data).desktopImages;
67+
}
68+
6269
function _findModules(args) {
6370
return new Promise((resolve, reject) => {
6471
fs.readFile(path.join(args.root, 'package.json'), (err, data) => {
@@ -121,7 +128,7 @@ function _buildModules(args, dependencies, resolve, reject) {
121128
});
122129
}
123130

124-
function _buildApplication(args, desktopExternalModules, desktopJSBundlePath, desktopFonts) {
131+
function _buildApplication(args, desktopExternalModules, desktopJSBundlePath, desktopFonts, desktopImages) {
125132
return new Promise((resolve, reject) => {
126133
console.log(chalk.bold('Building the app...'));
127134

@@ -140,6 +147,10 @@ function _buildApplication(args, desktopExternalModules, desktopJSBundlePath, de
140147
buildArguments.push("-f");
141148
buildArguments.push(desktopFonts.toString().replace(/,/g, ';'));
142149
}
150+
if (typeof desktopImages !== 'undefined' && desktopImages !== null) {
151+
buildArguments.push("-i");
152+
buildArguments.push(desktopImages.toString().replace(/,/g, ';'));
153+
}
143154
if (process.platform === "win32") {
144155
buildArguments.push("-g");
145156
buildArguments.push("MinGW Makefiles");

local-cli/generator-desktop/templates/build.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ SET option
2525
echo "build.bat external modules paths: "%option-e%
2626
echo "build.bat JS bundle path: "%option-j%
2727
echo "build.bat desktop fonts: "%option-f%
28+
echo "build.bat desktop images: "%option-i%
2829
echo "build.bat cmake generator: "%option-g%
2930

3031
@rem Workaround
3132
@rem rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile
3233

3334
@rem Build project
3435
echo %CD%
35-
cmake -DCMAKE_BUILD_TYPE=Debug -G %option-g% -DEXTERNAL_MODULES_DIR=%option-e% -DJS_BUNDLE_PATH=%option-j% -DDESKTOP_FONTS=%option-f% . && cmake --build .
36+
cmake -DCMAKE_BUILD_TYPE=Debug -G %option-g% -DEXTERNAL_MODULES_DIR=%option-e% -DJS_BUNDLE_PATH=%option-j% -DDESKTOP_FONTS=%option-f% -DDESKTOP_IMAGES=%option-i% . && cmake --build .

local-cli/generator-desktop/templates/build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ if [[ $1 == "-f" ]]; then
2323
shift
2424
desktopFonts="$1"
2525
fi
26+
if [[ $1 == "-i" ]]; then
27+
shift
28+
desktopImages="$1"
29+
fi
2630
shift
2731
done
2832

2933
echo "build.sh external modules paths: "$ExternalModulesPaths
3034
echo "build.sh JS bundle path: "$JsBundlePath
3135
echo "build.sh desktop fonts: "$desktopFonts
36+
echo "build.sh desktop images: "$desktopImages
3237

3338
# Workaround
3439
rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile
3540

3641
# Build project
37-
cmake -DCMAKE_BUILD_TYPE=Debug -DEXTERNAL_MODULES_DIR="$ExternalModulesPaths" -DJS_BUNDLE_PATH="$JsBundlePath" -DDESKTOP_FONTS="$desktopFonts" . && make
42+
cmake -DCMAKE_BUILD_TYPE=Debug -DEXTERNAL_MODULES_DIR="$ExternalModulesPaths" -DJS_BUNDLE_PATH="$JsBundlePath" -DDESKTOP_FONTS="$desktopFonts" -DDESKTOP_IMAGES="$desktopImages" . && make

local-cli/templates/HelloWorld/desktop/build.bat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ SET option
2525
echo "build.bat external modules paths: "%option-e%
2626
echo "build.bat JS bundle path: "%option-j%
2727
echo "build.bat desktop fonts: "%option-f%
28+
echo "build.bat desktop images: "%option-i%
2829
echo "build.bat cmake generator: "%option-g%
2930

3031
@rem Workaround
3132
@rem rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile
3233

3334
@rem Build project
3435
echo %CD%
35-
cmake -DCMAKE_BUILD_TYPE=Debug -G %option-g% -DEXTERNAL_MODULES_DIR=%option-e% -DJS_BUNDLE_PATH=%option-j% -DDESKTOP_FONTS=%option-f% . && cmake --build .
36+
cmake -DCMAKE_BUILD_TYPE=Debug -G %option-g% -DEXTERNAL_MODULES_DIR=%option-e% -DJS_BUNDLE_PATH=%option-j% -DDESKTOP_FONTS=%option-f% -DDESKTOP_IMAGES=%option-i% . && cmake --build .

local-cli/templates/HelloWorld/desktop/build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ if [[ $1 == "-f" ]]; then
2323
shift
2424
desktopFonts="$1"
2525
fi
26+
if [[ $1 == "-i" ]]; then
27+
shift
28+
desktopImages="$1"
29+
fi
2630
shift
2731
done
2832

2933
echo "build.sh external modules paths: "$ExternalModulesPaths
3034
echo "build.sh JS bundle path: "$JsBundlePath
3135
echo "build.sh desktop fonts: "$desktopFonts
36+
echo "build.sh desktop images: "$desktopImages
3237

3338
# Workaround
3439
rm -rf CMakeFiles CMakeCache.txt cmake_install.cmake Makefile
3540

3641
# Build project
37-
cmake -DCMAKE_BUILD_TYPE=Debug -DEXTERNAL_MODULES_DIR="$ExternalModulesPaths" -DJS_BUNDLE_PATH="$JsBundlePath" -DDESKTOP_FONTS="$desktopFonts" . && make
42+
cmake -DCMAKE_BUILD_TYPE=Debug -DEXTERNAL_MODULES_DIR="$ExternalModulesPaths" -DJS_BUNDLE_PATH="$JsBundlePath" -DDESKTOP_FONTS="$desktopFonts" -DDESKTOP_IMAGES="$desktopImages" . && make

0 commit comments

Comments
 (0)