1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324 |
- // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- //! [](https://tauri.app)
- //!
- //! The [`wry`] Tauri [`Runtime`].
- //!
- //! None of the exposed API of this crate is stable, and it may break semver
- //! compatibility in the future. The major version only signifies the intended Tauri version.
- #![doc(
- html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/.github/icon.png",
- html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/.github/icon.png"
- )]
- use http::Request;
- use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
- use tauri_runtime::{
- dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
- monitor::Monitor,
- webview::{DetachedWebview, DownloadEvent, PendingWebview, WebviewIpcHandler},
- window::{
- CursorIcon, DetachedWindow, DragDropEvent, PendingWindow, RawWindow, WebviewEvent,
- WindowBuilder, WindowBuilderBase, WindowEvent, WindowId, WindowSizeConstraints,
- },
- 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, Theme};
- use url::Url;
- use wry::{
- DragDropEvent as WryDragDropEvent, 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(Request<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) {
- self
- .0
- .borrow_mut()
- .retain(|t| !matches!(t, ActiveTracingSpan::WindowDraw { id: _, span: _ }));
- }
- }
- #[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),
- }
- }
- }
- pub struct RectWrapper(pub wry::Rect);
- impl From<tauri_runtime::Rect> for RectWrapper {
- fn from(value: tauri_runtime::Rect) -> Self {
- RectWrapper(wry::Rect {
- position: value.position,
- size: value.size,
- })
- }
- }
- /// Wrapper around a [`tao::window::Icon`] that can be created from an [`Icon`].
- pub struct TaoIcon(pub TaoWindowIcon);
- impl TryFrom<Icon<'_>> for TaoIcon {
- type Error = Error;
- fn try_from(icon: Icon<'_>) -> std::result::Result<Self, Self::Error> {
- TaoWindowIcon::from_rgba(icon.rgba.to_vec(), icon.width, icon.height)
- .map(Self)
- .map_err(|e| Error::InvalidIcon(Box::new(e)))
- }
- }
- 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 {
- let size = inner_size(
- w,
- &window.webviews,
- window.has_children.load(Ordering::Relaxed),
- );
- Self(Some(WindowEvent::Resized(PhysicalSizeWrapper(size).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 {
- #[allow(unused_mut)]
- let mut builder = Self::default().focused(true);
- #[cfg(target_os = "macos")]
- {
- // TODO: find a proper way to prevent webview being pushed out of the window.
- // Workround for issue: https://github.com/tauri-apps/tauri/issues/10225
- // The window requies `NSFullSizeContentViewWindowMask` flag to prevent devtools
- // pushing the content view out of the window.
- // By setting the default style to `TitleBarStyle::Visible` should fix the issue for most of the users.
- builder = builder.title_bar_style(TitleBarStyle::Visible);
- }
- builder
- }
- 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)
- .closable(config.closable)
- .maximizable(config.maximizable)
- .minimizable(config.minimizable)
- .shadow(config.shadow);
- let mut constraints = WindowSizeConstraints::default();
- if let Some(min_width) = config.min_width {
- constraints.min_width = Some(tao::dpi::LogicalUnit::new(min_width).into());
- }
- if let Some(min_height) = config.min_height {
- constraints.min_height = Some(tao::dpi::LogicalUnit::new(min_height).into());
- }
- if let Some(max_width) = config.max_width {
- constraints.max_width = Some(tao::dpi::LogicalUnit::new(max_width).into());
- }
- if let Some(max_height) = config.max_height {
- constraints.max_height = Some(tao::dpi::LogicalUnit::new(max_height).into());
- }
- window = window.inner_size_constraints(constraints);
- 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 inner_size_constraints(mut self, constraints: WindowSizeConstraints) -> Self {
- self.inner.window.inner_size_constraints = tao::window::WindowSizeConstraints {
- min_width: constraints.min_width,
- min_height: constraints.min_height,
- max_width: constraints.max_width,
- max_height: constraints.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 as _);
- self
- }
- #[cfg(windows)]
- fn parent(mut self, parent: HWND) -> Self {
- self.inner = self.inner.with_parent_window(parent.0 as _);
- 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);
- }
- unknown => {
- #[cfg(feature = "tracing")]
- tracing::warn!("unknown title bar style applied: {unknown}");
- #[cfg(not(feature = "tracing"))]
- eprintln!("unknown title bar style applied: {unknown}");
- }
- }
- 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()
- }
- fn get_theme(&self) -> Option<Theme> {
- self.inner.window.preferred_theme.map(|theme| match theme {
- TaoTheme::Dark => Theme::Dark,
- _ => Theme::Light,
- })
- }
- }
- #[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>>),
- MonitorFromPoint(Sender<Option<MonitorHandle>>, (f64, f64)),
- 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>),
- SetSizeConstraints(WindowSizeConstraints),
- SetPosition(Position),
- SetFullscreen(bool),
- SetFocus,
- SetIcon(TaoWindowIcon),
- SetSkipTaskbar(bool),
- SetCursorGrab(bool),
- SetCursorVisible(bool),
- SetCursorIcon(CursorIcon),
- SetCursorPosition(Position),
- SetIgnoreCursorEvents(bool),
- SetProgressBar(ProgressBarState),
- SetTitleBarStyle(tauri_utils::TitleBarStyle),
- DragWindow,
- ResizeDragWindow(tauri_runtime::ResizeDirection),
- RequestRedraw,
- }
- #[derive(Debug, Clone)]
- pub enum SynthesizedWindowEvent {
- Focused(bool),
- DragDrop(DragDropEvent),
- }
- impl From<SynthesizedWindowEvent> for WindowEventWrapper {
- fn from(event: SynthesizedWindowEvent) -> Self {
- let event = match event {
- SynthesizedWindowEvent::Focused(focused) => WindowEvent::Focused(focused),
- SynthesizedWindowEvent::DragDrop(event) => WindowEvent::DragDrop(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),
- SetBounds(tauri_runtime::Rect),
- SetFocus,
- Reparent(WindowId, Sender<Result<()>>),
- SetAutoResize(bool),
- SetZoom(f64),
- // Getters
- Url(Sender<Result<String>>),
- Bounds(Sender<Result<tauri_runtime::Rect>>),
- Position(Sender<Result<PhysicalPosition<i32>>>),
- Size(Sender<Result<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<String> {
- webview_getter!(self, WebviewMessage::Url)?
- }
- fn bounds(&self) -> Result<tauri_runtime::Rect> {
- webview_getter!(self, WebviewMessage::Bounds)?
- }
- 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_bounds(&self, bounds: tauri_runtime::Rect) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetBounds(bounds),
- ),
- )
- }
- 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();
- let (tx, rx) = channel();
- send_user_message(
- &self.context,
- Message::Webview(
- *current_window_id,
- self.webview_id,
- WebviewMessage::Reparent(window_id, tx),
- ),
- )?;
- rx.recv().unwrap()?;
- *current_window_id = window_id;
- Ok(())
- }
- fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetAutoResize(auto_resize),
- ),
- )
- }
- #[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()),
- ),
- )
- }
- fn set_zoom(&self, scale_factor: f64) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Webview(
- *self.window_id.lock().unwrap(),
- self.webview_id,
- WebviewMessage::SetZoom(scale_factor),
- ),
- )
- }
- }
- /// 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 monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
- let (tx, rx) = channel();
- let _ = send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::MonitorFromPoint(tx, (x, y))),
- );
- Ok(
- rx.recv()
- .map_err(|_| crate::Error::FailedToReceiveMessage)?
- .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_size_constraints(&self, constraints: WindowSizeConstraints) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(
- self.window_id,
- WindowMessage::SetSizeConstraints(constraints),
- ),
- )
- }
- 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),
- ),
- )
- }
- fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()> {
- send_user_message(
- &self.context,
- Message::Window(self.window_id, WindowMessage::SetTitleBarStyle(style)),
- )
- }
- }
- #[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: Arc<Mutex<Option<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_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 monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .monitor_from_point(x, y)
- .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()
- }
- fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
- self
- .context
- .main_thread
- .window_target
- .cursor_position()
- .map(PhysicalPositionWrapper)
- .map(Into::into)
- .map_err(|_| Error::FailedToGetCursorPosition)
- }
- #[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);
- }
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- if let Some(app_id) = args.app_id {
- use tao::platform::unix::EventLoopBuilderExtUnix;
- event_loop_builder.with_app_id(app_id);
- }
- 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 monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
- self
- .context
- .main_thread
- .window_target
- .monitor_from_point(x, y)
- .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()
- }
- fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
- self
- .context
- .main_thread
- .window_target
- .cursor_position()
- .map(PhysicalPositionWrapper)
- .map(Into::into)
- .map_err(|_| Error::FailedToGetCursorPosition)
- }
- #[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::MonitorFromPoint(tx, (x, y)) => {
- tx.send(window.monitor_from_point(x, y)).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 => {
- #[cfg(not(target_os = "macos"))]
- if let Some(monitor) = window.current_monitor() {
- #[allow(unused_mut)]
- let mut window_size = window.outer_size();
- #[cfg(windows)]
- if window.is_decorated() {
- use windows::Win32::Foundation::RECT;
- use windows::Win32::Graphics::Dwm::{
- DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS,
- };
- let mut rect = RECT::default();
- let result = unsafe {
- DwmGetWindowAttribute(
- HWND(window.hwnd() as _),
- DWMWA_EXTENDED_FRAME_BOUNDS,
- &mut rect as *mut _ as *mut _,
- std::mem::size_of::<RECT>() as u32,
- )
- };
- if result.is_ok() {
- window_size.height = (rect.bottom - rect.top) as u32;
- }
- }
- window.set_outer_position(calculate_window_center_position(window_size, monitor));
- }
- #[cfg(target_os = "macos")]
- {
- use cocoa::{appkit::NSWindow, base::id};
- let ns_window: id = window.ns_window() as _;
- unsafe { ns_window.center() };
- }
- }
- WindowMessage::RequestUserAttention(request_type) => {
- window.request_user_attention(request_type.map(|r| r.0));
- }
- WindowMessage::SetResizable(resizable) => {
- window.set_resizable(resizable);
- #[cfg(windows)]
- if !resizable {
- undecorated_resizing::detach_resize_handler(window.hwnd());
- } else if !window.is_decorated() {
- undecorated_resizing::attach_resize_handler(window.hwnd());
- }
- }
- 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);
- #[cfg(windows)]
- if decorations {
- undecorated_resizing::detach_resize_handler(window.hwnd());
- } else if window.is_resizable() {
- undecorated_resizing::attach_resize_handler(window.hwnd());
- }
- }
- 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::SetSizeConstraints(constraints) => {
- window.set_inner_size_constraints(tao::window::WindowSizeConstraints {
- min_width: constraints.min_width,
- min_height: constraints.min_height,
- max_width: constraints.max_width,
- max_height: constraints.max_height,
- });
- }
- 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)
- }
- }
- 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);
- }
- WindowMessage::SetTitleBarStyle(_style) => {
- #[cfg(target_os = "macos")]
- match _style {
- TitleBarStyle::Visible => {
- window.set_titlebar_transparent(false);
- window.set_fullsize_content_view(true);
- }
- TitleBarStyle::Transparent => {
- window.set_titlebar_transparent(true);
- window.set_fullsize_content_view(false);
- }
- TitleBarStyle::Overlay => {
- window.set_titlebar_transparent(true);
- window.set_fullsize_content_view(true);
- }
- unknown => {
- #[cfg(feature = "tracing")]
- tracing::warn!("unknown title bar style applied: {unknown}");
- #[cfg(not(feature = "tracing"))]
- eprintln!("unknown title bar style applied: {unknown}");
- }
- };
- }
- }
- }
- }
- Message::Webview(window_id, webview_id, webview_message) => {
- #[cfg(any(
- target_os = "macos",
- windows,
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- if let WebviewMessage::Reparent(new_parent_window_id, tx) = 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")]
- let reparent_result = {
- use wry::WebViewExtMacOS;
- webview.inner.reparent(new_parent_window.ns_window() as _)
- };
- #[cfg(windows)]
- let reparent_result = { webview.inner.reparent(new_parent_window.hwnd()) };
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- let reparent_result = {
- if let Some(container) = new_parent_window.default_vbox() {
- webview.inner.reparent(container)
- } else {
- Err(wry::Error::MessageSender)
- }
- };
- match reparent_result {
- Ok(_) => {
- new_parent_window_webviews.push(webview);
- tx.send(Ok(())).unwrap();
- }
- Err(e) => {
- log::error!("failed to reparent webview: {e}");
- tx.send(Err(Error::FailedToSendMessage)).unwrap();
- }
- }
- }
- } else {
- tx.send(Err(Error::FailedToSendMessage)).unwrap();
- }
- return;
- }
- 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, _tx) => { /* 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) {
- log::error!("{}", e);
- }
- tx.send(()).unwrap();
- }
- #[cfg(not(all(feature = "tracing", not(target_os = "android"))))]
- WebviewMessage::EvaluateScript(script) => {
- if let Err(e) = webview.evaluate_script(&script) {
- log::error!("{}", e);
- }
- }
- WebviewMessage::Navigate(url) => {
- if let Err(e) = webview.load_url(url.as_str()) {
- log::error!("failed to navigate to url {}: {}", url, e);
- }
- }
- 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::SetBounds(bounds) => {
- let bounds: RectWrapper = bounds.into();
- let bounds = bounds.0;
- if let Some(b) = &mut *webview.bounds.lock().unwrap() {
- let scale_factor = window.scale_factor();
- let size = bounds.size.to_logical::<f32>(scale_factor);
- let position = bounds.position.to_logical::<f32>(scale_factor);
- let window_size = window.inner_size().to_logical::<f32>(scale_factor);
- b.width_rate = size.width / window_size.width;
- b.height_rate = size.height / window_size.height;
- b.x_rate = position.x / window_size.width;
- b.y_rate = position.y / window_size.height;
- }
- if let Err(e) = webview.set_bounds(bounds) {
- log::error!("failed to set webview size: {e}");
- }
- }
- WebviewMessage::SetSize(size) => match webview.bounds() {
- Ok(mut bounds) => {
- bounds.size = size;
- let scale_factor = window.scale_factor();
- let size = size.to_logical::<f32>(scale_factor);
- if let Some(b) = &mut *webview.bounds.lock().unwrap() {
- let window_size = window.inner_size().to_logical::<f32>(scale_factor);
- b.width_rate = size.width / window_size.width;
- b.height_rate = size.height / window_size.height;
- }
- if let Err(e) = webview.set_bounds(bounds) {
- log::error!("failed to set webview size: {e}");
- }
- }
- Err(e) => {
- log::error!("failed to get webview bounds: {e}");
- }
- },
- WebviewMessage::SetPosition(position) => match webview.bounds() {
- Ok(mut bounds) => {
- bounds.position = position;
- let scale_factor = window.scale_factor();
- let position = position.to_logical::<f32>(scale_factor);
- if let Some(b) = &mut *webview.bounds.lock().unwrap() {
- let window_size = window.inner_size().to_logical::<f32>(scale_factor);
- b.x_rate = position.x / window_size.width;
- b.y_rate = position.y / window_size.height;
- }
- if let Err(e) = webview.set_bounds(bounds) {
- log::error!("failed to set webview position: {e}");
- }
- }
- Err(e) => {
- log::error!("failed to get webview bounds: {e}");
- }
- },
- WebviewMessage::SetZoom(scale_factor) => {
- if let Err(e) = webview.zoom(scale_factor) {
- log::error!("failed to set webview zoom: {e}");
- }
- }
- // Getters
- WebviewMessage::Url(tx) => {
- tx.send(
- webview
- .url()
- .map(|u| u.parse().expect("invalid webview URL"))
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap();
- }
- WebviewMessage::Bounds(tx) => {
- tx.send(
- webview
- .bounds()
- .map(|bounds| tauri_runtime::Rect {
- size: bounds.size,
- position: bounds.position,
- })
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap();
- }
- WebviewMessage::Position(tx) => {
- tx.send(
- webview
- .bounds()
- .map(|bounds| bounds.position.to_physical(window.scale_factor()))
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap();
- }
- WebviewMessage::Size(tx) => {
- tx.send(
- webview
- .bounds()
- .map(|bounds| bounds.size.to_physical(window.scale_factor()))
- .map_err(|_| Error::FailedToSendMessage),
- )
- .unwrap();
- }
- WebviewMessage::SetFocus => {
- if let Err(e) = webview.focus() {
- log::error!("failed to focus webview: {e}");
- }
- }
- WebviewMessage::SetAutoResize(auto_resize) => match webview.bounds() {
- Ok(bounds) => {
- let scale_factor = window.scale_factor();
- let window_size = window.inner_size().to_logical::<f32>(scale_factor);
- *webview.bounds.lock().unwrap() = if auto_resize {
- let size = bounds.size.to_logical::<f32>(scale_factor);
- let position = bounds.position.to_logical::<f32>(scale_factor);
- Some(WebviewBounds {
- x_rate: position.x / window_size.width,
- y_rate: position.y / window_size.height,
- width_rate: size.width / window_size.width,
- height_rate: size.height / window_size.height,
- })
- } else {
- None
- };
- }
- Err(e) => {
- log::error!("failed to get webview bounds: {e}");
- }
- },
- 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.has_children.store(true, Ordering::Relaxed);
- w
- });
- }
- Err(e) => {
- log::error!("{}", e);
- }
- }
- }
- }
- Message::CreateWindow(window_id, handler) => match handler(event_loop) {
- Ok(webview) => {
- windows.0.borrow_mut().insert(window_id, webview);
- }
- Err(e) => {
- log::error!("{}", e);
- }
- },
- Message::CreateRawWindow(window_id, handler, sender) => {
- let (label, builder) = handler();
- #[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_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(windows)]
- Event::RedrawRequested(id) => {
- 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")]
- Event::RedrawEventsCleared => {
- active_tracing_spans.remove_window_draw();
- }
- 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,
- };
- if let Err(e) = webview.set_theme(theme) {
- log::error!("failed to set theme: {e}");
- }
- }
- }
- }
- TaoWindowEvent::CloseRequested => {
- on_close_requested(callback, window_id, windows);
- }
- 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((Some(window), webviews)) = windows
- .0
- .borrow()
- .get(&window_id)
- .map(|w| (w.inner.clone(), w.webviews.clone()))
- {
- let size = size.to_logical::<f32>(window.scale_factor());
- for webview in webviews {
- if let Some(b) = &*webview.bounds.lock().unwrap() {
- if let Err(e) = webview.set_bounds(wry::Rect {
- position: LogicalPosition::new(size.width * b.x_rate, size.height * b.y_rate)
- .into(),
- size: LogicalSize::new(size.width * b.width_rate, size.height * b.height_rate)
- .into(),
- }) {
- log::error!("failed to autoresize webview: {e}");
- }
- }
- }
- }
- }
- _ => {}
- }
- }
- }
- 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);
- }
- Message::Window(id, WindowMessage::Destroy) => {
- on_window_close(id, windows);
- }
- 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 });
- }
- #[cfg(target_os = "macos")]
- Event::Reopen {
- has_visible_windows,
- ..
- } => callback(RunEvent::Reopen {
- has_visible_windows,
- }),
- _ => (),
- }
- }
- 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(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();
- // type annotations required for 32bit targets.
- let window_position: LogicalPosition<i32> = 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 scale_factor = monitor.scale_factor();
- #[allow(unused_mut)]
- let mut window_size = window_builder
- .inner
- .window
- .inner_size_constraints
- .clamp(desired_size, scale_factor)
- .to_physical::<u32>(scale_factor);
- #[cfg(windows)]
- {
- if window_builder.inner.window.decorations {
- use windows::Win32::UI::WindowsAndMessaging::{AdjustWindowRect, WS_OVERLAPPEDWINDOW};
- let mut rect = windows::Win32::Foundation::RECT::default();
- let result = unsafe { AdjustWindowRect(&mut rect, WS_OVERLAPPEDWINDOW, false) };
- if result.is_ok() {
- window_size.width += (rect.right - rect.left) as u32;
- // rect.bottom is made out of shadow, and we don't care about it
- window_size.height += -rect.top as u32;
- }
- }
- }
- let position = calculate_window_center_position(window_size, monitor);
- let logical_position = position.to_logical::<f64>(scale_factor);
- window_builder = window_builder.position(logical_position.x, logical_position.y);
- }
- }
- 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(
- #[cfg(feature = "unstable")]
- WebviewKind::WindowChild,
- #[cfg(not(feature = "unstable"))]
- 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_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)
- .with_hotkeys_zoom(webview_attributes.zoom_hotkeys_enabled);
- if webview_attributes.drag_drop_handler_enabled {
- let proxy = context.proxy.clone();
- let window_id_ = window_id.clone();
- webview_builder = webview_builder.with_drag_drop_handler(move |event| {
- let event = match event {
- WryDragDropEvent::Enter {
- paths,
- position: (x, y),
- } => DragDropEvent::Enter {
- paths,
- position: PhysicalPosition::new(x as _, y as _),
- },
- WryDragDropEvent::Over { position: (x, y) } => DragDropEvent::Over {
- position: PhysicalPosition::new(x as _, y as _),
- },
- WryDragDropEvent::Drop {
- paths,
- position: (x, y),
- } => DragDropEvent::Drop {
- paths,
- position: PhysicalPosition::new(x as _, y as _),
- },
- WryDragDropEvent::Leave => DragDropEvent::Leave,
- _ => unimplemented!(),
- };
- let message = if kind == WebviewKind::WindowContent {
- WebviewMessage::SynthesizedWindowEvent(SynthesizedWindowEvent::DragDrop(event))
- } else {
- WebviewMessage::WebviewEvent(WebviewEvent::DragDrop(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()
- .map(|url| navigation_handler(&url))
- .unwrap_or(true)
- });
- }
- let webview_bounds = if let Some(bounds) = webview_attributes.bounds {
- let bounds: RectWrapper = bounds.into();
- let bounds = bounds.0;
- let scale_factor = window.scale_factor();
- let position = bounds.position.to_logical::<f32>(scale_factor);
- let size = bounds.size.to_logical::<f32>(scale_factor);
- webview_builder = webview_builder.with_bounds(bounds);
- let window_size = window.inner_size().to_logical::<f32>(scale_factor);
- if webview_attributes.auto_resize {
- Some(WebviewBounds {
- x_rate: position.x / window_size.width,
- y_rate: position.y / window_size.height,
- width_rate: size.width / window_size.width,
- height_rate: size.height / window_size.height,
- })
- } else {
- None
- }
- } else {
- #[cfg(feature = "unstable")]
- {
- webview_builder = webview_builder.with_bounds(wry::Rect {
- position: LogicalPosition::new(0, 0).into(),
- size: window.inner_size().into(),
- });
- Some(WebviewBounds {
- x_rate: 0.,
- y_rate: 0.,
- width_rate: 1.,
- height_rate: 1.,
- })
- }
- #[cfg(not(feature = "unstable"))]
- 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().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)))?;
- if kind == WebviewKind::WindowContent {
- #[cfg(any(
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "netbsd",
- target_os = "openbsd"
- ))]
- undecorated_resizing::attach_resize_handler(&webview);
- #[cfg(windows)]
- if window.is_resizable() && !window.is_decorated() {
- undecorated_resizing::attach_resize_handler(window.hwnd());
- }
- }
- #[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: Arc::new(Mutex::new(webview_bounds)),
- })
- }
- /// 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| {
- 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.is_empty() {
- 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()
- }
- fn calculate_window_center_position(
- window_size: TaoPhysicalSize<u32>,
- target_monitor: MonitorHandle,
- ) -> TaoPhysicalPosition<i32> {
- #[cfg(windows)]
- {
- use tao::platform::windows::MonitorHandleExtWindows;
- use windows::Win32::Graphics::Gdi::{GetMonitorInfoW, HMONITOR, MONITORINFO};
- let mut monitor_info = MONITORINFO {
- cbSize: std::mem::size_of::<MONITORINFO>() as u32,
- ..Default::default()
- };
- let status =
- unsafe { GetMonitorInfoW(HMONITOR(target_monitor.hmonitor() as _), &mut monitor_info) };
- if status.into() {
- let available_width = monitor_info.rcWork.right - monitor_info.rcWork.left;
- let available_height = monitor_info.rcWork.bottom - monitor_info.rcWork.top;
- let x = (available_width - window_size.width as i32) / 2 + monitor_info.rcWork.left;
- let y = (available_height - window_size.height as i32) / 2 + monitor_info.rcWork.top;
- return TaoPhysicalPosition::new(x, y);
- }
- }
- let screen_size = target_monitor.size();
- let monitor_pos = target_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;
- TaoPhysicalPosition::new(x, y)
- }
- #[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();
- }
- }
|