create-basicjs 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/bin/create-basicjs.js +49 -4
- package/package.json +1 -1
- package/payload/projects/tauri/.github/workflows/build.yml +86 -0
- package/payload/projects/tauri/.github/workflows/tests.yml +87 -0
- package/payload/projects/tauri/AGENTS.md +128 -0
- package/payload/projects/tauri/CLAUDE.md +1 -0
- package/payload/projects/tauri/LICENSE +201 -0
- package/payload/projects/tauri/NOTICE +8 -0
- package/payload/projects/tauri/README.md +269 -0
- package/payload/projects/tauri/checks/window-check.js +189 -0
- package/payload/projects/tauri/gitignore +8 -0
- package/payload/projects/tauri/handbook/01-getting-started.md +188 -0
- package/payload/projects/tauri/handbook/02-the-project.md +264 -0
- package/payload/projects/tauri/handbook/03-the-page.md +260 -0
- package/payload/projects/tauri/handbook/04-commands.md +265 -0
- package/payload/projects/tauri/handbook/05-events-and-state.md +275 -0
- package/payload/projects/tauri/handbook/06-plugins.md +282 -0
- package/payload/projects/tauri/handbook/07-the-window-and-the-app.md +431 -0
- package/payload/projects/tauri/handbook/08-security.md +374 -0
- package/payload/projects/tauri/handbook/09-tests-and-checks.md +266 -0
- package/payload/projects/tauri/handbook/10-building-and-releasing.md +238 -0
- package/payload/projects/tauri/handbook/11-troubleshooting.md +259 -0
- package/payload/projects/tauri/handbook/README.md +125 -0
- package/payload/projects/tauri/handbook/turkce/01-getting-started.md +193 -0
- package/payload/projects/tauri/handbook/turkce/02-the-project.md +272 -0
- package/payload/projects/tauri/handbook/turkce/03-the-page.md +272 -0
- package/payload/projects/tauri/handbook/turkce/04-commands.md +271 -0
- package/payload/projects/tauri/handbook/turkce/05-events-and-state.md +284 -0
- package/payload/projects/tauri/handbook/turkce/06-plugins.md +286 -0
- package/payload/projects/tauri/handbook/turkce/07-the-window-and-the-app.md +433 -0
- package/payload/projects/tauri/handbook/turkce/08-security.md +384 -0
- package/payload/projects/tauri/handbook/turkce/09-tests-and-checks.md +275 -0
- package/payload/projects/tauri/handbook/turkce/10-building-and-releasing.md +241 -0
- package/payload/projects/tauri/handbook/turkce/11-troubleshooting.md +264 -0
- package/payload/projects/tauri/handbook/turkce/README.md +128 -0
- package/payload/projects/tauri/images/app.png +0 -0
- package/payload/projects/tauri/src-tauri/Cargo.lock +4389 -0
- package/payload/projects/tauri/src-tauri/Cargo.toml +31 -0
- package/payload/projects/tauri/src-tauri/app-icon.svg +16 -0
- package/payload/projects/tauri/src-tauri/build.rs +15 -0
- package/payload/projects/tauri/src-tauri/capabilities/default.json +7 -0
- package/payload/projects/tauri/src-tauri/gitignore +5 -0
- package/payload/projects/tauri/src-tauri/icons/128x128.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/128x128@2x.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/32x32.png +0 -0
- package/payload/projects/tauri/src-tauri/icons/icon.icns +0 -0
- package/payload/projects/tauri/src-tauri/icons/icon.ico +0 -0
- package/payload/projects/tauri/src-tauri/src/check.rs +144 -0
- package/payload/projects/tauri/src-tauri/src/files.rs +255 -0
- package/payload/projects/tauri/src-tauri/src/main.rs +45 -0
- package/payload/projects/tauri/src-tauri/src/sizes.rs +214 -0
- package/payload/projects/tauri/src-tauri/tauri.conf.json +44 -0
- package/payload/projects/tauri/src-tauri/tests/project.rs +243 -0
- package/payload/projects/tauri/ui/app.js +459 -0
- package/payload/projects/tauri/ui/backend.js +196 -0
- package/payload/projects/tauri/ui/basic/LICENSE +14 -0
- package/payload/projects/tauri/ui/basic/basic.min.css +1 -0
- package/payload/projects/tauri/ui/basic/basic.min.js +78 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/LICENSE.txt +206 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
- package/payload/projects/tauri/ui/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
- package/payload/projects/tauri/ui/basic/scroll-bar.min.js +3 -0
- package/payload/projects/tauri/ui/comp/_sources.txt +9 -0
- package/payload/projects/tauri/ui/comp/components.min.js +21 -0
- package/payload/projects/tauri/ui/index.html +28 -0
- package/payload/projects/tauri/ui/sizes.js +104 -0
- package/payload/starter/_basic/VERSION +1 -1
- package/payload/starter/_basic/comp/bottom-bar.js +31 -3
- package/payload/starter/_basic/comp/code-editor.js +219 -1
- package/payload/starter/_basic/comp/context-menu.js +6 -1
- package/payload/starter/_basic/comp/markdown-view.js +4 -2
- package/payload/starter/_basic/comp/phone-field.js +13 -5
- package/payload/starter/_basic/comp/property-panel.js +1 -1
- package/payload/starter/_basic/comp/slider.js +27 -12
- package/payload/starter/_basic/comp/smart-button.js +5 -1
- package/payload/starter/_basic/comp/smart-table.js +8 -1
- package/payload/starter/_basic/comp/stepper.js +29 -3
- package/payload/starter/_basic/comp/toggle.js +2 -1
- package/payload/starter/_basic/docs/component-index.md +53 -53
- package/payload/starter/_basic/examples/code-editor.htm +48 -1
- package/payload/starter/_basic/examples/gallery-styled.htm +896 -0
- package/payload/starter/_basic/examples/search-input.htm +1 -1
- package/payload/starter/_basic/examples/select-box-2-item-view.htm +304 -0
- package/payload/starter/_basic/examples/{slider-2-classic.htm → slider-2-split.htm} +36 -2
- package/payload/starter/_basic/examples/stepper-2.htm +22 -6
- package/payload/starter/_basic/min/bottom-bar.min.js +1 -1
- package/payload/starter/_basic/min/code-editor.min.js +1 -1
- package/payload/starter/_basic/min/context-menu.min.js +1 -1
- package/payload/starter/_basic/min/markdown-view.min.js +1 -1
- package/payload/starter/_basic/min/phone-field.min.js +1 -1
- package/payload/starter/_basic/min/slider.min.js +1 -1
- package/payload/starter/_basic/min/smart-button.min.js +1 -1
- package/payload/starter/_basic/min/smart-table.min.js +1 -1
- package/payload/starter/_basic/min/stepper.min.js +1 -1
- package/payload/starter/_basic/min/toggle.min.js +1 -1
- package/payload/starter/_basic/tools/check.js +13 -2
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "tauri-starter"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A desktop app: a Rust side and a basic.js interface"
|
|
5
|
+
authors = ["Bugra Ozden"]
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
edition = "2021"
|
|
8
|
+
# The oldest Rust that builds Tauri 2 of Cargo.lock: an older one says so, and "rustup update" brings a new one
|
|
9
|
+
rust-version = "1.90"
|
|
10
|
+
|
|
11
|
+
[build-dependencies]
|
|
12
|
+
tauri-build = { version = "2", features = [] }
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
tauri = { version = "2", features = [] }
|
|
16
|
+
serde = { version = "1", features = ["derive"] }
|
|
17
|
+
# The folder dialog of the system (the "Open..." button): added with "cargo tauri add dialog"
|
|
18
|
+
tauri-plugin-dialog = "2"
|
|
19
|
+
serde_json = { version = "1", optional = true }
|
|
20
|
+
|
|
21
|
+
[features]
|
|
22
|
+
# cargo run --features window-check: the page is played in its real window (src/check.rs, checks/window-check.js)
|
|
23
|
+
window-check = ["dep:serde_json"]
|
|
24
|
+
|
|
25
|
+
# A small app (the "App Size" guide of Tauri): the release build takes longer, the app is a few MB.
|
|
26
|
+
[profile.release]
|
|
27
|
+
codegen-units = 1
|
|
28
|
+
lto = true
|
|
29
|
+
opt-level = "s"
|
|
30
|
+
panic = "abort"
|
|
31
|
+
strip = true
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024" viewBox="0 0 1024 1024">
|
|
2
|
+
<!-- The icon of the app: the mark of basic.js (03-webpages/webpage-basicjs/assets/favicon.svg) on a light square
|
|
3
|
+
with round corners, in the 824 px square of the macOS icon grid. Every size: cargo tauri icon app-icon.svg -->
|
|
4
|
+
<rect x="100" y="100" width="824" height="824" rx="185" fill="#F4F5F2"/>
|
|
5
|
+
<rect x="100.5" y="100.5" width="823" height="823" rx="184.5" fill="none" stroke="rgba(0,0,0,0.12)" stroke-width="1"/>
|
|
6
|
+
<g transform="translate(512 512) scale(5.2) translate(-157 -163)">
|
|
7
|
+
<path d="M157 175.101V133L141.328 155.734L134.966 163.734L157 175.101Z" fill="#DCDCDC"/>
|
|
8
|
+
<path d="M129.881 192.783C149.595 193.196 161.124 192.917 182.424 192.783C173.34 186.251 167.897 182.339 157 175.101C145.945 181.959 139.995 185.775 129.881 192.783Z" fill="#DCDCDC"/>
|
|
9
|
+
<path d="M107 152.366L134.966 163.734L141.328 155.734C127.664 154.759 120.213 153.8 107 152.366Z" fill="#DCDCDC"/>
|
|
10
|
+
<path d="M179.034 163.734L207 152.366C193.318 153.892 185.717 154.665 172.672 155.734L179.034 163.734Z" fill="#DCDCDC"/>
|
|
11
|
+
<path d="M107 152.366L129.881 192.783C139.995 185.775 145.945 181.959 157 175.101L134.966 163.734L107 152.366Z" fill="#BCBCBC"/>
|
|
12
|
+
<path d="M157 175.101L179.034 163.734L172.672 155.734L157 133V175.101Z" fill="white"/>
|
|
13
|
+
<path d="M182.424 192.783L207 152.366L179.034 163.734L157 175.101C167.897 182.339 173.34 186.251 182.424 192.783Z" fill="white"/>
|
|
14
|
+
<path d="M141.328 155.734C127.664 154.759 120.213 153.8 107 152.366L129.881 192.783C149.595 193.196 161.124 192.917 182.424 192.783L207 152.366C193.318 153.892 185.717 154.665 172.672 155.734M107 152.366L134.966 163.734M129.881 192.783C139.995 185.775 145.945 181.959 157 175.101M182.424 192.783C173.34 186.251 167.897 182.339 157 175.101M207 152.366L179.034 163.734M157 175.101L179.034 163.734M157 175.101V133M157 175.101L134.966 163.734M134.966 163.734L141.328 155.734L157 133L172.672 155.734L179.034 163.734" fill="none" stroke="#404040" stroke-width="3" stroke-linejoin="round" stroke-linecap="round"/>
|
|
15
|
+
</g>
|
|
16
|
+
</svg>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// build.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// Runs before the app is compiled: Tauri reads tauri.conf.json and capabilities/ here.
|
|
5
|
+
//
|
|
6
|
+
// Started Date: November 2026
|
|
7
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
8
|
+
//
|
|
9
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
10
|
+
// Licensed under the Apache License, Version 2.0
|
|
11
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
|
|
13
|
+
fn main() {
|
|
14
|
+
tauri_build::build()
|
|
15
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../gen/schemas/desktop-schema.json",
|
|
3
|
+
"identifier": "default",
|
|
4
|
+
"description": "What the page of the main window may use. The commands of src/ need no line here; a plugin needs its permission. dialog:allow-open: the folder dialog of tauri-plugin-dialog, the only dialog the page opens (cargo tauri add dialog wrote dialog:default, which also allows save and message).",
|
|
5
|
+
"windows": ["main"],
|
|
6
|
+
"permissions": ["core:default", "dialog:allow-open"]
|
|
7
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// check.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// THE WINDOW CHECK: cargo run --features window-check (in src-tauri/). The app opens its real window, the script
|
|
5
|
+
// checks/window-check.js plays the page in it (it opens a folder made for the check, shows the hidden files,
|
|
6
|
+
// measures the sizes), every step is printed as PASS or FAIL and the app closes: exit code 0 when nothing failed.
|
|
7
|
+
// WHY: the page in a browser shows sample data; only the real window tells that the page and Rust understand each
|
|
8
|
+
// other, on every system. This file is not in the app that is built: the feature is off by default.
|
|
9
|
+
//
|
|
10
|
+
// Started Date: November 2026
|
|
11
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
12
|
+
//
|
|
13
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
14
|
+
// Licensed under the Apache License, Version 2.0
|
|
15
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
use serde::Deserialize;
|
|
18
|
+
use std::fs;
|
|
19
|
+
use std::path::PathBuf;
|
|
20
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
21
|
+
use std::time::Duration;
|
|
22
|
+
use tauri::webview::PageLoadEvent;
|
|
23
|
+
use tauri::Listener;
|
|
24
|
+
|
|
25
|
+
const SCRIPT: &str = include_str!("../../checks/window-check.js");
|
|
26
|
+
// A folder with this name is in the check folder: a page that writes a name as HTML would make a picture of it.
|
|
27
|
+
// WHY not on Windows: a name can not hold < or > there
|
|
28
|
+
#[cfg(not(windows))]
|
|
29
|
+
const TAG_NAME: &str = "<img src=x onerror=window.CHECK_TAG_RAN=1>";
|
|
30
|
+
#[cfg(windows)]
|
|
31
|
+
const TAG_NAME: &str = "";
|
|
32
|
+
// WHY: a page that never answers (an error in its code) must not keep the window open
|
|
33
|
+
const TIME_LIMIT: Duration = Duration::from_secs(60);
|
|
34
|
+
// The errors of the page, kept from its first line (window-check.js reads them).
|
|
35
|
+
// WHY before the scripts of the page: an error while the page loads (a file that is not there or that the security
|
|
36
|
+
// policy stops, a mistake in a script) comes before window-check.js runs.
|
|
37
|
+
const PAGE_ERRORS_SCRIPT: &str = r#"
|
|
38
|
+
window.CHECK_PAGE_ERRORS = [];
|
|
39
|
+
// WHY true (the capture phase): the error of a file (a script, a picture) does not reach the window otherwise
|
|
40
|
+
window.addEventListener("error", function (event) {
|
|
41
|
+
const file = event.target && (event.target.src || event.target.href);
|
|
42
|
+
window.CHECK_PAGE_ERRORS.push(file ? "a file did not load: " + file : String(event.message));
|
|
43
|
+
}, true);
|
|
44
|
+
window.addEventListener("unhandledrejection", function (event) {
|
|
45
|
+
window.CHECK_PAGE_ERRORS.push(String(event.reason));
|
|
46
|
+
});
|
|
47
|
+
// A file or a style that the content security policy of tauri.conf.json stopped
|
|
48
|
+
window.addEventListener("securitypolicyviolation", function (event) {
|
|
49
|
+
// WHY: "eval" is the try of WindowCheck.canRunTextAsCode() itself
|
|
50
|
+
if (event.blockedURI == "eval") return;
|
|
51
|
+
window.CHECK_PAGE_ERRORS.push("security policy: " + event.violatedDirective + " " + event.blockedURI);
|
|
52
|
+
});
|
|
53
|
+
"#;
|
|
54
|
+
|
|
55
|
+
/// What the script sends with its "check-done" event
|
|
56
|
+
#[derive(Deserialize)]
|
|
57
|
+
struct Report {
|
|
58
|
+
lines: Vec<String>, // "PASS the home folder is listed", "FAIL ..."
|
|
59
|
+
failed: usize,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// Adds the check to the app of main.rs: the script runs when the page is loaded, its report ends the app.
|
|
63
|
+
// WHY a plugin, not .setup() and .on_page_load() of the builder: the builder keeps only one of each, so a .setup() of
|
|
64
|
+
// main.rs (a menu, a tray, a state) would not run under the check. A plugin has its own, and its script runs in the
|
|
65
|
+
// page before the scripts of the page, at every load.
|
|
66
|
+
pub fn add_to(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
67
|
+
let is_started = AtomicBool::new(false);
|
|
68
|
+
|
|
69
|
+
let check = tauri::plugin::Builder::<tauri::Wry>::new("window-check")
|
|
70
|
+
.js_init_script(PAGE_ERRORS_SCRIPT)
|
|
71
|
+
.setup(|app, _api| {
|
|
72
|
+
let handle = app.clone();
|
|
73
|
+
app.listen("check-done", move |event| {
|
|
74
|
+
end_check(&handle, event.payload())
|
|
75
|
+
});
|
|
76
|
+
std::thread::spawn(stop_at_the_time_limit);
|
|
77
|
+
Ok(())
|
|
78
|
+
})
|
|
79
|
+
.on_page_load(move |webview, payload| {
|
|
80
|
+
let is_loaded = payload.event() == PageLoadEvent::Finished;
|
|
81
|
+
// WHY main: a second window of the app (its own page) must not get the script of the check
|
|
82
|
+
let is_main = webview.label() == "main";
|
|
83
|
+
// WHY swap: the script runs once, also when the page is loaded again
|
|
84
|
+
if !is_loaded || !is_main || is_started.swap(true, Ordering::SeqCst) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
let folder = make_check_folder();
|
|
88
|
+
// WHY serde_json: a path of Windows has backslashes; as JSON it is a correct JavaScript text
|
|
89
|
+
let facts =
|
|
90
|
+
serde_json::json!({ "folder": folder.display().to_string(), "tagName": TAG_NAME });
|
|
91
|
+
let script = format!("window.CHECK = {facts};\n{SCRIPT}");
|
|
92
|
+
let _ = webview.eval(&script);
|
|
93
|
+
})
|
|
94
|
+
.build();
|
|
95
|
+
|
|
96
|
+
builder.plugin(check)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn check_folder() -> PathBuf {
|
|
100
|
+
std::env::temp_dir().join(format!("tauri-starter-check-{}", std::process::id()))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// The folder the script opens: a folder with a file of 3 bytes, a file of 5 bytes, a hidden file, the tag folder
|
|
104
|
+
fn make_check_folder() -> PathBuf {
|
|
105
|
+
let folder = check_folder();
|
|
106
|
+
fs::create_dir_all(folder.join("b-folder")).unwrap();
|
|
107
|
+
fs::write(folder.join("b-folder").join("inside.txt"), "123").unwrap();
|
|
108
|
+
fs::write(folder.join("a-file.txt"), "hello").unwrap();
|
|
109
|
+
fs::write(folder.join(".hidden-file"), "").unwrap();
|
|
110
|
+
if !TAG_NAME.is_empty() {
|
|
111
|
+
fs::create_dir_all(folder.join(TAG_NAME)).unwrap();
|
|
112
|
+
}
|
|
113
|
+
folder
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn end_check(app: &tauri::AppHandle, payload: &str) {
|
|
117
|
+
let _ = fs::remove_dir_all(check_folder());
|
|
118
|
+
|
|
119
|
+
let Ok(report) = serde_json::from_str::<Report>(payload) else {
|
|
120
|
+
println!("FAIL the report of the page could not be read: {payload}");
|
|
121
|
+
app.exit(1);
|
|
122
|
+
return;
|
|
123
|
+
};
|
|
124
|
+
for line in &report.lines {
|
|
125
|
+
println!("{line}");
|
|
126
|
+
}
|
|
127
|
+
if report.failed == 0 {
|
|
128
|
+
println!("WINDOW CHECK: ALL PASS");
|
|
129
|
+
} else {
|
|
130
|
+
println!("WINDOW CHECK: {} FAILED", report.failed);
|
|
131
|
+
}
|
|
132
|
+
app.exit(if report.failed == 0 { 0 } else { 1 });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fn stop_at_the_time_limit() {
|
|
136
|
+
std::thread::sleep(TIME_LIMIT);
|
|
137
|
+
let _ = fs::remove_dir_all(check_folder());
|
|
138
|
+
println!(
|
|
139
|
+
"FAIL the page did not answer in {} seconds",
|
|
140
|
+
TIME_LIMIT.as_secs()
|
|
141
|
+
);
|
|
142
|
+
println!("WINDOW CHECK: 1 FAILED");
|
|
143
|
+
std::process::exit(1);
|
|
144
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// files.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// THE FIRST EXAMPLE: the page asks, Rust answers. The command that reads a folder. The page calls it in
|
|
5
|
+
// ui/backend.js:
|
|
6
|
+
// window.__TAURI__.core.invoke("list_folder", { path: "", showHidden: false })
|
|
7
|
+
// (the arguments are camelCase in JavaScript and snake_case here: Tauri matches them)
|
|
8
|
+
//
|
|
9
|
+
// Started Date: November 2026
|
|
10
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
11
|
+
//
|
|
12
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
13
|
+
// Licensed under the Apache License, Version 2.0
|
|
14
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
15
|
+
|
|
16
|
+
use serde::Serialize;
|
|
17
|
+
use std::fs;
|
|
18
|
+
use std::path::{Path, PathBuf};
|
|
19
|
+
use std::time::{Instant, UNIX_EPOCH};
|
|
20
|
+
use tauri::Manager;
|
|
21
|
+
|
|
22
|
+
// WHY: the answer of a folder of 100 000 entries is about 15 MB; the page shows them in a SmartTable, which draws
|
|
23
|
+
// only the rows that are seen, so the limit is for the size of the answer, not for the table
|
|
24
|
+
const MAX_ENTRIES: usize = 100_000;
|
|
25
|
+
|
|
26
|
+
/// A file or a folder of the list. Its fields reach JavaScript in camelCase (isFolder).
|
|
27
|
+
#[derive(Serialize)]
|
|
28
|
+
#[serde(rename_all = "camelCase")]
|
|
29
|
+
pub struct Entry {
|
|
30
|
+
name: String,
|
|
31
|
+
path: String,
|
|
32
|
+
is_folder: bool,
|
|
33
|
+
size: u64, // bytes (0 for a folder)
|
|
34
|
+
modified: u64, // milliseconds since 1970, like a JavaScript Date (0: unknown)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// A part of the path, for the breadcrumbs of the page: { name: "Documents", path: "/Users/me/Documents" }
|
|
38
|
+
#[derive(Serialize)]
|
|
39
|
+
pub struct Crumb {
|
|
40
|
+
name: String,
|
|
41
|
+
path: String,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// The answer of list_folder
|
|
45
|
+
#[derive(Serialize)]
|
|
46
|
+
#[serde(rename_all = "camelCase")]
|
|
47
|
+
pub struct Folder {
|
|
48
|
+
path: String,
|
|
49
|
+
parent: Option<String>, // null at the root
|
|
50
|
+
crumbs: Vec<Crumb>, // from the root to this folder
|
|
51
|
+
entries: Vec<Entry>, // the folders first, then by name; at most MAX_ENTRIES
|
|
52
|
+
total: usize, // all the entries (more than entries.len() when the list is cut)
|
|
53
|
+
millis: f64, // the time Rust took
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Reads a folder. An empty path is the home folder of the user.
|
|
57
|
+
/// An Err reaches JavaScript as a rejected promise with its text.
|
|
58
|
+
// WHY async: Tauri runs it off the main thread, so a slow folder does not stop the window
|
|
59
|
+
#[tauri::command(async)]
|
|
60
|
+
pub fn list_folder(
|
|
61
|
+
app: tauri::AppHandle,
|
|
62
|
+
path: String,
|
|
63
|
+
show_hidden: bool,
|
|
64
|
+
) -> Result<Folder, String> {
|
|
65
|
+
let folder = if path.is_empty() {
|
|
66
|
+
app.path().home_dir().map_err(|error| error.to_string())?
|
|
67
|
+
} else {
|
|
68
|
+
PathBuf::from(path)
|
|
69
|
+
};
|
|
70
|
+
read_folder(&folder, show_hidden)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// The work of list_folder, without Tauri: the tests below call it (cargo test)
|
|
74
|
+
pub fn read_folder(folder: &Path, show_hidden: bool) -> Result<Folder, String> {
|
|
75
|
+
let started = Instant::now();
|
|
76
|
+
let items = fs::read_dir(folder).map_err(|error| format!("{}: {}", folder.display(), error))?;
|
|
77
|
+
|
|
78
|
+
let mut entries: Vec<Entry> = items
|
|
79
|
+
.filter_map(|item| item.ok())
|
|
80
|
+
.filter(|item| show_hidden || !is_hidden(item))
|
|
81
|
+
.map(|item| to_entry(&item))
|
|
82
|
+
.collect();
|
|
83
|
+
entries.sort_by_cached_key(|entry| (!entry.is_folder, entry.name.to_lowercase()));
|
|
84
|
+
let total = entries.len();
|
|
85
|
+
entries.truncate(MAX_ENTRIES);
|
|
86
|
+
|
|
87
|
+
Ok(Folder {
|
|
88
|
+
path: folder.display().to_string(),
|
|
89
|
+
parent: folder.parent().map(|parent| parent.display().to_string()),
|
|
90
|
+
crumbs: to_crumbs(folder),
|
|
91
|
+
entries,
|
|
92
|
+
total,
|
|
93
|
+
millis: started.elapsed().as_secs_f64() * 1000.0,
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// A name that starts with a dot is hidden (the rule of macOS and Linux, kept on Windows for .git and the like)
|
|
98
|
+
fn is_hidden(item: &fs::DirEntry) -> bool {
|
|
99
|
+
item.file_name().to_string_lossy().starts_with('.') || has_hidden_attribute(item)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Windows marks a hidden file with an attribute (AppData, ntuser.dat in the home folder)
|
|
103
|
+
#[cfg(windows)]
|
|
104
|
+
fn has_hidden_attribute(item: &fs::DirEntry) -> bool {
|
|
105
|
+
use std::os::windows::fs::MetadataExt;
|
|
106
|
+
const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
|
|
107
|
+
item.metadata()
|
|
108
|
+
.is_ok_and(|metadata| metadata.file_attributes() & FILE_ATTRIBUTE_HIDDEN != 0)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[cfg(not(windows))]
|
|
112
|
+
fn has_hidden_attribute(_item: &fs::DirEntry) -> bool {
|
|
113
|
+
false
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn to_entry(item: &fs::DirEntry) -> Entry {
|
|
117
|
+
let path = item.path();
|
|
118
|
+
// WHY item.metadata(): it reads the file in its folder, without the whole path again (about 10% faster).
|
|
119
|
+
// It does not follow a link: a link is read again, so a link to a folder is a folder (a broken link is a file).
|
|
120
|
+
let mut metadata = item.metadata().ok();
|
|
121
|
+
if metadata
|
|
122
|
+
.as_ref()
|
|
123
|
+
.is_some_and(|meta| meta.file_type().is_symlink())
|
|
124
|
+
{
|
|
125
|
+
metadata = fs::metadata(&path).ok().or(metadata);
|
|
126
|
+
}
|
|
127
|
+
let is_folder = metadata.as_ref().is_some_and(|meta| meta.is_dir());
|
|
128
|
+
let size = match &metadata {
|
|
129
|
+
Some(meta) if !is_folder => meta.len(),
|
|
130
|
+
_ => 0,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
Entry {
|
|
134
|
+
name: name_of(&path),
|
|
135
|
+
path: path.display().to_string(),
|
|
136
|
+
is_folder,
|
|
137
|
+
size,
|
|
138
|
+
modified: metadata.as_ref().map_or(0, modified_millis),
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
fn modified_millis(metadata: &fs::Metadata) -> u64 {
|
|
143
|
+
metadata
|
|
144
|
+
.modified()
|
|
145
|
+
.ok()
|
|
146
|
+
.and_then(|time| time.duration_since(UNIX_EPOCH).ok())
|
|
147
|
+
.map_or(0, |duration| duration.as_millis() as u64)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The root has no name: its whole text is shown ("/", "C:\")
|
|
151
|
+
fn name_of(path: &Path) -> String {
|
|
152
|
+
match path.file_name() {
|
|
153
|
+
Some(name) => name.to_string_lossy().into_owned(),
|
|
154
|
+
None => path.display().to_string(),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
fn to_crumbs(folder: &Path) -> Vec<Crumb> {
|
|
159
|
+
let mut crumbs: Vec<Crumb> = folder
|
|
160
|
+
.ancestors()
|
|
161
|
+
.map(|ancestor| Crumb {
|
|
162
|
+
name: name_of(ancestor),
|
|
163
|
+
path: ancestor.display().to_string(),
|
|
164
|
+
})
|
|
165
|
+
.collect();
|
|
166
|
+
crumbs.reverse();
|
|
167
|
+
crumbs
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#[cfg(test)]
|
|
171
|
+
mod tests {
|
|
172
|
+
use super::*;
|
|
173
|
+
|
|
174
|
+
#[test]
|
|
175
|
+
fn lists_the_folders_first_and_hides_the_dot_files() {
|
|
176
|
+
let folder =
|
|
177
|
+
std::env::temp_dir().join(format!("tauri-starter-test-{}", std::process::id()));
|
|
178
|
+
fs::create_dir_all(folder.join("b-folder")).unwrap();
|
|
179
|
+
fs::write(folder.join("a-file.txt"), "hello").unwrap();
|
|
180
|
+
fs::write(folder.join(".hidden"), "").unwrap();
|
|
181
|
+
|
|
182
|
+
let shown = read_folder(&folder, false).unwrap();
|
|
183
|
+
let names: Vec<&str> = shown
|
|
184
|
+
.entries
|
|
185
|
+
.iter()
|
|
186
|
+
.map(|entry| entry.name.as_str())
|
|
187
|
+
.collect();
|
|
188
|
+
assert_eq!(names, ["b-folder", "a-file.txt"]);
|
|
189
|
+
assert!(shown.entries[0].is_folder);
|
|
190
|
+
assert_eq!(shown.entries[1].size, 5);
|
|
191
|
+
assert_eq!(read_folder(&folder, true).unwrap().total, 3);
|
|
192
|
+
|
|
193
|
+
fs::remove_dir_all(&folder).unwrap();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[cfg(windows)]
|
|
197
|
+
#[test]
|
|
198
|
+
fn a_file_with_the_hidden_attribute_of_windows_is_hidden() {
|
|
199
|
+
let folder =
|
|
200
|
+
std::env::temp_dir().join(format!("tauri-starter-attribute-{}", std::process::id()));
|
|
201
|
+
fs::create_dir_all(&folder).unwrap();
|
|
202
|
+
fs::write(folder.join("seen.txt"), "").unwrap();
|
|
203
|
+
fs::write(folder.join("unseen.txt"), "").unwrap();
|
|
204
|
+
let attrib = std::process::Command::new("attrib")
|
|
205
|
+
.arg("+h")
|
|
206
|
+
.arg(folder.join("unseen.txt"))
|
|
207
|
+
.status()
|
|
208
|
+
.unwrap();
|
|
209
|
+
assert!(attrib.success());
|
|
210
|
+
|
|
211
|
+
assert_eq!(read_folder(&folder, false).unwrap().total, 1);
|
|
212
|
+
assert_eq!(read_folder(&folder, true).unwrap().total, 2);
|
|
213
|
+
|
|
214
|
+
fs::remove_dir_all(&folder).unwrap();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#[cfg(unix)]
|
|
218
|
+
#[test]
|
|
219
|
+
fn a_link_to_a_folder_is_a_folder_and_a_broken_link_is_a_file() {
|
|
220
|
+
let folder =
|
|
221
|
+
std::env::temp_dir().join(format!("tauri-starter-links-{}", std::process::id()));
|
|
222
|
+
fs::create_dir_all(folder.join("real-folder")).unwrap();
|
|
223
|
+
std::os::unix::fs::symlink(folder.join("real-folder"), folder.join("link")).unwrap();
|
|
224
|
+
std::os::unix::fs::symlink(folder.join("missing"), folder.join("broken")).unwrap();
|
|
225
|
+
|
|
226
|
+
let shown = read_folder(&folder, false).unwrap();
|
|
227
|
+
let kinds: Vec<(&str, bool)> = shown
|
|
228
|
+
.entries
|
|
229
|
+
.iter()
|
|
230
|
+
.map(|entry| (entry.name.as_str(), entry.is_folder))
|
|
231
|
+
.collect();
|
|
232
|
+
assert_eq!(
|
|
233
|
+
kinds,
|
|
234
|
+
[("link", true), ("real-folder", true), ("broken", false)]
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
fs::remove_dir_all(&folder).unwrap();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
#[test]
|
|
241
|
+
fn a_missing_folder_is_an_error() {
|
|
242
|
+
let error = read_folder(Path::new("/no/such/folder"), false)
|
|
243
|
+
.err()
|
|
244
|
+
.unwrap();
|
|
245
|
+
assert!(error.starts_with("/no/such/folder: "));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
#[test]
|
|
249
|
+
fn the_crumbs_go_from_the_root_to_the_folder() {
|
|
250
|
+
let crumbs = to_crumbs(Path::new("/Users/me"));
|
|
251
|
+
let names: Vec<&str> = crumbs.iter().map(|crumb| crumb.name.as_str()).collect();
|
|
252
|
+
assert_eq!(names, ["/", "Users", "me"]);
|
|
253
|
+
assert_eq!(crumbs[1].path, "/Users");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Bismillah
|
|
2
|
+
//
|
|
3
|
+
// main.rs - Tauri Starter v0.1 (OzdenUI for Tauri)
|
|
4
|
+
// A desktop app with a Rust side and a basic.js interface. The window shows ui/index.html; its code calls the
|
|
5
|
+
// commands of this program: files.rs (the page asks, Rust answers) and sizes.rs (a long job that sends events to
|
|
6
|
+
// the page). The folder dialog of the page comes from a plugin of Tauri (tauri-plugin-dialog).
|
|
7
|
+
//
|
|
8
|
+
// Started Date: November 2026
|
|
9
|
+
// Webpage: https://bug7a.github.io/ozdenui-tauri-template/
|
|
10
|
+
//
|
|
11
|
+
// Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
|
|
12
|
+
// Licensed under the Apache License, Version 2.0
|
|
13
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
14
|
+
|
|
15
|
+
// WHY: no console window next to the app on Windows (the release build)
|
|
16
|
+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
17
|
+
|
|
18
|
+
#[cfg(feature = "window-check")]
|
|
19
|
+
mod check;
|
|
20
|
+
mod files;
|
|
21
|
+
mod sizes;
|
|
22
|
+
|
|
23
|
+
fn main() {
|
|
24
|
+
let builder = tauri::Builder::default()
|
|
25
|
+
// A plugin of Tauri brings its own Rust code and its own JavaScript (window.__TAURI__.dialog in the page).
|
|
26
|
+
// "cargo tauri add dialog" wrote this line, the line of Cargo.toml and "dialog:default" in capabilities/
|
|
27
|
+
// (made narrower there: "dialog:allow-open", as the page only opens folders).
|
|
28
|
+
.plugin(tauri_plugin_dialog::init())
|
|
29
|
+
// The state of the jobs of sizes.rs: a command gets it as State<sizes::Measure>
|
|
30
|
+
.manage(sizes::Measure::default())
|
|
31
|
+
// The commands the page can call. A new one: write it with #[tauri::command], then add it here.
|
|
32
|
+
.invoke_handler(tauri::generate_handler![
|
|
33
|
+
files::list_folder,
|
|
34
|
+
sizes::measure_folder,
|
|
35
|
+
sizes::stop_measure
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
// cargo run --features window-check: the page is played in its real window, then the app closes (check.rs)
|
|
39
|
+
#[cfg(feature = "window-check")]
|
|
40
|
+
let builder = check::add_to(builder);
|
|
41
|
+
|
|
42
|
+
builder
|
|
43
|
+
.run(tauri::generate_context!())
|
|
44
|
+
.expect("the app could not start");
|
|
45
|
+
}
|