123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900 |
- // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- //! [](https://tauri.app)
- //!
- //! The [`wry`] Tauri [`Runtime`].
- #![doc(
- html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
- html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
- )]
- use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
- use tauri_runtime::{
- monitor::Monitor,
- webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler},
- window::{
- dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
- CursorIcon, DetachedWindow, FileDropEvent, PendingWindow, RawWindow, WebviewEvent,
- WindowBuilder, WindowBuilderBase, WindowEvent, WindowId,
- },
- DeviceEventFilter, Error, EventLoopProxy, ExitRequestedEventAction, Icon, ProgressBarState,
- ProgressBarStatus, Result, RunEvent, Runtime, RuntimeHandle, RuntimeInitArgs, UserAttentionType,
- UserEvent, WebviewDispatch, WebviewEventId, WindowDispatch, WindowEventId,
- };
- #[cfg(target_os = "macos")]
- use tao::platform::macos::{EventLoopWindowTargetExtMacOS, WindowBuilderExtMacOS};
- #[cfg(target_os = "linux")]
- use tao::platform::unix::{WindowBuilderExtUnix, WindowExtUnix};
- #[cfg(windows)]
- use tao::platform::windows::{WindowBuilderExtWindows, WindowExtWindows};
- #[cfg(windows)]
- use webview2_com::FocusChangedEventHandler;
- #[cfg(windows)]
- use windows::Win32::{Foundation::HWND, System::WinRT::EventRegistrationToken};
- #[cfg(windows)]
- use wry::WebViewBuilderExtWindows;
- use tao::{
- dpi::{
- LogicalPosition as TaoLogicalPosition, LogicalSize as TaoLogicalSize,
- PhysicalPosition as TaoPhysicalPosition, PhysicalSize as TaoPhysicalSize,
- Position as TaoPosition, Size as TaoSize,
- },
- event::{Event, StartCause, WindowEvent as TaoWindowEvent},
- event_loop::{
- ControlFlow, DeviceEventFilter as TaoDeviceEventFilter, EventLoop, EventLoopBuilder,
- EventLoopProxy as TaoEventLoopProxy, EventLoopWindowTarget,
- },
- monitor::MonitorHandle,
- window::{
- CursorIcon as TaoCursorIcon, Fullscreen, Icon as TaoWindowIcon,
- ProgressBarState as TaoProgressBarState, ProgressState as TaoProgressState, Theme as TaoTheme,
- UserAttentionType as TaoUserAttentionType,
- },
- };
- #[cfg(target_os = "macos")]
- use tauri_utils::TitleBarStyle;
- use tauri_utils::{config::WindowConfig, debug_eprintln, Theme};
- use url::Url;
- use wry::{
- FileDropEvent as WryFileDropEvent, ProxyConfig, ProxyEndpoint, WebContext, WebView,
- WebViewBuilder,
- };
- pub use tao;
- pub use tao::window::{Window, WindowBuilder as TaoWindowBuilder, WindowId as TaoWindowId};
- pub use wry;
- pub use wry::webview_version;
- #[cfg(windows)]
- use wry::WebViewExtWindows;
- #[cfg(target_os = "android")]
- use wry::{
- prelude::{dispatch, find_class},
- WebViewBuilderExtAndroid, WebViewExtAndroid,
- };
- #[cfg(not(any(
- target_os = "windows",
- target_os = "macos",
- target_os = "ios",
- target_os = "android"
- )))]
- use wry::{WebViewBuilderExtUnix, WebViewExtUnix};
- #[cfg(target_os = "macos")]
- pub use tao::platform::macos::{
- ActivationPolicy as TaoActivationPolicy, EventLoopExtMacOS, WindowExtMacOS,
- };
- #[cfg(target_os = "macos")]
- use tauri_runtime::ActivationPolicy;
- use std::{
- cell::RefCell,
- collections::{
- hash_map::Entry::{Occupied, Vacant},
- BTreeMap, HashMap,
- },
- fmt,
- ops::Deref,
- path::PathBuf,
- rc::Rc,
- sync::{
- atomic::{AtomicBool, AtomicU32, Ordering},
- mpsc::{channel, Sender},
- Arc, Mutex, Weak,
- },
- thread::{current as current_thread, ThreadId},
- };
- pub type WebviewId = u32;
- type IpcHandler = dyn Fn(String) + 'static;
- #[cfg(any(
- windows,
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- mod undecorated_resizing;
- mod webview;
- pub use webview::Webview;
- pub type WebContextStore = Arc<Mutex<HashMap<Option<PathBuf>, WebContext>>>;
- // window
- pub type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
- pub type WindowEventListeners = Arc<Mutex<HashMap<WindowEventId, WindowEventHandler>>>;
- pub type WebviewEventHandler = Box<dyn Fn(&WebviewEvent) + Send>;
- pub type WebviewEventListeners = Arc<Mutex<HashMap<WebviewEventId, WebviewEventHandler>>>;
- #[derive(Debug, Clone, Default)]
- pub struct WindowIdStore(Arc<Mutex<HashMap<TaoWindowId, WindowId>>>);
- impl WindowIdStore {
- pub fn insert(&self, w: TaoWindowId, id: WindowId) {
- self.0.lock().unwrap().insert(w, id);
- }
- fn get(&self, w: &TaoWindowId) -> Option<WindowId> {
- self.0.lock().unwrap().get(w).copied()
- }
- }
- #[macro_export]
- macro_rules! getter {
- ($self: ident, $rx: expr, $message: expr) => {{
- $crate::send_user_message(&$self.context, $message)?;
- $rx
- .recv()
- .map_err(|_| $crate::Error::FailedToReceiveMessage)
- }};
- }
- macro_rules! window_getter {
- ($self: ident, $message: expr) => {{
- let (tx, rx) = channel();
- getter!($self, rx, Message::Window($self.window_id, $message(tx)))
- }};
- }
- macro_rules! webview_getter {
- ($self: ident, $message: expr) => {{
- let (tx, rx) = channel();
- getter!(
- $self,
- rx,
- Message::Webview(
- *$self.window_id.lock().unwrap(),
- $self.webview_id,
- $message(tx)
- )
- )
- }};
- }
- pub(crate) fn send_user_message<T: UserEvent>(
- context: &Context<T>,
- message: Message<T>,
- ) -> Result<()> {
- if current_thread().id() == context.main_thread_id {
- handle_user_message(
- &context.main_thread.window_target,
- message,
- UserMessageContext {
- window_id_map: context.window_id_map.clone(),
- windows: context.main_thread.windows.clone(),
- },
- );
- Ok(())
- } else {
- context
- .proxy
- .send_event(message)
- .map_err(|_| Error::FailedToSendMessage)
- }
- }
- #[derive(Clone)]
- pub struct Context<T: UserEvent> {
- pub window_id_map: WindowIdStore,
- main_thread_id: ThreadId,
- pub proxy: TaoEventLoopProxy<Message<T>>,
- main_thread: DispatcherMainThreadContext<T>,
- plugins: Arc<Mutex<Vec<Box<dyn Plugin<T> + Send>>>>,
- next_window_id: Arc<AtomicU32>,
- next_webview_id: Arc<AtomicU32>,
- next_window_event_id: Arc<AtomicU32>,
- next_webview_event_id: Arc<AtomicU32>,
- next_webcontext_id: Arc<AtomicU32>,
- }
- impl<T: UserEvent> Context<T> {
- pub fn run_threaded<R, F>(&self, f: F) -> R
- where
- F: FnOnce(Option<&DispatcherMainThreadContext<T>>) -> R,
- {
- f(if current_thread().id() == self.main_thread_id {
- Some(&self.main_thread)
- } else {
- None
- })
- }
- fn next_window_id(&self) -> WindowId {
- self.next_window_id.fetch_add(1, Ordering::Relaxed).into()
- }
- fn next_webview_id(&self) -> WebviewId {
- self.next_webview_id.fetch_add(1, Ordering::Relaxed)
- }
- fn next_window_event_id(&self) -> u32 {
- self.next_window_event_id.fetch_add(1, Ordering::Relaxed)
- }
- fn next_webview_event_id(&self) -> u32 {
- self.next_webview_event_id.fetch_add(1, Ordering::Relaxed)
- }
- fn next_webcontext_id(&self) -> u32 {
- self.next_webcontext_id.fetch_add(1, Ordering::Relaxed)
- }
- }
- impl<T: UserEvent> Context<T> {
- fn create_window<F: Fn(RawWindow) + Send + 'static>(
- &self,
- pending: PendingWindow<T, Wry<T>>,
- after_window_creation: Option<F>,
- ) -> Result<DetachedWindow<T, Wry<T>>> {
- let label = pending.label.clone();
- let context = self.clone();
- let window_id = self.next_window_id();
- let webview_id = pending.webview.as_ref().map(|_| context.next_webview_id());
- send_user_message(
- self,
- Message::CreateWindow(
- window_id,
- Box::new(move |event_loop| {
- create_window(
- window_id,
- webview_id.unwrap_or_default(),
- event_loop,
- &context,
- pending,
- after_window_creation,
- )
- }),
- ),
- )?;
- let dispatcher = WryWindowDispatcher {
- window_id,
- context: self.clone(),
- };
- let detached_webview = webview_id.map(|id| DetachedWebview {
- label: label.clone(),
- dispatcher: WryWebviewDispatcher {
- window_id: Arc::new(Mutex::new(window_id)),
- webview_id: id,
- context: self.clone(),
- },
- });
- Ok(DetachedWindow {
- id: window_id,
- label,
- dispatcher,
- webview: detached_webview,
- })
- }
- fn create_webview(
- &self,
- window_id: WindowId,
- pending: PendingWebview<T, Wry<T>>,
- ) -> Result<DetachedWebview<T, Wry<T>>> {
- let label = pending.label.clone();
- let context = self.clone();
- let webview_id = self.next_webview_id();
- let window_id_wrapper = Arc::new(Mutex::new(window_id));
- let window_id_wrapper_ = window_id_wrapper.clone();
- send_user_message(
- self,
- Message::CreateWebview(
- window_id,
- Box::new(move |window| {
- create_webview(
- WebviewKind::WindowChild,
- window,
- window_id_wrapper_,
- webview_id,
- &context,
- pending,
- )
- }),
- ),
- )?;
- let dispatcher = WryWebviewDispatcher {
- window_id: window_id_wrapper,
- webview_id,
- context: self.clone(),
- };
- Ok(DetachedWebview { label, dispatcher })
- }
- }
- #[cfg(feature = "tracing")]
- #[derive(Debug, Clone, Default)]
- pub struct ActiveTraceSpanStore(Rc<RefCell<Vec<ActiveTracingSpan>>>);
- #[cfg(feature = "tracing")]
- impl ActiveTraceSpanStore {
- pub fn remove_window_draw(&self, window_id: TaoWindowId) {
- let mut store = self.0.borrow_mut();
- if let Some(index) = store
- .iter()
- .position(|t| matches!(t, ActiveTracingSpan::WindowDraw { id, span: _ } if id == &window_id))
- {
- store.remove(index);
- }
- }
- }
- #[cfg(feature = "tracing")]
- #[derive(Debug)]
- pub enum ActiveTracingSpan {
- WindowDraw {
- id: TaoWindowId,
- span: tracing::span::EnteredSpan,
- },
- }
- #[derive(Debug)]
- pub struct WindowsStore(RefCell<BTreeMap<WindowId, WindowWrapper>>);
- // SAFETY: we ensure this type is only used on the main thread.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl Send for WindowsStore {}
- // SAFETY: we ensure this type is only used on the main thread.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl Sync for WindowsStore {}
- #[derive(Debug, Clone)]
- pub struct DispatcherMainThreadContext<T: UserEvent> {
- pub window_target: EventLoopWindowTarget<Message<T>>,
- pub web_context: WebContextStore,
- // changing this to an Rc will cause frequent app crashes.
- pub windows: Arc<WindowsStore>,
- #[cfg(feature = "tracing")]
- pub active_tracing_spans: ActiveTraceSpanStore,
- }
- // SAFETY: we ensure this type is only used on the main thread.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl<T: UserEvent> Send for DispatcherMainThreadContext<T> {}
- // SAFETY: we ensure this type is only used on the main thread.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl<T: UserEvent> Sync for DispatcherMainThreadContext<T> {}
- impl<T: UserEvent> fmt::Debug for Context<T> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("Context")
- .field("main_thread_id", &self.main_thread_id)
- .field("proxy", &self.proxy)
- .field("main_thread", &self.main_thread)
- .finish()
- }
- }
- pub struct DeviceEventFilterWrapper(pub TaoDeviceEventFilter);
- impl From<DeviceEventFilter> for DeviceEventFilterWrapper {
- fn from(item: DeviceEventFilter) -> Self {
- match item {
- DeviceEventFilter::Always => Self(TaoDeviceEventFilter::Always),
- DeviceEventFilter::Never => Self(TaoDeviceEventFilter::Never),
- DeviceEventFilter::Unfocused => Self(TaoDeviceEventFilter::Unfocused),
- }
- }
- }
- /// Wrapper around a [`tao::window::Icon`] that can be created from an [`Icon`].
- pub struct TaoIcon(pub TaoWindowIcon);
- fn icon_err<E: std::error::Error + Send + Sync + 'static>(e: E) -> Error {
- Error::InvalidIcon(Box::new(e))
- }
- impl TryFrom<Icon> for TaoIcon {
- type Error = Error;
- fn try_from(icon: Icon) -> std::result::Result<Self, Self::Error> {
- TaoWindowIcon::from_rgba(icon.rgba, icon.width, icon.height)
- .map(Self)
- .map_err(icon_err)
- }
- }
- pub struct WindowEventWrapper(pub Option<WindowEvent>);
- impl WindowEventWrapper {
- fn parse(window: &WindowWrapper, event: &TaoWindowEvent<'_>) -> Self {
- match event {
- // resized event from tao doesn't include a reliable size on macOS
- // because wry replaces the NSView
- TaoWindowEvent::Resized(_) => {
- if let Some(w) = &window.inner {
- Self(Some(WindowEvent::Resized(
- PhysicalSizeWrapper(inner_size(
- w,
- &window.webviews,
- window.has_children.load(Ordering::Relaxed),
- ))
- .into(),
- )))
- } else {
- Self(None)
- }
- }
- e => e.into(),
- }
- }
- }
- pub fn map_theme(theme: &TaoTheme) -> Theme {
- match theme {
- TaoTheme::Light => Theme::Light,
- TaoTheme::Dark => Theme::Dark,
- _ => Theme::Light,
- }
- }
- #[cfg(target_os = "macos")]
- fn tao_activation_policy(activation_policy: ActivationPolicy) -> TaoActivationPolicy {
- match activation_policy {
- ActivationPolicy::Regular => TaoActivationPolicy::Regular,
- ActivationPolicy::Accessory => TaoActivationPolicy::Accessory,
- ActivationPolicy::Prohibited => TaoActivationPolicy::Prohibited,
- _ => unimplemented!(),
- }
- }
- impl<'a> From<&TaoWindowEvent<'a>> for WindowEventWrapper {
- fn from(event: &TaoWindowEvent<'a>) -> Self {
- let event = match event {
- TaoWindowEvent::Resized(size) => WindowEvent::Resized(PhysicalSizeWrapper(*size).into()),
- TaoWindowEvent::Moved(position) => {
- WindowEvent::Moved(PhysicalPositionWrapper(*position).into())
- }
- TaoWindowEvent::Destroyed => WindowEvent::Destroyed,
- TaoWindowEvent::ScaleFactorChanged {
- scale_factor,
- new_inner_size,
- } => WindowEvent::ScaleFactorChanged {
- scale_factor: *scale_factor,
- new_inner_size: PhysicalSizeWrapper(**new_inner_size).into(),
- },
- #[cfg(any(target_os = "linux", target_os = "macos"))]
- TaoWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
- TaoWindowEvent::ThemeChanged(theme) => WindowEvent::ThemeChanged(map_theme(theme)),
- _ => return Self(None),
- };
- Self(Some(event))
- }
- }
- pub struct MonitorHandleWrapper(pub MonitorHandle);
- impl From<MonitorHandleWrapper> for Monitor {
- fn from(monitor: MonitorHandleWrapper) -> Monitor {
- Self {
- name: monitor.0.name(),
- position: PhysicalPositionWrapper(monitor.0.position()).into(),
- size: PhysicalSizeWrapper(monitor.0.size()).into(),
- scale_factor: monitor.0.scale_factor(),
- }
- }
- }
- pub struct PhysicalPositionWrapper<T>(pub TaoPhysicalPosition<T>);
- impl<T> From<PhysicalPositionWrapper<T>> for PhysicalPosition<T> {
- fn from(position: PhysicalPositionWrapper<T>) -> Self {
- Self {
- x: position.0.x,
- y: position.0.y,
- }
- }
- }
- impl<T> From<PhysicalPosition<T>> for PhysicalPositionWrapper<T> {
- fn from(position: PhysicalPosition<T>) -> Self {
- Self(TaoPhysicalPosition {
- x: position.x,
- y: position.y,
- })
- }
- }
- struct LogicalPositionWrapper<T>(TaoLogicalPosition<T>);
- impl<T> From<LogicalPosition<T>> for LogicalPositionWrapper<T> {
- fn from(position: LogicalPosition<T>) -> Self {
- Self(TaoLogicalPosition {
- x: position.x,
- y: position.y,
- })
- }
- }
- pub struct PhysicalSizeWrapper<T>(pub TaoPhysicalSize<T>);
- impl<T> From<PhysicalSizeWrapper<T>> for PhysicalSize<T> {
- fn from(size: PhysicalSizeWrapper<T>) -> Self {
- Self {
- width: size.0.width,
- height: size.0.height,
- }
- }
- }
- impl<T> From<PhysicalSize<T>> for PhysicalSizeWrapper<T> {
- fn from(size: PhysicalSize<T>) -> Self {
- Self(TaoPhysicalSize {
- width: size.width,
- height: size.height,
- })
- }
- }
- struct LogicalSizeWrapper<T>(TaoLogicalSize<T>);
- impl<T> From<LogicalSize<T>> for LogicalSizeWrapper<T> {
- fn from(size: LogicalSize<T>) -> Self {
- Self(TaoLogicalSize {
- width: size.width,
- height: size.height,
- })
- }
- }
- pub struct SizeWrapper(pub TaoSize);
- impl From<Size> for SizeWrapper {
- fn from(size: Size) -> Self {
- match size {
- Size::Logical(s) => Self(TaoSize::Logical(LogicalSizeWrapper::from(s).0)),
- Size::Physical(s) => Self(TaoSize::Physical(PhysicalSizeWrapper::from(s).0)),
- }
- }
- }
- pub struct PositionWrapper(pub TaoPosition);
- impl From<Position> for PositionWrapper {
- fn from(position: Position) -> Self {
- match position {
- Position::Logical(s) => Self(TaoPosition::Logical(LogicalPositionWrapper::from(s).0)),
- Position::Physical(s) => Self(TaoPosition::Physical(PhysicalPositionWrapper::from(s).0)),
- }
- }
- }
- #[derive(Debug, Clone)]
- pub struct UserAttentionTypeWrapper(pub TaoUserAttentionType);
- impl From<UserAttentionType> for UserAttentionTypeWrapper {
- fn from(request_type: UserAttentionType) -> Self {
- let o = match request_type {
- UserAttentionType::Critical => TaoUserAttentionType::Critical,
- UserAttentionType::Informational => TaoUserAttentionType::Informational,
- };
- Self(o)
- }
- }
- #[derive(Debug)]
- pub struct CursorIconWrapper(pub TaoCursorIcon);
- impl From<CursorIcon> for CursorIconWrapper {
- fn from(icon: CursorIcon) -> Self {
- use CursorIcon::*;
- let i = match icon {
- Default => TaoCursorIcon::Default,
- Crosshair => TaoCursorIcon::Crosshair,
- Hand => TaoCursorIcon::Hand,
- Arrow => TaoCursorIcon::Arrow,
- Move => TaoCursorIcon::Move,
- Text => TaoCursorIcon::Text,
- Wait => TaoCursorIcon::Wait,
- Help => TaoCursorIcon::Help,
- Progress => TaoCursorIcon::Progress,
- NotAllowed => TaoCursorIcon::NotAllowed,
- ContextMenu => TaoCursorIcon::ContextMenu,
- Cell => TaoCursorIcon::Cell,
- VerticalText => TaoCursorIcon::VerticalText,
- Alias => TaoCursorIcon::Alias,
- Copy => TaoCursorIcon::Copy,
- NoDrop => TaoCursorIcon::NoDrop,
- Grab => TaoCursorIcon::Grab,
- Grabbing => TaoCursorIcon::Grabbing,
- AllScroll => TaoCursorIcon::AllScroll,
- ZoomIn => TaoCursorIcon::ZoomIn,
- ZoomOut => TaoCursorIcon::ZoomOut,
- EResize => TaoCursorIcon::EResize,
- NResize => TaoCursorIcon::NResize,
- NeResize => TaoCursorIcon::NeResize,
- NwResize => TaoCursorIcon::NwResize,
- SResize => TaoCursorIcon::SResize,
- SeResize => TaoCursorIcon::SeResize,
- SwResize => TaoCursorIcon::SwResize,
- WResize => TaoCursorIcon::WResize,
- EwResize => TaoCursorIcon::EwResize,
- NsResize => TaoCursorIcon::NsResize,
- NeswResize => TaoCursorIcon::NeswResize,
- NwseResize => TaoCursorIcon::NwseResize,
- ColResize => TaoCursorIcon::ColResize,
- RowResize => TaoCursorIcon::RowResize,
- _ => TaoCursorIcon::Default,
- };
- Self(i)
- }
- }
- pub struct ProgressStateWrapper(pub TaoProgressState);
- impl From<ProgressBarStatus> for ProgressStateWrapper {
- fn from(status: ProgressBarStatus) -> Self {
- let state = match status {
- ProgressBarStatus::None => TaoProgressState::None,
- ProgressBarStatus::Normal => TaoProgressState::Normal,
- ProgressBarStatus::Indeterminate => TaoProgressState::Indeterminate,
- ProgressBarStatus::Paused => TaoProgressState::Paused,
- ProgressBarStatus::Error => TaoProgressState::Error,
- };
- Self(state)
- }
- }
- pub struct ProgressBarStateWrapper(pub TaoProgressBarState);
- impl From<ProgressBarState> for ProgressBarStateWrapper {
- fn from(progress_state: ProgressBarState) -> Self {
- Self(TaoProgressBarState {
- progress: progress_state.progress,
- state: progress_state
- .status
- .map(|state| ProgressStateWrapper::from(state).0),
- desktop_filename: progress_state.desktop_filename,
- })
- }
- }
- #[derive(Clone, Default)]
- pub struct WindowBuilderWrapper {
- inner: TaoWindowBuilder,
- center: bool,
- #[cfg(target_os = "macos")]
- tabbing_identifier: Option<String>,
- }
- impl std::fmt::Debug for WindowBuilderWrapper {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let mut s = f.debug_struct("WindowBuilderWrapper");
- s.field("inner", &self.inner).field("center", &self.center);
- #[cfg(target_os = "macos")]
- {
- s.field("tabbing_identifier", &self.tabbing_identifier);
- }
- s.finish()
- }
- }
- // SAFETY: this type is `Send` since `menu_items` are read only here
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl Send for WindowBuilderWrapper {}
- impl WindowBuilderBase for WindowBuilderWrapper {}
- impl WindowBuilder for WindowBuilderWrapper {
- fn new() -> Self {
- Self::default().focused(true)
- }
- fn with_config(config: &WindowConfig) -> Self {
- let mut window = WindowBuilderWrapper::new();
- #[cfg(target_os = "macos")]
- {
- window = window
- .hidden_title(config.hidden_title)
- .title_bar_style(config.title_bar_style);
- if let Some(identifier) = &config.tabbing_identifier {
- window = window.tabbing_identifier(identifier);
- }
- }
- #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
- {
- window = window.transparent(config.transparent);
- }
- #[cfg(all(
- target_os = "macos",
- not(feature = "macos-private-api"),
- debug_assertions
- ))]
- if config.transparent {
- eprintln!(
- "The window is set to be transparent but the `macos-private-api` is not enabled.
- This can be enabled via the `tauri.macOSPrivateApi` configuration property <https://tauri.app/docs/api/config#tauri.macOSPrivateApi>
- ");
- }
- #[cfg(target_os = "linux")]
- {
- // Mouse event is disabled on Linux since sudden event bursts could block event loop.
- window.inner = window.inner.with_cursor_moved_event(false);
- }
- #[cfg(desktop)]
- {
- window = window
- .title(config.title.to_string())
- .inner_size(config.width, config.height)
- .visible(config.visible)
- .resizable(config.resizable)
- .fullscreen(config.fullscreen)
- .decorations(config.decorations)
- .maximized(config.maximized)
- .always_on_bottom(config.always_on_bottom)
- .always_on_top(config.always_on_top)
- .visible_on_all_workspaces(config.visible_on_all_workspaces)
- .content_protected(config.content_protected)
- .skip_taskbar(config.skip_taskbar)
- .theme(config.theme)
- .shadow(config.shadow);
- if let (Some(min_width), Some(min_height)) = (config.min_width, config.min_height) {
- window = window.min_inner_size(min_width, min_height);
- }
- if let (Some(max_width), Some(max_height)) = (config.max_width, config.max_height) {
- window = window.max_inner_size(max_width, max_height);
- }
- if let (Some(x), Some(y)) = (config.x, config.y) {
- window = window.position(x, y);
- }
- if config.center {
- window = window.center();
- }
- }
- window
- }
- fn center(mut self) -> Self {
- self.center = true;
- self
- }
- fn position(mut self, x: f64, y: f64) -> Self {
- self.inner = self.inner.with_position(TaoLogicalPosition::new(x, y));
- self
- }
- fn inner_size(mut self, width: f64, height: f64) -> Self {
- self.inner = self
- .inner
- .with_inner_size(TaoLogicalSize::new(width, height));
- self
- }
- fn min_inner_size(mut self, min_width: f64, min_height: f64) -> Self {
- self.inner = self
- .inner
- .with_min_inner_size(TaoLogicalSize::new(min_width, min_height));
- self
- }
- fn max_inner_size(mut self, max_width: f64, max_height: f64) -> Self {
- self.inner = self
- .inner
- .with_max_inner_size(TaoLogicalSize::new(max_width, max_height));
- self
- }
- fn resizable(mut self, resizable: bool) -> Self {
- self.inner = self.inner.with_resizable(resizable);
- self
- }
- fn maximizable(mut self, maximizable: bool) -> Self {
- self.inner = self.inner.with_maximizable(maximizable);
- self
- }
- fn minimizable(mut self, minimizable: bool) -> Self {
- self.inner = self.inner.with_minimizable(minimizable);
- self
- }
- fn closable(mut self, closable: bool) -> Self {
- self.inner = self.inner.with_closable(closable);
- self
- }
- fn title<S: Into<String>>(mut self, title: S) -> Self {
- self.inner = self.inner.with_title(title.into());
- self
- }
- fn fullscreen(mut self, fullscreen: bool) -> Self {
- self.inner = if fullscreen {
- self
- .inner
- .with_fullscreen(Some(Fullscreen::Borderless(None)))
- } else {
- self.inner.with_fullscreen(None)
- };
- self
- }
- fn focused(mut self, focused: bool) -> Self {
- self.inner = self.inner.with_focused(focused);
- self
- }
- fn maximized(mut self, maximized: bool) -> Self {
- self.inner = self.inner.with_maximized(maximized);
- self
- }
- fn visible(mut self, visible: bool) -> Self {
- self.inner = self.inner.with_visible(visible);
- self
- }
- #[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
- fn transparent(mut self, transparent: bool) -> Self {
- self.inner = self.inner.with_transparent(transparent);
- self
- }
- fn decorations(mut self, decorations: bool) -> Self {
- self.inner = self.inner.with_decorations(decorations);
- self
- }
- fn always_on_bottom(mut self, always_on_bottom: bool) -> Self {
- self.inner = self.inner.with_always_on_bottom(always_on_bottom);
- self
- }
- fn always_on_top(mut self, always_on_top: bool) -> Self {
- self.inner = self.inner.with_always_on_top(always_on_top);
- self
- }
- fn visible_on_all_workspaces(mut self, visible_on_all_workspaces: bool) -> Self {
- self.inner = self
- .inner
- .with_visible_on_all_workspaces(visible_on_all_workspaces);
- self
- }
- fn content_protected(mut self, protected: bool) -> Self {
- self.inner = self.inner.with_content_protection(protected);
- self
- }
- fn shadow(#[allow(unused_mut)] mut self, _enable: bool) -> Self {
- #[cfg(windows)]
- {
- self.inner = self.inner.with_undecorated_shadow(_enable);
- }
- #[cfg(target_os = "macos")]
- {
- self.inner = self.inner.with_has_shadow(_enable);
- }
- self
- }
- #[cfg(windows)]
- fn owner(mut self, owner: HWND) -> Self {
- self.inner = self.inner.with_owner_window(owner.0);
- self
- }
- #[cfg(windows)]
- fn parent(mut self, parent: HWND) -> Self {
- self.inner = self.inner.with_parent_window(parent.0);
- self
- }
- #[cfg(target_os = "macos")]
- fn parent(mut self, parent: *mut std::ffi::c_void) -> Self {
- self.inner = self.inner.with_parent_window(parent);
- self
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- fn transient_for(mut self, parent: &impl gtk::glib::IsA<gtk::Window>) -> Self {
- self.inner = self.inner.with_transient_for(parent);
- self
- }
- #[cfg(windows)]
- fn drag_and_drop(mut self, enabled: bool) -> Self {
- self.inner = self.inner.with_drag_and_drop(enabled);
- self
- }
- #[cfg(target_os = "macos")]
- fn title_bar_style(mut self, style: TitleBarStyle) -> Self {
- match style {
- TitleBarStyle::Visible => {
- self.inner = self.inner.with_titlebar_transparent(false);
- // Fixes rendering issue when resizing window with devtools open (https://github.com/tauri-apps/tauri/issues/3914)
- self.inner = self.inner.with_fullsize_content_view(true);
- }
- TitleBarStyle::Transparent => {
- self.inner = self.inner.with_titlebar_transparent(true);
- self.inner = self.inner.with_fullsize_content_view(false);
- }
- TitleBarStyle::Overlay => {
- self.inner = self.inner.with_titlebar_transparent(true);
- self.inner = self.inner.with_fullsize_content_view(true);
- }
- }
- self
- }
- #[cfg(target_os = "macos")]
- fn hidden_title(mut self, hidden: bool) -> Self {
- self.inner = self.inner.with_title_hidden(hidden);
- self
- }
- #[cfg(target_os = "macos")]
- fn tabbing_identifier(mut self, identifier: &str) -> Self {
- self.inner = self.inner.with_tabbing_identifier(identifier);
- self.tabbing_identifier.replace(identifier.into());
- self
- }
- fn icon(mut self, icon: Icon) -> Result<Self> {
- self.inner = self
- .inner
- .with_window_icon(Some(TaoIcon::try_from(icon)?.0));
- Ok(self)
- }
- #[cfg(any(windows, target_os = "linux"))]
- fn skip_taskbar(mut self, skip: bool) -> Self {
- self.inner = self.inner.with_skip_taskbar(skip);
- self
- }
- #[cfg(any(target_os = "macos", target_os = "ios", target_os = "android"))]
- fn skip_taskbar(self, _skip: bool) -> Self {
- self
- }
- #[allow(unused_variables, unused_mut)]
- fn theme(mut self, theme: Option<Theme>) -> Self {
- self.inner = self.inner.with_theme(if let Some(t) = theme {
- match t {
- Theme::Dark => Some(TaoTheme::Dark),
- _ => Some(TaoTheme::Light),
- }
- } else {
- None
- });
- self
- }
- fn has_icon(&self) -> bool {
- self.inner.window.window_icon.is_some()
- }
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- pub struct GtkWindow(pub gtk::ApplicationWindow);
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl Send for GtkWindow {}
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- pub struct GtkBox(pub gtk::Box);
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl Send for GtkBox {}
- pub struct SendRawWindowHandle(pub raw_window_handle::RawWindowHandle);
- unsafe impl Send for SendRawWindowHandle {}
- #[cfg(target_os = "macos")]
- #[derive(Debug, Clone)]
- pub enum ApplicationMessage {
- Show,
- Hide,
- }
- pub enum WindowMessage {
- AddEventListener(WindowEventId, Box<dyn Fn(&WindowEvent) + Send>),
- // Getters
- ScaleFactor(Sender<f64>),
- InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
- OuterPosition(Sender<Result<PhysicalPosition<i32>>>),
- InnerSize(Sender<PhysicalSize<u32>>),
- OuterSize(Sender<PhysicalSize<u32>>),
- IsFullscreen(Sender<bool>),
- IsMinimized(Sender<bool>),
- IsMaximized(Sender<bool>),
- IsFocused(Sender<bool>),
- IsDecorated(Sender<bool>),
- IsResizable(Sender<bool>),
- IsMaximizable(Sender<bool>),
- IsMinimizable(Sender<bool>),
- IsClosable(Sender<bool>),
- IsVisible(Sender<bool>),
- Title(Sender<String>),
- CurrentMonitor(Sender<Option<MonitorHandle>>),
- PrimaryMonitor(Sender<Option<MonitorHandle>>),
- AvailableMonitors(Sender<Vec<MonitorHandle>>),
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- GtkWindow(Sender<GtkWindow>),
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- GtkBox(Sender<GtkBox>),
- RawWindowHandle(Sender<std::result::Result<SendRawWindowHandle, raw_window_handle::HandleError>>),
- Theme(Sender<Theme>),
- // Setters
- Center,
- RequestUserAttention(Option<UserAttentionTypeWrapper>),
- SetResizable(bool),
- SetMaximizable(bool),
- SetMinimizable(bool),
- SetClosable(bool),
- SetTitle(String),
- Maximize,
- Unmaximize,
- Minimize,
- Unminimize,
- Show,
- Hide,
- Close,
- Destroy,
- SetDecorations(bool),
- SetShadow(bool),
- SetAlwaysOnBottom(bool),
- SetAlwaysOnTop(bool),
- SetVisibleOnAllWorkspaces(bool),
- SetContentProtected(bool),
- SetSize(Size),
- SetMinSize(Option<Size>),
- SetMaxSize(Option<Size>),
- SetPosition(Position),
- SetFullscreen(bool),
- SetFocus,
- SetIcon(TaoWindowIcon),
- SetSkipTaskbar(bool),
- SetCursorGrab(bool),
- SetCursorVisible(bool),
- SetCursorIcon(CursorIcon),
- SetCursorPosition(Position),
- SetIgnoreCursorEvents(bool),
- SetProgressBar(ProgressBarState),
- DragWindow,
- ResizeDragWindow(tauri_runtime::ResizeDirection),
- RequestRedraw,
- }
- #[derive(Debug, Clone)]
- pub enum SynthesizedWindowEvent {
- Focused(bool),
- FileDrop(FileDropEvent),
- }
- impl From<SynthesizedWindowEvent> for WindowEventWrapper {
- fn from(event: SynthesizedWindowEvent) -> Self {
- let event = match event {
- SynthesizedWindowEvent::Focused(focused) => WindowEvent::Focused(focused),
- SynthesizedWindowEvent::FileDrop(event) => WindowEvent::FileDrop(event),
- };
- Self(Some(event))
- }
- }
- pub enum WebviewMessage {
- AddEventListener(WebviewEventId, Box<dyn Fn(&WebviewEvent) + Send>),
- #[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
- EvaluateScript(String),
- #[cfg(all(feature = "tracing", not(target_os = "android")))]
- EvaluateScript(String, Sender<()>, tracing::Span),
- WebviewEvent(WebviewEvent),
- SynthesizedWindowEvent(SynthesizedWindowEvent),
- Navigate(Url),
- Print,
- Close,
- SetPosition(Position),
- SetSize(Size),
- SetFocus,
- Reparent(WindowId),
- // Getters
- Url(Sender<Url>),
- Position(Sender<PhysicalPosition<i32>>),
- Size(Sender<PhysicalSize<u32>>),
- WithWebview(Box<dyn FnOnce(Webview) + Send>),
- // Devtools
- #[cfg(any(debug_assertions, feature = "devtools"))]
- OpenDevTools,
- #[cfg(any(debug_assertions, feature = "devtools"))]
- CloseDevTools,
- #[cfg(any(debug_assertions, feature = "devtools"))]
- IsDevToolsOpen(Sender<bool>),
- }
- pub type CreateWindowClosure<T> =
- Box<dyn FnOnce(&EventLoopWindowTarget<Message<T>>) -> Result<WindowWrapper> + Send>;
- pub type CreateWebviewClosure = Box<dyn FnOnce(&Window) -> Result<WebviewWrapper> + Send>;
- pub enum Message<T: 'static> {
- Task(Box<dyn FnOnce() + Send>),
- #[cfg(target_os = "macos")]
- SetActivationPolicy(ActivationPolicy),
- RequestExit(i32),
- #[cfg(target_os = "macos")]
- Application(ApplicationMessage),
- Window(WindowId, WindowMessage),
- Webview(WindowId, WebviewId, WebviewMessage),
- CreateWebview(WindowId, CreateWebviewClosure),
- CreateWindow(WindowId, CreateWindowClosure<T>),
- CreateRawWindow(
- WindowId,
- Box<dyn FnOnce() -> (String, TaoWindowBuilder) + Send>,
- Sender<Result<Weak<Window>>>,
- ),
- UserEvent(T),
- }
- impl<T: UserEvent> Clone for Message<T> {
- fn clone(&self) -> Self {
- match self {
- Self::UserEvent(t) => Self::UserEvent(t.clone()),
- _ => unimplemented!(),
- }
- }
- }
- /// The Tauri [`WebviewDispatch`] for [`Wry`].
- #[derive(Debug, Clone)]
- pub struct WryWebviewDispatcher<T: UserEvent> {
- window_id: Arc<Mutex<WindowId>>,
- webview_id: WebviewId,
- context: Context<T>,
- }
- impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
- type Runtime = Wry<T>;
- fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
- send_user_message(&self.context, Message::Task(Box::new(f)))
- }
- fn on_webview_event<F: Fn(&WebviewEvent) + Send + 'static>(&self, f: F) -> WindowEventId {
- let id = self.context.next_webview_event_id();
- let _ = self.context.proxy.send_event(Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::AddEventListener(id, Box::new(f)),
- ));
- id
- }
- fn with_webview<F: FnOnce(Box<dyn std::any::Any>) + Send + 'static>(&self, f: F) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::WithWebview(Box::new(move |webview| f(Box::new(webview)))),
- ),
- )
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- fn open_devtools(&self) {
- let _ = send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::OpenDevTools,
- ),
- );
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- fn close_devtools(&self) {
- let _ = send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::CloseDevTools,
- ),
- );
- }
- /// Gets the devtools window's current open state.
- #[cfg(any(debug_assertions, feature = "devtools"))]
- fn is_devtools_open(&self) -> Result<bool> {
- webview_getter!(self, WebviewMessage::IsDevToolsOpen)
- }
- // Getters
- fn url(&self) -> Result<Url> {
- webview_getter!(self, WebviewMessage::Url)
- }
- fn position(&self) -> Result<PhysicalPosition<i32>> {
- webview_getter!(self, WebviewMessage::Position)
- }
- fn size(&self) -> Result<PhysicalSize<u32>> {
- webview_getter!(self, WebviewMessage::Size)
- }
- // Setters
- fn navigate(&self, url: Url) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::Navigate(url),
- ),
- )
- }
- fn print(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::Print,
- ),
- )
- }
- fn close(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::Close,
- ),
- )
- }
- fn set_size(&self, size: Size) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetSize(size),
- ),
- )
- }
- fn set_position(&self, position: Position) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetPosition(position),
- ),
- )
- }
- fn set_focus(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetFocus,
- ),
- )
- }
- fn reparent(&self, window_id: WindowId) -> Result<()> {
- let mut current_window_id = self.window_id.lock().unwrap();
- send_user_message(
- &self.context,
- Message::Webview(
- *current_window_id,
- self.webview_id,
- WebviewMessage::Reparent(window_id),
- ),
- )?;
- *current_window_id = window_id;
- Ok(())
- }
- #[cfg(all(feature = "tracing", not(target_os = "android")))]
- fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
- // use a channel so the EvaluateScript task uses the current span as parent
- let (tx, rx) = channel();
- getter!(
- self,
- rx,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::EvaluateScript(script.into(), tx, tracing::Span::current()),
- )
- )
- }
- #[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
- fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::EvaluateScript(script.into()),
- ),
- )
- }
- }
- /// The Tauri [`WindowDispatch`] for [`Wry`].
- #[derive(Debug, Clone)]
- pub struct WryWindowDispatcher<T: UserEvent> {
- window_id: WindowId,
- context: Context<T>,
- }
- // SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl<T: UserEvent> Sync for WryWindowDispatcher<T> {}
- fn get_raw_window_handle<T: UserEvent>(
- dispatcher: &WryWindowDispatcher<T>,
- ) -> Result<std::result::Result<SendRawWindowHandle, raw_window_handle::HandleError>> {
- window_getter!(dispatcher, WindowMessage::RawWindowHandle)
- }
- impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
- type Runtime = Wry<T>;
- type WindowBuilder = WindowBuilderWrapper;
- fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
- send_user_message(&self.context, Message::Task(Box::new(f)))
- }
- fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F) -> WindowEventId {
- let id = self.context.next_window_event_id();
- let _ = self.context.proxy.send_event(Message::Window(
- self.window_id,
- WindowMessage::AddEventListener(id, Box::new(f)),
- ));
- id
- }
- // Getters
- fn scale_factor(&self) -> Result<f64> {
- window_getter!(self, WindowMessage::ScaleFactor)
- }
- fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
- window_getter!(self, WindowMessage::InnerPosition)?
- }
- fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
- window_getter!(self, WindowMessage::OuterPosition)?
- }
- fn inner_size(&self) -> Result<PhysicalSize<u32>> {
- window_getter!(self, WindowMessage::InnerSize)
- }
- fn outer_size(&self) -> Result<PhysicalSize<u32>> {
- window_getter!(self, WindowMessage::OuterSize)
- }
- fn is_fullscreen(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsFullscreen)
- }
- fn is_minimized(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsMinimized)
- }
- fn is_maximized(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsMaximized)
- }
- fn is_focused(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsFocused)
- }
- /// Gets the window’s current decoration state.
- fn is_decorated(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsDecorated)
- }
- /// Gets the window’s current resizable state.
- fn is_resizable(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsResizable)
- }
- /// Gets the current native window's maximize button state
- fn is_maximizable(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsMaximizable)
- }
- /// Gets the current native window's minimize button state
- fn is_minimizable(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsMinimizable)
- }
- /// Gets the current native window's close button state
- fn is_closable(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsClosable)
- }
- fn is_visible(&self) -> Result<bool> {
- window_getter!(self, WindowMessage::IsVisible)
- }
- fn title(&self) -> Result<String> {
- window_getter!(self, WindowMessage::Title)
- }
- fn current_monitor(&self) -> Result<Option<Monitor>> {
- Ok(window_getter!(self, WindowMessage::CurrentMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
- }
- fn primary_monitor(&self) -> Result<Option<Monitor>> {
- Ok(window_getter!(self, WindowMessage::PrimaryMonitor)?.map(|m| MonitorHandleWrapper(m).into()))
- }
- fn available_monitors(&self) -> Result<Vec<Monitor>> {
- Ok(
- window_getter!(self, WindowMessage::AvailableMonitors)?
- .into_iter()
- .map(|m| MonitorHandleWrapper(m).into())
- .collect(),
- )
- }
- fn theme(&self) -> Result<Theme> {
- window_getter!(self, WindowMessage::Theme)
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- fn gtk_window(&self) -> Result<gtk::ApplicationWindow> {
- window_getter!(self, WindowMessage::GtkWindow).map(|w| w.0)
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- fn default_vbox(&self) -> Result<gtk::Box> {
- window_getter!(self, WindowMessage::GtkBox).map(|w| w.0)
- }
- fn window_handle(
- &self,
- ) -> std::result::Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
- get_raw_window_handle(self)
- .map_err(|_| raw_window_handle::HandleError::Unavailable)
- .and_then(|r| r.map(|h| unsafe { raw_window_handle::WindowHandle::borrow_raw(h.0) }))
- }
- // Setters
- fn center(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Center),
- )
- }
- fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::RequestUserAttention(request_type.map(Into::into)),
- ),
- )
- }
- // Creates a window by dispatching a message to the event loop.
- // Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
- fn create_window<F: Fn(RawWindow) + Send + 'static>(
- &mut self,
- pending: PendingWindow<T, Self::Runtime>,
- after_window_creation: Option<F>,
- ) -> Result<DetachedWindow<T, Self::Runtime>> {
- self.context.create_window(pending, after_window_creation)
- }
- // Creates a webview by dispatching a message to the event loop.
- // Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
- fn create_webview(
- &mut self,
- pending: PendingWebview<T, Self::Runtime>,
- ) -> Result<DetachedWebview<T, Self::Runtime>> {
- self.context.create_webview(self.window_id, pending)
- }
- fn set_resizable(&self, resizable: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetResizable(resizable)),
- )
- }
- fn set_maximizable(&self, maximizable: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetMaximizable(maximizable)),
- )
- }
- fn set_minimizable(&self, minimizable: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetMinimizable(minimizable)),
- )
- }
- fn set_closable(&self, closable: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetClosable(closable)),
- )
- }
- fn set_title<S: Into<String>>(&self, title: S) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetTitle(title.into())),
- )
- }
- fn maximize(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Maximize),
- )
- }
- fn unmaximize(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Unmaximize),
- )
- }
- fn minimize(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Minimize),
- )
- }
- fn unminimize(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Unminimize),
- )
- }
- fn show(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Show),
- )
- }
- fn hide(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::Hide),
- )
- }
- fn close(&self) -> Result<()> {
- // NOTE: close cannot use the `send_user_message` function because it accesses the event loop callback
- self
- .context
- .proxy
- .send_event(Message::Window(self.window_id, WindowMessage::Close))
- .map_err(|_| Error::FailedToSendMessage)
- }
- fn destroy(&self) -> Result<()> {
- // NOTE: destroy cannot use the `send_user_message` function because it accesses the event loop callback
- self
- .context
- .proxy
- .send_event(Message::Window(self.window_id, WindowMessage::Destroy))
- .map_err(|_| Error::FailedToSendMessage)
- }
- fn set_decorations(&self, decorations: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetDecorations(decorations)),
- )
- }
- fn set_shadow(&self, enable: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetShadow(enable)),
- )
- }
- fn set_always_on_bottom(&self, always_on_bottom: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetAlwaysOnBottom(always_on_bottom),
- ),
- )
- }
- fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetAlwaysOnTop(always_on_top)),
- )
- }
- fn set_visible_on_all_workspaces(&self, visible_on_all_workspaces: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetVisibleOnAllWorkspaces(visible_on_all_workspaces),
- ),
- )
- }
- fn set_content_protected(&self, protected: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetContentProtected(protected),
- ),
- )
- }
- fn set_size(&self, size: Size) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetSize(size)),
- )
- }
- fn set_min_size(&self, size: Option<Size>) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetMinSize(size)),
- )
- }
- fn set_max_size(&self, size: Option<Size>) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetMaxSize(size)),
- )
- }
- fn set_position(&self, position: Position) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetPosition(position)),
- )
- }
- fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetFullscreen(fullscreen)),
- )
- }
- fn set_focus(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetFocus),
- )
- }
- fn set_icon(&self, icon: Icon) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetIcon(TaoIcon::try_from(icon)?.0),
- ),
- )
- }
- fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetSkipTaskbar(skip)),
- )
- }
- fn set_cursor_grab(&self, grab: bool) -> crate::Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetCursorGrab(grab)),
- )
- }
- fn set_cursor_visible(&self, visible: bool) -> crate::Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetCursorVisible(visible)),
- )
- }
- fn set_cursor_icon(&self, icon: CursorIcon) -> crate::Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetCursorIcon(icon)),
- )
- }
- fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> crate::Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetCursorPosition(position.into()),
- ),
- )
- }
- fn set_ignore_cursor_events(&self, ignore: bool) -> crate::Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetIgnoreCursorEvents(ignore)),
- )
- }
- fn start_dragging(&self) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::DragWindow),
- )
- }
- fn start_resize_dragging(&self, direction: tauri_runtime::ResizeDirection) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::ResizeDragWindow(direction)),
- )
- }
- fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetProgressBar(progress_state),
- ),
- )
- }
- }
- #[derive(Clone)]
- pub struct WebviewWrapper {
- label: String,
- id: WebviewId,
- inner: Rc<WebView>,
- context_store: WebContextStore,
- webview_event_listeners: WebviewEventListeners,
- // the key of the WebContext if it's not shared
- context_key: Option<PathBuf>,
- bounds: Option<Arc<Mutex<WebviewBounds>>>,
- }
- impl Deref for WebviewWrapper {
- type Target = WebView;
- #[inline(always)]
- fn deref(&self) -> &Self::Target {
- &self.inner
- }
- }
- impl Drop for WebviewWrapper {
- fn drop(&mut self) {
- if Rc::get_mut(&mut self.inner).is_some() {
- self.context_store.lock().unwrap().remove(&self.context_key);
- }
- }
- }
- pub struct WindowWrapper {
- label: String,
- inner: Option<Arc<Window>>,
- // whether this window has child webviews
- // or it's just a container for a single webview
- has_children: AtomicBool,
- webviews: Vec<WebviewWrapper>,
- window_event_listeners: WindowEventListeners,
- #[cfg(windows)]
- is_window_fullscreen: bool,
- #[cfg(windows)]
- is_window_transparent: bool,
- #[cfg(windows)]
- surface: Option<softbuffer::Surface<Arc<Window>, Arc<Window>>>,
- }
- impl fmt::Debug for WindowWrapper {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("WindowWrapper")
- .field("label", &self.label)
- .field("inner", &self.inner)
- .finish()
- }
- }
- #[derive(Debug, Clone)]
- pub struct EventProxy<T: UserEvent>(TaoEventLoopProxy<Message<T>>);
- #[cfg(target_os = "ios")]
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl<T: UserEvent> Sync for EventProxy<T> {}
- impl<T: UserEvent> EventLoopProxy<T> for EventProxy<T> {
- fn send_event(&self, event: T) -> Result<()> {
- self
- .0
- .send_event(Message::UserEvent(event))
- .map_err(|_| Error::EventLoopClosed)
- }
- }
- pub trait PluginBuilder<T: UserEvent> {
- type Plugin: Plugin<T>;
- fn build(self, context: Context<T>) -> Self::Plugin;
- }
- pub trait Plugin<T: UserEvent> {
- fn on_event(
- &mut self,
- event: &Event<Message<T>>,
- event_loop: &EventLoopWindowTarget<Message<T>>,
- proxy: &TaoEventLoopProxy<Message<T>>,
- control_flow: &mut ControlFlow,
- context: EventLoopIterationContext<'_, T>,
- web_context: &WebContextStore,
- ) -> bool;
- }
- /// A Tauri [`Runtime`] wrapper around wry.
- pub struct Wry<T: UserEvent> {
- context: Context<T>,
- event_loop: EventLoop<Message<T>>,
- }
- impl<T: UserEvent> fmt::Debug for Wry<T> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("Wry")
- .field("main_thread_id", &self.context.main_thread_id)
- .field("event_loop", &self.event_loop)
- .field("windows", &self.context.main_thread.windows)
- .field("web_context", &self.context.main_thread.web_context)
- .finish()
- }
- }
- /// A handle to the Wry runtime.
- #[derive(Debug, Clone)]
- pub struct WryHandle<T: UserEvent> {
- context: Context<T>,
- }
- // SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`.
- #[allow(clippy::non_send_fields_in_send_ty)]
- unsafe impl<T: UserEvent> Sync for WryHandle<T> {}
- impl<T: UserEvent> WryHandle<T> {
- /// Creates a new tao window using a callback, and returns its window id.
- pub fn create_tao_window<F: FnOnce() -> (String, TaoWindowBuilder) + Send + 'static>(
- &self,
- f: F,
- ) -> Result<Weak<Window>> {
- let id = self.context.next_window_id();
- let (tx, rx) = channel();
- send_user_message(&self.context, Message::CreateRawWindow(id, Box::new(f), tx))?;
- rx.recv().unwrap()
- }
- /// Gets the [`WebviewId'] associated with the given [`WindowId`].
- pub fn window_id(&self, window_id: TaoWindowId) -> WindowId {
- *self
- .context
- .window_id_map
- .0
- .lock()
- .unwrap()
- .get(&window_id)
- .unwrap()
- }
- /// Send a message to the event loop.
- pub fn send_event(&self, message: Message<T>) -> Result<()> {
- self
- .context
- .proxy
- .send_event(message)
- .map_err(|_| Error::FailedToSendMessage)?;
- Ok(())
- }
- pub fn plugin<P: PluginBuilder<T> + 'static>(&mut self, plugin: P)
- where
- <P as PluginBuilder<T>>::Plugin: Send,
- {
- self
- .context
- .plugins
- .lock()
- .unwrap()
- .push(Box::new(plugin.build(self.context.clone())));
- }
- }
- impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
- type Runtime = Wry<T>;
- fn create_proxy(&self) -> EventProxy<T> {
- EventProxy(self.context.proxy.clone())
- }
- #[cfg(target_os = "macos")]
- fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()> {
- send_user_message(
- &self.context,
- Message::SetActivationPolicy(activation_policy),
- )
- }
- fn request_exit(&self, code: i32) -> Result<()> {
- // NOTE: request_exit cannot use the `send_user_message` function because it accesses the event loop callback
- self
- .context
- .proxy
- .send_event(Message::RequestExit(code))
- .map_err(|_| Error::FailedToSendMessage)
- }
- // Creates a window by dispatching a message to the event loop.
- // Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
- fn create_window<F: Fn(RawWindow) + Send + 'static>(
- &self,
- pending: PendingWindow<T, Self::Runtime>,
- after_window_creation: Option<F>,
- ) -> Result<DetachedWindow<T, Self::Runtime>> {
- self.context.create_window(pending, after_window_creation)
- }
- // Creates a webview by dispatching a message to the event loop.
- // Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
- fn create_webview(
- &self,
- window_id: WindowId,
- pending: PendingWebview<T, Self::Runtime>,
- ) -> Result<DetachedWebview<T, Self::Runtime>> {
- self.context.create_webview(window_id, pending)
- }
- fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
- send_user_message(&self.context, Message::Task(Box::new(f)))
- }
- fn display_handle(&self) -> std::result::Result<DisplayHandle, raw_window_handle::HandleError> {
- self.context.main_thread.window_target.display_handle()
- }
- fn primary_monitor(&self) -> Option<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .primary_monitor()
- .map(|m| MonitorHandleWrapper(m).into())
- }
- fn available_monitors(&self) -> Vec<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .available_monitors()
- .map(|m| MonitorHandleWrapper(m).into())
- .collect()
- }
- #[cfg(target_os = "macos")]
- fn show(&self) -> tauri_runtime::Result<()> {
- send_user_message(
- &self.context,
- Message::Application(ApplicationMessage::Show),
- )
- }
- #[cfg(target_os = "macos")]
- fn hide(&self) -> tauri_runtime::Result<()> {
- send_user_message(
- &self.context,
- Message::Application(ApplicationMessage::Hide),
- )
- }
- #[cfg(target_os = "android")]
- fn find_class<'a>(
- &self,
- env: &mut jni::JNIEnv<'a>,
- activity: &jni::objects::JObject<'_>,
- name: impl Into<String>,
- ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error> {
- find_class(env, activity, name.into())
- }
- #[cfg(target_os = "android")]
- fn run_on_android_context<F>(&self, f: F)
- where
- F: FnOnce(&mut jni::JNIEnv, &jni::objects::JObject, &jni::objects::JObject) + Send + 'static,
- {
- dispatch(f)
- }
- }
- impl<T: UserEvent> Wry<T> {
- fn init_with_builder(
- mut event_loop_builder: EventLoopBuilder<Message<T>>,
- #[allow(unused_variables)] args: RuntimeInitArgs,
- ) -> Result<Self> {
- #[cfg(windows)]
- if let Some(hook) = args.msg_hook {
- use tao::platform::windows::EventLoopBuilderExtWindows;
- event_loop_builder.with_msg_hook(hook);
- }
- Self::init(event_loop_builder.build())
- }
- fn init(event_loop: EventLoop<Message<T>>) -> Result<Self> {
- let main_thread_id = current_thread().id();
- let web_context = WebContextStore::default();
- let windows = Arc::new(WindowsStore(RefCell::new(BTreeMap::default())));
- let window_id_map = WindowIdStore::default();
- let context = Context {
- window_id_map,
- main_thread_id,
- proxy: event_loop.create_proxy(),
- main_thread: DispatcherMainThreadContext {
- window_target: event_loop.deref().clone(),
- web_context,
- windows,
- #[cfg(feature = "tracing")]
- active_tracing_spans: Default::default(),
- },
- plugins: Default::default(),
- next_window_id: Default::default(),
- next_webview_id: Default::default(),
- next_window_event_id: Default::default(),
- next_webview_event_id: Default::default(),
- next_webcontext_id: Default::default(),
- };
- Ok(Self {
- context,
- event_loop,
- })
- }
- }
- impl<T: UserEvent> Runtime<T> for Wry<T> {
- type WindowDispatcher = WryWindowDispatcher<T>;
- type WebviewDispatcher = WryWebviewDispatcher<T>;
- type Handle = WryHandle<T>;
- type EventLoopProxy = EventProxy<T>;
- fn new(args: RuntimeInitArgs) -> Result<Self> {
- Self::init_with_builder(EventLoopBuilder::<Message<T>>::with_user_event(), args)
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- fn new_any_thread(args: RuntimeInitArgs) -> Result<Self> {
- use tao::platform::unix::EventLoopBuilderExtUnix;
- let mut event_loop_builder = EventLoopBuilder::<Message<T>>::with_user_event();
- event_loop_builder.with_any_thread(true);
- Self::init_with_builder(event_loop_builder, args)
- }
- #[cfg(windows)]
- fn new_any_thread(args: RuntimeInitArgs) -> Result<Self> {
- use tao::platform::windows::EventLoopBuilderExtWindows;
- let mut event_loop_builder = EventLoopBuilder::<Message<T>>::with_user_event();
- event_loop_builder.with_any_thread(true);
- Self::init_with_builder(event_loop_builder, args)
- }
- fn create_proxy(&self) -> EventProxy<T> {
- EventProxy(self.event_loop.create_proxy())
- }
- fn handle(&self) -> Self::Handle {
- WryHandle {
- context: self.context.clone(),
- }
- }
- fn create_window<F: Fn(RawWindow) + Send + 'static>(
- &self,
- pending: PendingWindow<T, Self>,
- after_window_creation: Option<F>,
- ) -> Result<DetachedWindow<T, Self>> {
- let label = pending.label.clone();
- let window_id = self.context.next_window_id();
- let webview_id = pending
- .webview
- .as_ref()
- .map(|_| self.context.next_webview_id());
- let window = create_window(
- window_id,
- webview_id.unwrap_or_default(),
- &self.event_loop,
- &self.context,
- pending,
- after_window_creation,
- )?;
- let dispatcher = WryWindowDispatcher {
- window_id,
- context: self.context.clone(),
- };
- self
- .context
- .main_thread
- .windows
- .0
- .borrow_mut()
- .insert(window_id, window);
- let detached_webview = webview_id.map(|id| DetachedWebview {
- label: label.clone(),
- dispatcher: WryWebviewDispatcher {
- window_id: Arc::new(Mutex::new(window_id)),
- webview_id: id,
- context: self.context.clone(),
- },
- });
- Ok(DetachedWindow {
- id: window_id,
- label,
- dispatcher,
- webview: detached_webview,
- })
- }
- fn create_webview(
- &self,
- window_id: WindowId,
- pending: PendingWebview<T, Self>,
- ) -> Result<DetachedWebview<T, Self>> {
- let label = pending.label.clone();
- let window = self
- .context
- .main_thread
- .windows
- .0
- .borrow()
- .get(&window_id)
- .and_then(|w| w.inner.clone());
- if let Some(window) = window {
- let window_id_wrapper = Arc::new(Mutex::new(window_id));
- let webview_id = self.context.next_webview_id();
- let webview = create_webview(
- WebviewKind::WindowChild,
- &window,
- window_id_wrapper.clone(),
- webview_id,
- &self.context,
- pending,
- )?;
- self
- .context
- .main_thread
- .windows
- .0
- .borrow_mut()
- .get_mut(&window_id)
- .map(|w| {
- w.webviews.push(webview);
- w.has_children.store(true, Ordering::Relaxed);
- w
- });
- let dispatcher = WryWebviewDispatcher {
- window_id: window_id_wrapper,
- webview_id,
- context: self.context.clone(),
- };
- Ok(DetachedWebview { label, dispatcher })
- } else {
- Err(Error::WindowNotFound)
- }
- }
- fn primary_monitor(&self) -> Option<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .primary_monitor()
- .map(|m| MonitorHandleWrapper(m).into())
- }
- fn available_monitors(&self) -> Vec<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .available_monitors()
- .map(|m| MonitorHandleWrapper(m).into())
- .collect()
- }
- #[cfg(target_os = "macos")]
- fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
- self
- .event_loop
- .set_activation_policy(tao_activation_policy(activation_policy));
- }
- #[cfg(target_os = "macos")]
- fn show(&self) {
- self.event_loop.show_application();
- }
- #[cfg(target_os = "macos")]
- fn hide(&self) {
- self.event_loop.hide_application();
- }
- fn set_device_event_filter(&mut self, filter: DeviceEventFilter) {
- self
- .event_loop
- .set_device_event_filter(DeviceEventFilterWrapper::from(filter).0);
- }
- #[cfg(desktop)]
- fn run_iteration<F: FnMut(RunEvent<T>) + 'static>(&mut self, mut callback: F) {
- use tao::platform::run_return::EventLoopExtRunReturn;
- let windows = self.context.main_thread.windows.clone();
- let window_id_map = self.context.window_id_map.clone();
- let web_context = &self.context.main_thread.web_context;
- let plugins = self.context.plugins.clone();
- #[cfg(feature = "tracing")]
- let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone();
- let proxy = self.event_loop.create_proxy();
- self
- .event_loop
- .run_return(|event, event_loop, control_flow| {
- *control_flow = ControlFlow::Wait;
- if let Event::MainEventsCleared = &event {
- *control_flow = ControlFlow::Exit;
- }
- for p in plugins.lock().unwrap().iter_mut() {
- let prevent_default = p.on_event(
- &event,
- event_loop,
- &proxy,
- control_flow,
- EventLoopIterationContext {
- callback: &mut callback,
- window_id_map: window_id_map.clone(),
- windows: windows.clone(),
- #[cfg(feature = "tracing")]
- active_tracing_spans: active_tracing_spans.clone(),
- },
- web_context,
- );
- if prevent_default {
- return;
- }
- }
- handle_event_loop(
- event,
- event_loop,
- control_flow,
- EventLoopIterationContext {
- callback: &mut callback,
- windows: windows.clone(),
- window_id_map: window_id_map.clone(),
- #[cfg(feature = "tracing")]
- active_tracing_spans: active_tracing_spans.clone(),
- },
- );
- });
- }
- fn run<F: FnMut(RunEvent<T>) + 'static>(self, mut callback: F) {
- let windows = self.context.main_thread.windows.clone();
- let window_id_map = self.context.window_id_map.clone();
- let web_context = self.context.main_thread.web_context;
- let plugins = self.context.plugins.clone();
- #[cfg(feature = "tracing")]
- let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone();
- let proxy = self.event_loop.create_proxy();
- self.event_loop.run(move |event, event_loop, control_flow| {
- for p in plugins.lock().unwrap().iter_mut() {
- let prevent_default = p.on_event(
- &event,
- event_loop,
- &proxy,
- control_flow,
- EventLoopIterationContext {
- callback: &mut callback,
- window_id_map: window_id_map.clone(),
- windows: windows.clone(),
- #[cfg(feature = "tracing")]
- active_tracing_spans: active_tracing_spans.clone(),
- },
- &web_context,
- );
- if prevent_default {
- return;
- }
- }
- handle_event_loop(
- event,
- event_loop,
- control_flow,
- EventLoopIterationContext {
- callback: &mut callback,
- window_id_map: window_id_map.clone(),
- windows: windows.clone(),
- #[cfg(feature = "tracing")]
- active_tracing_spans: active_tracing_spans.clone(),
- },
- );
- })
- }
- }
- pub struct EventLoopIterationContext<'a, T: UserEvent> {
- pub callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
- pub window_id_map: WindowIdStore,
- pub windows: Arc<WindowsStore>,
- #[cfg(feature = "tracing")]
- pub active_tracing_spans: ActiveTraceSpanStore,
- }
- struct UserMessageContext {
- windows: Arc<WindowsStore>,
- window_id_map: WindowIdStore,
- }
- fn handle_user_message<T: UserEvent>(
- event_loop: &EventLoopWindowTarget<Message<T>>,
- message: Message<T>,
- context: UserMessageContext,
- ) {
- let UserMessageContext {
- window_id_map,
- windows,
- } = context;
- match message {
- Message::Task(task) => task(),
- #[cfg(target_os = "macos")]
- Message::SetActivationPolicy(activation_policy) => {
- event_loop.set_activation_policy_at_runtime(tao_activation_policy(activation_policy))
- }
- Message::RequestExit(_code) => panic!("cannot handle RequestExit on the main thread"),
- #[cfg(target_os = "macos")]
- Message::Application(application_message) => match application_message {
- ApplicationMessage::Show => {
- event_loop.show_application();
- }
- ApplicationMessage::Hide => {
- event_loop.hide_application();
- }
- },
- Message::Window(id, window_message) => {
- let w = windows.0.borrow().get(&id).map(|w| {
- (
- w.inner.clone(),
- w.webviews.clone(),
- w.has_children.load(Ordering::Relaxed),
- w.window_event_listeners.clone(),
- )
- });
- if let Some((Some(window), webviews, has_children, window_event_listeners)) = w {
- match window_message {
- WindowMessage::AddEventListener(id, listener) => {
- window_event_listeners.lock().unwrap().insert(id, listener);
- }
- // Getters
- WindowMessage::ScaleFactor(tx) => tx.send(window.scale_factor()).unwrap(),
- WindowMessage::InnerPosition(tx) => tx
- .send(
- window
- .inner_position()
- .map(|p| PhysicalPositionWrapper(p).into())
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap(),
- WindowMessage::OuterPosition(tx) => tx
- .send(
- window
- .outer_position()
- .map(|p| PhysicalPositionWrapper(p).into())
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap(),
- WindowMessage::InnerSize(tx) => tx
- .send(PhysicalSizeWrapper(inner_size(&window, &webviews, has_children)).into())
- .unwrap(),
- WindowMessage::OuterSize(tx) => tx
- .send(PhysicalSizeWrapper(window.outer_size()).into())
- .unwrap(),
- WindowMessage::IsFullscreen(tx) => tx.send(window.fullscreen().is_some()).unwrap(),
- WindowMessage::IsMinimized(tx) => tx.send(window.is_minimized()).unwrap(),
- WindowMessage::IsMaximized(tx) => tx.send(window.is_maximized()).unwrap(),
- WindowMessage::IsFocused(tx) => tx.send(window.is_focused()).unwrap(),
- WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
- WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
- WindowMessage::IsMaximizable(tx) => tx.send(window.is_maximizable()).unwrap(),
- WindowMessage::IsMinimizable(tx) => tx.send(window.is_minimizable()).unwrap(),
- WindowMessage::IsClosable(tx) => tx.send(window.is_closable()).unwrap(),
- WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
- WindowMessage::Title(tx) => tx.send(window.title()).unwrap(),
- WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
- WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
- WindowMessage::AvailableMonitors(tx) => {
- tx.send(window.available_monitors().collect()).unwrap()
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- WindowMessage::GtkWindow(tx) => tx.send(GtkWindow(window.gtk_window().clone())).unwrap(),
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- WindowMessage::GtkBox(tx) => tx
- .send(GtkBox(window.default_vbox().unwrap().clone()))
- .unwrap(),
- WindowMessage::RawWindowHandle(tx) => tx
- .send(
- window
- .window_handle()
- .map(|h| SendRawWindowHandle(h.as_raw())),
- )
- .unwrap(),
- WindowMessage::Theme(tx) => {
- tx.send(map_theme(&window.theme())).unwrap();
- }
- // Setters
- WindowMessage::Center => {
- if let Some(monitor) = window.current_monitor() {
- let window_size = inner_size(&window, &webviews, has_children);
- let screen_size = monitor.size();
- let monitor_pos = monitor.position();
- let x = (screen_size.width as i32 - window_size.width as i32) / 2 + monitor_pos.x;
- let y = (screen_size.height as i32 - window_size.height as i32) / 2 + monitor_pos.y;
- window.set_outer_position(TaoPhysicalPosition::new(x, y));
- }
- }
- WindowMessage::RequestUserAttention(request_type) => {
- window.request_user_attention(request_type.map(|r| r.0));
- }
- WindowMessage::SetResizable(resizable) => window.set_resizable(resizable),
- WindowMessage::SetMaximizable(maximizable) => window.set_maximizable(maximizable),
- WindowMessage::SetMinimizable(minimizable) => window.set_minimizable(minimizable),
- WindowMessage::SetClosable(closable) => window.set_closable(closable),
- WindowMessage::SetTitle(title) => window.set_title(&title),
- WindowMessage::Maximize => window.set_maximized(true),
- WindowMessage::Unmaximize => window.set_maximized(false),
- WindowMessage::Minimize => window.set_minimized(true),
- WindowMessage::Unminimize => window.set_minimized(false),
- WindowMessage::Show => window.set_visible(true),
- WindowMessage::Hide => window.set_visible(false),
- WindowMessage::Close => {
- panic!("cannot handle `WindowMessage::Close` on the main thread")
- }
- WindowMessage::Destroy => {
- panic!("cannot handle `WindowMessage::Destroy` on the main thread")
- }
- WindowMessage::SetDecorations(decorations) => window.set_decorations(decorations),
- WindowMessage::SetShadow(_enable) => {
- #[cfg(windows)]
- window.set_undecorated_shadow(_enable);
- #[cfg(target_os = "macos")]
- window.set_has_shadow(_enable);
- }
- WindowMessage::SetAlwaysOnBottom(always_on_bottom) => {
- window.set_always_on_bottom(always_on_bottom)
- }
- WindowMessage::SetAlwaysOnTop(always_on_top) => window.set_always_on_top(always_on_top),
- WindowMessage::SetVisibleOnAllWorkspaces(visible_on_all_workspaces) => {
- window.set_visible_on_all_workspaces(visible_on_all_workspaces)
- }
- WindowMessage::SetContentProtected(protected) => window.set_content_protection(protected),
- WindowMessage::SetSize(size) => {
- window.set_inner_size(SizeWrapper::from(size).0);
- }
- WindowMessage::SetMinSize(size) => {
- window.set_min_inner_size(size.map(|s| SizeWrapper::from(s).0));
- }
- WindowMessage::SetMaxSize(size) => {
- window.set_max_inner_size(size.map(|s| SizeWrapper::from(s).0));
- }
- WindowMessage::SetPosition(position) => {
- window.set_outer_position(PositionWrapper::from(position).0)
- }
- WindowMessage::SetFullscreen(fullscreen) => {
- if fullscreen {
- window.set_fullscreen(Some(Fullscreen::Borderless(None)))
- } else {
- window.set_fullscreen(None)
- }
- #[cfg(windows)]
- if let Some(w) = windows.0.borrow_mut().get_mut(&id) {
- w.is_window_fullscreen = fullscreen;
- }
- }
- WindowMessage::SetFocus => {
- window.set_focus();
- }
- WindowMessage::SetIcon(icon) => {
- window.set_window_icon(Some(icon));
- }
- #[allow(unused_variables)]
- WindowMessage::SetSkipTaskbar(skip) => {
- #[cfg(any(windows, target_os = "linux"))]
- window.set_skip_taskbar(skip);
- }
- WindowMessage::SetCursorGrab(grab) => {
- let _ = window.set_cursor_grab(grab);
- }
- WindowMessage::SetCursorVisible(visible) => {
- window.set_cursor_visible(visible);
- }
- WindowMessage::SetCursorIcon(icon) => {
- window.set_cursor_icon(CursorIconWrapper::from(icon).0);
- }
- WindowMessage::SetCursorPosition(position) => {
- let _ = window.set_cursor_position(PositionWrapper::from(position).0);
- }
- WindowMessage::SetIgnoreCursorEvents(ignore) => {
- let _ = window.set_ignore_cursor_events(ignore);
- }
- WindowMessage::DragWindow => {
- let _ = window.drag_window();
- }
- WindowMessage::ResizeDragWindow(direction) => {
- let _ = window.drag_resize_window(match direction {
- tauri_runtime::ResizeDirection::East => tao::window::ResizeDirection::East,
- tauri_runtime::ResizeDirection::North => tao::window::ResizeDirection::North,
- tauri_runtime::ResizeDirection::NorthEast => tao::window::ResizeDirection::NorthEast,
- tauri_runtime::ResizeDirection::NorthWest => tao::window::ResizeDirection::NorthWest,
- tauri_runtime::ResizeDirection::South => tao::window::ResizeDirection::South,
- tauri_runtime::ResizeDirection::SouthEast => tao::window::ResizeDirection::SouthEast,
- tauri_runtime::ResizeDirection::SouthWest => tao::window::ResizeDirection::SouthWest,
- tauri_runtime::ResizeDirection::West => tao::window::ResizeDirection::West,
- });
- }
- WindowMessage::RequestRedraw => {
- window.request_redraw();
- }
- WindowMessage::SetProgressBar(progress_state) => {
- window.set_progress_bar(ProgressBarStateWrapper::from(progress_state).0);
- }
- }
- }
- }
- Message::Webview(window_id, webview_id, webview_message) => {
- if let WebviewMessage::Reparent(new_parent_window_id) = webview_message {
- let webview_handle = windows.0.borrow_mut().get_mut(&window_id).and_then(|w| {
- w.webviews
- .iter()
- .position(|w| w.id == webview_id)
- .map(|webview_index| w.webviews.remove(webview_index))
- });
- if let Some(webview) = webview_handle {
- if let Some((Some(new_parent_window), new_parent_window_webviews)) = windows
- .0
- .borrow_mut()
- .get_mut(&new_parent_window_id)
- .map(|w| (w.inner.clone(), &mut w.webviews))
- {
- #[cfg(target_os = "macos")]
- {
- use wry::WebViewExtMacOS;
- webview.inner.reparent(new_parent_window.ns_window() as _);
- new_parent_window_webviews.push(webview);
- }
- #[cfg(windows)]
- {
- webview.inner.reparent(new_parent_window.hwnd());
- new_parent_window_webviews.push(webview);
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- {
- if let Some(container) = new_parent_window.default_vbox() {
- webview.inner.reparent(container);
- new_parent_window_webviews.push(webview);
- }
- }
- }
- }
- } else {
- let webview_handle = windows.0.borrow().get(&window_id).map(|w| {
- (
- w.inner.clone(),
- w.webviews.iter().find(|w| w.id == webview_id).cloned(),
- )
- });
- if let Some((Some(window), Some(webview))) = webview_handle {
- match webview_message {
- WebviewMessage::WebviewEvent(_) => { /* already handled */ }
- WebviewMessage::SynthesizedWindowEvent(_) => { /* already handled */ }
- WebviewMessage::Reparent(_window_id) => { /* already handled */ }
- WebviewMessage::AddEventListener(id, listener) => {
- webview
- .webview_event_listeners
- .lock()
- .unwrap()
- .insert(id, listener);
- }
- #[cfg(all(feature = "tracing", not(target_os = "android")))]
- WebviewMessage::EvaluateScript(script, tx, span) => {
- let _span = span.entered();
- if let Err(e) = webview.evaluate_script(&script) {
- debug_eprintln!("{}", e);
- }
- tx.send(()).unwrap();
- }
- #[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
- WebviewMessage::EvaluateScript(script) => {
- if let Err(e) = webview.evaluate_script(&script) {
- debug_eprintln!("{}", e);
- }
- }
- WebviewMessage::Navigate(url) => webview.load_url(url.as_str()),
- WebviewMessage::Print => {
- let _ = webview.print();
- }
- WebviewMessage::Close => {
- windows.0.borrow_mut().get_mut(&window_id).map(|window| {
- if let Some(i) = window.webviews.iter().position(|w| w.id == webview.id) {
- window.webviews.remove(i);
- }
- window
- });
- }
- WebviewMessage::SetSize(size) => {
- let mut bounds = webview.bounds();
- let size = size.to_logical(window.scale_factor());
- bounds.width = size.width;
- bounds.height = size.height;
- if let Some(b) = &webview.bounds {
- let window_size = window.inner_size();
- let mut bounds = b.lock().unwrap();
- bounds.width_rate = size.width as f32 / window_size.width as f32;
- bounds.height_rate = size.height as f32 / window_size.height as f32;
- }
- webview.set_bounds(bounds);
- }
- WebviewMessage::SetPosition(position) => {
- let mut bounds = webview.bounds();
- let position = position.to_logical(window.scale_factor());
- bounds.x = position.x;
- bounds.y = position.y;
- if let Some(b) = &webview.bounds {
- let window_size = window.inner_size();
- let mut bounds = b.lock().unwrap();
- bounds.width_rate = position.x as f32 / window_size.width as f32;
- bounds.height_rate = position.y as f32 / window_size.height as f32;
- }
- webview.set_bounds(bounds);
- }
- // Getters
- WebviewMessage::Url(tx) => {
- tx.send(webview.url().parse().unwrap()).unwrap();
- }
- WebviewMessage::Position(tx) => {
- let bounds = webview.bounds();
- let position =
- LogicalPosition::new(bounds.x, bounds.y).to_physical(window.scale_factor());
- tx.send(position).unwrap();
- }
- WebviewMessage::Size(tx) => {
- let bounds = webview.bounds();
- let size =
- LogicalSize::new(bounds.width, bounds.height).to_physical(window.scale_factor());
- tx.send(size).unwrap();
- }
- WebviewMessage::SetFocus => {
- webview.focus();
- }
- WebviewMessage::WithWebview(f) => {
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- {
- f(webview.webview());
- }
- #[cfg(target_os = "macos")]
- {
- use wry::WebViewExtMacOS;
- f(Webview {
- webview: webview.webview(),
- manager: webview.manager(),
- ns_window: webview.ns_window(),
- });
- }
- #[cfg(target_os = "ios")]
- {
- use tao::platform::ios::WindowExtIOS;
- use wry::WebViewExtIOS;
- f(Webview {
- webview: webview.inner.webview(),
- manager: webview.inner.manager(),
- view_controller: window.ui_view_controller() as cocoa::base::id,
- });
- }
- #[cfg(windows)]
- {
- f(Webview {
- controller: webview.controller(),
- });
- }
- #[cfg(target_os = "android")]
- {
- f(webview.handle())
- }
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- WebviewMessage::OpenDevTools => {
- webview.open_devtools();
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- WebviewMessage::CloseDevTools => {
- webview.close_devtools();
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- WebviewMessage::IsDevToolsOpen(tx) => {
- tx.send(webview.is_devtools_open()).unwrap();
- }
- }
- }
- }
- }
- Message::CreateWebview(window_id, handler) => {
- let window = windows
- .0
- .borrow()
- .get(&window_id)
- .and_then(|w| w.inner.clone());
- if let Some(window) = window {
- match handler(&window) {
- Ok(webview) => {
- windows.0.borrow_mut().get_mut(&window_id).map(|w| {
- w.webviews.push(webview);
- w
- });
- }
- Err(e) => {
- debug_eprintln!("{}", e);
- }
- }
- }
- }
- Message::CreateWindow(window_id, handler) => match handler(event_loop) {
- Ok(webview) => {
- windows.0.borrow_mut().insert(window_id, webview);
- }
- Err(e) => {
- debug_eprintln!("{}", e);
- }
- },
- Message::CreateRawWindow(window_id, handler, sender) => {
- let (label, builder) = handler();
- #[cfg(windows)]
- let is_window_fullscreen = builder.window.fullscreen.is_some();
- #[cfg(windows)]
- let is_window_transparent = builder.window.transparent;
- if let Ok(window) = builder.build(event_loop) {
- window_id_map.insert(window.id(), window_id);
- let window = Arc::new(window);
- #[cfg(windows)]
- let surface = if is_window_transparent {
- if let Ok(context) = softbuffer::Context::new(window.clone()) {
- if let Ok(mut surface) = softbuffer::Surface::new(&context, window.clone()) {
- clear_window_surface(&window, &mut surface);
- Some(surface)
- } else {
- None
- }
- } else {
- None
- }
- } else {
- None
- };
- windows.0.borrow_mut().insert(
- window_id,
- WindowWrapper {
- label,
- has_children: AtomicBool::new(false),
- inner: Some(window.clone()),
- window_event_listeners: Default::default(),
- webviews: Vec::new(),
- #[cfg(windows)]
- is_window_fullscreen,
- #[cfg(windows)]
- is_window_transparent,
- #[cfg(windows)]
- surface,
- },
- );
- sender.send(Ok(Arc::downgrade(&window))).unwrap();
- } else {
- sender.send(Err(Error::CreateWindow)).unwrap();
- }
- }
- Message::UserEvent(_) => (),
- }
- }
- fn handle_event_loop<T: UserEvent>(
- event: Event<'_, Message<T>>,
- event_loop: &EventLoopWindowTarget<Message<T>>,
- control_flow: &mut ControlFlow,
- context: EventLoopIterationContext<'_, T>,
- ) {
- let EventLoopIterationContext {
- callback,
- window_id_map,
- windows,
- #[cfg(feature = "tracing")]
- active_tracing_spans,
- } = context;
- if *control_flow != ControlFlow::Exit {
- *control_flow = ControlFlow::Wait;
- }
- match event {
- Event::NewEvents(StartCause::Init) => {
- callback(RunEvent::Ready);
- }
- Event::NewEvents(StartCause::Poll) => {
- callback(RunEvent::Resumed);
- }
- Event::MainEventsCleared => {
- callback(RunEvent::MainEventsCleared);
- }
- Event::LoopDestroyed => {
- callback(RunEvent::Exit);
- }
- #[cfg(any(feature = "tracing", windows))]
- Event::RedrawRequested(id) => {
- #[cfg(windows)]
- if let Some(window_id) = window_id_map.get(&id) {
- let mut windows_ref = windows.0.borrow_mut();
- if let Some(window) = windows_ref.get_mut(&window_id) {
- if window.is_window_transparent {
- if let Some(surface) = &mut window.surface {
- if let Some(window) = &window.inner {
- clear_window_surface(window, surface)
- }
- }
- }
- }
- }
- #[cfg(feature = "tracing")]
- active_tracing_spans.remove_window_draw(id);
- }
- Event::UserEvent(Message::Webview(
- window_id,
- webview_id,
- WebviewMessage::WebviewEvent(event),
- )) => {
- let windows_ref = windows.0.borrow();
- if let Some(window) = windows_ref.get(&window_id) {
- if let Some(webview) = window.webviews.iter().find(|w| w.id == webview_id) {
- let label = webview.label.clone();
- let webview_event_listeners = webview.webview_event_listeners.clone();
- drop(windows_ref);
- callback(RunEvent::WebviewEvent {
- label,
- event: event.clone(),
- });
- let listeners = webview_event_listeners.lock().unwrap();
- let handlers = listeners.values();
- for handler in handlers {
- handler(&event);
- }
- }
- }
- }
- Event::UserEvent(Message::Webview(
- window_id,
- _webview_id,
- WebviewMessage::SynthesizedWindowEvent(event),
- )) => {
- if let Some(event) = WindowEventWrapper::from(event).0 {
- let windows_ref = windows.0.borrow();
- let window = windows_ref.get(&window_id);
- if let Some(window) = window {
- let label = window.label.clone();
- let window_event_listeners = window.window_event_listeners.clone();
- drop(windows_ref);
- callback(RunEvent::WindowEvent {
- label,
- event: event.clone(),
- });
- let listeners = window_event_listeners.lock().unwrap();
- let handlers = listeners.values();
- for handler in handlers {
- handler(&event);
- }
- }
- }
- }
- Event::WindowEvent {
- event, window_id, ..
- } => {
- if let Some(window_id) = window_id_map.get(&window_id) {
- {
- let windows_ref = windows.0.borrow();
- if let Some(window) = windows_ref.get(&window_id) {
- if let Some(event) = WindowEventWrapper::parse(window, &event).0 {
- let label = window.label.clone();
- let window_event_listeners = window.window_event_listeners.clone();
- drop(windows_ref);
- callback(RunEvent::WindowEvent {
- label,
- event: event.clone(),
- });
- let listeners = window_event_listeners.lock().unwrap();
- let handlers = listeners.values();
- for handler in handlers {
- handler(&event);
- }
- }
- }
- }
- match event {
- #[cfg(windows)]
- TaoWindowEvent::ThemeChanged(theme) => {
- if let Some(window) = windows.0.borrow().get(&window_id) {
- for webview in &window.webviews {
- let theme = match theme {
- TaoTheme::Dark => wry::Theme::Dark,
- TaoTheme::Light => wry::Theme::Light,
- _ => wry::Theme::Light,
- };
- webview.set_theme(theme);
- }
- }
- }
- TaoWindowEvent::CloseRequested => {
- on_close_requested(callback, window_id, windows.clone());
- }
- TaoWindowEvent::Destroyed => {
- let removed = windows.0.borrow_mut().remove(&window_id).is_some();
- if removed {
- let is_empty = windows.0.borrow().is_empty();
- if is_empty {
- let (tx, rx) = channel();
- callback(RunEvent::ExitRequested { code: None, tx });
- let recv = rx.try_recv();
- let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
- if !should_prevent {
- *control_flow = ControlFlow::Exit;
- }
- }
- }
- }
- TaoWindowEvent::Resized(size) => {
- if let Some(webviews) = windows
- .0
- .borrow()
- .get(&window_id)
- .map(|w| w.webviews.clone())
- {
- for webview in webviews {
- if let Some(bounds) = &webview.bounds {
- let b = bounds.lock().unwrap();
- webview.set_bounds(wry::Rect {
- x: (size.width as f32 * b.x_rate) as i32,
- y: (size.height as f32 * b.y_rate) as i32,
- width: (size.width as f32 * b.width_rate) as u32,
- height: (size.height as f32 * b.height_rate) as u32,
- });
- }
- }
- }
- }
- _ => {}
- }
- }
- }
- Event::UserEvent(message) => match message {
- Message::RequestExit(code) => {
- let (tx, rx) = channel();
- callback(RunEvent::ExitRequested {
- code: Some(code),
- tx,
- });
- let recv = rx.try_recv();
- let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent));
- if !should_prevent {
- *control_flow = ControlFlow::Exit;
- }
- }
- Message::Window(id, WindowMessage::Close) => {
- on_close_requested(callback, id, windows.clone());
- }
- Message::Window(id, WindowMessage::Destroy) => {
- on_window_close(id, windows.clone());
- }
- Message::UserEvent(t) => callback(RunEvent::UserEvent(t)),
- message => {
- handle_user_message(
- event_loop,
- message,
- UserMessageContext {
- window_id_map,
- windows,
- },
- );
- }
- },
- #[cfg(any(target_os = "macos", target_os = "ios"))]
- Event::Opened { urls } => {
- callback(RunEvent::Opened { urls });
- }
- _ => (),
- }
- }
- fn on_close_requested<'a, T: UserEvent>(
- callback: &'a mut (dyn FnMut(RunEvent<T>) + 'static),
- window_id: WindowId,
- windows: Arc<WindowsStore>,
- ) {
- let (tx, rx) = channel();
- let windows_ref = windows.0.borrow();
- if let Some(w) = windows_ref.get(&window_id) {
- let label = w.label.clone();
- let window_event_listeners = w.window_event_listeners.clone();
- drop(windows_ref);
- let listeners = window_event_listeners.lock().unwrap();
- let handlers = listeners.values();
- for handler in handlers {
- handler(&WindowEvent::CloseRequested {
- signal_tx: tx.clone(),
- });
- }
- callback(RunEvent::WindowEvent {
- label,
- event: WindowEvent::CloseRequested { signal_tx: tx },
- });
- if let Ok(true) = rx.try_recv() {
- } else {
- on_window_close(window_id, windows);
- }
- }
- }
- fn on_window_close(window_id: WindowId, windows: Arc<WindowsStore>) {
- if let Some(window_wrapper) = windows.0.borrow_mut().get_mut(&window_id) {
- window_wrapper.inner = None;
- #[cfg(windows)]
- window_wrapper.surface.take();
- }
- }
- fn parse_proxy_url(url: &Url) -> Result<ProxyConfig> {
- let host = url.host().map(|h| h.to_string()).unwrap_or_default();
- let port = url.port().map(|p| p.to_string()).unwrap_or_default();
- if url.scheme() == "http" {
- let config = ProxyConfig::Http(ProxyEndpoint { host, port });
- Ok(config)
- } else if url.scheme() == "socks5" {
- let config = ProxyConfig::Socks5(ProxyEndpoint { host, port });
- Ok(config)
- } else {
- Err(Error::InvalidProxyUrl)
- }
- }
- fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
- window_id: WindowId,
- webview_id: u32,
- event_loop: &EventLoopWindowTarget<Message<T>>,
- context: &Context<T>,
- pending: PendingWindow<T, Wry<T>>,
- after_window_creation: Option<F>,
- ) -> Result<WindowWrapper> {
- #[allow(unused_mut)]
- let PendingWindow {
- mut window_builder,
- label,
- webview,
- } = pending;
- #[cfg(feature = "tracing")]
- let _webview_create_span = tracing::debug_span!("wry::webview::create").entered();
- #[cfg(feature = "tracing")]
- let window_draw_span = tracing::debug_span!("wry::window::draw").entered();
- #[cfg(feature = "tracing")]
- let window_create_span =
- tracing::debug_span!(parent: &window_draw_span, "wry::window::create").entered();
- let window_event_listeners = WindowEventListeners::default();
- #[cfg(windows)]
- let is_window_transparent = window_builder.inner.window.transparent;
- #[cfg(windows)]
- let is_window_fullscreen = window_builder.inner.window.fullscreen.is_some();
- #[cfg(target_os = "macos")]
- {
- if window_builder.tabbing_identifier.is_none()
- || window_builder.inner.window.transparent
- || !window_builder.inner.window.decorations
- {
- window_builder.inner = window_builder.inner.with_automatic_window_tabbing(false);
- }
- }
- #[cfg(desktop)]
- if window_builder.center {
- let monitor = if let Some(window_position) = &window_builder.inner.window.position {
- event_loop.available_monitors().find(|m| {
- let monitor_pos = m.position();
- let monitor_size = m.size();
- let window_position = window_position.to_logical(m.scale_factor());
- monitor_pos.x <= window_position.x
- && window_position.x <= monitor_pos.x + monitor_size.width as i32
- && monitor_pos.y <= window_position.y
- && window_position.y <= monitor_pos.y + monitor_size.height as i32
- })
- } else {
- event_loop.primary_monitor()
- };
- if let Some(monitor) = monitor {
- let desired_size = window_builder
- .inner
- .window
- .inner_size
- .unwrap_or_else(|| TaoPhysicalSize::new(800, 600).into());
- let window_size = window_builder
- .inner
- .window
- .inner_size_constraints
- .clamp(desired_size, monitor.scale_factor())
- .to_logical::<i32>(monitor.scale_factor());
- let screen_size = monitor.size();
- let monitor_pos = monitor.position();
- let x = (screen_size.width as i32 - window_size.width) / 2 + monitor_pos.x;
- let y = (screen_size.height as i32 - window_size.height) / 2 + monitor_pos.y;
- window_builder = window_builder.position(x as f64, y as f64);
- }
- }
- let window = window_builder.inner.build(event_loop).unwrap();
- #[cfg(feature = "tracing")]
- {
- drop(window_create_span);
- context
- .main_thread
- .active_tracing_spans
- .0
- .borrow_mut()
- .push(ActiveTracingSpan::WindowDraw {
- id: window.id(),
- span: window_draw_span,
- });
- }
- context.window_id_map.insert(window.id(), window_id);
- if let Some(handler) = after_window_creation {
- let raw = RawWindow {
- #[cfg(windows)]
- hwnd: window.hwnd(),
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- gtk_window: window.gtk_window(),
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- default_vbox: window.default_vbox(),
- _marker: &std::marker::PhantomData,
- };
- handler(raw);
- }
- let mut webviews = Vec::new();
- if let Some(webview) = webview {
- webviews.push(create_webview(
- WebviewKind::WindowContent,
- &window,
- Arc::new(Mutex::new(window_id)),
- webview_id,
- context,
- webview,
- )?);
- }
- let window = Arc::new(window);
- #[cfg(windows)]
- let surface = if is_window_transparent {
- if let Ok(context) = softbuffer::Context::new(window.clone()) {
- if let Ok(mut surface) = softbuffer::Surface::new(&context, window.clone()) {
- clear_window_surface(&window, &mut surface);
- Some(surface)
- } else {
- None
- }
- } else {
- None
- }
- } else {
- None
- };
- Ok(WindowWrapper {
- label,
- has_children: AtomicBool::new(false),
- inner: Some(window),
- webviews,
- window_event_listeners,
- #[cfg(windows)]
- is_window_fullscreen,
- #[cfg(windows)]
- is_window_transparent,
- #[cfg(windows)]
- surface,
- })
- }
- /// the kind of the webview
- #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
- enum WebviewKind {
- // webview is the entire window content
- WindowContent,
- // webview is a child of the window, which can contain other webviews too
- WindowChild,
- }
- #[derive(Debug, Clone)]
- struct WebviewBounds {
- x_rate: f32,
- y_rate: f32,
- width_rate: f32,
- height_rate: f32,
- }
- fn create_webview<T: UserEvent>(
- kind: WebviewKind,
- window: &Window,
- window_id: Arc<Mutex<WindowId>>,
- id: WebviewId,
- context: &Context<T>,
- pending: PendingWebview<T, Wry<T>>,
- ) -> Result<WebviewWrapper> {
- #[allow(unused_mut)]
- let PendingWebview {
- webview_attributes,
- uri_scheme_protocols,
- label,
- ipc_handler,
- url,
- ..
- } = pending;
- let builder = match kind {
- #[cfg(not(any(
- target_os = "windows",
- target_os = "macos",
- target_os = "ios",
- target_os = "android"
- )))]
- WebviewKind::WindowChild => {
- // only way to account for menu bar height, and also works for multiwebviews :)
- let vbox = window.default_vbox().unwrap();
- WebViewBuilder::new_gtk(vbox)
- }
- #[cfg(any(
- target_os = "windows",
- target_os = "macos",
- target_os = "ios",
- target_os = "android"
- ))]
- WebviewKind::WindowChild => WebViewBuilder::new_as_child(&window),
- WebviewKind::WindowContent => {
- #[cfg(any(
- target_os = "windows",
- target_os = "macos",
- target_os = "ios",
- target_os = "android"
- ))]
- let builder = WebViewBuilder::new(&window);
- #[cfg(not(any(
- target_os = "windows",
- target_os = "macos",
- target_os = "ios",
- target_os = "android"
- )))]
- let builder = {
- let vbox = window.default_vbox().unwrap();
- WebViewBuilder::new_gtk(vbox)
- };
- builder
- }
- };
- let mut webview_builder = builder
- .with_focused(window.is_focused())
- .with_url(&url)
- .with_transparent(webview_attributes.transparent)
- .with_accept_first_mouse(webview_attributes.accept_first_mouse);
- #[cfg(windows)]
- if kind == WebviewKind::WindowContent {
- webview_builder = webview_builder.with_initialization_script(undecorated_resizing::SCRIPT);
- }
- if webview_attributes.file_drop_handler_enabled {
- let proxy = context.proxy.clone();
- let window_id_ = window_id.clone();
- webview_builder = webview_builder.with_file_drop_handler(move |event| {
- let event = match event {
- WryFileDropEvent::Hovered {
- paths,
- position: (x, y),
- } => FileDropEvent::Hovered {
- paths,
- position: PhysicalPosition::new(x as _, y as _),
- },
- WryFileDropEvent::Dropped {
- paths,
- position: (x, y),
- } => FileDropEvent::Dropped {
- paths,
- position: PhysicalPosition::new(x as _, y as _),
- },
- WryFileDropEvent::Cancelled => FileDropEvent::Cancelled,
- _ => unimplemented!(),
- };
- let message = if kind == WebviewKind::WindowContent {
- WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::FileDrop(event))
- } else {
- WebviewMessage::WebviewEvent(WebviewEvent::FileDrop(event))
- };
- let _ = proxy.send_event(Message::Webview(*window_id_.lock().unwrap(), id, message));
- true
- });
- }
- if let Some(navigation_handler) = pending.navigation_handler {
- webview_builder = webview_builder.with_navigation_handler(move |url| {
- Url::parse(&url)
- .map(|url| navigation_handler(&url))
- .unwrap_or(true)
- });
- }
- let webview_bounds = if let Some((position, size)) = webview_attributes.bounds {
- let size = size.to_logical(window.scale_factor());
- let position = position.to_logical(window.scale_factor());
- webview_builder = webview_builder.with_bounds(wry::Rect {
- x: position.x,
- y: position.y,
- width: size.width,
- height: size.height,
- });
- let window_size = window.inner_size();
- if webview_attributes.auto_resize {
- Some(WebviewBounds {
- x_rate: (position.x as f32) / window_size.width as f32,
- y_rate: (position.y as f32) / window_size.height as f32,
- width_rate: (size.width as f32) / window_size.width as f32,
- height_rate: (size.height as f32) / window_size.height as f32,
- })
- } else {
- None
- }
- } else {
- None
- };
- if let Some(download_handler) = pending.download_handler {
- let download_handler_ = download_handler.clone();
- webview_builder = webview_builder.with_download_started_handler(move |url, path| {
- if let Ok(url) = url.parse() {
- download_handler_(DownloadEvent::Requested {
- url,
- destination: path,
- })
- } else {
- false
- }
- });
- webview_builder = webview_builder.with_download_completed_handler(move |url, path, success| {
- if let Ok(url) = url.parse() {
- download_handler(DownloadEvent::Finished { url, path, success });
- }
- });
- }
- if let Some(page_load_handler) = pending.on_page_load_handler {
- webview_builder = webview_builder.with_on_page_load_handler(move |event, url| {
- let _ = Url::parse(&url).map(|url| {
- page_load_handler(
- url,
- match event {
- wry::PageLoadEvent::Started => tauri_runtime::webview::PageLoadEvent::Started,
- wry::PageLoadEvent::Finished => tauri_runtime::webview::PageLoadEvent::Finished,
- },
- )
- });
- });
- }
- if let Some(user_agent) = webview_attributes.user_agent {
- webview_builder = webview_builder.with_user_agent(&user_agent);
- }
- if let Some(proxy_url) = webview_attributes.proxy_url {
- let config = parse_proxy_url(&proxy_url)?;
- webview_builder = webview_builder.with_proxy_config(config);
- }
- #[cfg(windows)]
- {
- if let Some(additional_browser_args) = webview_attributes.additional_browser_args {
- webview_builder = webview_builder.with_additional_browser_args(&additional_browser_args);
- }
- webview_builder = webview_builder.with_theme(match window.theme() {
- TaoTheme::Dark => wry::Theme::Dark,
- TaoTheme::Light => wry::Theme::Light,
- _ => wry::Theme::Light,
- });
- }
- #[cfg(windows)]
- {
- webview_builder = webview_builder.with_https_scheme(false);
- }
- webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
- kind,
- window_id.clone(),
- id,
- context.clone(),
- label.clone(),
- ipc_handler,
- ));
- for (scheme, protocol) in uri_scheme_protocols {
- webview_builder =
- webview_builder.with_asynchronous_custom_protocol(scheme, move |request, responder| {
- protocol(
- request,
- Box::new(move |response| responder.respond(response)),
- )
- });
- }
- for script in webview_attributes.initialization_scripts {
- webview_builder = webview_builder.with_initialization_script(&script);
- }
- let mut web_context = context
- .main_thread
- .web_context
- .lock()
- .expect("poisoned WebContext store");
- let is_first_context = web_context.is_empty();
- let automation_enabled = std::env::var("TAURI_WEBVIEW_AUTOMATION").as_deref() == Ok("true");
- let web_context_key = // force a unique WebContext when automation is false;
- // the context must be stored on the HashMap because it must outlive the WebView on macOS
- if automation_enabled {
- webview_attributes.data_directory.clone()
- } else {
- // unique key
- let key = context.next_webcontext_id().to_string().into();
- Some(key)
- };
- let entry = web_context.entry(web_context_key.clone());
- let web_context = match entry {
- Occupied(occupied) => occupied.into_mut(),
- Vacant(vacant) => {
- let mut web_context = WebContext::new(webview_attributes.data_directory);
- web_context.set_allows_automation(if automation_enabled {
- is_first_context
- } else {
- false
- });
- vacant.insert(web_context)
- }
- };
- if webview_attributes.clipboard {
- webview_builder.attrs.clipboard = true;
- }
- if webview_attributes.incognito {
- webview_builder.attrs.incognito = true;
- }
- #[cfg(any(debug_assertions, feature = "devtools"))]
- {
- webview_builder = webview_builder.with_devtools(true);
- }
- #[cfg(target_os = "android")]
- {
- if let Some(on_webview_created) = pending.on_webview_created {
- webview_builder = webview_builder.on_webview_created(move |ctx| {
- on_webview_created(tauri_runtime::webview::CreationContext {
- env: ctx.env,
- activity: ctx.activity,
- webview: ctx.webview,
- })
- });
- }
- }
- let webview = webview_builder
- .with_web_context(web_context)
- .build()
- .map_err(|e| Error::CreateWebview(Box::new(e)))?;
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- if kind == WebviewKind::WindowContent {
- undecorated_resizing::attach_resize_handler(&webview);
- }
- #[cfg(windows)]
- if kind == WebviewKind::WindowContent {
- let controller = webview.controller();
- let proxy = context.proxy.clone();
- let proxy_ = proxy.clone();
- let window_id_ = window_id.clone();
- let mut token = EventRegistrationToken::default();
- unsafe {
- controller.add_GotFocus(
- &FocusChangedEventHandler::create(Box::new(move |_, _| {
- let _ = proxy.send_event(Message::Webview(
- *window_id_.lock().unwrap(),
- id,
- WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::Focused(true)),
- ));
- Ok(())
- })),
- &mut token,
- )
- }
- .unwrap();
- unsafe {
- controller.add_LostFocus(
- &FocusChangedEventHandler::create(Box::new(move |_, _| {
- let _ = proxy_.send_event(Message::Webview(
- *window_id.lock().unwrap(),
- id,
- WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::Focused(false)),
- ));
- Ok(())
- })),
- &mut token,
- )
- }
- .unwrap();
- }
- Ok(WebviewWrapper {
- label,
- id,
- inner: Rc::new(webview),
- context_store: context.main_thread.web_context.clone(),
- webview_event_listeners: Default::default(),
- context_key: if automation_enabled {
- None
- } else {
- web_context_key
- },
- bounds: webview_bounds.map(|b| Arc::new(Mutex::new(b))),
- })
- }
- /// Create a wry ipc handler from a tauri ipc handler.
- fn create_ipc_handler<T: UserEvent>(
- _kind: WebviewKind,
- window_id: Arc<Mutex<WindowId>>,
- webview_id: WebviewId,
- context: Context<T>,
- label: String,
- ipc_handler: Option<WebviewIpcHandler<T, Wry<T>>>,
- ) -> Box<IpcHandler> {
- Box::new(move |request| {
- #[cfg(windows)]
- if _kind == WebviewKind::WindowContent
- && undecorated_resizing::handle_request(context.clone(), *window_id.lock().unwrap(), &request)
- {
- return;
- }
- if let Some(handler) = &ipc_handler {
- handler(
- DetachedWebview {
- label: label.clone(),
- dispatcher: WryWebviewDispatcher {
- window_id: window_id.clone(),
- webview_id,
- context: context.clone(),
- },
- },
- request,
- );
- }
- })
- }
- #[cfg(target_os = "macos")]
- fn inner_size(
- window: &Window,
- webviews: &[WebviewWrapper],
- has_children: bool,
- ) -> TaoPhysicalSize<u32> {
- if has_children && webviews.len() == 1 {
- use wry::WebViewExtMacOS;
- let webview = webviews.first().unwrap();
- let view_frame = unsafe { cocoa::appkit::NSView::frame(webview.webview()) };
- let logical: TaoLogicalSize<f64> = (view_frame.size.width, view_frame.size.height).into();
- return logical.to_physical(window.scale_factor());
- }
- window.inner_size()
- }
- #[cfg(not(target_os = "macos"))]
- #[allow(unused_variables)]
- fn inner_size(
- window: &Window,
- webviews: &[WebviewWrapper],
- has_children: bool,
- ) -> TaoPhysicalSize<u32> {
- window.inner_size()
- }
- #[cfg(windows)]
- fn clear_window_surface(
- window: &Window,
- surface: &mut softbuffer::Surface<Arc<Window>, Arc<Window>>,
- ) {
- let size = window.inner_size();
- if let (Some(width), Some(height)) = (
- std::num::NonZeroU32::new(size.width),
- std::num::NonZeroU32::new(size.height),
- ) {
- surface.resize(width, height).unwrap();
- let mut buffer = surface.buffer_mut().unwrap();
- buffer.fill(0);
- let _ = buffer.present();
- }
- }
|