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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Programmatic control of a running browser window.
//!
//! There are two patterns for wiring up a [`BrowserHandle`]:
//!
//! ## Pattern A — `launch_with_controller` (recommended, zero boilerplate)
//!
//! Pass an async closure; the channel pair and `block_in_place` are managed
//! for you.
//!
//! ```no_run
//! # #[cfg(feature = "browser")]
//! use toolkit_zero::browser::{self, Target, api::{BrowserHandle, TabTarget, ThemeMode}};
//!
//! # #[cfg(feature = "browser")]
//! #[tokio::main(flavor = "multi_thread")]
//! async fn main() -> Result<(), browser::BrowserError> {
//! browser::launch_with_controller(Target::Default, |handle: BrowserHandle| async move {
//! tokio::time::sleep(std::time::Duration::from_secs(2)).await;
//! handle.navigate(TabTarget::Active, "https://docs.rs");
//! handle.set_theme(ThemeMode::Dark);
//! })
//! }
//! ```
//!
//! ## Pattern B — `launch_with_api` (explicit, full control)
//!
//! Create the [`BrowserHandle`] / [`ApiReceiver`] pair yourself, spawn your
//! own tasks, then hand the receiver to [`super::launch_with_api`].
//!
//! ```no_run
//! # #[cfg(feature = "browser")]
//! use toolkit_zero::browser::{self, Target, api::{BrowserHandle, TabTarget, ThemeMode}};
//!
//! # #[cfg(feature = "browser")]
//! #[tokio::main(flavor = "multi_thread")]
//! async fn main() {
//! let (handle, receiver) = BrowserHandle::new();
//!
//! let h = handle.clone();
//! tokio::spawn(async move {
//! tokio::time::sleep(std::time::Duration::from_secs(3)).await;
//! h.navigate(TabTarget::Active, "https://example.com");
//! h.open_new_tab(Some("https://docs.rs"));
//! h.set_theme(ThemeMode::Dark);
//! });
//!
//! // Blocks the main thread until the window is closed.
//! tokio::task::block_in_place(|| {
//! browser::launch_with_api(Target::Default, receiver)
//! }).unwrap();
//! }
//! ```
//!
//! ## How it works
//!
//! Both patterns keep the tokio executor alive on other threads while the iced
//! event-loop blocks the calling thread (`tokio::task::block_in_place`).
//! Commands sent through the handle are picked up by the browser's Tick loop
//! (≤ 16 ms latency).
use PathBuf;
use Mutex;
use mpsc;
// Re-export ThemeMode so callers only need to import from api.
pub use ThemeMode;
// ── Internal message type ─────────────────────────────────────────────────────
/// Commands sent from [`BrowserHandle`] to the running browser.
/// `pub(super)` — external code enqueues via the typed `BrowserHandle` methods.
pub
// ─ Safety: all fields are Send (String, ThemeMode: Copy, TabTarget: primitive) ─
unsafe
// ── Global receiver slot ──────────────────────────────────────────────────────
//
// The iced init closure is `Fn` (not `FnOnce`), so we cannot move the receiver
// directly into it. We park it in a global `Mutex<Option<…>>` before calling
// `launch_with_api`; `BrowserState::new` takes it out exactly once.
static RECEIVER: = new;
/// Park `rx` in the global slot so `BrowserState::new` can retrieve it.
/// Called by `launch_with_api` before iced starts.
pub
/// Pull the receiver out of the global slot. Returns `None` if
/// `install_receiver` was never called (plain `launch` / `launch_default`).
/// Called exactly once by `BrowserState::new`.
pub
// ── Public types ──────────────────────────────────────────────────────────────
/// Which tab a navigation or new-tab command should target.
/// A cloneable handle to a running browser window.
///
/// All methods are **fire-and-forget**: they enqueue a command and return
/// immediately. The browser processes commands on the next Tick (≤16 ms).
///
/// Obtain one with [`BrowserHandle::new`] and pass the accompanying
/// [`ApiReceiver`] to [`super::launch_with_api`].
/// The receiving end of the API channel.
///
/// Pass this **once** to [`super::launch_with_api`]. Keep the
/// [`BrowserHandle`] for all subsequent communication.
UnboundedReceiver);
// ── Internal helpers ──────────────────────────────────────────────────────────
/// Compute `(temp_dest, final_dest)` for an API-initiated download.
///
/// Files land in `~/Downloads/` (or `%USERPROFILE%\Downloads` on Windows).
/// The filename is the last URL path segment with the query string stripped.
pub