Expand description
A guard for a state value.
See Manager::manage
for usage examples.
Implementations§
source§impl<'r, T: Send + Sync + 'static> State<'r, T>
impl<'r, T: Send + Sync + 'static> State<'r, T>
sourcepub fn inner(&self) -> &'r T
pub fn inner(&self) -> &'r T
Retrieve a borrow to the underlying value with a lifetime of 'r
.
Using this method is typically unnecessary as State
implements
std::ops::Deref
with a std::ops::Deref::Target
of T
.
Examples found in repository?
src/lib.rs (line 774)
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
fn env(&self) -> Env {
self.state::<Env>().inner().clone()
}
/// Gets the scope for the filesystem APIs.
fn fs_scope(&self) -> FsScope {
self.state::<Scopes>().inner().fs.clone()
}
/// Gets the scope for the asset protocol.
#[cfg(protocol_asset)]
fn asset_protocol_scope(&self) -> FsScope {
self.state::<Scopes>().inner().asset_protocol.clone()
}
/// Gets the scope for the shell execute APIs.
#[cfg(shell_scope)]
fn shell_scope(&self) -> ShellScope {
self.state::<Scopes>().inner().shell.clone()
}
More examples
src/endpoints/path.rs (line 54)
46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn resolve_path<R: Runtime>(
context: InvokeContext<R>,
path: String,
directory: Option<BaseDirectory>,
) -> super::Result<PathBuf> {
crate::api::path::resolve_path(
&context.config,
&context.package_info,
context.window.state::<Env>().inner(),
path,
directory,
)
.map_err(Into::into)
}
src/endpoints/file_system.rs (line 374)
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
fn resolve_path<R: Runtime>(
config: &Config,
package_info: &PackageInfo,
window: &Window<R>,
path: SafePathBuf,
dir: Option<BaseDirectory>,
) -> super::Result<SafePathBuf> {
let env = window.state::<Env>().inner();
match crate::api::path::resolve_path(config, package_info, env, &path, dir) {
Ok(path) => {
if window.state::<Scopes>().fs.is_allowed(&path) {
Ok(
// safety: the path is resolved by Tauri so it is safe
unsafe { SafePathBuf::new_unchecked(path) },
)
} else {
Err(anyhow::anyhow!(
crate::Error::PathNotAllowed(path).to_string()
))
}
}
Err(e) => super::Result::<SafePathBuf>::Err(e.into())
.with_context(|| format!("path: {}, base dir: {:?}", path.display(), dir)),
}
}
src/manager.rs (line 1208)
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
pub fn prepare_window(
&self,
app_handle: AppHandle<R>,
mut pending: PendingWindow<EventLoopMessage, R>,
window_labels: &[String],
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
) -> crate::Result<PendingWindow<EventLoopMessage, R>> {
if self.windows_lock().contains_key(&pending.label) {
return Err(crate::Error::WindowLabelAlreadyExists(pending.label));
}
#[allow(unused_mut)] // mut url only for the data-url parsing
let (is_local, mut url) = match &pending.webview_attributes.url {
WindowUrl::App(path) => {
let url = self.get_url();
(
true,
// ignore "index.html" just to simplify the url
if path.to_str() != Some("index.html") {
url
.join(&path.to_string_lossy())
.map_err(crate::Error::InvalidUrl)
// this will never fail
.unwrap()
} else {
url.into_owned()
},
)
}
WindowUrl::External(url) => {
let config_url = self.get_url();
(config_url.make_relative(url).is_some(), url.clone())
}
_ => unimplemented!(),
};
#[cfg(not(feature = "window-data-url"))]
if url.scheme() == "data" {
return Err(crate::Error::InvalidWindowUrl(
"data URLs are not supported without the `window-data-url` feature.",
));
}
#[cfg(feature = "window-data-url")]
if let Some(csp) = self.csp() {
if url.scheme() == "data" {
if let Ok(data_url) = data_url::DataUrl::process(url.as_str()) {
let (body, _) = data_url.decode_to_vec().unwrap();
let html = String::from_utf8_lossy(&body).into_owned();
// naive way to check if it's an html
if html.contains('<') && html.contains('>') {
let mut document = tauri_utils::html::parse(html);
tauri_utils::html::inject_csp(&mut document, &csp.to_string());
url.set_path(&format!("text/html,{}", document.to_string()));
}
}
}
}
pending.url = url.to_string();
if !pending.window_builder.has_icon() {
if let Some(default_window_icon) = self.inner.default_window_icon.clone() {
pending.window_builder = pending
.window_builder
.icon(default_window_icon.try_into()?)?;
}
}
if pending.window_builder.get_menu().is_none() {
if let Some(menu) = &self.inner.menu {
pending = pending.set_menu(menu.clone());
}
}
if is_local {
let label = pending.label.clone();
pending = self.prepare_pending_window(
pending,
&label,
window_labels,
app_handle.clone(),
web_resource_request_handler,
)?;
pending.ipc_handler = Some(self.prepare_ipc_handler(app_handle));
}
// in `Windows`, we need to force a data_directory
// but we do respect user-specification
#[cfg(any(target_os = "linux", target_os = "windows"))]
if pending.webview_attributes.data_directory.is_none() {
let local_app_data = resolve_path(
&self.inner.config,
&self.inner.package_info,
self.inner.state.get::<crate::Env>().inner(),
&self.inner.config.tauri.bundle.identifier,
Some(BaseDirectory::LocalData),
);
if let Ok(user_data_dir) = local_app_data {
pending.webview_attributes.data_directory = Some(user_data_dir);
}
}
// make sure the directory is created and available to prevent a panic
if let Some(user_data_dir) = &pending.webview_attributes.data_directory {
if !user_data_dir.exists() {
create_dir_all(user_data_dir)?;
}
}
Ok(pending)
}
Trait Implementations§
source§impl<'r, 'de: 'r, T: Send + Sync + 'static, R: Runtime> CommandArg<'de, R> for State<'r, T>
impl<'r, 'de: 'r, T: Send + Sync + 'static, R: Runtime> CommandArg<'de, R> for State<'r, T>
source§fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>
fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>
Grabs the State
from the CommandItem
. This will never fail.