Crafter.CppDOM/README.md

219 lines
7.3 KiB
Markdown
Raw Normal View History

2025-01-02 02:48:44 +01:00
# About
![alt text](https://github.com/Catcrafts/Crafter.Web/blob/master/hello.png?raw=true)
2025-03-25 20:52:25 +01:00
Crafter.CppDOM is a C++ library that exposes the browser DOM api's to C++ WebAssembly.
2025-01-02 02:48:44 +01:00
# How to use
2025-11-09 20:11:22 +01:00
Please view the examples folder, this is a snippit from the HelloElement example:
2025-02-21 03:52:46 +01:00
```cpp
import Crafter.CppDOM;
using namespace Crafter::CppDOM;
int main(){
HtmlElement body("body");
body.SetInnerHTML("Hello World!");
}
```
You can also view the wiki for more detailed information.
2025-01-02 02:48:44 +01:00
2025-11-09 20:11:22 +01:00
It is highly recommended to use this with [Crafter.Build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build), but it is not strictly required if the same way of injecting the env is followed. The following instructions will be for Crafter.Build.
2025-01-02 02:48:44 +01:00
## Quickstart
create a ``project.json`` in an empty folder, open it in your preferred text editor.
Create a basic project file, that describes your web project.
```JSON
{
2025-11-09 20:11:22 +01:00
"name": "main",
2025-01-02 02:48:44 +01:00
"configurations": [
{
2025-11-09 20:11:22 +01:00
"name": "executable",
"implementations": ["main"],
"target": "wasm32-wasi",
"debug" : true,
2025-01-02 02:48:44 +01:00
"dependencies": [
{
2025-11-09 20:11:22 +01:00
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM.git",
"configuration":"lib-debug"
2025-01-02 02:48:44 +01:00
}
2025-11-09 20:11:22 +01:00
],
2025-01-02 02:48:44 +01:00
}
]
}
2025-02-21 03:53:03 +01:00
```
2025-02-21 03:52:46 +01:00
2025-01-02 02:48:44 +01:00
Save and close the file, create a ``main.cpp``
```cpp
2025-02-12 22:22:06 +01:00
import Crafter.CppDOM;
2025-02-12 23:06:56 +01:00
using namespace Crafter::CppDOM;
2025-01-02 02:48:44 +01:00
2025-02-12 22:22:06 +01:00
int main(){
2025-02-12 23:06:56 +01:00
HtmlElement body("body");
body.SetInnerHTML("Hello World!");
2025-01-02 02:48:44 +01:00
}
```
2025-02-21 03:52:46 +01:00
2025-11-09 20:11:22 +01:00
Save and close, then run ``crafter-build build executable && caddy file-server --listen :8080 --root bin/executable``. if you have caddy installed, if not use your favorite static file server instead. Now you can open the browser at ``http://localhost:8080`` and ``Hello World!`` will appear in the browser.
2025-02-12 22:22:06 +01:00
2025-11-09 22:56:29 +01:00
This sample can also be viewed in the [HelloElement example](https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM/src/branch/master/examples)
2025-11-10 20:02:11 +01:00
## Enhanced Event Handling Capabilities
2025-11-09 22:56:29 +01:00
2025-11-10 20:02:11 +01:00
The library now supports comprehensive event handling with rich event data for modern web applications:
2025-11-09 22:56:29 +01:00
### Mouse Events
- `AddClickListener()` - Handles click events
- `AddMouseOverListener()` - Handles mouse over events
- `AddMouseOutListener()` - Handles mouse out events
- `AddMouseMoveListener()` - Handles mouse move events
2025-11-10 20:02:11 +01:00
- `AddMouseDownListener()` - Handles mouse down events
- `AddMouseUpListener()` - Handles mouse up events
2025-11-09 22:56:29 +01:00
### Focus Events
- `AddFocusListener()` - Handles focus events
- `AddBlurListener()` - Handles blur events
### Keyboard Events
2025-11-10 20:02:11 +01:00
- `AddKeyDownListener()` - Handles key down events with event data
- `AddKeyUpListener()` - Handles key up events with event data
- `AddKeyPressListener()` - Handles key press events with event data
2025-11-09 22:56:29 +01:00
### Form Events
2025-11-10 20:02:11 +01:00
- `AddChangeListener()` - Handles change events with event data
2025-11-09 22:56:29 +01:00
- `AddSubmitListener()` - Handles form submit events
2025-11-10 20:02:11 +01:00
- `AddInputListener()` - Handles input events with event data
2025-11-09 22:56:29 +01:00
### Window Events
- `AddLoadListener()` - Handles page load events
2025-11-10 20:02:11 +01:00
- `AddErrorListener()` - Handles error events with event data
- `AddResizeListener()` - Handles window resize events with event data
- `AddScrollListener()` - Handles scroll events with event data
2025-11-09 22:56:29 +01:00
### Context Menu Events
2025-11-10 20:02:11 +01:00
- `AddContextMenuListener()` - Handles context menu events with event data
2025-11-09 22:56:29 +01:00
### Drag and Drop Events
2025-11-10 20:02:11 +01:00
- `AddDragStartListener()` - Handles drag start events with event data
- `AddDragEndListener()` - Handles drag end events with event data
- `AddDropListener()` - Handles drop events with event data
- `AddDragOverListener()` - Handles drag over events with event data
- `AddDragEnterListener()` - Handles drag enter events with event data
- `AddDragLeaveListener()` - Handles drag leave events with event data
### Wheel Events
- `AddWheelListener()` - Handles wheel events with event data
### Example Usage with Event Data
2025-11-09 22:56:29 +01:00
```cpp
import Crafter.CppDOM;
2025-11-10 20:02:11 +01:00
using namespace Crafter;
2025-11-09 22:56:29 +01:00
int main(){
HtmlElement button("myButton");
HtmlElement input("textInput");
2025-11-10 20:02:11 +01:00
// Handle click event with position data
2025-11-09 22:56:29 +01:00
button.AddClickListener([]() {
2025-11-10 20:02:11 +01:00
// Simple click handler without event data
});
// Handle keyboard events with rich event data
input.AddKeyDownListener([](Crafter::CppDOM::EventTypes::KeyboardEvent event) {
// Access key information
if (event.key == "Enter") {
// Handle Enter key specifically
}
// Check modifier keys
if (event.ctrlKey) {
// Handle Ctrl+key combinations
}
2025-11-09 22:56:29 +01:00
});
2025-11-10 20:02:11 +01:00
// Handle input changes with event data
input.AddInputListener([](Crafter::CppDOM::EventTypes::InputEvent event) {
// Access input data
std::string data = event.data;
bool isComposing = event.isComposing;
2025-11-09 22:56:29 +01:00
});
2025-11-10 20:02:11 +01:00
// Handle mouse movements with coordinates
button.AddMouseMoveListener([](Crafter::CppDOM::EventTypes::MouseEvent event) {
// Access mouse position
double x = event.clientX;
double y = event.clientY;
});
// Handle form changes with value
input.AddChangeListener([](Crafter::CppDOM::EventTypes::ChangeEvent event) {
// Access the new value
std::string value = event.value;
});
// Handle window resize with dimensions
button.AddResizeListener([](Crafter::CppDOM::EventTypes::ResizeEvent event) {
// Access window dimensions
unsigned int width = event.width;
unsigned int height = event.height;
2025-11-09 22:56:29 +01:00
});
}
2025-11-10 20:02:11 +01:00
```
### Event Data Structures
The library provides specific event data structures for different event types:
#### KeyboardEvent
- `std::string key` - The key value
- `std::string code` - The physical key code
- `int keyCode` - Legacy key code
- `bool altKey` - Whether Alt key was pressed
- `bool ctrlKey` - Whether Ctrl key was pressed
- `bool shiftKey` - Whether Shift key was pressed
- `bool metaKey` - Whether Meta key was pressed
#### MouseEvent
- `double clientX` - X coordinate relative to viewport
- `double clientY` - Y coordinate relative to viewport
- `double screenX` - X coordinate relative to screen
- `double screenY` - Y coordinate relative to screen
- `int button` - Which mouse button was pressed
- `int buttons` - Which mouse buttons were pressed
- `bool altKey` - Whether Alt key was pressed
- `bool ctrlKey` - Whether Ctrl key was pressed
- `bool shiftKey` - Whether Shift key was pressed
- `bool metaKey` - Whether Meta key was pressed
#### InputEvent
- `std::string data` - The input data
- `bool isComposing` - Whether input is in composition mode
#### ChangeEvent
- `std::string value` - The new value
#### ResizeEvent
- `unsigned int width` - Window width
- `unsigned int height` - Window height
#### ScrollEvent
- `double scrollX` - Horizontal scroll position
- `double scrollY` - Vertical scroll position
#### ErrorEvent
- `std::string message` - Error message
- `std::string filename` - File where error occurred
- `unsigned int lineno` - Line number
- `unsigned int colno` - Column number
#### DragEvent
- All MouseEvent properties plus:
- `double offsetX` - X offset from drag start
- `double offsetY` - Y offset from drag start
#### WheelEvent
- All MouseEvent properties plus:
- `double deltaX` - Horizontal scroll amount
- `double deltaY` - Vertical scroll amount
- `double deltaZ` - Depth scroll amount
- `int deltaMode` - Scroll mode