Skip to content

Commit 0d36d15

Browse files
committed
Add examples
1 parent 8680d39 commit 0d36d15

File tree

9 files changed

+148
-10
lines changed

9 files changed

+148
-10
lines changed

.cargo/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-unknown-unknown"

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ readme = "README.md"
1010

1111
[dependencies]
1212
lazy_static = "1.4"
13-
stdweb = "0.4"
13+
stdweb = "0.4"
14+
15+
[dev-dependencies]
16+
wasm-bindgen-test = "0.3"

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ It works with absolute positions and can therefore be used to place HTML text el
77

88
TODO
99

10+
### Running the examples
11+
To run the examples included in the library, it's recommended to install [cargo-web](https://github.com/koute/cargo-web) and then simply call `cargo web start --example hello-world`. See the [examples](./examples) folder for all available examples.
12+
1013
## Implementation policy for Panes
1114
* Compatibility with stable Rust
1215
* Light-weight (only major dependency is [stdweb](https://github.com/koute/stdweb))

Web.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default-target = "wasm32-unknown-unknown"

examples/dynamic_pane.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use stdweb::js;
2+
use stdweb::web::*;
3+
use stdweb::unstable::TryInto;
4+
fn main() {
5+
stdweb::initialize();
6+
panes::init().unwrap();
7+
8+
// Create a new pane with a div as grid container.
9+
// Because we want low-level access afterwards, it is important here that
10+
// the div starts at the first line. Otherwise, there will be a text node before the div
11+
let html =
12+
r#"<div style="display: grid; grid-template-columns: repeat(5, 1fr); grid-gap: 10px; overflow: auto;"></div>"#;
13+
let pane = panes::new_pane(100,100,550,550,html).unwrap();
14+
15+
// Get low-level access to the DOM node representing the grid container
16+
let node = pane.get_first_inner_node().unwrap();
17+
18+
19+
/* Now we are in webstd territory */
20+
21+
// UpCast to an HtmlElement
22+
let table: HtmlElement = node.try_into().expect("Should be HTML");
23+
24+
// Append a single element to the grid
25+
table.append_html(
26+
r#"<div style="background-color: blue; height: 100px;">"#
27+
).unwrap();
28+
29+
// Clone table reference so we can move it into closure
30+
let cloned_table = table.clone();
31+
// Append more dynamically, using a closure registered as event listener on space bar key
32+
let append_closure = move || {
33+
cloned_table.append_html(
34+
r#"<div style="background-color: blue; height: 100px;">"#
35+
).unwrap();
36+
};
37+
// Registration is here done using the webstd js macro
38+
js!{
39+
document.addEventListener("keydown", event => {
40+
if (event.code === "Space") {
41+
@{append_closure}()
42+
}
43+
})
44+
}
45+
46+
// Also register to remove one on backspace
47+
let delete_closure = move || {
48+
if let Some(child) = table.last_child() {
49+
table.remove_child(&child).unwrap();
50+
}
51+
};
52+
// Registration is here done using the webstd js macro
53+
js!{
54+
document.addEventListener("keydown", event => {
55+
if (event.code === "Backspace") {
56+
@{delete_closure}()
57+
}
58+
})
59+
}
60+
}

examples/hello_world.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![allow(unused_must_use)]
2+
fn main() {
3+
// Initialize stdweb and panes
4+
stdweb::initialize();
5+
panes::init();
6+
7+
// Create a new pane at offset (100,100) from body
8+
// with size 500px/500px and then create a single
9+
// text node inside it with the text "Hello world"
10+
let x = 100;
11+
let y = 100;
12+
let w = 500;
13+
let h = 500;
14+
let html = "Hello world";
15+
panes::new_pane(x,y,w,h,html);
16+
}

examples/toggle_panes.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use stdweb::web::set_timeout;
2+
use panes::PaneHandle;
3+
4+
fn main() {
5+
stdweb::initialize();
6+
panes::init().expect("Init failed");
7+
8+
// Create two new panes with some HTML in it
9+
let html0 = r#"
10+
<div style="background-color:grey; color: white; height: 100%;">
11+
<div style="text-align: end; position: absolute; bottom: 0; right: 0;">
12+
Hi!
13+
</div>
14+
</div>
15+
"#;
16+
let html1 = r#"
17+
<div style="background-color:grey; color:white; height: 100%;">
18+
<div>
19+
Bye!
20+
</div>
21+
</div>
22+
"#;
23+
let pane0 = panes::new_pane(100,100,100,100,html0).unwrap();
24+
let pane1 = panes::new_pane(200,200,100,100,html1).unwrap();
25+
26+
toggle(pane0, pane1);
27+
}
28+
29+
// Function that takes to panes, shows the first and hides the second
30+
// and then calls itself again delayed, with the two panes swapped
31+
fn toggle(a: PaneHandle, b: PaneHandle) {
32+
a.show().expect("Error");
33+
b.hide().expect("Error");
34+
let closure = move || { toggle(b,a); };
35+
set_timeout(closure, 1000);
36+
}

src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use style::*;
1919
/// Mounts the panes to the HTML body
2020
pub fn init() -> Result<(), PanesError> {
2121
let mut state = get_mut()?;
22-
if state.is_none() {
22+
if state.is_some() {
2323
return Err(PanesError::AlreadyInitialized);
2424
}
2525
*state = Some(GlobalState::mount_body()?);
@@ -29,14 +29,17 @@ pub fn init() -> Result<(), PanesError> {
2929
/// Extended initialization function.
3030
/// Mounts the panes as a child of the HTML element with the defined ID.
3131
/// The specified dimensions restrict the area in which panes are visible.
32+
/// # Example
3233
/// ```
33-
/// let width = 1280;
34-
/// let height = 720;
34+
/// let width = 1280
35+
/// let height = 720
3536
/// panes::init_ex("panes-root", 0, 0, width, height);
3637
/// ```
3738
pub fn init_ex(id: &str, x: u32, y: u32, width: u32, height: u32) -> Result<(), PanesError> {
3839
let mut state = get_mut()?;
39-
assert!(state.is_none(), "Repeated initialization");
40+
if state.is_some() {
41+
return Err(PanesError::AlreadyInitialized);
42+
}
4043

4144
let document = stdweb::web::document();
4245
let root = document.get_element_by_id(id).ok_or(PanesError::MissingRoot(id.to_owned()))?;

src/pane_handle.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Almost the entire library interface is defined in this module.
44
55
use crate::*;
6-
use stdweb::web::HtmlElement;
6+
use stdweb::web::{HtmlElement, Node};
77
use stdweb::unstable::TryInto;
88

99
/// External representation of a Pane.
@@ -39,22 +39,36 @@ impl PaneHandle {
3939
let _pane = get_mut()?.as_mut().ok_or(PanesError::NotInitialized)?.delete_pane(self);
4040
Ok(())
4141
}
42-
/// Get a reference to a DOM node associated with the pane.
42+
/// Get a reference to the DOM element associated with the pane.
43+
/// The provided HTML when creating a new pane will be the child node(s) of the returned element.
4344
///
4445
/// The returned [`HtmlElement`] is from the crate [`stdweb`], allowing library users to
4546
/// escape the Panes crate and access the DOM directly.
4647
///
47-
/// The provided HTML when creating a new pane will be the child node(s) of the returned element.
48-
///
4948
/// TODO: Example
5049
///
5150
/// [`HtmlElement`]: https://docs.rs/stdweb/*/stdweb/web/struct.HtmlElement.html
5251
/// [`stdweb`]: https://docs.rs/stdweb/*/stdweb/
53-
pub fn get_node(&self) -> Result<HtmlElement, PanesError> {
52+
pub fn get_parent_element(&self) -> Result<HtmlElement, PanesError> {
5453
get()?.as_ref()
5554
.ok_or(PanesError::NotInitialized)?
5655
.get_node(&self)?.clone()
5756
.try_into()
5857
.map_err(|e|PanesError::BrowserError(Box::new(e)))
5958
}
59+
/// Get a reference to the DOM node created by the provided HTML when creating the pane.
60+
/// If multiple nodes have been created, the first node is returned.
61+
///
62+
/// The returned [`Node`] is from the crate [`stdweb`], allowing library users to
63+
/// escape the Panes crate and access the DOM directly.
64+
///
65+
/// TODO: Example
66+
///
67+
/// [`Node`]: https://docs.rs/stdweb/*/stdweb/web/struct.Node.html
68+
/// [`stdweb`]: https://docs.rs/stdweb/*/stdweb/
69+
pub fn get_first_inner_node(&self) -> Result<Node, PanesError> {
70+
self.get_parent_element()?
71+
.first_child()
72+
.ok_or(PanesError::MissingChild)
73+
}
6074
}

0 commit comments

Comments
 (0)