Commit 5e5c6244 authored by Florian Blasius's avatar Florian Blasius 🤘

drag event

parent ead47c9b
Pipeline #9141 passed with stage
in 1 minute and 11 seconds
......@@ -48,6 +48,8 @@ features = [
"KeyboardEvent",
"WheelEvent",
"FocusEvent",
"DragEvent",
"DataTransfer",
"DomRect"
]
......
......@@ -47,7 +47,7 @@ pub struct Window {
/// Mouse in relative mode
mouse_relative: Rc<RefCell<bool>>,
/// Content of the last drop (file | text) operation
drop_content: RefCell<Option<String>>,
drop_content: Rc<RefCell<Option<String>>>,
/// inner frame buffer
data: Vec<Color>,
/// html canvas that represents the window
......@@ -159,8 +159,15 @@ impl Window {
let events = Rc::new(RefCell::new(vec![]));
let button_state = Rc::new(RefCell::new((false, false, false)));
let mouse_relative = Rc::new(RefCell::new(false));
connect_event_handlers(mouse_relative.clone(), &canvas, &events, &button_state);
let drop_content = Rc::new(RefCell::new(None));
connect_event_handlers(
mouse_relative.clone(),
&canvas,
&events,
&button_state,
&drop_content,
);
if let Ok(context) = context_2d(&canvas) {
return Some(Window {
......@@ -172,7 +179,7 @@ impl Window {
window_async: false,
mode: Cell::new(Mode::Blend),
mouse_relative,
drop_content: RefCell::new(None),
drop_content,
data: vec![Color::rgb(255, 255, 255); (w * h * 4) as usize],
canvas,
context,
......@@ -197,7 +204,10 @@ impl Window {
/// Pops the content of the last drop event from the window.
pub fn pop_drop_content(&self) -> Option<String> {
None
let result = self.drop_content.borrow().clone();
*self.drop_content.borrow_mut() = None;
result
}
pub fn sync_path(&mut self) {}
......@@ -499,6 +509,7 @@ fn connect_event_handlers(
canvas: &HtmlCanvasElement,
events: &Rc<RefCell<Vec<Event>>>,
button_state: &Rc<RefCell<(bool, bool, bool)>>,
drop_content: &Rc<RefCell<Option<String>>>,
) {
// mouse event
{
......@@ -710,4 +721,24 @@ fn connect_event_handlers(
.unwrap();
closure.forget();
}
// drop
{
let events = events.clone();
let drop_content = drop_content.clone();
let closure = Closure::wrap(Box::new(move |e: web_sys::DragEvent| {
if let Some(data_transfer) = e.data_transfer() {
if let Ok(text) = data_transfer.get_data("text") {
*drop_content.borrow_mut() = Some(text);
events
.borrow_mut()
.push(DropEvent { kind: DROP_TEXT }.to_event());
}
}
}) as Box<dyn FnMut(_)>);
canvas
.add_event_listener_with_callback("drop", closure.as_ref().unchecked_ref())
.unwrap();
closure.forget();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment