#![allow(clippy::tabs_in_doc_comments)]
#[cfg(feature = "system-tray")]
pub(crate) mod tray;
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{mpsc::Sender, Arc, Weak}
};
use millennium_macros::default_runtime;
use millennium_runtime::window::{
dpi::{PhysicalPosition, PhysicalSize},
FileDropEvent
};
use millennium_utils::PackageInfo;
use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
use crate::runtime::RuntimeHandle;
#[cfg(feature = "system-tray")]
use crate::runtime::SystemTrayEvent as RuntimeSystemTrayEvent;
#[cfg(shell_scope)]
use crate::scope::ShellScope;
#[cfg(updater)]
use crate::updater;
#[cfg(target_os = "macos")]
use crate::ActivationPolicy;
use crate::{
api::ipc::CallbackFn,
command::{CommandArg, CommandItem},
hooks::{window_invoke_responder, InvokeHandler, InvokeResponder, OnPageLoad, PageLoadPayload, SetupHook},
manager::{Asset, CustomProtocol, WindowManager},
plugin::{Plugin, PluginStore},
runtime::{
http::{Request as HttpRequest, Response as HttpResponse},
webview::WebviewAttributes,
window::{PendingWindow, WindowEvent as RuntimeWindowEvent},
ExitRequestedEventAction, RunEvent as RuntimeRunEvent
},
scope::FsScope,
sealed::{ManagerBase, RuntimeOrDispatch},
utils::config::Config,
utils::{assets::Assets, resources::resource_relpath, Env},
Context, EventLoopMessage, Invoke, InvokeError, InvokeResponse, Manager, Runtime, Scopes, StateManager, Theme, Window
};
pub(crate) type GlobalMenuEventListener<R> = Box<dyn Fn(WindowMenuEvent<R>) + Send + Sync>;
pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
#[cfg(feature = "system-tray")]
type SystemTrayEventListener<R> = Box<dyn Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync>;
#[derive(Debug)]
pub struct ExitRequestApi(Sender<ExitRequestedEventAction>);
impl ExitRequestApi {
pub fn prevent_exit(&self) {
self.0.send(ExitRequestedEventAction::Prevent).unwrap();
}
}
#[derive(Debug, Clone)]
pub struct CloseRequestApi(Sender<bool>);
impl CloseRequestApi {
pub fn prevent_close(&self) {
self.0.send(true).unwrap();
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum WindowEvent {
Resized(PhysicalSize<u32>),
Moved(PhysicalPosition<i32>),
#[non_exhaustive]
CloseRequested {
api: CloseRequestApi
},
Destroyed,
Focused(bool),
#[non_exhaustive]
ScaleFactorChanged {
scale_factor: f64,
new_inner_size: PhysicalSize<u32>
},
FileDrop(FileDropEvent),
ThemeChanged(Theme)
}
impl From<RuntimeWindowEvent> for WindowEvent {
fn from(event: RuntimeWindowEvent) -> Self {
match event {
RuntimeWindowEvent::Resized(size) => Self::Resized(size),
RuntimeWindowEvent::Moved(position) => Self::Moved(position),
RuntimeWindowEvent::CloseRequested { signal_tx } => Self::CloseRequested { api: CloseRequestApi(signal_tx) },
RuntimeWindowEvent::Destroyed => Self::Destroyed,
RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
RuntimeWindowEvent::ScaleFactorChanged { scale_factor, new_inner_size } => Self::ScaleFactorChanged { scale_factor, new_inner_size },
RuntimeWindowEvent::FileDrop(event) => Self::FileDrop(event),
RuntimeWindowEvent::ThemeChanged(theme) => Self::ThemeChanged(theme)
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum RunEvent {
Exit,
#[non_exhaustive]
ExitRequested {
api: ExitRequestApi
},
#[non_exhaustive]
WindowEvent {
label: String,
event: WindowEvent
},
Ready,
Resumed,
MainEventsCleared,
#[cfg(updater)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "updater")))]
Updater(crate::UpdaterEvent)
}
impl From<EventLoopMessage> for RunEvent {
fn from(event: EventLoopMessage) -> Self {
match event {
#[cfg(updater)]
EventLoopMessage::Updater(event) => RunEvent::Updater(event)
}
}
}
#[default_runtime(crate::MillenniumWebview, millennium_webview)]
#[derive(Debug)]
pub struct WindowMenuEvent<R: Runtime> {
pub(crate) menu_item_id: MenuId,
pub(crate) window: Window<R>
}
impl<R: Runtime> WindowMenuEvent<R> {
pub fn menu_item_id(&self) -> MenuIdRef<'_> {
&self.menu_item_id
}
pub fn window(&self) -> &Window<R> {
&self.window
}
}
#[default_runtime(crate::MillenniumWebview, millennium_webview)]
#[derive(Debug)]
pub struct GlobalWindowEvent<R: Runtime> {
pub(crate) event: WindowEvent,
pub(crate) window: Window<R>
}
impl<R: Runtime> GlobalWindowEvent<R> {
pub fn event(&self) -> &WindowEvent {
&self.event
}
pub fn window(&self) -> &Window<R> {
&self.window
}
}
#[cfg(updater)]
#[derive(Debug, Clone, Default)]
pub(crate) struct UpdaterSettings {
pub(crate) target: Option<String>
}
#[derive(Debug, Clone)]
pub struct PathResolver {
env: Env,
config: Arc<Config>,
package_info: PackageInfo
}
impl PathResolver {
pub fn resource_dir(&self) -> Option<PathBuf> {
crate::api::path::resource_dir(&self.package_info, &self.env)
}
pub fn resolve_resource<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf> {
self.resource_dir().map(|dir| dir.join(resource_relpath(path.as_ref())))
}
pub fn app_dir(&self) -> Option<PathBuf> {
crate::api::path::app_dir(&self.config)
}
pub fn log_dir(&self) -> Option<PathBuf> {
crate::api::path::log_dir(&self.config)
}
}
#[derive(Debug, Clone)]
pub struct AssetResolver<R: Runtime> {
manager: WindowManager<R>
}
impl<R: Runtime> AssetResolver<R> {
pub fn get(&self, path: String) -> Option<Asset> {
self.manager.get_asset(path).ok()
}
}
#[default_runtime(crate::MillenniumWebview, millennium_webview)]
#[derive(Debug)]
pub struct AppHandle<R: Runtime> {
runtime_handle: R::Handle,
manager: WindowManager<R>,
#[cfg(feature = "global-shortcut")]
global_shortcut_manager: R::GlobalShortcutManager,
#[cfg(feature = "clipboard")]
clipboard_manager: R::ClipboardManager,
#[cfg(feature = "system-tray")]
tray_handle: Option<tray::SystemTrayHandle<R>>,
#[cfg(updater)]
pub(crate) updater_settings: UpdaterSettings
}
impl<R: Runtime> AppHandle<R> {
#[allow(dead_code)]
pub(crate) fn create_proxy(&self) -> R::EventLoopProxy {
self.runtime_handle.create_proxy()
}
}
#[cfg(feature = "millennium_webview")]
impl AppHandle<crate::MillenniumWebview> {
pub fn create_core_window<F: FnOnce() -> (String, millennium_runtime_webview::MillenniumWindowBuilder) + Send + 'static>(
&self,
f: F
) -> crate::Result<Weak<millennium_runtime_webview::Window>> {
self.runtime_handle.create_core_window(f).map_err(Into::into)
}
pub fn send_core_window_event(
&self,
window_id: millennium_runtime_webview::WindowId,
message: millennium_runtime_webview::WindowMessage
) -> crate::Result<()> {
self.runtime_handle
.send_event(millennium_runtime_webview::Message::Window(self.runtime_handle.window_id(window_id), message))
.map_err(Into::into)
}
}
impl<R: Runtime> Clone for AppHandle<R> {
fn clone(&self) -> Self {
Self {
runtime_handle: self.runtime_handle.clone(),
manager: self.manager.clone(),
#[cfg(feature = "global-shortcut")]
global_shortcut_manager: self.global_shortcut_manager.clone(),
#[cfg(feature = "clipboard")]
clipboard_manager: self.clipboard_manager.clone(),
#[cfg(feature = "system-tray")]
tray_handle: self.tray_handle.clone(),
#[cfg(updater)]
updater_settings: self.updater_settings.clone()
}
}
}
impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError> {
Ok(command.message.window().app_handle)
}
}
impl<R: Runtime> AppHandle<R> {
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
self.runtime_handle.run_on_main_thread(f).map_err(Into::into)
}
#[cfg(all(windows, feature = "system-tray"))]
#[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]
fn remove_system_tray(&self) -> crate::Result<()> {
self.runtime_handle.remove_system_tray().map_err(Into::into)
}
pub fn plugin<P: Plugin<R> + 'static>(&self, mut plugin: P) -> crate::Result<()> {
plugin
.initialize(self, self.config().plugins.0.get(plugin.name()).cloned().unwrap_or_default())
.map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))?;
self.manager().inner.plugins.lock().unwrap().register(plugin);
Ok(())
}
pub fn exit(&self, exit_code: i32) {
self.cleanup_before_exit();
std::process::exit(exit_code);
}
pub fn restart(&self) {
self.cleanup_before_exit();
crate::api::process::restart(&self.env());
}
fn cleanup_before_exit(&self) {
#[cfg(any(shell_execute, shell_sidecar))]
{
crate::api::process::kill_children();
}
#[cfg(all(windows, feature = "system-tray"))]
{
let _ = self.remove_system_tray();
}
}
}
impl<R: Runtime> Manager<R> for AppHandle<R> {}
impl<R: Runtime> ManagerBase<R> for AppHandle<R> {
fn manager(&self) -> &WindowManager<R> {
&self.manager
}
fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
RuntimeOrDispatch::RuntimeHandle(self.runtime_handle.clone())
}
fn managed_app_handle(&self) -> AppHandle<R> {
self.clone()
}
}
#[default_runtime(crate::MillenniumWebview, millennium_webview)]
#[derive(Debug)]
pub struct App<R: Runtime> {
runtime: Option<R>,
manager: WindowManager<R>,
#[cfg(feature = "global-shortcut")]
global_shortcut_manager: R::GlobalShortcutManager,
#[cfg(feature = "clipboard")]
clipboard_manager: R::ClipboardManager,
#[cfg(feature = "system-tray")]
tray_handle: Option<tray::SystemTrayHandle<R>>,
handle: AppHandle<R>
}
impl<R: Runtime> Manager<R> for App<R> {}
impl<R: Runtime> ManagerBase<R> for App<R> {
fn manager(&self) -> &WindowManager<R> {
&self.manager
}
fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
RuntimeOrDispatch::Runtime(self.runtime.as_ref().unwrap())
}
fn managed_app_handle(&self) -> AppHandle<R> {
self.handle()
}
}
#[cfg(feature = "millennium_webview")]
impl App<crate::MillenniumWebview> {
pub fn webview_plugin<P: millennium_runtime_webview::Plugin<EventLoopMessage> + 'static>(&mut self, plugin: P) {
self.runtime.as_mut().unwrap().plugin(plugin);
}
}
macro_rules! shared_app_impl {
($app: ty) => {
impl<R: Runtime> $app {
#[cfg(updater)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "updater")))]
#[cfg_attr(feature = "updater", doc = r#" let response = handle.updater().check().await;"#)]
pub fn updater(&self) -> updater::UpdateBuilder<R> {
updater::builder(self.app_handle())
}
#[cfg(feature = "system-tray")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
pub fn tray_handle(&self) -> tray::SystemTrayHandle<R> {
self.tray_handle
.clone()
.expect("tray not configured; use the `Builder#system_tray` API first.")
}
pub fn path_resolver(&self) -> PathResolver {
PathResolver {
env: self.state::<Env>().inner().clone(),
config: self.manager.config(),
package_info: self.manager.package_info().clone()
}
}
#[cfg(feature = "global-shortcut")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "global-shortcut")))]
pub fn global_shortcut_manager(&self) -> R::GlobalShortcutManager {
self.global_shortcut_manager.clone()
}
#[cfg(feature = "clipboard")]
pub fn clipboard_manager(&self) -> R::ClipboardManager {
self.clipboard_manager.clone()
}
pub fn config(&self) -> Arc<Config> {
self.manager.config()
}
pub fn package_info(&self) -> &PackageInfo {
self.manager.package_info()
}
pub fn asset_resolver(&self) -> AssetResolver<R> {
AssetResolver { manager: self.manager.clone() }
}
}
};
}
shared_app_impl!(App<R>);
shared_app_impl!(AppHandle<R>);
impl<R: Runtime> App<R> {
pub fn handle(&self) -> AppHandle<R> {
self.handle.clone()
}
#[cfg(target_os = "macos")]
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
pub fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
self.runtime.as_mut().unwrap().set_activation_policy(activation_policy);
}
#[cfg(cli)]
pub fn get_cli_matches(&self) -> crate::Result<crate::api::cli::Matches> {
if let Some(cli) = &self.manager.config().millennium.cli {
crate::api::cli::get_matches(cli, self.manager.package_info()).map_err(Into::into)
} else {
Ok(Default::default())
}
}
pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
let app_handle = self.handle();
let manager = self.manager.clone();
self.runtime.take().unwrap().run(move |event| match event {
RuntimeRunEvent::Exit => {
app_handle.cleanup_before_exit();
on_event_loop_event(&app_handle, RuntimeRunEvent::Exit, &manager, Some(&mut callback));
}
_ => {
on_event_loop_event(&app_handle, event, &manager, Some(&mut callback));
}
});
}
pub fn run_iteration(&mut self) -> crate::runtime::RunIteration {
let manager = self.manager.clone();
let app_handle = self.handle();
self.runtime
.as_mut()
.unwrap()
.run_iteration(move |event| on_event_loop_event(&app_handle, event, &manager, Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None))
}
}
#[cfg(updater)]
impl<R: Runtime> App<R> {
fn run_updater_dialog(&self) {
let handle = self.handle();
crate::async_runtime::spawn(async move { updater::check_update_with_dialog(handle).await });
}
fn run_updater(&self) {
let handle = self.handle();
let handle_ = handle.clone();
let updater_config = self.manager.config().millennium.updater.clone();
if updater_config.active {
if updater_config.dialog {
self.run_updater_dialog();
handle.listen_global(updater::EVENT_CHECK_UPDATE, move |_msg| {
let handle = handle_.clone();
crate::async_runtime::spawn(async move { updater::check_update_with_dialog(handle).await });
});
} else {
updater::listener(handle);
}
}
}
}
#[allow(clippy::type_complexity)]
pub struct Builder<R: Runtime> {
#[cfg(any(windows, target_os = "linux"))]
runtime_any_thread: bool,
invoke_handler: Box<InvokeHandler<R>>,
invoke_responder: Arc<InvokeResponder<R>>,
invoke_initialization_script: String,
setup: SetupHook<R>,
on_page_load: Box<OnPageLoad<R>>,
pending_windows: Vec<PendingWindow<EventLoopMessage, R>>,
plugins: PluginStore<R>,
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol<R>>>,
state: StateManager,
menu: Option<Menu>,
menu_event_listeners: Vec<GlobalMenuEventListener<R>>,
window_event_listeners: Vec<GlobalWindowEventListener<R>>,
#[cfg(feature = "system-tray")]
system_tray: Option<tray::SystemTray>,
#[cfg(feature = "system-tray")]
system_tray_event_listeners: Vec<SystemTrayEventListener<R>>,
#[cfg(updater)]
updater_settings: UpdaterSettings
}
impl<R: Runtime> Builder<R> {
pub fn new() -> Self {
Self {
#[cfg(any(windows, target_os = "linux"))]
runtime_any_thread: false,
setup: Box::new(|_| Ok(())),
invoke_handler: Box::new(|_| ()),
invoke_responder: Arc::new(window_invoke_responder),
invoke_initialization_script:
"Object.defineProperty(window, '__MILLENNIUM_POST_MESSAGE__', { value: message => window.ipc.postMessage(JSON.stringify(message)) })".into(),
on_page_load: Box::new(|_, _| ()),
pending_windows: Default::default(),
plugins: PluginStore::default(),
uri_scheme_protocols: Default::default(),
state: StateManager::new(),
menu: None,
menu_event_listeners: Vec::new(),
window_event_listeners: Vec::new(),
#[cfg(feature = "system-tray")]
system_tray: None,
#[cfg(feature = "system-tray")]
system_tray_event_listeners: Vec::new(),
#[cfg(updater)]
updater_settings: Default::default()
}
}
#[cfg(any(windows, target_os = "linux"))]
#[cfg_attr(doc_cfg, doc(cfg(any(windows, target_os = "linux"))))]
#[must_use]
pub fn any_thread(mut self) -> Self {
self.runtime_any_thread = true;
self
}
#[must_use]
pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
where
F: Fn(Invoke<R>) + Send + Sync + 'static
{
self.invoke_handler = Box::new(invoke_handler);
self
}
#[must_use]
pub fn invoke_system<F>(mut self, initialization_script: String, responder: F) -> Self
where
F: Fn(Window<R>, InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static
{
self.invoke_initialization_script = initialization_script;
self.invoke_responder = Arc::new(responder);
self
}
#[cfg_attr(
feature = "dialog",
doc = r#" millennium::api::dialog::blocking::message(Some(&main_window), "Hello", "Welcome back!");"#
)]
#[must_use]
pub fn setup<F>(mut self, setup: F) -> Self
where
F: FnOnce(&mut App<R>) -> Result<(), Box<dyn std::error::Error>> + Send + 'static
{
self.setup = Box::new(setup);
self
}
#[must_use]
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
where
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static
{
self.on_page_load = Box::new(on_page_load);
self
}
#[must_use]
pub fn plugin<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
self.plugins.register(plugin);
self
}
#[must_use]
pub fn manage<T>(self, state: T) -> Self
where
T: Send + Sync + 'static
{
let type_name = std::any::type_name::<T>();
assert!(self.state.set(state), "state for type '{}' is already being managed", type_name);
self
}
#[cfg(feature = "system-tray")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
#[must_use]
pub fn system_tray(mut self, system_tray: tray::SystemTray) -> Self {
self.system_tray.replace(system_tray);
self
}
#[must_use]
pub fn menu(mut self, menu: Menu) -> Self {
self.menu.replace(menu);
self
}
#[cfg_attr(
feature = "shell-open-api",
doc = r#" api::shell::open(&event.window().shell_scope(), "https://github.com/pykeio/millennium".to_string(), None).unwrap();"#
)]
#[must_use]
pub fn on_menu_event<F: Fn(WindowMenuEvent<R>) + Send + Sync + 'static>(mut self, handler: F) -> Self {
self.menu_event_listeners.push(Box::new(handler));
self
}
#[must_use]
pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(mut self, handler: F) -> Self {
self.window_event_listeners.push(Box::new(handler));
self
}
#[cfg(feature = "system-tray")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
#[must_use]
pub fn on_system_tray_event<F: Fn(&AppHandle<R>, tray::SystemTrayEvent) + Send + Sync + 'static>(mut self, handler: F) -> Self {
self.system_tray_event_listeners.push(Box::new(handler));
self
}
#[must_use]
pub fn register_uri_scheme_protocol<
N: Into<String>,
H: Fn(&AppHandle<R>, &HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync + 'static
>(
mut self,
uri_scheme: N,
protocol: H
) -> Self {
self.uri_scheme_protocols
.insert(uri_scheme.into(), Arc::new(CustomProtocol { protocol: Box::new(protocol) }));
self
}
#[cfg(updater)]
pub fn updater_target<T: Into<String>>(mut self, target: T) -> Self {
self.updater_settings.target.replace(target.into());
self
}
#[allow(clippy::type_complexity)]
pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
#[cfg(feature = "system-tray")]
let system_tray_icon = context.system_tray_icon.clone();
#[cfg(all(feature = "system-tray", target_os = "macos"))]
let system_tray_icon_as_template = context
.config
.millennium
.system_tray
.as_ref()
.map(|t| t.icon_as_template)
.unwrap_or_default();
#[cfg(shell_scope)]
let shell_scope = context.shell_scope.clone();
let manager = WindowManager::with_handlers(
context,
self.plugins,
self.invoke_handler,
self.on_page_load,
self.uri_scheme_protocols,
self.state,
self.window_event_listeners,
(self.menu, self.menu_event_listeners),
(self.invoke_responder, self.invoke_initialization_script)
);
for config in manager.config().millennium.windows.clone() {
let url = config.url.clone();
let label = config.label.clone();
let file_drop_enabled = config.file_drop_enabled;
let mut webview_attributes = WebviewAttributes::new(url);
if !file_drop_enabled {
webview_attributes = webview_attributes.disable_file_drop_handler();
}
self.pending_windows.push(PendingWindow::with_config(config, webview_attributes, label)?);
}
#[cfg(any(windows, target_os = "linux"))]
let runtime = if self.runtime_any_thread { R::new_any_thread()? } else { R::new()? };
#[cfg(not(any(windows, target_os = "linux")))]
let runtime = R::new()?;
let runtime_handle = runtime.handle();
#[cfg(feature = "global-shortcut")]
let global_shortcut_manager = runtime.global_shortcut_manager();
#[cfg(feature = "clipboard")]
let clipboard_manager = runtime.clipboard_manager();
let mut app = App {
runtime: Some(runtime),
manager: manager.clone(),
#[cfg(feature = "global-shortcut")]
global_shortcut_manager: global_shortcut_manager.clone(),
#[cfg(feature = "clipboard")]
clipboard_manager: clipboard_manager.clone(),
#[cfg(feature = "system-tray")]
tray_handle: None,
handle: AppHandle {
runtime_handle,
manager,
#[cfg(feature = "global-shortcut")]
global_shortcut_manager,
#[cfg(feature = "clipboard")]
clipboard_manager,
#[cfg(feature = "system-tray")]
tray_handle: None,
#[cfg(updater)]
updater_settings: self.updater_settings
}
};
let env = Env::default();
app.manage(Scopes {
fs: FsScope::for_fs_api(&app.manager.config(), app.package_info(), &env, &app.config().millennium.allowlist.fs.scope)?,
#[cfg(protocol_asset)]
asset_protocol: FsScope::for_fs_api(&app.manager.config(), app.package_info(), &env, &app.config().millennium.allowlist.protocol.asset_scope)?,
#[cfg(http_request)]
http: crate::scope::HttpScope::for_http_api(&app.config().millennium.allowlist.http.scope),
#[cfg(shell_scope)]
shell: ShellScope::new(shell_scope)
});
app.manage(env);
#[cfg(windows)]
{
if let Some(w) = &app.manager.config().millennium.bundle.windows.webview_fixed_runtime_path {
if let Some(resource_dir) = app.path_resolver().resource_dir() {
std::env::set_var("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", resource_dir.join(w));
} else {
#[cfg(debug_assertions)]
eprintln!("failed to resolve resource directory; fallback to the installed Webview2 runtime.");
}
}
}
#[cfg(feature = "system-tray")]
if let Some(system_tray) = self.system_tray {
#[allow(unused_mut)] let mut ids = HashMap::new();
if let Some(menu) = system_tray.menu() {
tray::get_menu_ids(&mut ids, menu);
}
let tray_icon = if let Some(icon) = system_tray.icon {
Some(icon)
} else if let Some(tray_icon) = system_tray_icon {
Some(tray_icon.try_into()?)
} else {
None
};
let mut tray = tray::SystemTray::new().with_icon(tray_icon.expect("tray icon not found; please configure it in .millenniumrc"));
if let Some(menu) = system_tray.menu {
tray = tray.with_menu(menu);
}
#[cfg(target_os = "macos")]
let tray = tray.with_icon_as_template(system_tray_icon_as_template);
let tray_handler = app.runtime.as_ref().unwrap().system_tray(tray).expect("failed to run tray");
let tray_handle = tray::SystemTrayHandle {
ids: Arc::new(std::sync::Mutex::new(ids)),
inner: tray_handler
};
let ids = tray_handle.ids.clone();
app.tray_handle.replace(tray_handle.clone());
app.handle.tray_handle.replace(tray_handle);
for listener in self.system_tray_event_listeners {
let app_handle = app.handle();
let ids = ids.clone();
let listener = Arc::new(std::sync::Mutex::new(listener));
app.runtime.as_mut().unwrap().on_system_tray_event(move |event| {
let app_handle = app_handle.clone();
let event = match event {
RuntimeSystemTrayEvent::MenuItemClick(id) => tray::SystemTrayEvent::MenuItemClick {
id: ids.lock().unwrap().get(id).unwrap().clone()
},
RuntimeSystemTrayEvent::LeftClick { position, size } => tray::SystemTrayEvent::LeftClick { position: *position, size: *size },
RuntimeSystemTrayEvent::RightClick { position, size } => tray::SystemTrayEvent::RightClick { position: *position, size: *size },
RuntimeSystemTrayEvent::DoubleClick { position, size } => tray::SystemTrayEvent::DoubleClick { position: *position, size: *size }
};
let listener = listener.clone();
listener.lock().unwrap()(&app_handle, event);
});
}
}
app.manager.initialize_plugins(&app.handle())?;
let window_labels = self.pending_windows.iter().map(|p| p.label.clone()).collect::<Vec<_>>();
for pending in self.pending_windows {
let pending = app.manager.prepare_window(app.handle.clone(), pending, &window_labels, None)?;
let detached = app.runtime.as_ref().unwrap().create_window(pending)?;
let _window = app.manager.attach_window(app.handle(), detached);
}
(self.setup)(&mut app).map_err(|e| crate::Error::Setup(e.into()))?;
#[cfg(updater)]
app.run_updater();
Ok(app)
}
pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
self.build(context)?.run(|_, _| {});
Ok(())
}
}
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
app_handle: &AppHandle<R>,
event: RuntimeRunEvent<EventLoopMessage>,
manager: &WindowManager<R>,
callback: Option<&mut F>
) {
if let RuntimeRunEvent::WindowEvent {
label,
event: RuntimeWindowEvent::Destroyed
} = &event
{
manager.on_window_close(label);
}
let event = match event {
RuntimeRunEvent::Exit => RunEvent::Exit,
RuntimeRunEvent::ExitRequested { tx } => RunEvent::ExitRequested { api: ExitRequestApi(tx) },
RuntimeRunEvent::WindowEvent { label, event } => RunEvent::WindowEvent { label, event: event.into() },
RuntimeRunEvent::Ready => {
#[cfg(all(dev, target_os = "macos"))]
unsafe {
use cocoa::{
appkit::NSImage,
base::{id, nil},
foundation::NSData
};
use objc::*;
if let Some(icon) = app_handle.manager.inner.app_icon.clone() {
let ns_app: id = msg_send![class!(NSApplication), sharedApplication];
let data = NSData::dataWithBytes_length_(nil, icon.as_ptr() as *const std::os::raw::c_void, icon.len() as u64);
let app_icon = NSImage::initWithData_(NSImage::alloc(nil), data);
let _: () = msg_send![ns_app, setApplicationIconImage: app_icon];
}
}
RunEvent::Ready
}
RuntimeRunEvent::Resumed => RunEvent::Resumed,
RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
RuntimeRunEvent::UserEvent(t) => t.into(),
_ => unimplemented!()
};
manager.inner.plugins.lock().expect("poisoned plugin store").on_event(app_handle, &event);
if let Some(c) = callback {
c(app_handle, event);
}
}
#[cfg(feature = "millennium_webview")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "millennium_webview")))]
impl Default for Builder<crate::MillenniumWebview> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
#[test]
fn is_send_sync() {
crate::test_utils::assert_send::<super::AppHandle>();
crate::test_utils::assert_sync::<super::AppHandle>();
#[cfg(feature = "millennium_webview")]
{
crate::test_utils::assert_send::<super::AssetResolver<crate::MillenniumWebview>>();
crate::test_utils::assert_sync::<super::AssetResolver<crate::MillenniumWebview>>();
}
crate::test_utils::assert_send::<super::PathResolver>();
crate::test_utils::assert_sync::<super::PathResolver>();
}
}