Skip to content

Commit

Permalink
feat: add rust build support for example
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Oct 28, 2024
1 parent 6d8575c commit 5ec968e
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 96 deletions.
1 change: 1 addition & 0 deletions webf/example/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
example_app
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
4 changes: 2 additions & 2 deletions webf/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ environment:
dependencies:
flutter:
sdk: flutter
# example_app:
# path: rust_builde r
example_app:
path: rust_builder
webf: ^0.10.0

# When depending on this package from a real application,
Expand Down
81 changes: 0 additions & 81 deletions webf/example/rust/src/lib.rs

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ crate-type = ["cdylib", "staticlib"]
webf-sys = "0.16.0"

[patch.crates-io]
webf-sys = { path = "../../../bridge/rusty_webf_sys" }
webf-sys = { path = "../../../../bridge/rusty_webf_sys" }
82 changes: 82 additions & 0 deletions webf/example/rust_builder/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::ffi::{c_void, CString};
use webf_sys::event::Event;
use webf_sys::executing_context::ExecutingContextRustMethods;
use webf_sys::{AddEventListenerOptions, element, EventTargetMethods, initialize_webf_api, RustValue};
use webf_sys::element::Element;
use webf_sys::node::NodeMethods;

#[no_mangle]
pub extern "C" fn init_webf_app(handle: RustValue<ExecutingContextRustMethods>) -> *mut c_void {
let context = initialize_webf_api(handle);
println!("Context created");
// let exception_state = context.create_exception_state();
// let document = context.document();

// let click_event = document.create_event("custom_click", &exception_state).unwrap();
// document.dispatch_event(&click_event, &exception_state);

// let div_element = document.create_element("div", &exception_state).unwrap();

// let event_listener_options = AddEventListenerOptions {
// passive: 0,
// once: 0,
// capture: 0,
// };

// let event_handler = Box::new(|event: &Event| {
// let context = event.context();
// let exception_state = context.create_exception_state();
// let document = context.document();
// let div = document.create_element("div", &exception_state).unwrap();
// let text_node = document.create_text_node("Created By Event Handler", &exception_state).unwrap();
// div.append_child(&text_node.as_node(), &exception_state).unwrap();
// document.body().append_child(&div.as_node(), &exception_state).unwrap();
// });

// div_element.add_event_listener("custom_click", event_handler.clone(), &event_listener_options, &exception_state).unwrap();

// let real_click_handler = Box::new(move |event: &Event| {
// let context = event.context();
// let exception_state = context.create_exception_state();
// let document = context.document();
// let custom_click_event = document.create_event("custom_click", &exception_state);

// match custom_click_event {
// Ok(custom_click_event) => {
// let event_target = event.target();
// let element: Element = event_target.as_element().unwrap();
// let _ = element.dispatch_event(&custom_click_event, &exception_state);
// },
// Err(err) => {
// println!("{err}");
// }
// }
// });

// div_element.add_event_listener("click", real_click_handler, &event_listener_options, &exception_state).unwrap();

// let text_node = document.create_text_node("From Rust", &exception_state).unwrap();

// div_element.append_child(&text_node.as_node(), &exception_state).expect("append Node Failed");

// document.body().append_child(&div_element.as_node(), &exception_state).unwrap();

// let event_cleaner_element = document.create_element("button", &exception_state).unwrap();

// let event_cleaner_text_node = document.create_text_node("Remove Event", &exception_state).unwrap();

// event_cleaner_element.append_child(&event_cleaner_text_node.as_node(), &exception_state).unwrap();

// let event_cleaner_handler = Box::new(move |event: &Event| {
// let context = event.context();
// let exception_state = context.create_exception_state();

// let _ = div_element.remove_event_listener("custom_click", event_handler.clone(), &exception_state);
// });

// event_cleaner_element.add_event_listener("click", event_cleaner_handler, &event_listener_options, &exception_state).unwrap();

// document.body().append_child(&event_cleaner_element.as_node(), &exception_state).unwrap();

std::ptr::null_mut()
}
25 changes: 14 additions & 11 deletions webf/example/rust_builder/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# The Flutter tooling requires that developers have a version of Visual Studio
# installed that includes CMake 3.14 or later. You should not increase this
# version, as doing so will cause the plugin to fail to compile for some
# customers of the plugin.
cmake_minimum_required(VERSION 3.14)
# The Flutter tooling requires that developers have CMake 3.10 or later
# installed. You should not increase this version, as doing so will cause
# the plugin to fail to compile for some customers of the plugin.
cmake_minimum_required(VERSION 3.10)

# Project-level configuration.
set(PROJECT_NAME "example_app")
project(${PROJECT_NAME} LANGUAGES CXX)

# Invoke the build for native code shared with the other target platforms.
# This can be changed to accommodate different builds.
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../src" "${CMAKE_CURRENT_BINARY_DIR}/shared")

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Replace add_subdirectory that references old C++ code with Cargokit:
include("../cargokit/cmake/cargokit.cmake")
apply_cargokit(${PROJECT_NAME} "../rust" example_app "")

# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# List of absolute paths to libraries that should be bundled with the plugin.
# This list could contain prebuilt libraries, or libraries created by an
# external build triggered from this build file.
set(rust_builder_bundled_libraries
set(example_app_bundled_libraries
"${${PROJECT_NAME}_cargokit_lib}"
# Defined in ../src/CMakeLists.txt.
# This can be changed to accommodate different builds.
$<TARGET_FILE:example_app>
PARENT_SCOPE
)
)
1 change: 1 addition & 0 deletions webf/example/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
example_app
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
1 change: 0 additions & 1 deletion webf/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)


# Invoke the build for native code shared with the other target platforms.
# This can be changed to accommodate different builds.
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../win_src" "${CMAKE_CURRENT_BINARY_DIR}/shared")
Expand Down

0 comments on commit 5ec968e

Please sign in to comment.