-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
73 lines (64 loc) · 2.51 KB
/
main.rs
File metadata and controls
73 lines (64 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::{webview::NewWindowResponse, WebviewUrl, WebviewWindowBuilder};
use url::Url;
const APP_TITLE: &str = "misskey-tauri";
const MISSKEY_URL: &str = "https://misskey.io";
const MISSKEY_SCHEME: &str = "https";
const MISSKEY_HOST: &str = "misskey.io";
fn is_allowed_in_webview(url: &Url) -> bool {
url.scheme() == MISSKEY_SCHEME && url.host_str() == Some(MISSKEY_HOST)
}
fn is_http_or_https(url: &Url) -> bool {
matches!(url.scheme(), "http" | "https")
}
fn open_external_in_browser(url: &Url) {
if let Err(error) = open::that(url.as_str()) {
eprintln!("failed to open external url in browser: {url} ({error})");
}
}
fn main() {
tauri::Builder::default()
.setup(|app| {
let misskey_url = Url::parse(MISSKEY_URL).expect("misskey.io URL must be valid");
WebviewWindowBuilder::new(app, "main", WebviewUrl::External(misskey_url))
.title(APP_TITLE)
.inner_size(1320.0, 860.0)
.min_inner_size(920.0, 640.0)
.resizable(true)
.on_document_title_changed(|window, title| {
let trimmed = title.trim();
let next_title = if trimmed.is_empty() {
APP_TITLE.to_string()
} else {
trimmed.to_string()
};
if let Err(error) = window.set_title(&next_title) {
eprintln!("failed to update window title: {error}");
}
})
.on_navigation(|url| {
if is_allowed_in_webview(url) {
true
} else if is_http_or_https(url) {
open_external_in_browser(url);
false
} else {
false
}
})
.on_new_window(|url, _features| {
if is_allowed_in_webview(&url) {
NewWindowResponse::Allow
} else if is_http_or_https(&url) {
open_external_in_browser(&url);
NewWindowResponse::Deny
} else {
NewWindowResponse::Deny
}
})
.build()?;
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}