Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WebView enhancements #4018

Merged
merged 16 commits into from
Nov 8, 2024
Merged

feat: WebView enhancements #4018

merged 16 commits into from
Nov 8, 2024

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Sep 20, 2024

Summary by Sourcery

Enhance the WebView component with new event handling capabilities and methods, including platform-specific implementations for mobile, macOS, Windows, and Linux. Deprecate the 'javascript_enabled' property in favor of 'enable_javascript'. Refactor platform detection logic to improve code maintainability. Update dependencies to support new features and remove unused imports from test files.

New Features:

  • Introduce new event handling capabilities for WebView, including scroll, console message, and JavaScript alert dialog events.
  • Add support for various WebView methods such as reload, go back, go forward, enable/disable zoom, clear cache, and run JavaScript.
  • Implement platform-specific WebView handling for mobile, macOS, Windows, and Linux platforms.

Enhancements:

  • Deprecate the 'javascript_enabled' property in favor of 'enable_javascript' for better clarity and future-proofing.
  • Refactor platform detection logic to use a new 'platform.dart' utility, improving code readability and maintainability.

Build:

  • Update dependencies in 'pubspec.yaml' to include new webview-related packages and update existing ones.

Tests:

  • Remove unused 'pytest' imports from various test files, streamlining the test setup.

Copy link
Contributor

sourcery-ai bot commented Sep 20, 2024

Reviewer's Guide by Sourcery

This pull request introduces significant enhancements to the WebView component in the Flet framework, improving its functionality across different platforms (mobile, web, and desktop). The changes include new features, better platform support, and code refactoring for improved maintainability.

File-Level Changes

Change Details Files
Enhanced WebView functionality with new methods and events
  • Added new methods like reload(), can_go_back(), can_go_forward(), go_back(), go_forward()
  • Implemented new events such as on_progress, on_url_change, on_scroll, on_console_message
  • Added support for JavaScript execution and HTML loading
  • Implemented methods for cache and local storage clearing
sdk/python/packages/flet-core/src/flet_core/webview.py
Improved cross-platform support for WebView
  • Created separate implementations for mobile/macOS, web, and Windows/Linux platforms
  • Implemented platform-specific checks to use appropriate WebView implementation
  • Added new utility functions for platform detection
packages/flet_webview/lib/src/webview_mobile_and_mac.dart
packages/flet_webview/lib/src/webview_web.dart
packages/flet_webview/lib/src/webview_windows_and_linux.dart
packages/flet/lib/src/utils/platform.dart
Refactored existing code for better maintainability
  • Moved platform-specific utility functions from desktop.dart to platform.dart
  • Updated import statements across multiple files to use the new platform.dart
  • Refactored WebView control to use the new platform-specific implementations
packages/flet/lib/src/utils/desktop.dart
packages/flet/lib/src/controls/webview.dart
packages/flet/lib/src/controls/page.dart
packages/flet/lib/src/controls/grid_view.dart
packages/flet/lib/src/controls/list_view.dart
packages/flet/lib/src/controls/scrollable_control.dart
Updated dependencies and configurations
  • Added new WebView-related dependencies in pubspec.yaml
  • Updated iOS and macOS configurations for WebView support
packages/flet_webview/pubspec.yaml
client/ios/Runner/AppDelegate.swift
client/macos/Flutter/GeneratedPluginRegistrant.swift

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ndonkoHenri - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding more comprehensive error handling for WebView operations, especially for platform-specific implementations.
  • It might be beneficial to add some performance benchmarks or profiling for the new WebView features, particularly for mobile platforms.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

arguments={"url": url, "method": method.value},
)

def run_javascript(self, value: str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 suggestion (security): Add security warning for run_javascript method

Consider adding a prominent warning in the method documentation about the potential security risks of executing arbitrary JavaScript.

    def run_javascript(self, value: str):
        """
        Execute JavaScript in the WebView.

        WARNING: This method can pose significant security risks.
        Only use with trusted input to prevent potential
        cross-site scripting (XSS) attacks.
        """

@@ -337,6 +337,9 @@ def invoke_method(
assert (
self.__page
), f"{self.__class__.__qualname__} Control must be added to the page first"
if arguments:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider moving argument processing to a separate method

Extracting the argument processing logic to a separate method could improve readability and reusability, especially since it's duplicated in invoke_method_async.

    def _process_arguments(self, arguments):
        if arguments:
            return {k: str(v) for k, v in arguments.items() if v is not None}
        return {}

    if arguments:
        arguments = self._process_arguments(arguments)

sdk/python/packages/flet-core/src/flet_core/webview.py Outdated Show resolved Hide resolved
sdk/python/packages/flet-core/src/flet_core/webview.py Outdated Show resolved Hide resolved
sdk/python/packages/flet-core/src/flet_core/webview.py Outdated Show resolved Hide resolved
@Muddassir-Farooq-official
Copy link

Muddassir-Farooq-official commented Sep 28, 2024

Kindly merge and one more thing add permission in manifest for Android to display local html files in web view

@Muddassir-Farooq-official

This comment has been minimized.

@Muddassir-Farooq-official

This comment was marked as spam.

client/pubspec.lock Outdated Show resolved Hide resolved
@kalatawei
Copy link

Is there any chance for supporting WebView CookieManager ?

@cayubweeums
Copy link

This looks great! Can't wait to implement this in my project once it gets merged to main!!

@ndonkoHenri
Copy link
Contributor Author

@sourcery-ai review

@flet-dev flet-dev deleted a comment from sourcery-ai bot Nov 7, 2024
@Muddassir-Farooq-official

This comment was marked as spam.

@FeodorFitsner FeodorFitsner merged commit 20216b0 into main Nov 8, 2024
2 checks passed
@FeodorFitsner FeodorFitsner deleted the webview-enhancement branch November 8, 2024 19:14
@Muddassir-Farooq-official

Have documentation updated in flet docs?

@ndonkoHenri
Copy link
Contributor Author

Working on it.

@Benitmulindwa
Copy link

Good stuff @ndonkoHenri 😎! But i have some questions, are we going to get the run_javascripts() methods support on other platforms soon? I thought this PR would add WV support for the windows,

@ndonkoHenri
Copy link
Contributor Author

Windows and Linux are not yet supported. Will have a look at them after we release 0.25.0.

@Benitmulindwa
Copy link

Benitmulindwa commented Nov 11, 2024

Would be great to have the run_javascripts() method on Web too

Edit: Am not able to update the url value when running on web. Is it a bug or that’s how it’s supposed to be?

@Benitmulindwa
Copy link

Am able to change the url value outside of a function,… but once am inside a function/eventhandler an error is shown saying that the method is supported on Android, iOS and macOS only.

@ndonkoHenri
Copy link
Contributor Author

@Benitmulindwa let's continue this in a new discussion. Can you create one and explain the issues you face?

@Benitmulindwa
Copy link

@ndonkoHenri all right lemme do it

@Benitmulindwa
Copy link

#4356

@icstos
Copy link

icstos commented Nov 29, 2024

On Windows, an error is reported: Webview is not yet supported on this Platform.

@ndonkoHenri
Copy link
Contributor Author

Windows and Linux support is still to be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants