From b8756cd60e6ef42b2eb71c65477aa1f3922561f7 Mon Sep 17 00:00:00 2001 From: Danilo Guanabara Date: Wed, 10 Jun 2020 08:13:42 -0300 Subject: [PATCH] Fixed textfield key masking on Mac (https://github.com/enso-org/ide/pull/552) Original commit: https://github.com/enso-org/ide/commit/63ad62cf3db57592b4ec97626424427845117c6b --- gui/.github/workflows/build.yml | 2 +- gui/.github/workflows/check.yml | 12 ++++++------ gui/run | 4 ++-- .../shape/text/text_field/frp/keyboard.rs | 19 +++++++++++++------ gui/src/rust/ide/src/view/text_editor.rs | 11 ++++++++++- gui/src/rust/lib/frp/src/io/keyboard.rs | 5 +++++ 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/gui/.github/workflows/build.yml b/gui/.github/workflows/build.yml index bfc69b5b638..a9c2e03ae54 100644 --- a/gui/.github/workflows/build.yml +++ b/gui/.github/workflows/build.yml @@ -35,7 +35,7 @@ jobs: - name: Install Node uses: actions/setup-node@v1 with: - node-version: '12.16.1' + node-version: '12.18.0' - name: Build run: node ./run dist diff --git a/gui/.github/workflows/check.yml b/gui/.github/workflows/check.yml index b13cd7d5869..ce5c57c376f 100644 --- a/gui/.github/workflows/check.yml +++ b/gui/.github/workflows/check.yml @@ -48,7 +48,7 @@ jobs: - name: Install Node uses: actions/setup-node@v1 with: - node-version: '12.16.1' + node-version: '12.18.0' - name: Build run: node ./run build @@ -86,7 +86,7 @@ jobs: - name: Install Node uses: actions/setup-node@v1 with: - node-version: '12.16.1' + node-version: '12.18.0' - name: Building Rust Sources run: node ./run lint @@ -121,7 +121,7 @@ jobs: - name: Install Node uses: actions/setup-node@v1 with: - node-version: '12.16.1' + node-version: '12.18.0' - name: Run tests run: node ./run test --no-wasm @@ -168,7 +168,7 @@ jobs: - name: Install Node uses: actions/setup-node@v1 with: - node-version: '12.16.1' + node-version: '12.18.0' - name: Run tests run: node ./run test --no-native @@ -190,7 +190,7 @@ jobs: # - name: Install Node # uses: actions/setup-node@v1 # with: - # node-version: '12.16.1' + # node-version: '12.18.0' # # - name: Generate test profile # working-directory: src/rust @@ -210,4 +210,4 @@ jobs: # uses: codecov/codecov-action@v1.0.2 # with: # token: ${{ secrets.CODECOV_TOKEN }} - # file: ./lcov.info \ No newline at end of file + # file: ./lcov.info diff --git a/gui/run b/gui/run index 18e53c67daa..15f2e458eb7 100755 --- a/gui/run +++ b/gui/run @@ -12,8 +12,8 @@ let args = process.argv.slice(2) let no_validation = '--no-validation' async function init () { if(!args.includes(no_validation)) { - await cmd.check_version('npm','6.13.4',{silent:true}) - await cmd.check_version('node','v12.16.1',{silent:true}) + await cmd.check_version('npm','6.14.4',{silent:true}) + await cmd.check_version('node','v12.18.0',{silent:true}) await cmd.check_version('rustc','1.40.0-nightly',{ preprocess:(v)=>v.substring(6,20),silent:true }) diff --git a/gui/src/rust/ensogl/src/display/shape/text/text_field/frp/keyboard.rs b/gui/src/rust/ensogl/src/display/shape/text/text_field/frp/keyboard.rs index cdcfa9fc9c5..f27dd6c1ce0 100644 --- a/gui/src/rust/ensogl/src/display/shape/text/text_field/frp/keyboard.rs +++ b/gui/src/rust/ensogl/src/display/shape/text/text_field/frp/keyboard.rs @@ -133,16 +133,23 @@ impl TextFieldKeyboardFrp { } } + fn enables_writing(mask:&keyboard::KeyMask) -> bool { + let modifiers = &[keyboard::Key::Control,keyboard::Key::Alt,keyboard::Key::Meta]; + let is_modifier = modifiers.iter().any(|key| mask.contains(key)); + let is_alt_graph = mask.contains(&keyboard::Key::AltGraph); + match Platform::query() { + // On Windows AltGraph is emitted as both AltGraph and Ctrl. Therefore we don't + // care about modifiers when AltGraph is pressed. + Platform::Windows => !is_modifier || is_alt_graph, + _ => !is_modifier + } + } + fn char_typed_lambda(text_field:WeakTextField) -> impl Fn(&keyboard::Key,&keyboard::KeyMask) { move |key,mask| { text_field.upgrade().for_each(|text_field| { if let keyboard::Key::Character(string) = key { - let modifiers = &[keyboard::Key::Control,keyboard::Key::Alt]; - let is_modifier = modifiers.iter().any(|key| mask.contains(key)); - let is_alt_graph = mask.contains(&keyboard::Key::AltGraph); - // On Windows AltGraph is emitted as both AltGraph and Ctrl. Therefore we don't - // care about modifiers when AltGraph is pressed. - if !is_modifier || is_alt_graph { + if Self::enables_writing(mask) { text_field.write(string); } } diff --git a/gui/src/rust/ide/src/view/text_editor.rs b/gui/src/rust/ide/src/view/text_editor.rs index e4bebfc896d..7e23ae66df1 100644 --- a/gui/src/rust/ide/src/view/text_editor.rs +++ b/gui/src/rust/ide/src/view/text_editor.rs @@ -14,6 +14,7 @@ use ensogl::display::shape::text::text_field::TextField; use ensogl::display::shape::text::text_field::TextFieldProperties; use ensogl::display::world::*; use ensogl::display; +use ensogl::system::web::platform::Platform; use nalgebra::Vector2; use nalgebra::zero; use utils::channel::process_stream_with_handle; @@ -88,8 +89,16 @@ impl TextEditor { Self::new_from_data(data).initialize(keyboard_actions) } + fn get_save_keys_mask() -> KeyMask { + if let Platform::MacOS = Platform::query() { + KeyMask::meta_plus('s') + } else { + KeyMask::control_plus('s') + } + } + fn initialize(self, keyboard_actions:&mut keyboard::Actions) -> Self { - let save_keys = KeyMask::control_plus('s'); + let save_keys = Self::get_save_keys_mask(); let text_editor = Rc::downgrade(&self.rc); keyboard_actions.add_action_for_key_mask(save_keys,enclose!((text_editor) move || { if let Some(text_editor) = text_editor.upgrade() { diff --git a/gui/src/rust/lib/frp/src/io/keyboard.rs b/gui/src/rust/lib/frp/src/io/keyboard.rs index 0d39505fe27..720be0be6f8 100644 --- a/gui/src/rust/lib/frp/src/io/keyboard.rs +++ b/gui/src/rust/lib/frp/src/io/keyboard.rs @@ -31,6 +31,11 @@ pub use keyboard_types::Key; pub struct KeyMask(pub BitField256); impl KeyMask { + /// Creates Key::Meta + Key::Character. + pub fn meta_plus(character:char) -> Self { + Self::from_vec(vec![Key::Meta, Key::Character(character.to_string())]) + } + /// Creates Key::Control + Key::Character. pub fn control_plus(character:char) -> Self { Self::from_vec(vec![Key::Control, Key::Character(character.to_string())])