Skip to content

Commit

Permalink
Fixed lots of bugs & QoL
Browse files Browse the repository at this point in the history
  • Loading branch information
YomoSK committed Nov 9, 2024
1 parent 77bf38b commit 30cab5b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 75 deletions.
10 changes: 10 additions & 0 deletions rust-src/components/home/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

</body>
</html>
34 changes: 0 additions & 34 deletions rust-src/components/script.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
<div id="tab-title" style="display: none;"></div>
</div>

<input
id="searchbar"
type="url"
placeholder="Search in the universe..."
autocomplete="off"
autofocus
/>
<div id="searchbar">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path fill-rule="evenodd" d="M12 1.5a5.25 5.25 0 0 0-5.25 5.25v3a3 3 0 0 0-3 3v6.75a3 3 0 0 0 3 3h10.5a3 3 0 0 0 3-3v-6.75a3 3 0 0 0-3-3v-3c0-2.9-2.35-5.25-5.25-5.25Zm3.75 8.25v-3a3.75 3.75 0 1 0-7.5 0v3h7.5Z" clip-rule="evenodd" />
</svg>
<input
type="url"
placeholder="Search in the universe..."
autocomplete="off"
autofocus
/>
</div>

<div id="controls">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
Expand Down
64 changes: 64 additions & 0 deletions rust-src/components/topbar/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { invoke } = window.__TAURI__.core;
const { listen } = window.__TAURI__.event;

const title = document.getElementById('tab-title');
const searchbar = document.querySelector('#searchbar>input');

function subpayload(payload, length = 20) {
if(payload.length > length) payload = payload.substring(0, length) + '...';
return payload;
}

document.addEventListener('DOMContentLoaded', () => {
// invoke('get_title').then(t => {
// title.innerText = t;
// if(t) title.style.display = null;
// });
localStorage.clear();

searchbar.addEventListener('keydown', ({ target, key }) => {
const url = target.value.trim();
if(key.toUpperCase() == 'ENTER' && url != 'about:blank') {
target.blur();
invoke('load_url', {
url: url.includes(' ') ? 'https://google.com/search?q=' + url.split(' ').join('+') : url.includes('://') ? url : 'https://' + url
});
}
});

searchbar.addEventListener('focus', ({ target }) => {
if(target.value) target.value = localStorage.getItem('origin_url') || target.value;
});

searchbar.addEventListener('blur', ({ target }) => {
target.value = localStorage.getItem('pretty_url') || target.value;
});

document.querySelector('#controls>svg').addEventListener('click', () => {
invoke('close');
});

window.addEventListener('contextmenu', event => event.preventDefault());
});

listen('title_change', ({ payload }) => {
if(payload) {
title.innerText = subpayload(payload);
title.style.display = null;
}
else title.style.display = 'none';
});

listen('url_change', ({ payload }) => {
const url = payload.endsWith('/') ? payload.substring(0, payload.length - 1) : payload;
if(payload && url != 'about:blank') {
const isHTTPS = url.startsWith('https://');

document.querySelector('#searchbar>svg').style.display = isHTTPS ? 'block' : null;
searchbar.style.paddingLeft = isHTTPS ? 'calc(2*.4rem + 20px)' : null;

localStorage.setItem('origin_url', url);
searchbar.value = (isHTTPS ? url.split('://')[1] : url).replace('www.', '');
localStorage.setItem('pretty_url', searchbar.value);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ body::before {
top: 0;
left: 0;
width: 100%;
height: 100%;
height: 60px;
background: #E3F4FE;
opacity: .75;
z-index: -1;
Expand Down Expand Up @@ -45,7 +45,7 @@ nav > #tabs > svg, nav > #controls > svg {
cursor: pointer;
}

nav > #tabs > div {
nav > #tabs > #tab-title {
padding: 0 .5rem;
background: #E2E5E8;
border-radius: 8px;
Expand All @@ -54,10 +54,26 @@ nav > #tabs > div {
display: flex;
justify-content: center;
align-items: center;
text-wrap: nowrap;
}

nav > #searchbar {
position: relative;
height: -webkit-fill-available;
}

nav > input#searchbar {
nav > #searchbar > svg {
position: absolute;
top: 50%;
left: .4rem;
transform: translateY(-50%);
width: 20px;
display: none;
}

nav > #searchbar > input {
padding: .4rem 1rem;
width: 100%;
height: -webkit-fill-available;
background: #E2E5E8;
border: none;
Expand Down
53 changes: 22 additions & 31 deletions rust-src/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ use isahc::ReadResponseExt;
use tauri::{AppHandle, Builder, Emitter, LogicalPosition, LogicalSize, Manager, WebviewBuilder, WebviewUrl, WindowBuilder};
use serde_json::Value;

pub struct _Yomea {
pub url: &'static str
}

pub static YOMEA: _Yomea = _Yomea {
url: "about:blank"
};

pub fn get_title_url(url: String, followredirects: bool) -> Result<String, u16> {
pub fn get_title_url(url: &str, followredirects: bool) -> Result<String, u16> {
let mut http = match isahc::get(url) {
Ok(res) => res,
Err(_) => {
Expand All @@ -24,7 +16,7 @@ pub fn get_title_url(url: String, followredirects: bool) -> Result<String, u16>
if status.is_redirection() && followredirects {
if let Some(location) = http.headers().get("Location") {
if let Ok(redirect) = location.to_str() {
return Ok(get_title_url(redirect.to_string(), true).unwrap());
return Ok(get_title_url(redirect, true).unwrap());
}
}
}
Expand All @@ -45,28 +37,17 @@ pub fn get_title_url(url: String, followredirects: bool) -> Result<String, u16>
}

#[tauri::command]
fn get_title() -> String {
let url = YOMEA.url.to_string();
if url == "about:blank" {
return "".to_string();
}

match get_title_url(url, true) {
Ok(title) => title,
Err(code) => code.to_string()
}
}

#[tauri::command]
fn load_url(app: AppHandle, url: String) {
fn load_url(app: AppHandle, url: &str) {
let topbar = app.get_webview("topbar").unwrap();
let mut webview = app.get_webview("webview").unwrap();
// TODO: Basically this requests the site twice:
// 1. window.location.href
// 2. get_title_url()
webview.navigate(url.parse().unwrap()).unwrap();

let title = get_title_url(url, true).unwrap_or("".to_string());
topbar.emit("title_change", title).unwrap();

// if title.is_err() {
// topbar.emit("title_change", title.err());
// }
Expand All @@ -77,7 +58,6 @@ fn load_url(app: AppHandle, url: String) {
// Ok(title) => topbar.emit("title_change", title),
// Err(code) => topbar.emit("title_change", "code")
// }
topbar.emit("title_change", title).unwrap();
}

#[tauri::command]
Expand All @@ -90,9 +70,10 @@ pub fn run() {
let size = serde_json::json!({ "width": 1400, "height": 800 });

Builder::default()
.invoke_handler(tauri::generate_handler![get_title, load_url, close])
.invoke_handler(tauri::generate_handler![load_url, close])
.setup(move |app| {
let topbarcomponent = "topbar.html".into();
let topbarcomponent = "topbar/index.html".into();
let homecomponent = "home/index.html".into();

let width = size.get("width").and_then(Value::as_f64).unwrap();
let height = size.get("height").and_then(Value::as_f64).unwrap();
Expand All @@ -112,11 +93,21 @@ pub fn run() {
let handle = app.app_handle().clone();
let webview = WebviewBuilder::new(
"webview",
WebviewUrl::External(YOMEA.url.parse().unwrap())
WebviewUrl::App(homecomponent)
).on_navigation(move |url| {
let topbar = handle.get_webview("topbar").unwrap();
topbar.emit("url_change", url.to_string()).unwrap();
topbar.emit("title_change", get_title_url(url.to_string(), false).unwrap_or("".to_string())).unwrap();
let strurl = url.to_string();

if !strurl.starts_with("http") {
println!("Blocked access for {}", strurl);
return false;
}

if !strurl.contains("tauri.localhost") && !strurl.contains("127.0.0.1:1430") {
topbar.emit("url_change", strurl).unwrap();
topbar.emit("title_change", get_title_url(url.as_str(), false).unwrap_or("".to_string())).unwrap();
}

true
});

Expand Down

0 comments on commit 30cab5b

Please sign in to comment.