Skip to content

Commit

Permalink
improve desktop_webview_window on macos (#14)
Browse files Browse the repository at this point in the history
* implement addScriptToExecuteOnDocumentCreated on macos

* macos release webview on destroy

* short key to close window on macos

* fix window dll files
  • Loading branch information
boyan01 authored Oct 22, 2021
1 parent 2052556 commit 3bd7392
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 9 deletions.
13 changes: 13 additions & 0 deletions packages/desktop_webview_window/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 0.0.4 (2021/10/22)

1. implement `addScriptToExecuteOnDocumentCreated` on macOS.
2. add hot key `command + w` to close window.

## 0.0.3 (2021/10/22)

fix linux build

## 0.0.2

* rename project to desktop_webview_window

## 0.0.1

* add Windows, Linux, macOS support.
2 changes: 1 addition & 1 deletion packages/desktop_webview_window/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.0.4"
fake_async:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop_webview_window/lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ abstract class Webview {
/// available: macOS (Brightness.dark only 10.14+)
void setBrightness(Brightness? brightness);

/// available: Windows, Linux
/// available: Windows, Linux, macOS
void addScriptToExecuteOnDocumentCreated(String javaScript);
}
2 changes: 1 addition & 1 deletion packages/desktop_webview_window/lib/src/webview_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class WebviewImpl extends Webview {

@override
void addScriptToExecuteOnDocumentCreated(String javaScript) {
if (!(Platform.isWindows || Platform.isLinux)) {
if (!(Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
return;
}
assert(javaScript.trim().isNotEmpty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ public class DesktopWebviewWindowPlugin: NSObject, FlutterPlugin {
wc.setAppearance(brightness: brightness)
result(nil)
break
case "addScriptToExecuteOnDocumentCreated":
guard let argument = call.arguments as? [String: Any?] else {
result(FlutterError(code: "0", message: "arg is not map", details: nil))
return
}
guard let viewId = argument["viewId"] as? Int64 else {
result(FlutterError(code: "0", message: "param viewId not found", details: nil))
return
}
guard let wc = webviews[viewId] else {
result(FlutterError(code: "0", message: "can not find webview for id: \(viewId)", details: nil))
return
}
guard let javaScript = argument["javaScript"] as? String else {
result(FlutterError(code: "0", message: "param javaScript not found", details: nil))
return
}
wc.addScriptToExecuteOnDocumentCreated(javaScript: javaScript)
result(nil)
break
default:
result(FlutterMethodNotImplemented)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class WebviewWindowController: NSWindowController {

window?.setContentSize(NSSize(width: width, height: height))
window?.center()

window?.title = initialTitle

webview.navigationDelegate = self
Expand All @@ -61,6 +61,12 @@ class WebviewWindowController: NSWindowController {
webview.configuration.allowsAirPlayForMediaPlayback = true
webview.configuration.mediaTypesRequiringUserActionForPlayback = .video
}

override func keyDown(with event: NSEvent) {
if event.charactersIgnoringModifiers == "w" && event.modifierFlags.contains(.command) {
close()
}
}

func load(url: URL) {
webview.load(URLRequest(url: url))
Expand All @@ -79,6 +85,8 @@ class WebviewWindowController: NSWindowController {
}

func destroy() {
window?.delegate = nil

webview.removeFromSuperview()
webview.uiDelegate = nil
webview.navigationDelegate = nil
Expand All @@ -87,6 +95,8 @@ class WebviewWindowController: NSWindowController {
}

webview.configuration.userContentController.removeAllUserScripts()

webview = nil
}

func setAppearance(brightness: Int) {
Expand All @@ -107,6 +117,11 @@ class WebviewWindowController: NSWindowController {
}
}

func addScriptToExecuteOnDocumentCreated(javaScript: String) {
webview.configuration.userContentController.addUserScript(
WKUserScript(source: javaScript, injectionTime: .atDocumentStart, forMainFrameOnly: true))
}

deinit {
#if DEBUG
print("\(self) deinited")
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop_webview_window/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: desktop_webview_window
description: Show a webview window on your flutter deksktop application.
version: 0.0.1
version: 0.0.4
homepage: https://github.com/MixinNetwork/flutter-plugins/tree/main/packages/desktop_webview_window

environment:
Expand Down
Empty file modified packages/desktop_webview_window/run_local_test_server.sh
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions packages/desktop_webview_window/windows/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ flutter/
*.sln.docstates

# Visual Studio build-related files.
x64/
x86/

!libs/x64
#x64/
#x86/
#
#!libs/x64

# Visual Studio cache files
# files ending in .cache can be ignored
Expand Down

0 comments on commit 3bd7392

Please sign in to comment.