|
| 1 | +# SixtyFPS-cpp |
| 2 | + |
| 3 | +**A C++ UI toolkit** |
| 4 | + |
| 5 | +SixtyFPS is a GUI engine, with libraries for different languages. |
| 6 | +SixtyFPS.cpp the a C++ API to interact with a SictyFPS UI from your C++ program. |
| 7 | + |
| 8 | +## Tutorial |
| 9 | + |
| 10 | +Let's make a UI for a todo list application using the SixtyFPS UI description language. |
| 11 | +Hopefully this should be self explainatory. Check out the documentation of the language for help |
| 12 | + |
| 13 | +NOTE: this is not yet implemented as is. |
| 14 | + |
| 15 | +```sixtyfps |
| 16 | +// file: todoapp.60 |
| 17 | +TodoApp := MainWindow { |
| 18 | + signal todo_added(string); |
| 19 | + property<model> todo_model; |
| 20 | +
|
| 21 | + ColumnLayout { |
| 22 | + RowLayout { |
| 23 | + text_edit := LineEdit {} |
| 24 | + Button { |
| 25 | + text: "Add Todo"; |
| 26 | + clicked => { |
| 27 | + todo_added(text_edit.text); |
| 28 | + text_edit.text = ""; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + NativeListView { |
| 33 | + model: todo_model; |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Now, we can generate the C++ code using the following command |
| 40 | + |
| 41 | +``` |
| 42 | +sixtyfpscpp_compiler todoapp.sixtyfps -o todoapp.h |
| 43 | +``` |
| 44 | + |
| 45 | +Note: You would usually not type this command yourself, this is done automatically by the build system |
| 46 | +See the documentation for how to integrate with cmake |
| 47 | + |
| 48 | +This will generate a todoapp.h header file. It basically contains the following code |
| 49 | +(edited for briefty) |
| 50 | + |
| 51 | +```C++ |
| 52 | +#include <sixtyfps> |
| 53 | +struct TodoApp : sixtyfps::window { |
| 54 | + sixtyfps::signal<std::string_view> &todo_added(); |
| 55 | + sixtyfps::property<std::shared_ptr<sixtyfps::data_model< |
| 56 | + sixtyfps::native_list_view_item>>> &todo_model(); |
| 57 | + //... |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +We can then use this from out .cpp file |
| 62 | +
|
| 63 | +```C++ |
| 64 | +// include the generated file |
| 65 | +#include "todoapp.h" |
| 66 | +
|
| 67 | +int main() { |
| 68 | + // Let's instantiate our window: this return a handle to it |
| 69 | + auto todo_app = sixtyfps::create_window<TodoApp>(); |
| 70 | +
|
| 71 | + // let's create a model: `simple_data_model` is a data model which is simply backed by |
| 72 | + // a vector behind the scene. |
| 73 | + auto model = std::make_shared<sixtyfps::simple_data_model<sixtyfps::native_list_view_item>>(); |
| 74 | + model->push_back({"Write documentation", sixtyfps::native_list_view_item::checkable }); |
| 75 | + todo_app->data_model().set(model); |
| 76 | +
|
| 77 | + // let's connect our "add" button to add an item in the model |
| 78 | + todo_app->todo_added().connect([=](std::string_view data) { |
| 79 | + model->push_back({data, sixtyfps::native_list_view_item::checkable}) |
| 80 | + }); |
| 81 | +
|
| 82 | + // Show the window |
| 83 | + todo_app->show(); |
| 84 | +
|
| 85 | + // Run the sixtyfps envent loop on this thread. |
| 86 | + sixtyfps::run(); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +That's it. |
| 91 | + |
| 92 | +Check the rest of the documentation for the reference. |
0 commit comments