Compare commits

...

2 commits

Author SHA1 Message Date
2254ad53c5 fixes 2025-11-10 20:25:50 +01:00
6210e9c99b working keydown 2025-11-10 20:02:11 +01:00
15 changed files with 1182 additions and 774 deletions

144
README.md
View file

@ -58,66 +58,162 @@ Save and close, then run ``crafter-build build executable && caddy file-server -
This sample can also be viewed in the [HelloElement example](https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM/src/branch/master/examples) This sample can also be viewed in the [HelloElement example](https://forgejo.catcrafts.net/Catcrafts/Crafter.CppDOM/src/branch/master/examples)
## New Event Handling Capabilities ## Enhanced Event Handling Capabilities
The library now supports comprehensive event handling for modern web applications: The library now supports comprehensive event handling with rich event data for modern web applications:
### Mouse Events ### Mouse Events
- `AddClickListener()` - Handles click events - `AddClickListener()` - Handles click events
- `AddMouseOverListener()` - Handles mouse over events - `AddMouseOverListener()` - Handles mouse over events
- `AddMouseOutListener()` - Handles mouse out events - `AddMouseOutListener()` - Handles mouse out events
- `AddMouseMoveListener()` - Handles mouse move events - `AddMouseMoveListener()` - Handles mouse move events
- `AddMouseDownListener()` - Handles mouse down events
- `AddMouseUpListener()` - Handles mouse up events
### Focus Events ### Focus Events
- `AddFocusListener()` - Handles focus events - `AddFocusListener()` - Handles focus events
- `AddBlurListener()` - Handles blur events - `AddBlurListener()` - Handles blur events
### Keyboard Events ### Keyboard Events
- `AddKeyDownListener()` - Handles key down events - `AddKeyDownListener()` - Handles key down events with event data
- `AddKeyUpListener()` - Handles key up events - `AddKeyUpListener()` - Handles key up events with event data
- `AddKeyPressListener()` - Handles key press events - `AddKeyPressListener()` - Handles key press events with event data
### Form Events ### Form Events
- `AddChangeListener()` - Handles change events - `AddChangeListener()` - Handles change events with event data
- `AddSubmitListener()` - Handles form submit events - `AddSubmitListener()` - Handles form submit events
- `AddInputListener()` - Handles input events - `AddInputListener()` - Handles input events with event data
### Window Events ### Window Events
- `AddLoadListener()` - Handles page load events - `AddLoadListener()` - Handles page load events
- `AddErrorListener()` - Handles error events - `AddErrorListener()` - Handles error events with event data
- `AddResizeListener()` - Handles window resize events - `AddResizeListener()` - Handles window resize events with event data
- `AddScrollListener()` - Handles scroll events - `AddScrollListener()` - Handles scroll events with event data
### Context Menu Events ### Context Menu Events
- `AddContextMenuListener()` - Handles context menu events - `AddContextMenuListener()` - Handles context menu events with event data
### Drag and Drop Events ### Drag and Drop Events
- `AddDragStartListener()` - Handles drag start events - `AddDragStartListener()` - Handles drag start events with event data
- `AddDragEndListener()` - Handles drag end events - `AddDragEndListener()` - Handles drag end events with event data
- `AddDropListener()` - Handles drop events - `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
Example usage:
```cpp ```cpp
import Crafter.CppDOM; import Crafter.CppDOM;
using namespace Crafter::CppDOM; using namespace Crafter;
int main(){ int main(){
HtmlElement button("myButton"); HtmlElement button("myButton");
HtmlElement input("textInput"); HtmlElement input("textInput");
// Handle click event // Handle click event with position data
button.AddClickListener([]() { button.AddClickListener([]() {
// Handle button click // Simple click handler without event data
}); });
// Handle keyboard events // Handle keyboard events with rich event data
input.AddKeyDownListener([]() { input.AddKeyDownListener([](Crafter::CppDOM::EventTypes::KeyboardEvent event) {
// Handle key press // Access key information
if (event.key == "Enter") {
// Handle Enter key specifically
}
// Check modifier keys
if (event.ctrlKey) {
// Handle Ctrl+key combinations
}
}); });
// Handle input changes // Handle input changes with event data
input.AddInputListener([]() { input.AddInputListener([](Crafter::CppDOM::EventTypes::InputEvent event) {
// Handle input changes // Access input data
std::string data = event.data;
bool isComposing = event.isComposing;
});
// 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;
}); });
} }
``` ```
### 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

View file

@ -29,6 +29,19 @@ function freeJs(ptr) {
jsmemory.delete(ptr); jsmemory.delete(ptr);
} }
function writeStringToWasm(str) {
const encoder = new TextEncoder();
const encoded = encoder.encode(str + '\0');
const len = encoded.length;
const { WasmAlloc } = window.crafter_webbuild_wasi.instance.exports;
const ptr = WasmAlloc(len);
const memView = new Uint8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, ptr, len);
memView.set(encoded);
return ptr;
}
function getElementById(id, idLenght) { function getElementById(id, idLenght) {
const obj = document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght))); const obj = document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght)));
jsmemory.set(++memorycounter, obj); jsmemory.set(++memorycounter, obj);
@ -44,7 +57,7 @@ function addClickListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
// Create a handler that will trigger a notification to C++ // Create a handler that will trigger a notification to C++
const handler = function(event) { const handler = function (event) {
const { ExecuteClickHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteClickHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteClickHandler(handlerID); ExecuteClickHandler(handlerID);
}; };
@ -63,11 +76,11 @@ function removeClickListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}`); eventHandlers.delete(`${ptr}-${handlerID}`);
} }
// Add more event handlers for modern websites // Mouse Events
function addMouseOverListener(ptr, handlerID) { function addMouseOverListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteMouseOverHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteMouseOverHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteMouseOverHandler(handlerID); ExecuteMouseOverHandler(handlerID);
}; };
@ -88,7 +101,7 @@ function removeMouseOverListener(ptr, handlerID) {
function addMouseOutListener(ptr, handlerID) { function addMouseOutListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteMouseOutHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteMouseOutHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteMouseOutHandler(handlerID); ExecuteMouseOutHandler(handlerID);
}; };
@ -109,7 +122,7 @@ function removeMouseOutListener(ptr, handlerID) {
function addMouseMoveListener(ptr, handlerID) { function addMouseMoveListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteMouseMoveHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteMouseMoveHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteMouseMoveHandler(handlerID); ExecuteMouseMoveHandler(handlerID);
}; };
@ -127,10 +140,53 @@ function removeMouseMoveListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-mousemove`); eventHandlers.delete(`${ptr}-${handlerID}-mousemove`);
} }
function addMouseDownListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteMouseDownHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteMouseDownHandler(handlerID);
};
eventHandlers.set(`${ptr}-${handlerID}-mousedown`, handler);
element.addEventListener("mousedown", handler);
}
function removeMouseDownListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-mousedown`);
element.removeEventListener("mousedown", handler);
eventHandlers.delete(`${ptr}-${handlerID}-mousedown`);
}
function addMouseUpListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteMouseUpHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteMouseUpHandler(handlerID);
};
eventHandlers.set(`${ptr}-${handlerID}-mouseup`, handler);
element.addEventListener("mouseup", handler);
}
function removeMouseUpListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-mouseup`);
element.removeEventListener("mouseup", handler);
eventHandlers.delete(`${ptr}-${handlerID}-mouseup`);
}
// Focus Events
function addFocusListener(ptr, handlerID) { function addFocusListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteFocusHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteFocusHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteFocusHandler(handlerID); ExecuteFocusHandler(handlerID);
}; };
@ -151,7 +207,7 @@ function removeFocusListener(ptr, handlerID) {
function addBlurListener(ptr, handlerID) { function addBlurListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteBlurHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteBlurHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteBlurHandler(handlerID); ExecuteBlurHandler(handlerID);
}; };
@ -169,12 +225,15 @@ function removeBlurListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-blur`); eventHandlers.delete(`${ptr}-${handlerID}-blur`);
} }
// Keyboard Events
function addKeyDownListener(ptr, handlerID) { function addKeyDownListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteKeyDownHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteKeyDownHandler, WasmFree } = window.crafter_webbuild_wasi.instance.exports;
ExecuteKeyDownHandler(handlerID, event.key, event.code, event.keyCode); const ptr = writeStringToWasm(event.key);
ExecuteKeyDownHandler(handlerID, ptr, event.keyCode, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
WasmFree(ptr);
}; };
eventHandlers.set(`${ptr}-${handlerID}-keydown`, handler); eventHandlers.set(`${ptr}-${handlerID}-keydown`, handler);
@ -193,9 +252,11 @@ function removeKeyDownListener(ptr, handlerID) {
function addKeyUpListener(ptr, handlerID) { function addKeyUpListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteKeyUpHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteKeyUpHandler, WasmFree } = window.crafter_webbuild_wasi.instance.exports;
ExecuteKeyUpHandler(handlerID, event.key, event.code, event.keyCode); const ptr = writeStringToWasm(event.key);
ExecuteKeyUpHandler(handlerID, ptr, event.keyCode, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
WasmFree(ptr);
}; };
eventHandlers.set(`${ptr}-${handlerID}-keyup`, handler); eventHandlers.set(`${ptr}-${handlerID}-keyup`, handler);
@ -214,9 +275,11 @@ function removeKeyUpListener(ptr, handlerID) {
function addKeyPressListener(ptr, handlerID) { function addKeyPressListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteKeyPressHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteKeyPressHandler, WasmFree } = window.crafter_webbuild_wasi.instance.exports;
ExecuteKeyPressHandler(handlerID, event.key, event.code, event.keyCode); const ptr = writeStringToWasm(event.key);
ExecuteKeyPressHandler(handlerID, ptr, event.keyCode, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
WasmFree(ptr);
}; };
eventHandlers.set(`${ptr}-${handlerID}-keypress`, handler); eventHandlers.set(`${ptr}-${handlerID}-keypress`, handler);
@ -235,9 +298,9 @@ function removeKeyPressListener(ptr, handlerID) {
function addChangeListener(ptr, handlerID) { function addChangeListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteChangeHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteChangeHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteChangeHandler(handlerID); ExecuteChangeHandler(handlerID, event.target.value);
}; };
eventHandlers.set(`${ptr}-${handlerID}-change`, handler); eventHandlers.set(`${ptr}-${handlerID}-change`, handler);
@ -256,7 +319,7 @@ function removeChangeListener(ptr, handlerID) {
function addSubmitListener(ptr, handlerID) { function addSubmitListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteSubmitHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteSubmitHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteSubmitHandler(handlerID); ExecuteSubmitHandler(handlerID);
}; };
@ -277,9 +340,9 @@ function removeSubmitListener(ptr, handlerID) {
function addInputListener(ptr, handlerID) { function addInputListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteInputHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteInputHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteInputHandler(handlerID); ExecuteInputHandler(handlerID, event.data || event.target.value, event.inputType === 'insertCompositionText');
}; };
eventHandlers.set(`${ptr}-${handlerID}-input`, handler); eventHandlers.set(`${ptr}-${handlerID}-input`, handler);
@ -295,54 +358,12 @@ function removeInputListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-input`); eventHandlers.delete(`${ptr}-${handlerID}-input`);
} }
function addLoadListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function(event) {
const { ExecuteLoadHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteLoadHandler(handlerID);
};
eventHandlers.set(`${ptr}-${handlerID}-load`, handler);
element.addEventListener("load", handler);
}
function removeLoadListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-load`);
element.removeEventListener("load", handler);
eventHandlers.delete(`${ptr}-${handlerID}-load`);
}
function addErrorListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function(event) {
const { ExecuteErrorHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteErrorHandler(handlerID);
};
eventHandlers.set(`${ptr}-${handlerID}-error`, handler);
element.addEventListener("error", handler);
}
function removeErrorListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-error`);
element.removeEventListener("error", handler);
eventHandlers.delete(`${ptr}-${handlerID}-error`);
}
function addResizeListener(ptr, handlerID) { function addResizeListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteResizeHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteResizeHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteResizeHandler(handlerID); ExecuteResizeHandler(handlerID, window.innerWidth, window.innerHeight);
}; };
eventHandlers.set(`${ptr}-${handlerID}-resize`, handler); eventHandlers.set(`${ptr}-${handlerID}-resize`, handler);
@ -361,9 +382,9 @@ function removeResizeListener(ptr, handlerID) {
function addScrollListener(ptr, handlerID) { function addScrollListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteScrollHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteScrollHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteScrollHandler(handlerID); ExecuteScrollHandler(handlerID, window.scrollX, window.scrollY);
}; };
eventHandlers.set(`${ptr}-${handlerID}-scroll`, handler); eventHandlers.set(`${ptr}-${handlerID}-scroll`, handler);
@ -379,12 +400,13 @@ function removeScrollListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-scroll`); eventHandlers.delete(`${ptr}-${handlerID}-scroll`);
} }
// Context Menu Events
function addContextMenuListener(ptr, handlerID) { function addContextMenuListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteContextMenuHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteContextMenuHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteContextMenuHandler(handlerID); ExecuteContextMenuHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
}; };
eventHandlers.set(`${ptr}-${handlerID}-contextmenu`, handler); eventHandlers.set(`${ptr}-${handlerID}-contextmenu`, handler);
@ -400,12 +422,13 @@ function removeContextMenuListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-contextmenu`); eventHandlers.delete(`${ptr}-${handlerID}-contextmenu`);
} }
// Drag and Drop Events
function addDragStartListener(ptr, handlerID) { function addDragStartListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteDragStartHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteDragStartHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDragStartHandler(handlerID); ExecuteDragStartHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
}; };
eventHandlers.set(`${ptr}-${handlerID}-dragstart`, handler); eventHandlers.set(`${ptr}-${handlerID}-dragstart`, handler);
@ -424,9 +447,9 @@ function removeDragStartListener(ptr, handlerID) {
function addDragEndListener(ptr, handlerID) { function addDragEndListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteDragEndHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteDragEndHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDragEndHandler(handlerID); ExecuteDragEndHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
}; };
eventHandlers.set(`${ptr}-${handlerID}-dragend`, handler); eventHandlers.set(`${ptr}-${handlerID}-dragend`, handler);
@ -445,9 +468,9 @@ function removeDragEndListener(ptr, handlerID) {
function addDropListener(ptr, handlerID) { function addDropListener(ptr, handlerID) {
const element = jsmemory.get(ptr); const element = jsmemory.get(ptr);
const handler = function(event) { const handler = function (event) {
const { ExecuteDropHandler } = window.crafter_webbuild_wasi.instance.exports; const { ExecuteDropHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDropHandler(handlerID); ExecuteDropHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
}; };
eventHandlers.set(`${ptr}-${handlerID}-drop`, handler); eventHandlers.set(`${ptr}-${handlerID}-drop`, handler);
@ -463,6 +486,92 @@ function removeDropListener(ptr, handlerID) {
eventHandlers.delete(`${ptr}-${handlerID}-drop`); eventHandlers.delete(`${ptr}-${handlerID}-drop`);
} }
// Additional Drag Events
function addDragOverListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteDragOverHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDragOverHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
};
eventHandlers.set(`${ptr}-${handlerID}-dragover`, handler);
element.addEventListener("dragover", handler);
}
function removeDragOverListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-dragover`);
element.removeEventListener("dragover", handler);
eventHandlers.delete(`${ptr}-${handlerID}-dragover`);
}
function addDragEnterListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteDragEnterHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDragEnterHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
};
eventHandlers.set(`${ptr}-${handlerID}-dragenter`, handler);
element.addEventListener("dragenter", handler);
}
function removeDragEnterListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-dragenter`);
element.removeEventListener("dragenter", handler);
eventHandlers.delete(`${ptr}-${handlerID}-dragenter`);
}
function addDragLeaveListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteDragLeaveHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteDragLeaveHandler(handlerID, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
};
eventHandlers.set(`${ptr}-${handlerID}-dragleave`, handler);
element.addEventListener("dragleave", handler);
}
function removeDragLeaveListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-dragleave`);
element.removeEventListener("dragleave", handler);
eventHandlers.delete(`${ptr}-${handlerID}-dragleave`);
}
// Wheel Event
function addWheelListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = function (event) {
const { ExecuteWheelHandler } = window.crafter_webbuild_wasi.instance.exports;
ExecuteWheelHandler(handlerID, event.deltaX, event.deltaY, event.deltaZ, event.deltaMode, event.clientX, event.clientY, event.screenX, event.screenY, event.button, event.altKey, event.ctrlKey, event.shiftKey, event.metaKey);
};
eventHandlers.set(`${ptr}-${handlerID}-wheel`, handler);
element.addEventListener("wheel", handler);
}
function removeWheelListener(ptr, handlerID) {
const element = jsmemory.get(ptr);
const handler = eventHandlers.get(`${ptr}-${handlerID}-wheel`);
element.removeEventListener("wheel", handler);
eventHandlers.delete(`${ptr}-${handlerID}-wheel`);
}
let env = { let env = {
freeJs: freeJs, freeJs: freeJs,
getElementById: getElementById, getElementById: getElementById,
@ -475,6 +584,10 @@ let env = {
removeMouseOutListener: removeMouseOutListener, removeMouseOutListener: removeMouseOutListener,
addMouseMoveListener: addMouseMoveListener, addMouseMoveListener: addMouseMoveListener,
removeMouseMoveListener: removeMouseMoveListener, removeMouseMoveListener: removeMouseMoveListener,
addMouseDownListener: addMouseDownListener,
removeMouseDownListener: removeMouseDownListener,
addMouseUpListener: addMouseUpListener,
removeMouseUpListener: removeMouseUpListener,
addFocusListener: addFocusListener, addFocusListener: addFocusListener,
removeFocusListener: removeFocusListener, removeFocusListener: removeFocusListener,
addBlurListener: addBlurListener, addBlurListener: addBlurListener,
@ -491,10 +604,6 @@ let env = {
removeSubmitListener: removeSubmitListener, removeSubmitListener: removeSubmitListener,
addInputListener: addInputListener, addInputListener: addInputListener,
removeInputListener: removeInputListener, removeInputListener: removeInputListener,
addLoadListener: addLoadListener,
removeLoadListener: removeLoadListener,
addErrorListener: addErrorListener,
removeErrorListener: removeErrorListener,
addResizeListener: addResizeListener, addResizeListener: addResizeListener,
removeResizeListener: removeResizeListener, removeResizeListener: removeResizeListener,
addScrollListener: addScrollListener, addScrollListener: addScrollListener,
@ -507,6 +616,14 @@ let env = {
removeDragEndListener: removeDragEndListener, removeDragEndListener: removeDragEndListener,
addDropListener: addDropListener, addDropListener: addDropListener,
removeDropListener: removeDropListener, removeDropListener: removeDropListener,
addWheelListener: addWheelListener,
removeWheelListener: removeWheelListener,
addDragOverListener: addDragOverListener,
removeDragOverListener: removeDragOverListener,
addDragEnterListener: addDragEnterListener,
removeDragEnterListener: removeDragEnterListener,
addDragLeaveListener: addDragLeaveListener,
removeDragLeaveListener: removeDragLeaveListener,
} }
window.crafter_webbuild_env = env; window.crafter_webbuild_env = env;

View file

@ -1,84 +0,0 @@
# Complete Event Handling Example
This example demonstrates comprehensive event handling capabilities in Crafter.CppDOM.
## Features
- Shows how to handle various types of DOM events using C++
- Demonstrates mouse, keyboard, form, and window events
- Illustrates proper event listener attachment and callback handling
- Shows real-time interaction with UI elements
## Event Types Demonstrated
### Mouse Events
- Click events
- Mouse over/out events
- Mouse move events
### Focus Events
- Focus and blur events
### Keyboard Events
- Key down, up, and press events
### Form Events
- Change, submit, and input events
### Window Events
- Load, error, resize, and scroll events
### Context Menu Events
- Context menu events
### Drag and Drop Events
- Drag start, end, and drop events
## Usage
```cpp
import Crafter.CppDOM;
using namespace Crafter::CppDOM;
int main(){
// Create UI elements
HtmlElement body("body");
body.SetInnerHTML("<h1>Complete Event Handling Demo</h1>"
"<button id='myButton'>Click Me!</button>"
"<input type='text' id='textInput' placeholder='Type something...'>"
"<p id='output'>Events will appear here</p>");
// Handle click event
HtmlElement button("myButton");
button.AddClickListener([]() {
HtmlElement output("output");
output.SetInnerHTML("Button was clicked!");
});
// Handle keyboard events
HtmlElement input("textInput");
input.AddInputListener([]() {
HtmlElement output("output");
output.SetInnerHTML("Input changed!");
});
// Handle form events
input.AddChangeListener([]() {
HtmlElement output("output");
output.SetInnerHTML("Form value changed!");
});
return 0;
}
```
## Building and Running
```bash
crafter-build build executable
run.sh
```
Then navigate to `http://localhost:8080/` in your browser.
If caddy is not installed, you can use your favorite static file server instead.

View file

@ -1,77 +0,0 @@
import Crafter.CppDOM;
using namespace Crafter;
HtmlElement* body = new HtmlElement("body", "<div id='container'><button id='myButton'>Click Me!</button><input type='text' id='textInput' placeholder='Type something'><div id='output'></div></div>");
HtmlElement* button = new HtmlElement("myButton");
HtmlElement* input = new HtmlElement("textInput");
HtmlElement* output = new HtmlElement("output");
int main(){
// Click event
button->AddClickListener([&]() {
output->SetInnerHTML("Button was clicked!");
});
// Mouse events
button->AddMouseOverListener([&]() {
output->SetInnerHTML("Mouse over button!");
});
button->AddMouseOutListener([&]() {
output->SetInnerHTML("Mouse left button!");
});
// Focus/Blur events
input->AddFocusListener([&]() {
output->SetInnerHTML("Input focused!");
});
input->AddBlurListener([&]() {
output->SetInnerHTML("Input blurred!");
});
// Keyboard events
input->AddKeyDownListener([&]() {
output->SetInnerHTML("Key pressed in input!");
});
input->AddKeyUpListener([&]() {
output->SetInnerHTML("Key released in input!");
});
// Input/change events
input->AddInputListener([&]() {
output->SetInnerHTML("Input value changed!");
});
input->AddChangeListener([&]() {
output->SetInnerHTML("Input value changed and lost focus!");
});
// Context menu
button->AddContextMenuListener([&]() {
output->SetInnerHTML("Context menu opened!");
});
// Scroll event on body
body->AddScrollListener([&]() {
output->SetInnerHTML("Page scrolled!");
});
// Resize event
body->AddResizeListener([&]() {
output->SetInnerHTML("Window resized!");
});
// Load event
body->AddLoadListener([&]() {
output->SetInnerHTML("Page loaded!");
});
// Error event
body->AddErrorListener([&]() {
output->SetInnerHTML("An error occurred!");
});
}

View file

@ -0,0 +1,21 @@
import Crafter.CppDOM;
using namespace Crafter;
import std;
HtmlElement* body = new HtmlElement("body", "<div id='container'>"
"<h1>Enhanced Event Handling Demo</h1>"
"<input type='text' id='keyInput' placeholder='Press keys here'>"
"<div id='output'></div>"
"</div>");
HtmlElement* keyInput = new HtmlElement("keyInput");
HtmlElement* output = new HtmlElement("output");
int main(){
keyInput->AddKeyDownListener([&](KeyboardEvent event) {
std::string keyInfo = std::format("<p>Key pressed: {}</p>", event.key);
if (event.ctrlKey) keyInfo += "<p>Ctrl key pressed</p>";
if (event.shiftKey) keyInfo += "<p>Shift key pressed</p>";
if (event.altKey) keyInfo += "<p>Alt key pressed</p>";
output->SetInnerHTML(keyInfo);
});
}

View file

@ -1,5 +1,5 @@
import Crafter.CppDOM; import Crafter.CppDOM;
using namespace Crafter::CppDOM::Bindings; using namespace Crafter::CppDOMBindings;
int main(){ int main(){
void* body = GetElementById("body"); void* body = GetElementById("body");

View file

@ -1,237 +0,0 @@
/*
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.CppDOM:Bindings;
import std;
export namespace Crafter::CppDOM::Bindings {
int clickHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* clickHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOverHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOverHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOutHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOutHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseMoveHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseMoveHandlers = new std::unordered_map<int, std::function<void(void)>>();
int focusHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* focusHandlers = new std::unordered_map<int, std::function<void(void)>>();
int blurHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* blurHandlers = new std::unordered_map<int, std::function<void(void)>>();
int keyDownHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* keyDownHandlers = new std::unordered_map<int, std::function<void(void)>>();
int keyUpHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* keyUpHandlers = new std::unordered_map<int, std::function<void(void)>>();
int keyPressHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* keyPressHandlers = new std::unordered_map<int, std::function<void(void)>>();
int changeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* changeHandlers = new std::unordered_map<int, std::function<void(void)>>();
int submitHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* submitHandlers = new std::unordered_map<int, std::function<void(void)>>();
int inputHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* inputHandlers = new std::unordered_map<int, std::function<void(void)>>();
int loadHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* loadHandlers = new std::unordered_map<int, std::function<void(void)>>();
int errorHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* errorHandlers = new std::unordered_map<int, std::function<void(void)>>();
int resizeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* resizeHandlers = new std::unordered_map<int, std::function<void(void)>>();
int scrollHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* scrollHandlers = new std::unordered_map<int, std::function<void(void)>>();
int contextMenuHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* contextMenuHandlers = new std::unordered_map<int, std::function<void(void)>>();
int dragStartHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* dragStartHandlers = new std::unordered_map<int, std::function<void(void)>>();
int dragEndHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* dragEndHandlers = new std::unordered_map<int, std::function<void(void)>>();
int dropHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* dropHandlers = new std::unordered_map<int, std::function<void(void)>>();
__attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr);
__attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght);
void* GetElementById(const std::string_view id) {
return GetElementById(id.data(), id.size());
}
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght);
void SetInnerHTML(void* ptr, const std::string_view html) {
SetInnerHTML(ptr, html.data(), html.size());
}
// Event handling functions
__attribute__((import_module("env"), import_name("addClickListener"))) void AddClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeClickListener"))) void RemoveClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOverListener"))) void AddMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOverListener"))) void RemoveMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOutListener"))) void AddMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOutListener"))) void RemoveMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseMoveListener"))) void AddMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseMoveListener"))) void RemoveMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addFocusListener"))) void AddFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeFocusListener"))) void RemoveFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addBlurListener"))) void AddBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeBlurListener"))) void RemoveBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyDownListener"))) void AddKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyDownListener"))) void RemoveKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyUpListener"))) void AddKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyUpListener"))) void RemoveKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyPressListener"))) void AddKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyPressListener"))) void RemoveKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addChangeListener"))) void AddChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeChangeListener"))) void RemoveChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addSubmitListener"))) void AddSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeSubmitListener"))) void RemoveSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addInputListener"))) void AddInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeInputListener"))) void RemoveInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addLoadListener"))) void AddLoadListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeLoadListener"))) void RemoveLoadListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addErrorListener"))) void AddErrorListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeErrorListener"))) void RemoveErrorListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addResizeListener"))) void AddResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeResizeListener"))) void RemoveResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addScrollListener"))) void AddScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeScrollListener"))) void RemoveScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addContextMenuListener"))) void AddContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeContextMenuListener"))) void RemoveContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragStartListener"))) void AddDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragStartListener"))) void RemoveDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragEndListener"))) void AddDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragEndListener"))) void RemoveDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDropListener"))) void AddDropListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDropListener"))) void RemoveDropListener(void* ptr, int id);
}
extern "C" {
__attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(int a) {
Crafter::CppDOM::Bindings::clickHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteMouseOverHandler"))) void ExecuteMouseOverHandler(int a) {
Crafter::CppDOM::Bindings::mouseOverHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteMouseOutHandler"))) void ExecuteMouseOutHandler(int a) {
Crafter::CppDOM::Bindings::mouseOutHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteMouseMoveHandler"))) void ExecuteMouseMoveHandler(int a) {
Crafter::CppDOM::Bindings::mouseMoveHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteFocusHandler"))) void ExecuteFocusHandler(int a) {
Crafter::CppDOM::Bindings::focusHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteBlurHandler"))) void ExecuteBlurHandler(int a) {
Crafter::CppDOM::Bindings::blurHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteKeyDownHandler"))) void ExecuteKeyDownHandler(int a, const char* key, const char* code, int keyCode) {
Crafter::CppDOM::Bindings::keyDownHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteKeyUpHandler"))) void ExecuteKeyUpHandler(int a, const char* key, const char* code, int keyCode) {
Crafter::CppDOM::Bindings::keyUpHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteKeyPressHandler"))) void ExecuteKeyPressHandler(int a, const char* key, const char* code, int keyCode) {
Crafter::CppDOM::Bindings::keyPressHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteChangeHandler"))) void ExecuteChangeHandler(int a) {
Crafter::CppDOM::Bindings::changeHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteSubmitHandler"))) void ExecuteSubmitHandler(int a) {
Crafter::CppDOM::Bindings::submitHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteInputHandler"))) void ExecuteInputHandler(int a) {
Crafter::CppDOM::Bindings::inputHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteLoadHandler"))) void ExecuteLoadHandler(int a) {
Crafter::CppDOM::Bindings::loadHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteErrorHandler"))) void ExecuteErrorHandler(int a) {
Crafter::CppDOM::Bindings::errorHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteResizeHandler"))) void ExecuteResizeHandler(int a) {
Crafter::CppDOM::Bindings::resizeHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteScrollHandler"))) void ExecuteScrollHandler(int a) {
Crafter::CppDOM::Bindings::scrollHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteContextMenuHandler"))) void ExecuteContextMenuHandler(int a) {
Crafter::CppDOM::Bindings::contextMenuHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteDragStartHandler"))) void ExecuteDragStartHandler(int a) {
Crafter::CppDOM::Bindings::dragStartHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteDragEndHandler"))) void ExecuteDragEndHandler(int a) {
Crafter::CppDOM::Bindings::dragEndHandlers->find(static_cast<int>(a))->second();
}
__attribute__((export_name("ExecuteDropHandler"))) void ExecuteDropHandler(int a) {
Crafter::CppDOM::Bindings::dropHandlers->find(static_cast<int>(a))->second();
}
}

View file

@ -0,0 +1,204 @@
/*
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.CppDOM:BindingsExport;
import std;
import :EventTypes;
export namespace Crafter::CppDOMBindings {
int clickHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* clickHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOverHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOverHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOutHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOutHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseMoveHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseMoveHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseDownHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseDownHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseUpHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseUpHandlers = new std::unordered_map<int, std::function<void(void)>>();
int focusHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* focusHandlers = new std::unordered_map<int, std::function<void(void)>>();
int blurHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* blurHandlers = new std::unordered_map<int, std::function<void(void)>>();
int keyDownHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyDownHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int keyUpHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyUpHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int keyPressHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyPressHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int changeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ChangeEvent)>>* changeHandlers = new std::unordered_map<int, std::function<void(Crafter::ChangeEvent)>>();
int submitHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* submitHandlers = new std::unordered_map<int, std::function<void(void)>>();
int inputHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::InputEvent)>>* inputHandlers = new std::unordered_map<int, std::function<void(Crafter::InputEvent)>>();
int resizeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ResizeEvent)>>* resizeHandlers = new std::unordered_map<int, std::function<void(Crafter::ResizeEvent)>>();
int scrollHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ScrollEvent)>>* scrollHandlers = new std::unordered_map<int, std::function<void(Crafter::ScrollEvent)>>();
int contextMenuHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* contextMenuHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragStartHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragStartHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragEndHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragEndHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dropHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dropHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int wheelHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::WheelEvent)>>* wheelHandlers = new std::unordered_map<int, std::function<void(Crafter::WheelEvent)>>();
int dragOverHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragOverHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragEnterHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragEnterHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragLeaveHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragLeaveHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
}
extern "C" {
__attribute__((export_name("WasmAlloc"))) void* WasmAlloc(std::int32_t size) {
std::cout << std::format("Alloc: {}", size) << std::endl;
return std::malloc(size);
}
__attribute__((export_name("WasmFree"))) void WasmFree(void* ptr) {
std::free(ptr);
}
__attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::clickHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseOverHandler"))) void ExecuteMouseOverHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseOverHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseOutHandler"))) void ExecuteMouseOutHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseOutHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseMoveHandler"))) void ExecuteMouseMoveHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseMoveHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseDownHandler"))) void ExecuteMouseDownHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseDownHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseUpHandler"))) void ExecuteMouseUpHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseUpHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteFocusHandler"))) void ExecuteFocusHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::focusHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteBlurHandler"))) void ExecuteBlurHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::blurHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteKeyDownHandler"))) void ExecuteKeyDownHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyDownHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteKeyUpHandler"))) void ExecuteKeyUpHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyUpHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteKeyPressHandler"))) void ExecuteKeyPressHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyPressHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteChangeHandler"))) void ExecuteChangeHandler(std::int32_t handlerID, const char* value) {
Crafter::CppDOMBindings::changeHandlers->find(handlerID)->second(Crafter::ChangeEvent(value));
}
__attribute__((export_name("ExecuteSubmitHandler"))) void ExecuteSubmitHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::submitHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteInputHandler"))) void ExecuteInputHandler(std::int32_t handlerID, const char* data, bool isComposing) {
Crafter::CppDOMBindings::inputHandlers->find(handlerID)->second(Crafter::InputEvent(data, isComposing));
}
__attribute__((export_name("ExecuteResizeHandler"))) void ExecuteResizeHandler(std::int32_t handlerID, unsigned int width, unsigned int height) {
Crafter::CppDOMBindings::resizeHandlers->find(handlerID)->second(Crafter::ResizeEvent(width, height));
}
__attribute__((export_name("ExecuteScrollHandler"))) void ExecuteScrollHandler(std::int32_t handlerID, double scrollX, double scrollY) {
Crafter::CppDOMBindings::scrollHandlers->find(handlerID)->second(Crafter::ScrollEvent(scrollX, scrollY));
}
__attribute__((export_name("ExecuteContextMenuHandler"))) void ExecuteContextMenuHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::contextMenuHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteDragStartHandler"))) void ExecuteDragStartHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dragStartHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteDragEndHandler"))) void ExecuteDragEndHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dragEndHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteDropHandler"))) void ExecuteDropHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dropHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteWheelHandler"))) void ExecuteWheelHandler(std::int32_t handlerID, double deltaX, double deltaY, double deltaZ, int deltaMode, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::wheelHandlers->find(handlerID)->second(Crafter::WheelEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey, deltaX, deltaY, deltaZ));
}
__attribute__((export_name("ExecuteDragOverHandler"))) void ExecuteDragOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dragOverHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteDragEnterHandler"))) void ExecuteDragEnterHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dragEnterHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteDragLeaveHandler"))) void ExecuteDragLeaveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::dragLeaveHandlers->find(handlerID)->second(Crafter::MouseEvent(clientX, clientY, screenX, screenY, button, 0, altKey, ctrlKey, shiftKey, metaKey));
}
}

View file

@ -0,0 +1,108 @@
/*
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.CppDOM:BindingsImport;
import std;
import :EventTypes;
export namespace Crafter::CppDOMBindings {
__attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr);
__attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght);
void* GetElementById(const std::string_view id) {
return GetElementById(id.data(), id.size());
}
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght);
void SetInnerHTML(void* ptr, const std::string_view html) {
SetInnerHTML(ptr, html.data(), html.size());
}
// Event handling functions
__attribute__((import_module("env"), import_name("addClickListener"))) void AddClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeClickListener"))) void RemoveClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOverListener"))) void AddMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOverListener"))) void RemoveMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOutListener"))) void AddMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOutListener"))) void RemoveMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseMoveListener"))) void AddMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseMoveListener"))) void RemoveMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseDownListener"))) void AddMouseDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseDownListener"))) void RemoveMouseDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseUpListener"))) void AddMouseUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseUpListener"))) void RemoveMouseUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addFocusListener"))) void AddFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeFocusListener"))) void RemoveFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addBlurListener"))) void AddBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeBlurListener"))) void RemoveBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyDownListener"))) void AddKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyDownListener"))) void RemoveKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyUpListener"))) void AddKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyUpListener"))) void RemoveKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyPressListener"))) void AddKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyPressListener"))) void RemoveKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addChangeListener"))) void AddChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeChangeListener"))) void RemoveChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addSubmitListener"))) void AddSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeSubmitListener"))) void RemoveSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addInputListener"))) void AddInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeInputListener"))) void RemoveInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addResizeListener"))) void AddResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeResizeListener"))) void RemoveResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addScrollListener"))) void AddScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeScrollListener"))) void RemoveScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addContextMenuListener"))) void AddContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeContextMenuListener"))) void RemoveContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragStartListener"))) void AddDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragStartListener"))) void RemoveDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragEndListener"))) void AddDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragEndListener"))) void RemoveDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDropListener"))) void AddDropListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDropListener"))) void RemoveDropListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addWheelListener"))) void AddWheelListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeWheelListener"))) void RemoveWheelListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragOverListener"))) void AddDragOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragOverListener"))) void RemoveDragOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragEnterListener"))) void AddDragEnterListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragEnterListener"))) void RemoveDragEnterListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragLeaveListener"))) void AddDragLeaveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragLeaveListener"))) void RemoveDragLeaveListener(void* ptr, int id);
}

View file

@ -0,0 +1,79 @@
/*
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.CppDOM:EventTypes;
import std;
export namespace Crafter {
struct KeyboardEvent {
std::string key;
std::uint_fast8_t keyCode;
bool altKey;
bool ctrlKey;
bool shiftKey;
bool metaKey;
KeyboardEvent(const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) : key(key), keyCode(keyCode), altKey(altKey), ctrlKey(ctrlKey), shiftKey(shiftKey), metaKey(metaKey) {}
};
// Mouse event structure
struct MouseEvent {
double clientX;
double clientY;
double screenX;
double screenY;
std::uint_fast32_t button;
std::uint_fast32_t buttons;
bool altKey;
bool ctrlKey;
bool shiftKey;
bool metaKey;
MouseEvent(double clientX, double clientY, double screenX, double screenY, std::uint_fast32_t button, std::uint_fast32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) : clientX(clientX), clientY(clientY), screenX(screenX), screenY(screenY), button(button), buttons(buttons), altKey(altKey), ctrlKey(ctrlKey), shiftKey(shiftKey), metaKey(metaKey) {}
};
struct InputEvent {
std::string data;
bool isComposing;
InputEvent(const char* data, bool isComposing) : data(data), isComposing(isComposing) {}
};
struct WheelEvent : public MouseEvent {
double deltaX;
double deltaY;
double deltaZ;
WheelEvent(double clientX, double clientY, double screenX, double screenY, std::uint_fast32_t button, std::uint_fast32_t buttons, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey, double deltaX, double deltaY, double deltaZ): MouseEvent(clientX, clientY, screenX, screenY, button, buttons, altKey, ctrlKey, shiftKey, metaKey), deltaX(deltaX), deltaY(deltaY), deltaZ(deltaZ) {}
};
struct ResizeEvent {
std::uint_fast32_t width;
std::uint_fast32_t height;
ResizeEvent(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) {}
};
struct ScrollEvent {
double scrollX;
double scrollY;
ScrollEvent(double scrollX, double scrollY) : scrollX(scrollX), scrollY(scrollY) {}
};
struct ChangeEvent {
std::string value;
ChangeEvent(const char* value) : value(value) {}
};
}

View file

@ -20,312 +20,491 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
export module Crafter.CppDOM:HtmlElement; export module Crafter.CppDOM:HtmlElement;
import std; import std;
import :Bindings; import :BindingsExport;
import :BindingsImport;
import :EventTypes;
namespace Crafter { namespace Crafter {
export class HtmlElement { export class HtmlElement {
public: public:
void* const ptr; void* const ptr;
std::vector<int> handlers; std::vector<std::int32_t> clickHandlers;
HtmlElement(const std::string_view id): ptr(CppDOM::Bindings::GetElementById(id)) { std::vector<std::int32_t> mouseOverHandlers;
std::vector<std::int32_t> mouseOutHandlers;
std::vector<std::int32_t> mouseMoveHandlers;
std::vector<std::int32_t> mouseDownHandlers;
std::vector<std::int32_t> mouseUpHandlers;
std::vector<std::int32_t> focusHandlers;
std::vector<std::int32_t> blurHandlers;
std::vector<std::int32_t> keyDownHandlers;
std::vector<std::int32_t> keyUpHandlers;
std::vector<std::int32_t> keyPressHandlers;
std::vector<std::int32_t> changeHandlers;
std::vector<std::int32_t> submitHandlers;
std::vector<std::int32_t> inputHandlers;
std::vector<std::int32_t> resizeHandlers;
std::vector<std::int32_t> scrollHandlers;
std::vector<std::int32_t> contextMenuHandlers;
std::vector<std::int32_t> dragStartHandlers;
std::vector<std::int32_t> dragEndHandlers;
std::vector<std::int32_t> dropHandlers;
std::vector<std::int32_t> dragOverHandlers;
std::vector<std::int32_t> dragEnterHandlers;
std::vector<std::int32_t> dragLeaveHandlers;
std::vector<std::int32_t> wheelHandlers;
HtmlElement(const std::string_view id): ptr(CppDOMBindings::GetElementById(id)) {
} }
HtmlElement(const std::string_view id, const std::string_view html): ptr(CppDOM::Bindings::GetElementById(id)) { HtmlElement(const std::string_view id, const std::string_view html): ptr(CppDOMBindings::GetElementById(id)) {
CppDOM::Bindings::SetInnerHTML(ptr, html); CppDOMBindings::SetInnerHTML(ptr, html);
} }
void SetInnerHTML(const std::string_view& html) { void SetInnerHTML(const std::string_view html) {
CppDOM::Bindings::SetInnerHTML(ptr, html); CppDOMBindings::SetInnerHTML(ptr, html);
} }
// Event handling methods std::int32_t AddClickListener(std::function<void(void)> callback) {
int AddClickListener(std::function<void(void)> callback) { std::int32_t id = CppDOMBindings::clickHandlerMaxId++;
int id = CppDOM::Bindings::clickHandlerMaxId++; clickHandlers.push_back(id);
handlers.push_back(id); CppDOMBindings::clickHandlers->insert({id, callback});
CppDOM::Bindings::clickHandlers->insert({id, callback}); CppDOMBindings::AddClickListener(ptr, id);
CppDOM::Bindings::AddClickListener(ptr, id);
return id; return id;
} }
void RemoveClickListener(int id) { void RemoveClickListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); clickHandlers.erase(std::remove(clickHandlers.begin(), clickHandlers.end(), id), clickHandlers.end());
CppDOM::Bindings::clickHandlers->erase(id); CppDOMBindings::clickHandlers->erase(id);
CppDOM::Bindings::RemoveClickListener(ptr, id); CppDOMBindings::RemoveClickListener(ptr, id);
} }
int AddMouseOverListener(std::function<void(void)> callback) { std::int32_t AddMouseOverListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::mouseOverHandlerMaxId++; std::int32_t id = CppDOMBindings::mouseOverHandlerMaxId++;
handlers.push_back(id); mouseOverHandlers.push_back(id);
CppDOM::Bindings::mouseOverHandlers->insert({id, callback}); CppDOMBindings::mouseOverHandlers->insert({id, callback});
CppDOM::Bindings::AddMouseOverListener(ptr, id); CppDOMBindings::AddMouseOverListener(ptr, id);
return id; return id;
} }
void RemoveMouseOverListener(int id) { void RemoveMouseOverListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); mouseOverHandlers.erase(std::remove(mouseOverHandlers.begin(), mouseOverHandlers.end(), id), mouseOverHandlers.end());
CppDOM::Bindings::mouseOverHandlers->erase(id); CppDOMBindings::mouseOverHandlers->erase(id);
CppDOM::Bindings::RemoveMouseOverListener(ptr, id); CppDOMBindings::RemoveMouseOverListener(ptr, id);
} }
int AddMouseOutListener(std::function<void(void)> callback) { std::int32_t AddMouseOutListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::mouseOutHandlerMaxId++; std::int32_t id = CppDOMBindings::mouseOutHandlerMaxId++;
handlers.push_back(id); mouseOutHandlers.push_back(id);
CppDOM::Bindings::mouseOutHandlers->insert({id, callback}); CppDOMBindings::mouseOutHandlers->insert({id, callback});
CppDOM::Bindings::AddMouseOutListener(ptr, id); CppDOMBindings::AddMouseOutListener(ptr, id);
return id; return id;
} }
void RemoveMouseOutListener(int id) { void RemoveMouseOutListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); mouseOutHandlers.erase(std::remove(mouseOutHandlers.begin(), mouseOutHandlers.end(), id), mouseOutHandlers.end());
CppDOM::Bindings::mouseOutHandlers->erase(id); CppDOMBindings::mouseOutHandlers->erase(id);
CppDOM::Bindings::RemoveMouseOutListener(ptr, id); CppDOMBindings::RemoveMouseOutListener(ptr, id);
} }
int AddMouseMoveListener(std::function<void(void)> callback) { std::int32_t AddMouseMoveListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::mouseMoveHandlerMaxId++; std::int32_t id = CppDOMBindings::mouseMoveHandlerMaxId++;
handlers.push_back(id); mouseMoveHandlers.push_back(id);
CppDOM::Bindings::mouseMoveHandlers->insert({id, callback}); CppDOMBindings::mouseMoveHandlers->insert({id, callback});
CppDOM::Bindings::AddMouseMoveListener(ptr, id); CppDOMBindings::AddMouseMoveListener(ptr, id);
return id; return id;
} }
void RemoveMouseMoveListener(int id) { void RemoveMouseMoveListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); mouseMoveHandlers.erase(std::remove(mouseMoveHandlers.begin(), mouseMoveHandlers.end(), id), mouseMoveHandlers.end());
CppDOM::Bindings::mouseMoveHandlers->erase(id); CppDOMBindings::mouseMoveHandlers->erase(id);
CppDOM::Bindings::RemoveMouseMoveListener(ptr, id); CppDOMBindings::RemoveMouseMoveListener(ptr, id);
} }
int AddFocusListener(std::function<void(void)> callback) { std::int32_t AddMouseDownListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::focusHandlerMaxId++; std::int32_t id = CppDOMBindings::mouseDownHandlerMaxId++;
handlers.push_back(id); mouseDownHandlers.push_back(id);
CppDOM::Bindings::focusHandlers->insert({id, callback}); CppDOMBindings::mouseDownHandlers->insert({id, callback});
CppDOM::Bindings::AddFocusListener(ptr, id); CppDOMBindings::AddMouseDownListener(ptr, id);
return id; return id;
} }
void RemoveFocusListener(int id) { void RemoveMouseDownListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); mouseDownHandlers.erase(std::remove(mouseDownHandlers.begin(), mouseDownHandlers.end(), id), mouseDownHandlers.end());
CppDOM::Bindings::focusHandlers->erase(id); CppDOMBindings::mouseDownHandlers->erase(id);
CppDOM::Bindings::RemoveFocusListener(ptr, id); CppDOMBindings::RemoveMouseDownListener(ptr, id);
} }
int AddBlurListener(std::function<void(void)> callback) { std::int32_t AddMouseUpListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::blurHandlerMaxId++; std::int32_t id = CppDOMBindings::mouseUpHandlerMaxId++;
handlers.push_back(id); mouseUpHandlers.push_back(id);
CppDOM::Bindings::blurHandlers->insert({id, callback}); CppDOMBindings::mouseUpHandlers->insert({id, callback});
CppDOM::Bindings::AddBlurListener(ptr, id); CppDOMBindings::AddMouseUpListener(ptr, id);
return id; return id;
} }
void RemoveBlurListener(int id) { void RemoveMouseUpListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); mouseUpHandlers.erase(std::remove(mouseUpHandlers.begin(), mouseUpHandlers.end(), id), mouseUpHandlers.end());
CppDOM::Bindings::blurHandlers->erase(id); CppDOMBindings::mouseUpHandlers->erase(id);
CppDOM::Bindings::RemoveBlurListener(ptr, id); CppDOMBindings::RemoveMouseUpListener(ptr, id);
} }
int AddKeyDownListener(std::function<void(void)> callback) { // Focus Events
int id = CppDOM::Bindings::keyDownHandlerMaxId++; std::int32_t AddFocusListener(std::function<void(void)> callback) {
handlers.push_back(id); std::int32_t id = CppDOMBindings::focusHandlerMaxId++;
CppDOM::Bindings::keyDownHandlers->insert({id, callback}); focusHandlers.push_back(id);
CppDOM::Bindings::AddKeyDownListener(ptr, id); CppDOMBindings::focusHandlers->insert({id, callback});
CppDOMBindings::AddFocusListener(ptr, id);
return id; return id;
} }
void RemoveKeyDownListener(int id) { void RemoveFocusListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); focusHandlers.erase(std::remove(focusHandlers.begin(), focusHandlers.end(), id), focusHandlers.end());
CppDOM::Bindings::keyDownHandlers->erase(id); CppDOMBindings::focusHandlers->erase(id);
CppDOM::Bindings::RemoveKeyDownListener(ptr, id); CppDOMBindings::RemoveFocusListener(ptr, id);
} }
int AddKeyUpListener(std::function<void(void)> callback) { std::int32_t AddBlurListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::keyUpHandlerMaxId++; std::int32_t id = CppDOMBindings::blurHandlerMaxId++;
handlers.push_back(id); blurHandlers.push_back(id);
CppDOM::Bindings::keyUpHandlers->insert({id, callback}); CppDOMBindings::blurHandlers->insert({id, callback});
CppDOM::Bindings::AddKeyUpListener(ptr, id); CppDOMBindings::AddBlurListener(ptr, id);
return id; return id;
} }
void RemoveKeyUpListener(int id) { void RemoveBlurListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); blurHandlers.erase(std::remove(blurHandlers.begin(), blurHandlers.end(), id), blurHandlers.end());
CppDOM::Bindings::keyUpHandlers->erase(id); CppDOMBindings::blurHandlers->erase(id);
CppDOM::Bindings::RemoveKeyUpListener(ptr, id); CppDOMBindings::RemoveBlurListener(ptr, id);
} }
int AddKeyPressListener(std::function<void(void)> callback) { // Keyboard Events
int id = CppDOM::Bindings::keyPressHandlerMaxId++; std::int32_t AddKeyDownListener(std::function<void(KeyboardEvent)> callback) {
handlers.push_back(id); std::int32_t id = CppDOMBindings::keyDownHandlerMaxId++;
CppDOM::Bindings::keyPressHandlers->insert({id, callback}); keyDownHandlers.push_back(id);
CppDOM::Bindings::AddKeyPressListener(ptr, id); CppDOMBindings::keyDownHandlers->insert({id, callback});
CppDOMBindings::AddKeyDownListener(ptr, id);
return id; return id;
} }
void RemoveKeyPressListener(int id) { void RemoveKeyDownListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); keyDownHandlers.erase(std::remove(keyDownHandlers.begin(), keyDownHandlers.end(), id), keyDownHandlers.end());
CppDOM::Bindings::keyPressHandlers->erase(id); CppDOMBindings::keyDownHandlers->erase(id);
CppDOM::Bindings::RemoveKeyPressListener(ptr, id); CppDOMBindings::RemoveKeyDownListener(ptr, id);
} }
int AddChangeListener(std::function<void(void)> callback) { std::int32_t AddKeyUpListener(std::function<void(KeyboardEvent)> callback) {
int id = CppDOM::Bindings::changeHandlerMaxId++; std::int32_t id = CppDOMBindings::keyUpHandlerMaxId++;
handlers.push_back(id); keyUpHandlers.push_back(id);
CppDOM::Bindings::changeHandlers->insert({id, callback}); CppDOMBindings::keyUpHandlers->insert({id, callback});
CppDOM::Bindings::AddChangeListener(ptr, id); CppDOMBindings::AddKeyUpListener(ptr, id);
return id; return id;
} }
void RemoveChangeListener(int id) { void RemoveKeyUpListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); keyUpHandlers.erase(std::remove(keyUpHandlers.begin(), keyUpHandlers.end(), id), keyUpHandlers.end());
CppDOM::Bindings::changeHandlers->erase(id); CppDOMBindings::keyUpHandlers->erase(id);
CppDOM::Bindings::RemoveChangeListener(ptr, id); CppDOMBindings::RemoveKeyUpListener(ptr, id);
} }
int AddSubmitListener(std::function<void(void)> callback) { std::int32_t AddKeyPressListener(std::function<void(KeyboardEvent)> callback) {
int id = CppDOM::Bindings::submitHandlerMaxId++; std::int32_t id = CppDOMBindings::keyPressHandlerMaxId++;
handlers.push_back(id); keyPressHandlers.push_back(id);
CppDOM::Bindings::submitHandlers->insert({id, callback}); CppDOMBindings::keyPressHandlers->insert({id, callback});
CppDOM::Bindings::AddSubmitListener(ptr, id); CppDOMBindings::AddKeyPressListener(ptr, id);
return id; return id;
} }
void RemoveSubmitListener(int id) { void RemoveKeyPressListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); keyPressHandlers.erase(std::remove(keyPressHandlers.begin(), keyPressHandlers.end(), id), keyPressHandlers.end());
CppDOM::Bindings::submitHandlers->erase(id); CppDOMBindings::keyPressHandlers->erase(id);
CppDOM::Bindings::RemoveSubmitListener(ptr, id); CppDOMBindings::RemoveKeyPressListener(ptr, id);
} }
int AddInputListener(std::function<void(void)> callback) { // Form Events
int id = CppDOM::Bindings::inputHandlerMaxId++; std::int32_t AddChangeListener(std::function<void(ChangeEvent)> callback) {
handlers.push_back(id); std::int32_t id = CppDOMBindings::changeHandlerMaxId++;
CppDOM::Bindings::inputHandlers->insert({id, callback}); changeHandlers.push_back(id);
CppDOM::Bindings::AddInputListener(ptr, id); CppDOMBindings::changeHandlers->insert({id, callback});
CppDOMBindings::AddChangeListener(ptr, id);
return id; return id;
} }
void RemoveInputListener(int id) { void RemoveChangeListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); changeHandlers.erase(std::remove(changeHandlers.begin(), changeHandlers.end(), id), changeHandlers.end());
CppDOM::Bindings::inputHandlers->erase(id); CppDOMBindings::changeHandlers->erase(id);
CppDOM::Bindings::RemoveInputListener(ptr, id); CppDOMBindings::RemoveChangeListener(ptr, id);
} }
int AddLoadListener(std::function<void(void)> callback) { std::int32_t AddSubmitListener(std::function<void(void)> callback) {
int id = CppDOM::Bindings::loadHandlerMaxId++; std::int32_t id = CppDOMBindings::submitHandlerMaxId++;
handlers.push_back(id); submitHandlers.push_back(id);
CppDOM::Bindings::loadHandlers->insert({id, callback}); CppDOMBindings::submitHandlers->insert({id, callback});
CppDOM::Bindings::AddLoadListener(ptr, id); CppDOMBindings::AddSubmitListener(ptr, id);
return id; return id;
} }
void RemoveLoadListener(int id) { void RemoveSubmitListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); submitHandlers.erase(std::remove(submitHandlers.begin(), submitHandlers.end(), id), submitHandlers.end());
CppDOM::Bindings::loadHandlers->erase(id); CppDOMBindings::submitHandlers->erase(id);
CppDOM::Bindings::RemoveLoadListener(ptr, id); CppDOMBindings::RemoveSubmitListener(ptr, id);
} }
int AddErrorListener(std::function<void(void)> callback) { std::int32_t AddInputListener(std::function<void(InputEvent)> callback) {
int id = CppDOM::Bindings::errorHandlerMaxId++; std::int32_t id = CppDOMBindings::inputHandlerMaxId++;
handlers.push_back(id); inputHandlers.push_back(id);
CppDOM::Bindings::errorHandlers->insert({id, callback}); CppDOMBindings::inputHandlers->insert({id, callback});
CppDOM::Bindings::AddErrorListener(ptr, id); CppDOMBindings::AddInputListener(ptr, id);
return id; return id;
} }
void RemoveErrorListener(int id) { void RemoveInputListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); inputHandlers.erase(std::remove(inputHandlers.begin(), inputHandlers.end(), id), inputHandlers.end());
CppDOM::Bindings::errorHandlers->erase(id); CppDOMBindings::inputHandlers->erase(id);
CppDOM::Bindings::RemoveErrorListener(ptr, id); CppDOMBindings::RemoveInputListener(ptr, id);
} }
int AddResizeListener(std::function<void(void)> callback) { std::int32_t AddResizeListener(std::function<void(ResizeEvent)> callback) {
int id = CppDOM::Bindings::resizeHandlerMaxId++; std::int32_t id = CppDOMBindings::resizeHandlerMaxId++;
handlers.push_back(id); resizeHandlers.push_back(id);
CppDOM::Bindings::resizeHandlers->insert({id, callback}); CppDOMBindings::resizeHandlers->insert({id, callback});
CppDOM::Bindings::AddResizeListener(ptr, id); CppDOMBindings::AddResizeListener(ptr, id);
return id; return id;
} }
void RemoveResizeListener(int id) { void RemoveResizeListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); resizeHandlers.erase(std::remove(resizeHandlers.begin(), resizeHandlers.end(), id), resizeHandlers.end());
CppDOM::Bindings::resizeHandlers->erase(id); CppDOMBindings::resizeHandlers->erase(id);
CppDOM::Bindings::RemoveResizeListener(ptr, id); CppDOMBindings::RemoveResizeListener(ptr, id);
} }
int AddScrollListener(std::function<void(void)> callback) { std::int32_t AddScrollListener(std::function<void(ScrollEvent)> callback) {
int id = CppDOM::Bindings::scrollHandlerMaxId++; std::int32_t id = CppDOMBindings::scrollHandlerMaxId++;
handlers.push_back(id); scrollHandlers.push_back(id);
CppDOM::Bindings::scrollHandlers->insert({id, callback}); CppDOMBindings::scrollHandlers->insert({id, callback});
CppDOM::Bindings::AddScrollListener(ptr, id); CppDOMBindings::AddScrollListener(ptr, id);
return id; return id;
} }
void RemoveScrollListener(int id) { void RemoveScrollListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); scrollHandlers.erase(std::remove(scrollHandlers.begin(), scrollHandlers.end(), id), scrollHandlers.end());
CppDOM::Bindings::scrollHandlers->erase(id); CppDOMBindings::scrollHandlers->erase(id);
CppDOM::Bindings::RemoveScrollListener(ptr, id); CppDOMBindings::RemoveScrollListener(ptr, id);
} }
int AddContextMenuListener(std::function<void(void)> callback) { // Context Menu Events
int id = CppDOM::Bindings::contextMenuHandlerMaxId++; std::int32_t AddContextMenuListener(std::function<void(MouseEvent)> callback) {
handlers.push_back(id); std::int32_t id = CppDOMBindings::contextMenuHandlerMaxId++;
CppDOM::Bindings::contextMenuHandlers->insert({id, callback}); contextMenuHandlers.push_back(id);
CppDOM::Bindings::AddContextMenuListener(ptr, id); CppDOMBindings::contextMenuHandlers->insert({id, callback});
CppDOMBindings::AddContextMenuListener(ptr, id);
return id; return id;
} }
void RemoveContextMenuListener(int id) { void RemoveContextMenuListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); contextMenuHandlers.erase(std::remove(contextMenuHandlers.begin(), contextMenuHandlers.end(), id), contextMenuHandlers.end());
CppDOM::Bindings::contextMenuHandlers->erase(id); CppDOMBindings::contextMenuHandlers->erase(id);
CppDOM::Bindings::RemoveContextMenuListener(ptr, id); CppDOMBindings::RemoveContextMenuListener(ptr, id);
} }
int AddDragStartListener(std::function<void(void)> callback) { // Drag and Drop Events
int id = CppDOM::Bindings::dragStartHandlerMaxId++; std::int32_t AddDragStartListener(std::function<void(MouseEvent)> callback) {
handlers.push_back(id); std::int32_t id = CppDOMBindings::dragStartHandlerMaxId++;
CppDOM::Bindings::dragStartHandlers->insert({id, callback}); dragStartHandlers.push_back(id);
CppDOM::Bindings::AddDragStartListener(ptr, id); CppDOMBindings::dragStartHandlers->insert({id, callback});
CppDOMBindings::AddDragStartListener(ptr, id);
return id; return id;
} }
void RemoveDragStartListener(int id) { void RemoveDragStartListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); dragStartHandlers.erase(std::remove(dragStartHandlers.begin(), dragStartHandlers.end(), id), dragStartHandlers.end());
CppDOM::Bindings::dragStartHandlers->erase(id); CppDOMBindings::dragStartHandlers->erase(id);
CppDOM::Bindings::RemoveDragStartListener(ptr, id); CppDOMBindings::RemoveDragStartListener(ptr, id);
} }
int AddDragEndListener(std::function<void(void)> callback) { std::int32_t AddDragEndListener(std::function<void(MouseEvent)> callback) {
int id = CppDOM::Bindings::dragEndHandlerMaxId++; std::int32_t id = CppDOMBindings::dragEndHandlerMaxId++;
handlers.push_back(id); dragEndHandlers.push_back(id);
CppDOM::Bindings::dragEndHandlers->insert({id, callback}); CppDOMBindings::dragEndHandlers->insert({id, callback});
CppDOM::Bindings::AddDragEndListener(ptr, id); CppDOMBindings::AddDragEndListener(ptr, id);
return id; return id;
} }
void RemoveDragEndListener(int id) { void RemoveDragEndListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); dragEndHandlers.erase(std::remove(dragEndHandlers.begin(), dragEndHandlers.end(), id), dragEndHandlers.end());
CppDOM::Bindings::dragEndHandlers->erase(id); CppDOMBindings::dragEndHandlers->erase(id);
CppDOM::Bindings::RemoveDragEndListener(ptr, id); CppDOMBindings::RemoveDragEndListener(ptr, id);
} }
int AddDropListener(std::function<void(void)> callback) { std::int32_t AddDropListener(std::function<void(MouseEvent)> callback) {
int id = CppDOM::Bindings::dropHandlerMaxId++; std::int32_t id = CppDOMBindings::dropHandlerMaxId++;
handlers.push_back(id); dropHandlers.push_back(id);
CppDOM::Bindings::dropHandlers->insert({id, callback}); CppDOMBindings::dropHandlers->insert({id, callback});
CppDOM::Bindings::AddDropListener(ptr, id); CppDOMBindings::AddDropListener(ptr, id);
return id; return id;
} }
void RemoveDropListener(int id) { void RemoveDropListener(std::int32_t id) {
handlers.erase(std::remove(handlers.begin(), handlers.end(), id), handlers.end()); dropHandlers.erase(std::remove(dropHandlers.begin(), dropHandlers.end(), id), dropHandlers.end());
CppDOM::Bindings::dropHandlers->erase(id); CppDOMBindings::dropHandlers->erase(id);
CppDOM::Bindings::RemoveDropListener(ptr, id); CppDOMBindings::RemoveDropListener(ptr, id);
}
// Additional Drag Events
std::int32_t AddDragOverListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragOverHandlerMaxId++;
dragOverHandlers.push_back(id);
CppDOMBindings::dragOverHandlers->insert({id, callback});
CppDOMBindings::AddDragOverListener(ptr, id);
return id;
}
void RemoveDragOverListener(std::int32_t id) {
dragOverHandlers.erase(std::remove(dragOverHandlers.begin(), dragOverHandlers.end(), id), dragOverHandlers.end());
CppDOMBindings::dragOverHandlers->erase(id);
CppDOMBindings::RemoveDragOverListener(ptr, id);
}
std::int32_t AddDragEnterListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragEnterHandlerMaxId++;
dragEnterHandlers.push_back(id);
CppDOMBindings::dragEnterHandlers->insert({id, callback});
CppDOMBindings::AddDragEnterListener(ptr, id);
return id;
}
void RemoveDragEnterListener(std::int32_t id) {
dragEnterHandlers.erase(std::remove(dragEnterHandlers.begin(), dragEnterHandlers.end(), id), dragEnterHandlers.end());
CppDOMBindings::dragEnterHandlers->erase(id);
CppDOMBindings::RemoveDragEnterListener(ptr, id);
}
std::int32_t AddDragLeaveListener(std::function<void(MouseEvent)> callback) {
std::int32_t id = CppDOMBindings::dragLeaveHandlerMaxId++;
dragLeaveHandlers.push_back(id);
CppDOMBindings::dragLeaveHandlers->insert({id, callback});
CppDOMBindings::AddDragLeaveListener(ptr, id);
return id;
}
void RemoveDragLeaveListener(std::int32_t id) {
dragLeaveHandlers.erase(std::remove(dragLeaveHandlers.begin(), dragLeaveHandlers.end(), id), dragLeaveHandlers.end());
CppDOMBindings::dragLeaveHandlers->erase(id);
CppDOMBindings::RemoveDragLeaveListener(ptr, id);
}
// Wheel Event
std::int32_t AddWheelListener(std::function<void(WheelEvent)> callback) {
std::int32_t id = CppDOMBindings::wheelHandlerMaxId++;
wheelHandlers.push_back(id);
CppDOMBindings::wheelHandlers->insert({id, callback});
CppDOMBindings::AddWheelListener(ptr, id);
return id;
}
void RemoveWheelListener(std::int32_t id) {
wheelHandlers.erase(std::remove(wheelHandlers.begin(), wheelHandlers.end(), id), wheelHandlers.end());
CppDOMBindings::wheelHandlers->erase(id);
CppDOMBindings::RemoveWheelListener(ptr, id);
} }
~HtmlElement(){ ~HtmlElement(){
for(int handler : handlers) { // Clean up all handlers by type
// Clean up all handlers based on type - this is a simplified approach for(std::int32_t handler : clickHandlers) {
// In practice, you'd want to track handler types to clean them properly CppDOMBindings::clickHandlers->erase(handler);
CppDOM::Bindings::clickHandlers->erase(handler); CppDOMBindings::RemoveClickListener(ptr, handler);
CppDOM::Bindings::RemoveClickListener(ptr, handler);
} }
CppDOM::Bindings::FreeJs(ptr); for(std::int32_t handler : mouseOverHandlers) {
CppDOMBindings::mouseOverHandlers->erase(handler);
CppDOMBindings::RemoveMouseOverListener(ptr, handler);
}
for(std::int32_t handler : mouseOutHandlers) {
CppDOMBindings::mouseOutHandlers->erase(handler);
CppDOMBindings::RemoveMouseOutListener(ptr, handler);
}
for(std::int32_t handler : mouseMoveHandlers) {
CppDOMBindings::mouseMoveHandlers->erase(handler);
CppDOMBindings::RemoveMouseMoveListener(ptr, handler);
}
for(std::int32_t handler : mouseDownHandlers) {
CppDOMBindings::mouseDownHandlers->erase(handler);
CppDOMBindings::RemoveMouseDownListener(ptr, handler);
}
for(std::int32_t handler : mouseUpHandlers) {
CppDOMBindings::mouseUpHandlers->erase(handler);
CppDOMBindings::RemoveMouseUpListener(ptr, handler);
}
for(std::int32_t handler : focusHandlers) {
CppDOMBindings::focusHandlers->erase(handler);
CppDOMBindings::RemoveFocusListener(ptr, handler);
}
for(std::int32_t handler : blurHandlers) {
CppDOMBindings::blurHandlers->erase(handler);
CppDOMBindings::RemoveBlurListener(ptr, handler);
}
for(std::int32_t handler : keyDownHandlers) {
CppDOMBindings::keyDownHandlers->erase(handler);
CppDOMBindings::RemoveKeyDownListener(ptr, handler);
}
for(std::int32_t handler : keyUpHandlers) {
CppDOMBindings::keyUpHandlers->erase(handler);
CppDOMBindings::RemoveKeyUpListener(ptr, handler);
}
for(std::int32_t handler : keyPressHandlers) {
CppDOMBindings::keyPressHandlers->erase(handler);
CppDOMBindings::RemoveKeyPressListener(ptr, handler);
}
for(std::int32_t handler : changeHandlers) {
CppDOMBindings::changeHandlers->erase(handler);
CppDOMBindings::RemoveChangeListener(ptr, handler);
}
for(std::int32_t handler : submitHandlers) {
CppDOMBindings::submitHandlers->erase(handler);
CppDOMBindings::RemoveSubmitListener(ptr, handler);
}
for(std::int32_t handler : inputHandlers) {
CppDOMBindings::inputHandlers->erase(handler);
CppDOMBindings::RemoveInputListener(ptr, handler);
}
for(std::int32_t handler : resizeHandlers) {
CppDOMBindings::resizeHandlers->erase(handler);
CppDOMBindings::RemoveResizeListener(ptr, handler);
}
for(std::int32_t handler : scrollHandlers) {
CppDOMBindings::scrollHandlers->erase(handler);
CppDOMBindings::RemoveScrollListener(ptr, handler);
}
for(std::int32_t handler : contextMenuHandlers) {
CppDOMBindings::contextMenuHandlers->erase(handler);
CppDOMBindings::RemoveContextMenuListener(ptr, handler);
}
for(std::int32_t handler : dragStartHandlers) {
CppDOMBindings::dragStartHandlers->erase(handler);
CppDOMBindings::RemoveDragStartListener(ptr, handler);
}
for(std::int32_t handler : dragEndHandlers) {
CppDOMBindings::dragEndHandlers->erase(handler);
CppDOMBindings::RemoveDragEndListener(ptr, handler);
}
for(std::int32_t handler : dropHandlers) {
CppDOMBindings::dropHandlers->erase(handler);
CppDOMBindings::RemoveDropListener(ptr, handler);
}
for(std::int32_t handler : dragOverHandlers) {
CppDOMBindings::dragOverHandlers->erase(handler);
CppDOMBindings::RemoveDragOverListener(ptr, handler);
}
for(std::int32_t handler : dragEnterHandlers) {
CppDOMBindings::dragEnterHandlers->erase(handler);
CppDOMBindings::RemoveDragEnterListener(ptr, handler);
}
for(std::int32_t handler : dragLeaveHandlers) {
CppDOMBindings::dragLeaveHandlers->erase(handler);
CppDOMBindings::RemoveDragLeaveListener(ptr, handler);
}
for(std::int32_t handler : wheelHandlers) {
CppDOMBindings::wheelHandlers->erase(handler);
CppDOMBindings::RemoveWheelListener(ptr, handler);
}
CppDOMBindings::FreeJs(ptr);
} }
}; };
} }

View file

@ -19,5 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
export module Crafter.CppDOM; export module Crafter.CppDOM;
export import :Bindings;
export import :HtmlElement; export import :HtmlElement;
export import :EventTypes;
export import :BindingsExport;
export import :BindingsImport;

View file

@ -3,7 +3,7 @@
"configurations": [ "configurations": [
{ {
"name": "lib", "name": "lib",
"interfaces": ["interfaces/Crafter.CppDOM-HtmlElement", "interfaces/Crafter.CppDOM", "interfaces/Crafter.CppDOM-Bindings"], "interfaces": ["interfaces/Crafter.CppDOM-HtmlElement", "interfaces/Crafter.CppDOM", "interfaces/Crafter.CppDOM-BindingsExport", "interfaces/Crafter.CppDOM-BindingsImport", "interfaces/Crafter.CppDOM-EventTypes"],
"additional_files": ["additional/env.js"], "additional_files": ["additional/env.js"],
"type":"library", "type":"library",
"target":"wasm32-wasi" "target":"wasm32-wasi"