super_native_extensions

repository·main·Indexed 20 days ago

https://github.com/superlistapp/super_native_extensions

A Flutter monorepo providing low-level platform capabilities through specialized native extension packages. Included packages are super_clipboard for clipboard operations, super_drag_and_drop for drag and drop functionality, super_context_menu for context menu implementations, super_keyboard_layout for keyboard layout interaction, and super_hot_key for hotkey and shortcut registrations.

Tokens
21.2K
Snippets
55
Records
80
Agent score
68%

What's inside super_native_extensions

  1. Overview of super_native_extensions packages

    main

    The super_native_extensions repository contains several specialized native extension packages for Flutter. Depending on your requirements, you may need one or more of the following:

    • super_clipboard: For clipboard operations.
    • super_drag_and_drop: For drag and drop functionality.
    • super_context_menu: For context menu implementations.
    • super_keyboard_layout: For interacting with keyboard layouts.
    • super_hot_key: For handling hotkey/shortcut registrations.
  2. Overview of super_native_extensions

    main

    The super_native_extensions plugin provides the underlying native implementation required for the following Flutter plugins to function:

    If you are using either of these packages, you likely need this native extension package to bridge the Flutter code with platform-specific (native) capabilities.

  3. How Cargokit uses precompiled binaries

    main

    To avoid requiring a Rust toolchain on every developer's machine, Cargokit can download and verify precompiled binaries. The workflow follows these steps:

    1. Check Configuration: Cargokit looks for cargokit_options.yaml in the target application's root. If use_precompiled_binaries is set to false, it forces a build from source. If the file is missing but rustup is installed, it also builds from source.
    2. Verify Crate Support: Cargokit looks for a cargokit.yaml file inside the Rust crate. This file must contain a precompiled_binaries section with a public_key and a url_prefix.
    3. Compute Crate Hash: A crate-hash (SHA256) is generated from all Rust files, Cargo.toml, Cargo.lock, and cargokit.yaml. This hash is used to identify the specific version of the binaries needed.
    4. Download and Verify: Cargokit attempts to download the binary and its signature for the current platform and crate-hash. It verifies the signature using the public_key from cargokit.yaml.
    5. Fallback: If verification fails or download fails, Cargokit falls back to building from source.
  4. Run the super_keyboard_layout example

    main

    To run the example project located in super_keyboard_layout/example, use melos to bootstrap the repository dependencies:

    1. Activate melos: flutter pub global activate melos
    2. Clone the repository.
    3. Run melos bootstrap in the root directory.
    4. Open the project in VSCode and run the super_keyboard_layout launcher configuration.
    flutter pub global activate melos
    git clone https://github.com/superlistapp/super_native_extensions.git
    cd super_native_extensions
    melos bootstrap
  5. Enable multi-item dragging on iOS

    main

    To support dragging multiple items simultaneously on iOS, configure the DragItemWidget with canAddItemToExistingSession: true.

    In the dragItemProvider callback, you must check if the item is already part of the current session using request.session.hasLocalData(key). If it is already present, return null to prevent the same item from being added multiple times.

      return DragItemWidget(
        allowedOperations: () => [DropOperation.copy],
        canAddItemToExistingSession: true,
        dragItemProvider: (request) async {
          // For multi drag on iOS check if this item is already in the session
          if (await request.session.hasLocalData('image-item')) {
            return null;
          }
          final item = DragItem(
            localData: 'image-item',
            suggestedName: 'Green.png',
          );
          item.add(Formats.png(await createImageData(Colors.green)));
          return item;
        }
      );
  6. Install super_drag_and_drop

    main

    The super_drag_and_drop plugin uses Rust for low-level platform functionality.

    • Automatic Installation: If Rust is not installed, the plugin will automatically download precompiled binaries for your target platform during the build process.
    • Manual Compilation: To compile from source, install Rust via rustup. The build integration will automatically detect rustup and install required Rust targets and dependencies like the NDK.

    Rust Installation Commands:

    • macOS/Linux: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • Windows: Use the Rust Installer.

    To ensure you have the latest version, run:

    rustup update
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  7. Build and upload precompiled binaries via GitHub Actions

    main

    You can automate the precompilation and uploading of binaries using a GitHub Action. The action requires two secrets: RELEASE_PRIVATE_KEY (for signing) and RELEASE_GITHUB_TOKEN (for uploading to releases).

    Use the dart run build_tool precompile-binaries command to perform the work.

    Key Arguments:

    • --manifest-dir: Path to the Rust manifest.
    • --repository: The repository identifier (e.g., owner/repo).
    • --target <rust-triple>: (Optional) Override to build for a specific target.
    • --android-sdk-location and --android-ndk-version: Required to build Android binaries.

    Example workflow configuration:

    on:
      push:
        branches: [ main ]
    
    name: Precompile Binaries
    
    jobs:
      Precompile:
        runs-on: ${{ matrix.os }}
        strategy:
          fail-fast: false
          matrix:
            os:
              - ubuntu-latest
              - macOS-latest
              - windows-latest
        steps:
          - uses: actions/checkout@v2
          - uses: dart-lang/setup-dart@v1
          - name: Install GTK
            if: (matrix.os == 'ubuntu-latest')
            run: sudo apt-get update && sudo apt-get install libgtk-3-dev
          - name: Precompile
            if: (matrix.os == 'macOS-latest') || (matrix.os == 'windows-latest')
            run: dart run build_tool precompile-binaries -v --manifest-dir=../../rust --repository=superlistapp/super_native_extensions
            working-directory: super_native_extensions/cargokit/build_tool
            env:
              GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
              PRIVATE_KEY: ${{ secrets.RELEASE_PRIVATE_KEY }}
          - name: Precompile (with Android)
            if: (matrix.os == 'ubuntu-latest')
            run: dart run build_tool precompile-binaries -v --manifest-dir=../../rust --repository=superlistapp/super_native_extensions --android-sdk-location=/usr/local/lib/android/sdk --android-ndk-version=24.0.8215888 --android-min-sdk-version=23
            working-directory: super_native_extensions/cargokit/build_tool
            env:
              GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
              PRIVATE_KEY: ${{ secrets.RELEASE_PRIVATE_KEY }}
  8. Integrate Cargokit into a Flutter plugin

    main

    Cargokit is designed to be included in a Flutter plugin or application that contains a Rust crate. It automates the building of Rust artifacts during the Flutter build process.

    Integration Methods:

    • Git Submodule: Recommended for general use.
    • Git Subtree: Required for Flutter plugins, as the pub package manager does not support submodules for git dependencies.

    For a detailed step-by-step tutorial, refer to the external guide: https://matejknopp.com/post/flutter_plugin_in_rust_with_no_prebuilt_binaries/.