1use crate::{MapProjection, Tile, ffi};
7#[cfg(feature = "v1_7")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v1_7")))]
9use glib::object::ObjectType as _;
10use glib::{
11 prelude::*,
12 signal::{SignalHandlerId, connect_raw},
13 translate::*,
14};
15use std::{boxed::Box as Box_, pin::Pin};
16
17glib::wrapper! {
18 #[doc(alias = "ShumateMapSource")]
92 pub struct MapSource(Object<ffi::ShumateMapSource, ffi::ShumateMapSourceClass>);
93
94 match fn {
95 type_ => || ffi::shumate_map_source_get_type(),
96 }
97}
98
99impl MapSource {
100 pub const NONE: Option<&'static MapSource> = None;
101}
102
103pub trait MapSourceExt: IsA<MapSource> + 'static {
109 #[doc(alias = "shumate_map_source_fill_tile_async")]
117 fn fill_tile_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
118 &self,
119 tile: &Tile,
120 cancellable: Option<&impl IsA<gio::Cancellable>>,
121 callback: P,
122 ) {
123 let main_context = glib::MainContext::ref_thread_default();
124 let is_main_context_owner = main_context.is_owner();
125 let has_acquired_main_context = (!is_main_context_owner)
126 .then(|| main_context.acquire().ok())
127 .flatten();
128 assert!(
129 is_main_context_owner || has_acquired_main_context.is_some(),
130 "Async operations only allowed if the thread is owning the MainContext"
131 );
132
133 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
134 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
135 unsafe extern "C" fn fill_tile_async_trampoline<
136 P: FnOnce(Result<(), glib::Error>) + 'static,
137 >(
138 _source_object: *mut glib::gobject_ffi::GObject,
139 res: *mut gio::ffi::GAsyncResult,
140 user_data: glib::ffi::gpointer,
141 ) {
142 unsafe {
143 let mut error = std::ptr::null_mut();
144 ffi::shumate_map_source_fill_tile_finish(_source_object as *mut _, res, &mut error);
145 let result = if error.is_null() {
146 Ok(())
147 } else {
148 Err(from_glib_full(error))
149 };
150 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
151 Box_::from_raw(user_data as *mut _);
152 let callback: P = callback.into_inner();
153 callback(result);
154 }
155 }
156 let callback = fill_tile_async_trampoline::<P>;
157 unsafe {
158 ffi::shumate_map_source_fill_tile_async(
159 self.as_ref().to_glib_none().0,
160 tile.to_glib_none().0,
161 cancellable.map(|p| p.as_ref()).to_glib_none().0,
162 Some(callback),
163 Box_::into_raw(user_data) as *mut _,
164 );
165 }
166 }
167
168 fn fill_tile_future(
169 &self,
170 tile: &Tile,
171 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
172 let tile = tile.clone();
173 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
174 obj.fill_tile_async(&tile, Some(cancellable), move |res| {
175 send.resolve(res);
176 });
177 }))
178 }
179
180 #[doc(alias = "shumate_map_source_get_column_count")]
189 #[doc(alias = "get_column_count")]
190 fn column_count(&self, zoom_level: u32) -> u32 {
191 unsafe {
192 ffi::shumate_map_source_get_column_count(self.as_ref().to_glib_none().0, zoom_level)
193 }
194 }
195
196 #[doc(alias = "shumate_map_source_get_id")]
202 #[doc(alias = "get_id")]
203 fn id(&self) -> Option<glib::GString> {
204 unsafe {
205 from_glib_none(ffi::shumate_map_source_get_id(
206 self.as_ref().to_glib_none().0,
207 ))
208 }
209 }
210
211 #[doc(alias = "shumate_map_source_get_latitude")]
222 #[doc(alias = "get_latitude")]
223 fn latitude(&self, zoom_level: f64, y: f64) -> f64 {
224 unsafe {
225 ffi::shumate_map_source_get_latitude(self.as_ref().to_glib_none().0, zoom_level, y)
226 }
227 }
228
229 #[doc(alias = "shumate_map_source_get_license")]
235 #[doc(alias = "get_license")]
236 fn license(&self) -> Option<glib::GString> {
237 unsafe {
238 from_glib_none(ffi::shumate_map_source_get_license(
239 self.as_ref().to_glib_none().0,
240 ))
241 }
242 }
243
244 #[doc(alias = "shumate_map_source_get_license_uri")]
250 #[doc(alias = "get_license_uri")]
251 #[doc(alias = "license-uri")]
252 fn license_uri(&self) -> Option<glib::GString> {
253 unsafe {
254 from_glib_none(ffi::shumate_map_source_get_license_uri(
255 self.as_ref().to_glib_none().0,
256 ))
257 }
258 }
259
260 #[doc(alias = "shumate_map_source_get_longitude")]
271 #[doc(alias = "get_longitude")]
272 fn longitude(&self, zoom_level: f64, x: f64) -> f64 {
273 unsafe {
274 ffi::shumate_map_source_get_longitude(self.as_ref().to_glib_none().0, zoom_level, x)
275 }
276 }
277
278 #[doc(alias = "shumate_map_source_get_max_zoom_level")]
284 #[doc(alias = "get_max_zoom_level")]
285 #[doc(alias = "max-zoom-level")]
286 fn max_zoom_level(&self) -> u32 {
287 unsafe { ffi::shumate_map_source_get_max_zoom_level(self.as_ref().to_glib_none().0) }
288 }
289
290 #[doc(alias = "shumate_map_source_get_meters_per_pixel")]
302 #[doc(alias = "get_meters_per_pixel")]
303 fn meters_per_pixel(&self, zoom_level: f64, latitude: f64, longitude: f64) -> f64 {
304 unsafe {
305 ffi::shumate_map_source_get_meters_per_pixel(
306 self.as_ref().to_glib_none().0,
307 zoom_level,
308 latitude,
309 longitude,
310 )
311 }
312 }
313
314 #[doc(alias = "shumate_map_source_get_min_zoom_level")]
320 #[doc(alias = "get_min_zoom_level")]
321 #[doc(alias = "min-zoom-level")]
322 fn min_zoom_level(&self) -> u32 {
323 unsafe { ffi::shumate_map_source_get_min_zoom_level(self.as_ref().to_glib_none().0) }
324 }
325
326 #[doc(alias = "shumate_map_source_get_name")]
332 #[doc(alias = "get_name")]
333 fn name(&self) -> Option<glib::GString> {
334 unsafe {
335 from_glib_none(ffi::shumate_map_source_get_name(
336 self.as_ref().to_glib_none().0,
337 ))
338 }
339 }
340
341 #[doc(alias = "shumate_map_source_get_projection")]
347 #[doc(alias = "get_projection")]
348 fn projection(&self) -> MapProjection {
349 unsafe {
350 from_glib(ffi::shumate_map_source_get_projection(
351 self.as_ref().to_glib_none().0,
352 ))
353 }
354 }
355
356 #[doc(alias = "shumate_map_source_get_row_count")]
364 #[doc(alias = "get_row_count")]
365 fn row_count(&self, zoom_level: u32) -> u32 {
366 unsafe { ffi::shumate_map_source_get_row_count(self.as_ref().to_glib_none().0, zoom_level) }
367 }
368
369 #[doc(alias = "shumate_map_source_get_tile_size")]
375 #[doc(alias = "get_tile_size")]
376 #[doc(alias = "tile-size")]
377 fn tile_size(&self) -> u32 {
378 unsafe { ffi::shumate_map_source_get_tile_size(self.as_ref().to_glib_none().0) }
379 }
380
381 #[doc(alias = "shumate_map_source_get_tile_size_at_zoom")]
394 #[doc(alias = "get_tile_size_at_zoom")]
395 fn tile_size_at_zoom(&self, zoom_level: f64) -> f64 {
396 unsafe {
397 ffi::shumate_map_source_get_tile_size_at_zoom(
398 self.as_ref().to_glib_none().0,
399 zoom_level,
400 )
401 }
402 }
403
404 #[doc(alias = "shumate_map_source_get_x")]
415 #[doc(alias = "get_x")]
416 fn x(&self, zoom_level: f64, longitude: f64) -> f64 {
417 unsafe {
418 ffi::shumate_map_source_get_x(self.as_ref().to_glib_none().0, zoom_level, longitude)
419 }
420 }
421
422 #[doc(alias = "shumate_map_source_get_y")]
433 #[doc(alias = "get_y")]
434 fn y(&self, zoom_level: f64, latitude: f64) -> f64 {
435 unsafe {
436 ffi::shumate_map_source_get_y(self.as_ref().to_glib_none().0, zoom_level, latitude)
437 }
438 }
439
440 #[doc(alias = "shumate_map_source_set_id")]
444 #[doc(alias = "id")]
445 fn set_id(&self, id: &str) {
446 unsafe {
447 ffi::shumate_map_source_set_id(self.as_ref().to_glib_none().0, id.to_glib_none().0);
448 }
449 }
450
451 #[doc(alias = "shumate_map_source_set_license")]
455 #[doc(alias = "license")]
456 fn set_license(&self, license: &str) {
457 unsafe {
458 ffi::shumate_map_source_set_license(
459 self.as_ref().to_glib_none().0,
460 license.to_glib_none().0,
461 );
462 }
463 }
464
465 #[doc(alias = "shumate_map_source_set_license_uri")]
469 #[doc(alias = "license-uri")]
470 fn set_license_uri(&self, license_uri: &str) {
471 unsafe {
472 ffi::shumate_map_source_set_license_uri(
473 self.as_ref().to_glib_none().0,
474 license_uri.to_glib_none().0,
475 );
476 }
477 }
478
479 #[doc(alias = "shumate_map_source_set_max_zoom_level")]
483 #[doc(alias = "max-zoom-level")]
484 fn set_max_zoom_level(&self, zoom_level: u32) {
485 unsafe {
486 ffi::shumate_map_source_set_max_zoom_level(self.as_ref().to_glib_none().0, zoom_level);
487 }
488 }
489
490 #[doc(alias = "shumate_map_source_set_min_zoom_level")]
494 #[doc(alias = "min-zoom-level")]
495 fn set_min_zoom_level(&self, zoom_level: u32) {
496 unsafe {
497 ffi::shumate_map_source_set_min_zoom_level(self.as_ref().to_glib_none().0, zoom_level);
498 }
499 }
500
501 #[doc(alias = "shumate_map_source_set_name")]
505 #[doc(alias = "name")]
506 fn set_name(&self, name: &str) {
507 unsafe {
508 ffi::shumate_map_source_set_name(self.as_ref().to_glib_none().0, name.to_glib_none().0);
509 }
510 }
511
512 #[doc(alias = "shumate_map_source_set_projection")]
516 #[doc(alias = "projection")]
517 fn set_projection(&self, projection: MapProjection) {
518 unsafe {
519 ffi::shumate_map_source_set_projection(
520 self.as_ref().to_glib_none().0,
521 projection.into_glib(),
522 );
523 }
524 }
525
526 #[doc(alias = "shumate_map_source_set_tile_size")]
530 #[doc(alias = "tile-size")]
531 fn set_tile_size(&self, tile_size: u32) {
532 unsafe {
533 ffi::shumate_map_source_set_tile_size(self.as_ref().to_glib_none().0, tile_size);
534 }
535 }
536
537 #[cfg(feature = "v1_7")]
542 #[cfg_attr(docsrs, doc(cfg(feature = "v1_7")))]
543 #[doc(alias = "modified")]
544 fn connect_modified<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
545 unsafe extern "C" fn modified_trampoline<P: IsA<MapSource>, F: Fn(&P) + 'static>(
546 this: *mut ffi::ShumateMapSource,
547 f: glib::ffi::gpointer,
548 ) {
549 unsafe {
550 let f: &F = &*(f as *const F);
551 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
552 }
553 }
554 unsafe {
555 let f: Box_<F> = Box_::new(f);
556 connect_raw(
557 self.as_ptr() as *mut _,
558 c"modified".as_ptr(),
559 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
560 modified_trampoline::<Self, F> as *const (),
561 )),
562 Box_::into_raw(f),
563 )
564 }
565 }
566
567 #[doc(alias = "id")]
568 fn connect_id_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
569 unsafe extern "C" fn notify_id_trampoline<P: IsA<MapSource>, F: Fn(&P) + 'static>(
570 this: *mut ffi::ShumateMapSource,
571 _param_spec: glib::ffi::gpointer,
572 f: glib::ffi::gpointer,
573 ) {
574 unsafe {
575 let f: &F = &*(f as *const F);
576 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
577 }
578 }
579 unsafe {
580 let f: Box_<F> = Box_::new(f);
581 connect_raw(
582 self.as_ptr() as *mut _,
583 c"notify::id".as_ptr(),
584 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
585 notify_id_trampoline::<Self, F> as *const (),
586 )),
587 Box_::into_raw(f),
588 )
589 }
590 }
591
592 #[doc(alias = "license")]
593 fn connect_license_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
594 unsafe extern "C" fn notify_license_trampoline<P: IsA<MapSource>, F: Fn(&P) + 'static>(
595 this: *mut ffi::ShumateMapSource,
596 _param_spec: glib::ffi::gpointer,
597 f: glib::ffi::gpointer,
598 ) {
599 unsafe {
600 let f: &F = &*(f as *const F);
601 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
602 }
603 }
604 unsafe {
605 let f: Box_<F> = Box_::new(f);
606 connect_raw(
607 self.as_ptr() as *mut _,
608 c"notify::license".as_ptr(),
609 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
610 notify_license_trampoline::<Self, F> as *const (),
611 )),
612 Box_::into_raw(f),
613 )
614 }
615 }
616
617 #[doc(alias = "license-uri")]
618 fn connect_license_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
619 unsafe extern "C" fn notify_license_uri_trampoline<
620 P: IsA<MapSource>,
621 F: Fn(&P) + 'static,
622 >(
623 this: *mut ffi::ShumateMapSource,
624 _param_spec: glib::ffi::gpointer,
625 f: glib::ffi::gpointer,
626 ) {
627 unsafe {
628 let f: &F = &*(f as *const F);
629 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
630 }
631 }
632 unsafe {
633 let f: Box_<F> = Box_::new(f);
634 connect_raw(
635 self.as_ptr() as *mut _,
636 c"notify::license-uri".as_ptr(),
637 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
638 notify_license_uri_trampoline::<Self, F> as *const (),
639 )),
640 Box_::into_raw(f),
641 )
642 }
643 }
644
645 #[doc(alias = "max-zoom-level")]
646 fn connect_max_zoom_level_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
647 unsafe extern "C" fn notify_max_zoom_level_trampoline<
648 P: IsA<MapSource>,
649 F: Fn(&P) + 'static,
650 >(
651 this: *mut ffi::ShumateMapSource,
652 _param_spec: glib::ffi::gpointer,
653 f: glib::ffi::gpointer,
654 ) {
655 unsafe {
656 let f: &F = &*(f as *const F);
657 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
658 }
659 }
660 unsafe {
661 let f: Box_<F> = Box_::new(f);
662 connect_raw(
663 self.as_ptr() as *mut _,
664 c"notify::max-zoom-level".as_ptr(),
665 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
666 notify_max_zoom_level_trampoline::<Self, F> as *const (),
667 )),
668 Box_::into_raw(f),
669 )
670 }
671 }
672
673 #[doc(alias = "min-zoom-level")]
674 fn connect_min_zoom_level_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
675 unsafe extern "C" fn notify_min_zoom_level_trampoline<
676 P: IsA<MapSource>,
677 F: Fn(&P) + 'static,
678 >(
679 this: *mut ffi::ShumateMapSource,
680 _param_spec: glib::ffi::gpointer,
681 f: glib::ffi::gpointer,
682 ) {
683 unsafe {
684 let f: &F = &*(f as *const F);
685 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
686 }
687 }
688 unsafe {
689 let f: Box_<F> = Box_::new(f);
690 connect_raw(
691 self.as_ptr() as *mut _,
692 c"notify::min-zoom-level".as_ptr(),
693 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
694 notify_min_zoom_level_trampoline::<Self, F> as *const (),
695 )),
696 Box_::into_raw(f),
697 )
698 }
699 }
700
701 #[doc(alias = "name")]
702 fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
703 unsafe extern "C" fn notify_name_trampoline<P: IsA<MapSource>, F: Fn(&P) + 'static>(
704 this: *mut ffi::ShumateMapSource,
705 _param_spec: glib::ffi::gpointer,
706 f: glib::ffi::gpointer,
707 ) {
708 unsafe {
709 let f: &F = &*(f as *const F);
710 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
711 }
712 }
713 unsafe {
714 let f: Box_<F> = Box_::new(f);
715 connect_raw(
716 self.as_ptr() as *mut _,
717 c"notify::name".as_ptr(),
718 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
719 notify_name_trampoline::<Self, F> as *const (),
720 )),
721 Box_::into_raw(f),
722 )
723 }
724 }
725
726 #[doc(alias = "projection")]
727 fn connect_projection_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
728 unsafe extern "C" fn notify_projection_trampoline<
729 P: IsA<MapSource>,
730 F: Fn(&P) + 'static,
731 >(
732 this: *mut ffi::ShumateMapSource,
733 _param_spec: glib::ffi::gpointer,
734 f: glib::ffi::gpointer,
735 ) {
736 unsafe {
737 let f: &F = &*(f as *const F);
738 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
739 }
740 }
741 unsafe {
742 let f: Box_<F> = Box_::new(f);
743 connect_raw(
744 self.as_ptr() as *mut _,
745 c"notify::projection".as_ptr(),
746 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
747 notify_projection_trampoline::<Self, F> as *const (),
748 )),
749 Box_::into_raw(f),
750 )
751 }
752 }
753
754 #[doc(alias = "tile-size")]
755 fn connect_tile_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
756 unsafe extern "C" fn notify_tile_size_trampoline<P: IsA<MapSource>, F: Fn(&P) + 'static>(
757 this: *mut ffi::ShumateMapSource,
758 _param_spec: glib::ffi::gpointer,
759 f: glib::ffi::gpointer,
760 ) {
761 unsafe {
762 let f: &F = &*(f as *const F);
763 f(MapSource::from_glib_borrow(this).unsafe_cast_ref())
764 }
765 }
766 unsafe {
767 let f: Box_<F> = Box_::new(f);
768 connect_raw(
769 self.as_ptr() as *mut _,
770 c"notify::tile-size".as_ptr(),
771 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
772 notify_tile_size_trampoline::<Self, F> as *const (),
773 )),
774 Box_::into_raw(f),
775 )
776 }
777 }
778}
779
780impl<O: IsA<MapSource>> MapSourceExt for O {}