Skip to content

Commit f6c3ea6

Browse files
authored
chore: simplify windows_subsystem attribute (tauri-apps#6273)
1 parent bfa6969 commit f6c3ea6

File tree

34 files changed

+205
-333
lines changed

34 files changed

+205
-333
lines changed

core/tauri-codegen/src/embedded_assets.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ impl ToTokens for EmbeddedAssets {
433433

434434
// we expect phf related items to be in path when generating the path code
435435
tokens.append_all(quote! {{
436+
#[allow(unused)]
436437
use ::tauri::utils::assets::{CspHash, EmbeddedAssets, phf, phf::phf_map};
437438
EmbeddedAssets::new(phf_map! { #assets }, &[#global_hashes], phf_map! { #html_hashes })
438439
}});

core/tests/app-updater/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#![cfg_attr(
6-
all(not(debug_assertions), target_os = "windows"),
7-
windows_subsystem = "windows"
8-
)]
5+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
96

107
fn main() {
118
let mut context = tauri::generate_context!();

examples/api/dist/assets/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/dist/assets/index.js

Lines changed: 31 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.lock

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ edition = "2021"
66
rust-version = "1.59"
77
license = "Apache-2.0 OR MIT"
88

9+
[lib]
10+
name = "api_lib"
11+
912
[build-dependencies]
1013
tauri-build = { path = "../../../core/tauri-build", features = ["codegen", "isolation"] }
1114

@@ -32,7 +35,6 @@ features = [
3235
]
3336

3437
[target."cfg(target_os = \"windows\")".dependencies]
35-
window-vibrancy = "0.2"
3638
window-shadows= "0.2"
3739

3840
[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]

examples/api/src-tauri/src/cmd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Deserialize;
66
use tauri::command;
77

88
#[derive(Debug, Deserialize)]
9-
#[allow(dead_code)]
9+
#[allow(unused)]
1010
pub struct RequestBody {
1111
id: i32,
1212
name: String,

examples/api/src-tauri/src/lib.rs

Lines changed: 134 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,156 +2,162 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#![cfg_attr(
6-
all(not(debug_assertions), target_os = "windows"),
7-
windows_subsystem = "windows"
8-
)]
9-
105
mod cmd;
6+
mod tray;
117

128
use serde::Serialize;
13-
use tauri::{window::WindowBuilder, App, AppHandle, RunEvent, WindowUrl};
9+
use tauri::{
10+
api::dialog::{ask, message},
11+
GlobalShortcutManager, Manager, RunEvent, WindowBuilder, WindowEvent, WindowUrl,
12+
};
1413

1514
#[derive(Clone, Serialize)]
1615
struct Reply {
1716
data: String,
1817
}
1918

20-
pub type SetupHook = Box<dyn FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send>;
21-
pub type OnEvent = Box<dyn FnMut(&AppHandle, RunEvent)>;
22-
23-
#[derive(Default)]
24-
pub struct AppBuilder {
25-
setup: Option<SetupHook>,
26-
on_event: Option<OnEvent>,
27-
}
28-
29-
impl AppBuilder {
30-
pub fn new() -> Self {
31-
Self::default()
32-
}
19+
#[cfg_attr(mobile, tauri::mobile_entry_point)]
20+
pub fn run() {
21+
#[allow(unused_mut)]
22+
let mut builder = tauri::Builder::default()
23+
.setup(move |app| {
24+
tray::create_tray(app)?;
25+
26+
#[allow(unused_mut)]
27+
let mut window_builder = WindowBuilder::new(app, "main", WindowUrl::default())
28+
.user_agent("Tauri API")
29+
.title("Tauri API Validation")
30+
.inner_size(1000., 800.)
31+
.min_inner_size(600., 400.)
32+
.content_protected(true);
33+
34+
#[cfg(target_os = "windows")]
35+
{
36+
window_builder = window_builder.transparent(true).decorations(false);
37+
}
3338

34-
#[must_use]
35-
pub fn setup<F>(mut self, setup: F) -> Self
36-
where
37-
F: FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
38-
{
39-
self.setup.replace(Box::new(setup));
40-
self
41-
}
39+
let window = window_builder.build().unwrap();
4240

43-
#[must_use]
44-
pub fn on_event<F>(mut self, on_event: F) -> Self
45-
where
46-
F: Fn(&AppHandle, RunEvent) + 'static,
47-
{
48-
self.on_event.replace(Box::new(on_event));
49-
self
50-
}
51-
52-
pub fn run(self) {
53-
let setup = self.setup;
54-
let mut on_event = self.on_event;
41+
#[cfg(target_os = "windows")]
42+
{
43+
let _ = window_shadows::set_shadow(&window, true);
44+
}
5545

56-
#[allow(unused_mut)]
57-
let mut builder = tauri::Builder::default()
58-
.setup(move |app| {
59-
if let Some(setup) = setup {
60-
(setup)(app)?;
61-
}
46+
#[cfg(debug_assertions)]
47+
window.open_devtools();
6248

63-
#[allow(unused_mut)]
64-
let mut window_builder = WindowBuilder::new(app, "main", WindowUrl::default())
65-
.user_agent("Tauri API")
66-
.title("Tauri API Validation")
67-
.inner_size(1000., 800.)
68-
.min_inner_size(600., 400.)
69-
.content_protected(true);
70-
71-
#[cfg(target_os = "windows")]
72-
{
73-
window_builder = window_builder.transparent(true);
74-
window_builder = window_builder.decorations(false);
49+
std::thread::spawn(|| {
50+
let server = match tiny_http::Server::http("localhost:3003") {
51+
Ok(s) => s,
52+
Err(e) => {
53+
eprintln!("{}", e);
54+
std::process::exit(1);
55+
}
56+
};
57+
loop {
58+
if let Ok(mut request) = server.recv() {
59+
let mut body = Vec::new();
60+
let _ = request.as_reader().read_to_end(&mut body);
61+
let response = tiny_http::Response::new(
62+
tiny_http::StatusCode(200),
63+
request.headers().to_vec(),
64+
std::io::Cursor::new(body),
65+
request.body_length(),
66+
None,
67+
);
68+
let _ = request.respond(response);
69+
}
7570
}
71+
});
7672

77-
let window = window_builder.build().unwrap();
78-
79-
#[cfg(target_os = "windows")]
80-
{
81-
let _ = window_shadows::set_shadow(&window, true);
82-
let _ = window_vibrancy::apply_blur(&window, Some((0, 0, 0, 0)));
83-
}
73+
Ok(())
74+
})
75+
.on_page_load(|window, _| {
76+
let window_ = window.clone();
77+
window.listen("js-event", move |event| {
78+
println!("got js-event with message '{:?}'", event.payload());
79+
let reply = Reply {
80+
data: "something else".to_string(),
81+
};
82+
83+
window_
84+
.emit("rust-event", Some(reply))
85+
.expect("failed to emit");
86+
});
87+
});
8488

85-
#[cfg(debug_assertions)]
86-
window.open_devtools();
89+
#[cfg(target_os = "macos")]
90+
{
91+
builder = builder.menu(tauri::Menu::os_default("Tauri API Validation"));
92+
}
8793

88-
std::thread::spawn(|| {
89-
let server = match tiny_http::Server::http("localhost:3003") {
90-
Ok(s) => s,
91-
Err(e) => {
92-
eprintln!("{}", e);
93-
std::process::exit(1);
94-
}
95-
};
96-
loop {
97-
if let Ok(mut request) = server.recv() {
98-
let mut body = Vec::new();
99-
let _ = request.as_reader().read_to_end(&mut body);
100-
let response = tiny_http::Response::new(
101-
tiny_http::StatusCode(200),
102-
request.headers().to_vec(),
103-
std::io::Cursor::new(body),
104-
request.body_length(),
105-
None,
94+
#[allow(unused_mut)]
95+
let mut app = builder
96+
.invoke_handler(tauri::generate_handler![
97+
cmd::log_operation,
98+
cmd::perform_request,
99+
])
100+
.build(tauri::tauri_build_context!())
101+
.expect("error while building tauri application");
102+
103+
#[cfg(target_os = "macos")]
104+
app.set_activation_policy(tauri::ActivationPolicy::Regular);
105+
106+
#[allow(unused_variables)]
107+
app.run(move |app_handle, e| {
108+
match e {
109+
// Application is ready (triggered only once)
110+
RunEvent::Ready => {
111+
let app_handle = app_handle.clone();
112+
app_handle
113+
.global_shortcut_manager()
114+
.register("CmdOrCtrl+1", move || {
115+
let app_handle = app_handle.clone();
116+
if let Some(window) = app_handle.get_window("main") {
117+
message(
118+
Some(&window),
119+
"Tauri API",
120+
"CmdOrCtrl+1 global shortcut triggered",
106121
);
107-
let _ = request.respond(response);
108122
}
109-
}
110-
});
111-
112-
Ok(())
113-
})
114-
.on_page_load(|window, _| {
115-
let window_ = window.clone();
116-
window.listen("js-event", move |event| {
117-
println!("got js-event with message '{:?}'", event.payload());
118-
let reply = Reply {
119-
data: "something else".to_string(),
120-
};
121-
122-
window_
123-
.emit("rust-event", Some(reply))
124-
.expect("failed to emit");
125-
});
126-
});
127-
128-
#[cfg(target_os = "macos")]
129-
{
130-
builder = builder.menu(tauri::Menu::os_default("Tauri API Validation"));
131-
}
123+
})
124+
.unwrap();
125+
}
132126

133-
#[allow(unused_mut)]
134-
let mut app = builder
135-
.invoke_handler(tauri::generate_handler![
136-
cmd::log_operation,
137-
cmd::perform_request,
138-
])
139-
.build(tauri::tauri_build_context!())
140-
.expect("error while building tauri application");
141-
142-
#[cfg(target_os = "macos")]
143-
app.set_activation_policy(tauri::ActivationPolicy::Regular);
144-
145-
#[allow(unused_variables)]
146-
app.run(move |app_handle, e| {
147-
if let RunEvent::ExitRequested { api, .. } = &e {
127+
// Triggered when a window is trying to close
128+
RunEvent::WindowEvent {
129+
label,
130+
event: WindowEvent::CloseRequested { api, .. },
131+
..
132+
} => {
133+
// for other windows, we handle it in JS
134+
if label == "main" {
135+
let app_handle = app_handle.clone();
136+
let window = app_handle.get_window(&label).unwrap();
137+
// use the exposed close api, and prevent the event loop to close
138+
api.prevent_close();
139+
// ask the user if he wants to quit
140+
ask(
141+
Some(&window),
142+
"Tauri API",
143+
"Are you sure that you want to close this window?",
144+
move |answer| {
145+
if answer {
146+
// .close() cannot be called on the main thread
147+
std::thread::spawn(move || {
148+
app_handle.get_window(&label).unwrap().close().unwrap();
149+
});
150+
}
151+
},
152+
);
153+
}
154+
}
155+
RunEvent::ExitRequested { api, .. } => {
148156
// Keep the event loop running even if all windows are closed
149157
// This allow us to catch system tray events when there is no window
150158
api.prevent_exit();
151159
}
152-
if let Some(on_event) = &mut on_event {
153-
(on_event)(app_handle, e);
154-
}
155-
})
156-
}
160+
_ => (),
161+
}
162+
})
157163
}

examples/api/src-tauri/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
#![cfg_attr(
2-
all(not(debug_assertions), target_os = "windows"),
3-
windows_subsystem = "windows"
4-
)]
5-
6-
#[cfg(desktop)]
7-
mod desktop;
1+
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
2+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
83

94
fn main() {
105
#[cfg(desktop)]
11-
desktop::main();
6+
api_lib::run();
127
}

0 commit comments

Comments
 (0)