-
Notifications
You must be signed in to change notification settings - Fork 148
feat(browser): Dioxus Tabs #412
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| .about-newtab { | ||
| height: 100%; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| background: #f0f0f0; | ||
| } | ||
| .about-newtab .container { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| gap: 24px; | ||
| } | ||
| .about-newtab img { | ||
| width: 160px; | ||
| height: 160px; | ||
| } | ||
| .about-newtab .search-input { | ||
| width: 320px; | ||
| padding: 10px 14px; | ||
| font-size: 16px; | ||
| border: 1px solid #ccc; | ||
| border-radius: 8px; | ||
| outline: none; | ||
| } | ||
| .about-newtab .search-input:focus { | ||
| border-color: #5e9ed6; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| .about-stub { | ||
| height: 100%; | ||
| background: #f0f0f0; | ||
| color: #333; | ||
| padding: 40px; | ||
| } | ||
| .about-stub h1 { | ||
| margin: 0 0 8px; | ||
| font-size: 28px; | ||
| } | ||
| .about-stub p { | ||
| color: #888; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| use blitz_traits::net::{Request, Url}; | ||
| use dioxus_native::prelude::*; | ||
|
|
||
| use crate::nav::{is_enter_key, req_from_string}; | ||
|
|
||
| const NEWTAB_CSS: Asset = asset!("../assets/about-newtab.css"); | ||
| const STUB_CSS: Asset = asset!("../assets/about-stub.css"); | ||
| const BLITZ_LOGO: Asset = asset!("../assets/blitz-logo.png"); | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| pub enum AboutPage { | ||
| NewTab, | ||
| Settings, | ||
| History, | ||
| Bookmarks, | ||
| } | ||
|
|
||
| impl AboutPage { | ||
| pub fn from_url(url: &Url) -> Option<Self> { | ||
| if url.scheme() != "about" { | ||
| return None; | ||
| } | ||
| match url.path() { | ||
| "newtab" | "" => Some(Self::NewTab), | ||
| "settings" => Some(Self::Settings), | ||
| "history" => Some(Self::History), | ||
| "bookmarks" => Some(Self::Bookmarks), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub const fn title(self) -> &'static str { | ||
| match self { | ||
| Self::NewTab => "New Tab", | ||
| Self::Settings => "Settings", | ||
| Self::History => "History", | ||
| Self::Bookmarks => "Bookmarks", | ||
| } | ||
| } | ||
|
|
||
| pub const fn url_str(self) -> &'static str { | ||
| match self { | ||
| Self::NewTab => "about:newtab", | ||
| Self::Settings => "about:settings", | ||
| Self::History => "about:history", | ||
| Self::Bookmarks => "about:bookmarks", | ||
| } | ||
| } | ||
|
|
||
| #[allow( | ||
| clippy::unwrap_used, | ||
| reason = "URL strings are static literals — Url::parse cannot fail" | ||
| )] | ||
| pub fn parsed_url(self) -> Url { | ||
| Url::parse(self.url_str()).unwrap() | ||
| } | ||
| } | ||
|
|
||
| #[component] | ||
| pub fn AboutPageView(page: AboutPage, on_navigate: Callback<Request>) -> Element { | ||
| match page { | ||
| AboutPage::NewTab => rsx!(NewTabPage { on_navigate }), | ||
| AboutPage::Settings => rsx!(StubPage { name: "Settings" }), | ||
| AboutPage::History => rsx!(StubPage { name: "History" }), | ||
| AboutPage::Bookmarks => rsx!(StubPage { name: "Bookmarks" }), | ||
| } | ||
| } | ||
|
|
||
| #[component] | ||
| fn NewTabPage(on_navigate: Callback<Request>) -> Element { | ||
| let mut query = use_signal(String::new); | ||
| rsx! { | ||
| document::Link { rel: "stylesheet", href: NEWTAB_CSS } | ||
| div { class: "about-newtab", | ||
| div { class: "container", | ||
| img { src: BLITZ_LOGO, alt: "Blitz" } | ||
| input { | ||
| class: "search-input", | ||
| r#type: "text", | ||
| name: "q", | ||
| autofocus: true, | ||
| value: "{query}", | ||
| oninput: move |evt| query.set(evt.value()), | ||
| onkeydown: move |evt| { | ||
| if is_enter_key(&evt.key()) { | ||
| evt.prevent_default(); | ||
| let q = query.read().clone(); | ||
| if !q.is_empty() { | ||
| if let Some(req) = req_from_string(&q) { | ||
| on_navigate(req); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[component] | ||
| fn StubPage(name: &'static str) -> Element { | ||
| rsx! { | ||
| document::Link { rel: "stylesheet", href: STUB_CSS } | ||
| div { class: "about-stub", | ||
| h1 { "{name}" } | ||
| p { "Coming soon." } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn parses_known_paths() { | ||
| for (path, expected) in [ | ||
| ("about:newtab", AboutPage::NewTab), | ||
| ("about:settings", AboutPage::Settings), | ||
| ("about:history", AboutPage::History), | ||
| ("about:bookmarks", AboutPage::Bookmarks), | ||
| ] { | ||
| let url = Url::parse(path).unwrap(); | ||
| assert_eq!(AboutPage::from_url(&url), Some(expected), "{path}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn rejects_unknown() { | ||
| assert_eq!( | ||
| AboutPage::from_url(&Url::parse("about:nope").unwrap()), | ||
| None | ||
| ); | ||
| assert_eq!( | ||
| AboutPage::from_url(&Url::parse("https://example.com").unwrap()), | ||
| None | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn url_roundtrip() { | ||
| for p in [ | ||
| AboutPage::NewTab, | ||
| AboutPage::Settings, | ||
| AboutPage::History, | ||
| AboutPage::Bookmarks, | ||
| ] { | ||
| assert_eq!(AboutPage::from_url(&p.parsed_url()), Some(p)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| use blitz_traits::navigation::NavigationOptions; | ||
| use blitz_traits::net::{Body, Entry, EntryValue, FormData, Method, Request, Url}; | ||
| use dioxus_native::prelude::Key; | ||
|
|
||
| pub fn req_from_string(url_s: &str) -> Option<Request> { | ||
| if let Ok(url) = Url::parse(url_s) { | ||
| return Some(Request::get(url)); | ||
| }; | ||
|
|
||
| let contains_space = url_s.contains(' '); | ||
| let contains_dot = url_s.contains('.'); | ||
| if contains_dot && !contains_space { | ||
| if let Ok(url) = Url::parse(&format!("https://{}", &url_s)) { | ||
| return Some(Request::get(url)); | ||
| } | ||
| } | ||
|
|
||
| Some(synthesize_duckduckgo_search_req(url_s)) | ||
| } | ||
|
|
||
| fn synthesize_duckduckgo_search_req(query: &str) -> Request { | ||
| NavigationOptions::new( | ||
| Url::parse("https://html.duckduckgo.com/html/").unwrap(), | ||
| Some(String::from("application/x-www-form-urlencoded")), | ||
| 0, | ||
| ) | ||
| .set_method(Method::POST) | ||
| .set_document_resource(Body::Form(FormData(vec![Entry { | ||
| name: String::from("q"), | ||
| value: EntryValue::String(query.to_string()), | ||
| }]))) | ||
| .into_request() | ||
| } | ||
|
|
||
| pub fn open_in_external_browser(req: &Request) { | ||
| if req.method == Method::GET && matches!(req.url.scheme(), "http" | "https" | "mailto") { | ||
| if let Err(err) = webbrowser::open(req.url.as_str()) { | ||
| tracing::error!("Failed to open URL: {}", err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn is_enter_key(key: &Key) -> bool { | ||
| matches!(key, Key::Enter) || matches!(key, Key::Character(s) if s == "\n") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
try_from_urlwould probably be a more idiomatic name as this is fallible and returns option.