diff --git a/Package.swift b/Package.swift index 858fbb2..002f85c 100644 --- a/Package.swift +++ b/Package.swift @@ -1,10 +1,10 @@ -// swift-tools-version: 5.10 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( - name: "adwitaweb", + name: "AdwaitaWebViewSwift", platforms: [ .macOS(.v10_15) ], diff --git a/README.md b/README.md index 8703ce4..fc7c898 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ struct ContentView: View { } ``` -Take a look at the simple [sample app](Tests/program.swift). +Take a look at the simple [sample app](Tests/main.swift). Check out the [Adwaita](https://github.com/AparokshaUI/Adwaita) packages. ## Thanks diff --git a/Sources/WebView/WebView.swift b/Sources/WebView/WebView.swift index a8be216..479abee 100644 --- a/Sources/WebView/WebView.swift +++ b/Sources/WebView/WebView.swift @@ -1,7 +1,8 @@ -import Adwaita +import Meta import CWebView +import Foundation -public struct WebView: Widget { +public struct WebView: AdwaitaWidget { @Binding var url: String var width: Int = 800 var height: Int = 600 @@ -10,17 +11,19 @@ public struct WebView: Widget { self._url = url } - public func container(modifiers: [(View) -> View]) -> ViewStorage { - let webView = ViewStorage(webkit_web_view_new()?.opaque()) - update(webView, modifiers: modifiers, updateProperties: true) - return webView + public func container(data: WidgetData, type: Data.Type) -> ViewStorage where Data: ViewRenderData { + let storage = ViewStorage(webkit_web_view_new()?.opaque()) + update(storage, data: data, updateProperties: true, type: type) + return storage } - public func update(_ storage: ViewStorage, modifiers: [(View) -> View], updateProperties: Bool) { - if updateProperties { - webkit_web_view_load_uri(storage.pointer?.cast(), url) - gtk_widget_set_size_request(storage.pointer?.cast(), width.cInt, height.cInt) - } + public func update(_ storage: ViewStorage, data: WidgetData, updateProperties: Bool, type: Data.Type) where Data: ViewRenderData { + guard updateProperties else { return } + + webkit_web_view_load_uri(storage.opaquePointer?.cast(), url) + gtk_widget_set_size_request(storage.opaquePointer?.cast(), Int32(width), Int32(height)) + + storage.previousState = self } public func setSize(width: Int, height: Int) -> Self { @@ -29,4 +32,4 @@ public struct WebView: Widget { view.height = height return view } -} +} \ No newline at end of file diff --git a/Tests/main.swift b/Tests/main.swift new file mode 100644 index 0000000..3273c5c --- /dev/null +++ b/Tests/main.swift @@ -0,0 +1,19 @@ +import Adwaita +import WebView + +@main +struct Test: App{ + let id = "xyz.zaph.webview" + var app: AdwaitaApp! + + @State private var url: String = "https://github.com/AparokshaUI/adwaita-swift" + + var scene: Scene { + Window(id: "main"){ window in + WebView(url: $url) + .setSize(width: 800, height: 600) + + } + } +} + diff --git a/Tests/program.swift b/Tests/program.swift deleted file mode 100644 index 2075e6a..0000000 --- a/Tests/program.swift +++ /dev/null @@ -1,64 +0,0 @@ -import Adwaita -import WebView - -@main -struct Test: App{ - let id = "xyz.zaph.webview" - var app: GTUIApp! - - @State private var url: String = "https://google.com" - - var scene: Scene { - Window(id: "main"){ window in - WebView(url: $url) - .setSize(width: 800, height: 600) - .topToolbar { - ToolbarView(app: app, window: window) - } - .onAppear { - print("Loading url: \(url)") - } - - } - } -} - - -struct ToolbarView: View { - - @State private var about = false - var app: GTUIApp - var window: GTUIApplicationWindow - - var view: Body { - HeaderBar.end { - Menu(icon: .default(icon: .openMenu), app: app, window: window) { - MenuButton("New Window", window: false) { - app.addWindow("main") - } - .keyboardShortcut("n".ctrl()) - MenuButton("Close Window") { - window.close() - } - .keyboardShortcut("w".ctrl()) - MenuSection { - MenuButton("About me", window: false) { - about = true - } - } - } - .primary() - .tooltip("Main Menu") - .aboutDialog( - visible: $about, - app: "WebViewAdwaita", - developer: "zaph", - version: "dev", - icon: .custom(name: "xyz.zaph.webview"), - website: .init(string: "https://google.com")!, - issues: .init(string: "https://google.com")! - ) - } - } - -}