![]() |
CYD-UI
A C++ library for building native graphic user interfaces
|
Cyd-UI is a C++ library for building native user interfaces. It is aimed at desktop applications and can be easily integrated into existing code bases. Cyd-UI is module based, only preprocessor macros can be included through header files.
Since this is a module library, the recommended way to integrate it into your project is with CMake. This is easy to do with FetchContent
:
As an alternative for projects that do not use CMake as a build system, this repository can be added as a submodule. The whole library can be found within the include/
directory. Make sure module dependency scanning can find this directory.
Components are declared with the macro COMPONENT() {}
, which must be included from the file include/cyd_ui/components/component_macros.h
. The syntax is as follows:
The name must be a valid class name. The properties and state declarations are both struct declarations, so fields must be separated by semicolons ;
.
Components may implement as many or as few event handlers as they need. User input events follow the pattern ON_<EVENT> {}
. For instance, if a component needs to react to a key press, it should implement ON_KEY_PRESS {}
:
If the event handler determines that the component has changed and that it should be updated and redrawn, it must call state.mark_dirty()
.
There are two special handlers, CHILDREN {}
and FRAGMENT {}
which do not handle specific events. These are called when it is determined that a component has changed in any way. The CHILDREN {}
handler specifies the children components, their attributes/props and their dimensions. The FRAGMENT {}
handler specifies graphic elements to be drawn inside the component. Fragment elements are inspired by SVG element tags and allow for rendering primitives (line, rectangle, circle, ...) as well as text and more.
In this way, the CHILDREN {}
handler defines the structure of the UI whereas the FRAGMENT {}
handler defines the actual visual elements that get drawn. Note that components may implement either or both depending on their role.
Each Cyd-UI window provides its own thread-safe asynchronous bus which is exposed to components as the window
variable. This includes an event queue and a coroutine runtime.
Events are identified by their type and can be emitted with the following syntax:
Where the event type is defined with the EVENT() {}
macro as follows:
Components can then implement handlers for any event. These handlers will only be active for as long as the component is mounted and showing on the UI.
The coroutine runtime is still a work in progress and so only implements minimal features. Nonetheless, the syntax for enqueuing an asynchronous operation is as follows:
The return type and the use of co_return
are needed to tell the compiler that this is a coroutine. This enables the use of async constructs such as co_await
and co_yield
. However, the runtime is still in its early stages of development and may not properly handle this yet.
This example declares two components. The first SomeComponent
just draws a blue circle. The second component ExampleComponent
draws an orange rectangle with black text inside as well as including the first component as a child.
In order to show the components on the screen, a window must be created. This is done as follows:
The moment the method show()
is called, the window will appear on the screen and events will start being processed. The main thread is then free to do anything. Events can be used for communication between the main thread and the window thread.
The window will self-terminate when the CWindow
object is destroyed. For easier management of ownership, the window is wrapped within a std::shared_ptr<>
on creation.
This software uses the following open source projects:
GPL 3.0 · LICENSE.MD
GitHub @castle055 ·