mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-30 23:27:22 +00:00
Optional C++ namespaces (#4759)
Co-authored-by: Michael Winkelmann <michael@winkelmann.site>
This commit is contained in:
parent
43266f7f76
commit
bef532b5fc
11 changed files with 148 additions and 19 deletions
|
@ -11,6 +11,19 @@ set_property(CACHE DEFAULT_SLINT_EMBED_RESOURCES PROPERTY STRINGS
|
|||
# INITIALIZE_FROM_VARIABLE DEFAULT_SLINT_EMBED_RESOURCES)
|
||||
|
||||
function(SLINT_TARGET_SOURCES target)
|
||||
# Parse the NAMESPACE argument
|
||||
cmake_parse_arguments(SLINT_TARGET_SOURCES "" "NAMESPACE" "" ${ARGN})
|
||||
|
||||
if (DEFINED SLINT_TARGET_SOURCES_NAMESPACE)
|
||||
# Remove the NAMESPACE argument from the list
|
||||
list(FIND ARGN "NAMESPACE" _index)
|
||||
list(REMOVE_AT ARGN ${_index})
|
||||
list(FIND ARGN "${SLINT_TARGET_SOURCES_NAMESPACE}" _index)
|
||||
list(REMOVE_AT ARGN ${_index})
|
||||
# If the namespace is not empty, add the --cpp-namespace argument
|
||||
set(_SLINT_CPP_NAMESPACE_ARG "--cpp-namespace=${SLINT_TARGET_SOURCES_NAMESPACE}")
|
||||
endif()
|
||||
|
||||
foreach (it IN ITEMS ${ARGN})
|
||||
get_filename_component(_SLINT_BASE_NAME ${it} NAME_WE)
|
||||
get_filename_component(_SLINT_ABSOLUTE ${it} REALPATH BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
@ -28,6 +41,7 @@ function(SLINT_TARGET_SOURCES target)
|
|||
--style ${_SLINT_STYLE}
|
||||
--embed-resources=${embed}
|
||||
--translation-domain="${target}"
|
||||
${_SLINT_CPP_NAMESPACE_ARG}
|
||||
DEPENDS Slint::slint-compiler ${_SLINT_ABSOLUTE}
|
||||
COMMENT "Generating ${_SLINT_BASE_NAME}.h"
|
||||
DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${_SLINT_BASE_NAME}.d
|
||||
|
|
|
@ -5,23 +5,26 @@
|
|||
## `slint_target_sources`
|
||||
|
||||
```
|
||||
slint_target_sources(<target> <files>....)
|
||||
slint_target_sources(<target> <files>.... [NAMESPACE namespace])
|
||||
```
|
||||
|
||||
Use this function to tell cmake about the .slint files of your application, similar to the builtin cmake [target_sources](https://cmake.org/cmake/help/latest/command/target_sources.html) function.
|
||||
The function takes care of running the slint-compiler to convert `.slint` files to `.h` files in the build directory,
|
||||
and extend the include directories of your target so that the generated file is found when including it in your application.
|
||||
|
||||
The optional NAMESPACE argument will put the generated components in the given C++ namespace.
|
||||
|
||||
Given a file called `the_window.slint`, the following example will create a file called `the_window.h` that can
|
||||
be included from your .cpp file.
|
||||
be included from your .cpp file. Assuming the `the_window.slint` contains a component `TheWindow`, the output
|
||||
C++ class will be put in the namespace `ui`, resulting to `ui::TheWindow`.
|
||||
|
||||
```cmake
|
||||
add_executable(my_application main.cpp)
|
||||
target_link_libraries(my_application PRIVATE Slint::Slint)
|
||||
slint_target_sources(my_application the_window.slint)
|
||||
slint_target_sources(my_application the_window.slint NAMESPACE ui)
|
||||
```
|
||||
|
||||
|
||||
## Resource Embedding
|
||||
|
||||
By default, images from [`@image-url()`](slint-reference:src/language/syntax/types#images) or fonts that your Slint files reference are loaded from disk at run-time. This minimises build times, but requires that the directory structure with the files remains stable. If you want to build a program that runs anywhere, then you can configure the Slint compiler to embed such sources into the binary.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue