Files
tauri/examples/api/src-tauri/src/lib.rs
Amr Bashir e1e85dc2a5 chore: fix ci and cleanup leftovers (#6925)
* chore: fix ci and cleanup leftovers

1. removed leftover feature flags
2. remove zip/extract apis which are not used anymore
3. removed leftoever scopes
4. removed leftover allowlist and updater options
5. updated the example api

* remove leftover updater structs

* move updater under bundle and remove uneeded options

* fix cli on linux

* remove create_proxy

* clippy

* more clippy

* clippppy

* readd path api

* fix api example

* remove window allowlist

* remove window from allowlist config

* remove `all` allowlist option

* remove file_move

* lint

* fix windows build

* remvoe unused deps

* remvoe allowlist config option, move protocol to `security > asset_protocol`

* fix diffing features

* fmt and test

* fix scope alias

* change files

* android lint

* simplify allow_file impl

* Revert "simplify allow_file impl"

This reverts commit b8882f2fd0.

* expose scopes

* remove unused error variants

* protocol-asset on docs.rs

* ignore reqwest on udeps

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
2023-05-13 13:48:28 +03:00

135 lines
3.6 KiB
Rust

// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
mod cmd;
#[cfg(desktop)]
mod tray;
use serde::Serialize;
use tauri::{window::WindowBuilder, App, AppHandle, RunEvent, WindowUrl};
use tauri_plugin_sample::{PingRequest, SampleExt};
#[derive(Clone, Serialize)]
struct Reply {
data: String,
}
pub type SetupHook = Box<dyn FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send>;
pub type OnEvent = Box<dyn FnMut(&AppHandle, RunEvent)>;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
#[allow(unused_mut)]
let mut builder = tauri::Builder::default()
.plugin(
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)
.plugin(tauri_plugin_sample::init())
.setup(move |app| {
#[cfg(desktop)]
{
tray::create_tray(app)?;
app.handle().plugin(tauri_plugin_cli::init())?;
}
let mut window_builder = WindowBuilder::new(app, "main", WindowUrl::default());
#[cfg(desktop)]
{
window_builder = window_builder
.title("Tauri API Validation")
.inner_size(1000., 800.)
.min_inner_size(600., 400.)
.content_protected(true);
}
let window = window_builder.build().unwrap();
#[cfg(debug_assertions)]
window.open_devtools();
let value = Some("test".to_string());
let response = app.sample().ping(PingRequest {
value: value.clone(),
});
log::info!("got response: {:?}", response);
if let Ok(res) = response {
assert_eq!(res.value, value);
}
#[cfg(desktop)]
std::thread::spawn(|| {
let server = match tiny_http::Server::http("localhost:3003") {
Ok(s) => s,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
};
loop {
if let Ok(mut request) = server.recv() {
let mut body = Vec::new();
let _ = request.as_reader().read_to_end(&mut body);
let response = tiny_http::Response::new(
tiny_http::StatusCode(200),
request.headers().to_vec(),
std::io::Cursor::new(body),
request.body_length(),
None,
);
let _ = request.respond(response);
}
}
});
Ok(())
})
.on_page_load(|window, _| {
let window_ = window.clone();
window.listen("js-event", move |event| {
println!("got js-event with message '{:?}'", event.payload());
let reply = Reply {
data: "something else".to_string(),
};
window_
.emit("rust-event", Some(reply))
.expect("failed to emit");
});
});
#[cfg(target_os = "macos")]
{
builder = builder.menu(tauri::Menu::os_default("Tauri API Validation"));
}
#[allow(unused_mut)]
let mut app = builder
.invoke_handler(tauri::generate_handler![
cmd::log_operation,
cmd::perform_request,
])
.build(tauri::tauri_build_context!())
.expect("error while building tauri application");
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Regular);
app.run(move |_app_handle, _event| {
#[cfg(desktop)]
if let RunEvent::ExitRequested { api, .. } = &_event {
// Keep the event loop running even if all windows are closed
// This allow us to catch system tray events when there is no window
api.prevent_exit();
}
})
}