-
-
Notifications
You must be signed in to change notification settings - Fork 90
library: Add WebView entry #299
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Gtk 4.0; | ||
| using Adw 1; | ||
|
|
||
| Adw.Clamp { | ||
| maximum-size: 700; | ||
|
|
||
| Box { | ||
| orientation: vertical; | ||
|
|
||
| Box { | ||
| orientation: vertical; | ||
| margin-top: 12; | ||
| margin-bottom: 12; | ||
| spacing: 6; | ||
|
|
||
| Label { | ||
| label: "Web View"; | ||
|
|
||
| styles ["title-1"] | ||
| } | ||
|
|
||
| Label { | ||
| label: "Load and display webpages and HTML"; | ||
| } | ||
| } | ||
|
|
||
| Box controls { | ||
| spacing: 6; | ||
|
|
||
| Button button_back { | ||
| icon-name: "arrow1-left-symbolic"; | ||
| tooltip-text: _("Back"); | ||
| } | ||
|
|
||
| Button button_forward { | ||
| icon-name: "arrow1-right-symbolic"; | ||
| tooltip-text: _("Forward"); | ||
| } | ||
|
|
||
| Entry url_bar { | ||
| input-purpose: url; | ||
| hexpand: true; | ||
| } | ||
|
|
||
| Button button_reload { | ||
| icon-name: "refresh-symbolic"; | ||
| tooltip-text: _("Reload"); | ||
| } | ||
|
|
||
| Button button_stop { | ||
| icon-name: "stop-sign-symbolic"; | ||
| tooltip-text: _("Stop"); | ||
| } | ||
| } | ||
|
|
||
| Adw.Bin container { | ||
| margin-top: 18; | ||
| margin-bottom: 18; | ||
| vexpand: true; | ||
| hexpand: true; | ||
| styles ["view"] | ||
| } | ||
|
|
||
| LinkButton { | ||
| margin-bottom: 12; | ||
| label: "API Reference"; | ||
| uri: "https://webkitgtk.org/reference/webkit2gtk/stable/class.WebView.html"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import WebKit from "gi://WebKit?version=6.0"; | ||
| import GObject from "gi://GObject"; | ||
| import GLib from "gi://GLib"; | ||
|
|
||
| const container = workbench.builder.get_object("container"); | ||
| const button_back = workbench.builder.get_object("button_back"); | ||
| const button_forward = workbench.builder.get_object("button_forward"); | ||
| const button_reload = workbench.builder.get_object("button_reload"); | ||
| const button_stop = workbench.builder.get_object("button_stop"); | ||
| const url_bar = workbench.builder.get_object("url_bar"); | ||
| const web_view = new WebKit.WebView({ | ||
| zoom_level: 0.8, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any particular reason?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh it was because you couldn't really see much of the webpage because the view is small, so I decreased the zoom level. It also gave me a reason to show the property.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting find. I can confirm it's the same with |
||
| }); | ||
| container.child = web_view; | ||
|
|
||
| // URL bar displays the current loaded page | ||
| web_view.bind_property( | ||
| "uri", | ||
| url_bar.buffer, | ||
| "text", | ||
| GObject.BindingFlags.DEFAULT, | ||
| ); | ||
|
|
||
| web_view.load_uri("https://www.gnome.org/"); | ||
|
|
||
| url_bar.connect("activate", () => { | ||
| let url = url_bar.buffer.text; | ||
| const scheme = GLib.Uri.peek_scheme(url); | ||
| if (scheme == null) { | ||
| url = `http://${url}`; | ||
| } | ||
| web_view.load_uri(url); | ||
| }); | ||
|
|
||
| button_forward.connect("clicked", () => { | ||
| web_view.go_forward(); | ||
| }); | ||
|
|
||
| button_back.connect("clicked", () => { | ||
| web_view.go_back(); | ||
| }); | ||
|
|
||
| button_reload.connect("clicked", () => { | ||
| web_view.reload(); | ||
| }); | ||
|
|
||
| button_stop.connect("clicked", () => { | ||
| web_view.stop_loading(); | ||
| }); | ||
|
|
||
| web_view.connect("load-changed", (view, load_event) => { | ||
| switch (load_event) { | ||
| case WebKit.LoadEvent.STARTED: | ||
| console.log("Page loading started"); | ||
| break; | ||
| case WebKit.LoadEvent.FINISHED: | ||
| console.log("Page loading has finished "); | ||
| break; | ||
| } | ||
| }); | ||
|
|
||
| web_view.connect("load-failed", (view, load_event, fail_url, error) => { | ||
| // Dont display error page if it is caused by stop_loading() | ||
| if (error.code !== WebKit.NetworkError.CANCELLED) { | ||
| web_view.load_alternate_html( | ||
| error_page(fail_url, error.message), | ||
| fail_url, | ||
| null, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| web_view.connect("notify::estimated-load-progress", () => { | ||
| url_bar.progress_fraction = web_view.estimated_load_progress; | ||
| if (url_bar.progress_fraction === 1) { | ||
| setTimeout(() => { | ||
| url_bar.progress_fraction = 0; | ||
| }, 500); | ||
| } | ||
| }); | ||
|
|
||
| function error_page(fail_url, msg) { | ||
| const error = ` | ||
| <div style="text-align:center; margin:24px;"> | ||
| <h2>An error occurred while loading ${fail_url}</h2> | ||
| <p>${msg}</p> | ||
| </div> | ||
| `; | ||
| return error; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "Web View", | ||
| "category": "network", | ||
| "description": "Load and display webpages and HTML", | ||
| "panels": ["code", "preview"], | ||
| "autorun": true | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.