From d133e1bc4556a01b70f605dcca527c79b76be05e Mon Sep 17 00:00:00 2001 From: Korede Aderele Date: Thu, 30 Apr 2020 10:48:07 -0400 Subject: [PATCH] upgrade to Yew 0.15, with keyed attributes --- Cargo.toml | 4 +-- src/model.rs | 28 +++++++++-------- src/util.rs | 9 ------ src/view.rs | 87 ++++++++++++++++++++++++++-------------------------- 4 files changed, 62 insertions(+), 66 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 77eb64a..0240512 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Korede Aderele "] edition = "2018" [dependencies] -yew = { version = "0.12", features = ["toml", "yaml", "msgpack", "cbor"] } +yew = { version = "0.15", package = "yew-stdweb", features = ["toml", "yaml", "msgpack", "cbor"] } maplit = "1.0.2" # cargo-web = "0.6.26" stdweb = "0.4.3" @@ -43,4 +43,4 @@ features = [ 'HtmlElement', 'Node', 'Window', -] \ No newline at end of file +] diff --git a/src/model.rs b/src/model.rs index 2939841..3c4f996 100644 --- a/src/model.rs +++ b/src/model.rs @@ -7,9 +7,11 @@ use std::ops::Deref; use std::option::Option; use stdweb::traits::IEvent; use stdweb::unstable::TryInto; +use stdweb::web::event::{ + ContextMenuEvent, IKeyboardEvent, KeyDownEvent, KeyPressEvent, KeyUpEvent, +}; use stdweb::web::{document, IElement, INode, IParentNode}; use wasm_bindgen::JsValue; -use yew::events::{KeyDownEvent, KeyPressEvent, KeyUpEvent}; use yew::prelude::*; use yew::services::reader::{File, FileData, ReaderService, ReaderTask}; use yew::services::ConsoleService; @@ -832,8 +834,9 @@ impl Component for Model { Action::ReadSession(file) => { let callback = self.link.callback(Action::LoadSession); - let task = self.reader.read_file(file, callback); - self.tasks.push(task); + if let Ok(task) = self.reader.read_file(file, callback) { + self.tasks.push(task); + } false } @@ -974,16 +977,19 @@ impl Component for Model { .link .callback(|file_data| Action::UploadDriverMiscFile(file_data)); for file in misc_files { - let task = self.reader.read_file(file, upload_callback.clone()); - self.tasks.push(task); + if let Ok(task) = self.reader.read_file(file, upload_callback.clone()) { + self.tasks.push(task); + } } // Load main driver file. After this task has been scheduled and executed, the // driver is ready for use. - self.tasks.push( - self.reader - .read_file(main_file, self.link.callback(Action::LoadDriverMainFile)), - ); + if let Ok(task) = self + .reader + .read_file(main_file, self.link.callback(Action::LoadDriverMainFile)) + { + self.tasks.push(task); + } false } @@ -996,10 +1002,9 @@ impl Component for Model { // https://www.tutorialspoint.com/electron/electron_inter_process_communication.htm // And here, for the documentation for the electon_sys Rust bindings for electron.ipcRenderer: // https://docs.rs/electron-sys/0.4.0/electron_sys/struct.IpcRenderer.html - let args: [JsValue; 2] = [ JsValue::from_str(file_data.name.deref()), - JsValue::from_str(std::str::from_utf8(&file_data.content).unwrap()), + JsValue::from_str(std::str::from_utf8(&file_data.content).unwrap_or("")), ]; ipc_renderer.send_sync("upload-driver-misc-file", Box::new(args)); false @@ -1651,7 +1656,6 @@ impl Component for Model { } Action::SetCurrentDefinitionName(name) => { - info! {"current defn name: {}", name}; self.default_definition_name = name; false } diff --git a/src/util.rs b/src/util.rs index 13e3c6d..b42ed4d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -35,15 +35,6 @@ pub fn move_grammar(m: &mut Model, source: Coordinate, dest: Coordinate) { m.get_session_mut() .grammars .insert(dest.clone(), source_grammar.clone()); - // if a grammar is being moved from root to the meta, clear the original value - // this might not be the behaviour moving forward so we can take that out - if coord!("root").is_n_parent(&source.clone()).is_some() - && coord!("meta").is_n_parent(&dest.clone()).is_some() - { - m.get_session_mut() - .grammars - .insert(source.clone(), Grammar::default()); - } // resizes new grammar let row_height = m.row_heights.get(&source.full_row()).unwrap_or(&30.0); let col_width = m.col_widths.get(&source.full_col()).unwrap_or(&90.0); diff --git a/src/view.rs b/src/view.rs index 5d4e4ff..b955873 100644 --- a/src/view.rs +++ b/src/view.rs @@ -4,8 +4,11 @@ use std::rc::Rc; use stdweb::traits::IEvent; use stdweb::unstable::TryFrom; use stdweb::unstable::TryInto; +use stdweb::web::event::{ + ClickEvent, IKeyboardEvent, IMouseEvent, KeyDownEvent, KeyPressEvent, MouseDownEvent, + MouseOverEvent, +}; use stdweb::web::{html_element::InputElement, HtmlElement, IHtmlElement}; -use yew::events::{ClickEvent, IKeyboardEvent, IMouseEvent, KeyPressEvent}; use yew::prelude::*; use yew::services::reader::File; use yew::virtual_dom::vlist::VList; @@ -203,56 +206,54 @@ pub fn view_menu_bar(m: &Model) -> Html { // - the last selected cell is the last (bottom-rightmost) child of the parent // cell, which should be a Kind::Grid grammar (Some(first), Some(last)) if first.parent() == last.parent() => { - if let Some((Kind::Grid(sub_coords))) = - /* get the coordinate of the parent, lookup the grammar, then get the grammar.kind */ - first + if let Some((Kind::Grid(sub_coords))) = /* get the coordinate of the parent, lookup the grammar, then get the grammar.kind */ + first .parent() .and_then(|c| m.get_session().grammars.get(&c)) .map(|g| (g.kind.clone())) - { - use std::cmp::Ordering; - let mut sc = sub_coords.clone(); - sc.sort_by(|(a_row, a_col), (b_row, b_col)| { - if a_row > b_row { + { + use std::cmp::Ordering; + let mut sc = sub_coords.clone(); + sc.sort_by(|(a_row, a_col), (b_row, b_col)| { + if a_row > b_row { + Ordering::Greater + } else if a_row < b_row { + Ordering::Less + } else { + if a_col > b_col { Ordering::Greater - } else if a_row < b_row { + } else if a_col < b_col { Ordering::Less } else { - if a_col > b_col { - Ordering::Greater - } else if a_col < b_col { - Ordering::Less - } else { - Ordering::Equal - } + Ordering::Equal } - }); - let first_sc = sc.first().expect( - "add_definition_button: expect selection parent sub_coords.len > 1", - ); - let last_sc = sc.last().expect( - "add_definition_button: expect selection parent sub_coords.len > 1", - ); - let defn_name = if m.default_definition_name == "" { - first.parent().unwrap().to_string().replace("-", "_") - } else { - m.default_definition_name.clone() - }; - ( - // can add definition? - *first_sc == first.row_col() && - *last_sc == last.row_col(), - // definition name - defn_name.clone(), - // callback - m.link.callback(move |_| { - // Action::AddDefinition(first.parent().unwrap(), defn_name.clone(), true) - Action::StageDefinition(first.parent().unwrap(), defn_name.clone()) - }), - ) + } + }); + let first_sc = sc.first().expect( + "add_definition_button: expect selection parent sub_coords.len > 1", + ); + let last_sc = sc.last().expect( + "add_definition_button: expect selection parent sub_coords.len > 1", + ); + let defn_name = if m.default_definition_name == "" { + first.parent().unwrap().to_string().replace("-", "_") } else { - (false, "".to_string(), m.link.callback(|_| Action::Noop)) - } + m.default_definition_name.clone() + }; + ( + // can add definition? + *first_sc == first.row_col() && *last_sc == last.row_col(), + // definition name + defn_name.clone(), + // callback + m.link.callback(move |_| { + // Action::AddDefinition(first.parent().unwrap(), defn_name.clone(), true) + Action::StageDefinition(first.parent().unwrap(), defn_name.clone()) + }), + ) + } else { + (false, "".to_string(), m.link.callback(|_| Action::Noop)) + } } _ => (false, "".to_string(), m.link.callback(|_| Action::Noop)), };