@maplibre/maplibre-react-native 11.0.0-beta.20 → 11.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,9 @@ import android.view.MotionEvent
13
13
  import android.view.View
14
14
  import android.view.ViewGroup
15
15
  import android.widget.FrameLayout
16
+ import androidx.core.graphics.Insets
17
+ import androidx.core.view.ViewCompat
18
+ import androidx.core.view.WindowInsetsCompat
16
19
  import com.facebook.react.bridge.Arguments
17
20
  import com.facebook.react.bridge.LifecycleEventListener
18
21
  import com.facebook.react.bridge.ReactContext
@@ -160,6 +163,8 @@ open class MLRNMapView(
160
163
  private var scaleBarMarginLeft: Float? = null
161
164
  private var scaleBarPlugin: ScaleBarPlugin? = null
162
165
 
166
+ private var windowInsets: WindowInsetsCompat? = null
167
+
163
168
  private var symbolManager: SymbolManager? = null
164
169
 
165
170
  private var markerViewManager: MarkerViewManager? = null
@@ -198,6 +203,22 @@ open class MLRNMapView(
198
203
  destroyed = true
199
204
  }
200
205
 
206
+ override fun onAttachedToWindow() {
207
+ super.onAttachedToWindow()
208
+ ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
209
+ windowInsets = insets
210
+ updateUISettings()
211
+ updateScaleBar()
212
+ insets
213
+ }
214
+ ViewCompat.requestApplyInsets(this)
215
+ }
216
+
217
+ override fun onDetachedFromWindow() {
218
+ super.onDetachedFromWindow()
219
+ ViewCompat.setOnApplyWindowInsetsListener(this, null)
220
+ }
221
+
201
222
  fun addFeature(
202
223
  childView: View?,
203
224
  childPosition: Int,
@@ -393,6 +414,37 @@ open class MLRNMapView(
393
414
  override fun onMapReady(mapLibreMap: MapLibreMap) {
394
415
  this.mapLibreMap = mapLibreMap
395
416
 
417
+ val uiSettings = mapLibreMap.uiSettings
418
+ attributionGravity = Gravity.END or Gravity.BOTTOM
419
+ attributionMargin =
420
+ intArrayOf(
421
+ 0,
422
+ 0,
423
+ (4 * displayDensity).roundToInt(),
424
+ (4 * displayDensity).roundToInt(),
425
+ )
426
+
427
+ if (logoGravity == null) logoGravity = uiSettings.logoGravity
428
+ if (logoMargins == null) {
429
+ logoMargins =
430
+ intArrayOf(
431
+ uiSettings.logoMarginLeft,
432
+ uiSettings.logoMarginTop,
433
+ uiSettings.logoMarginRight,
434
+ uiSettings.logoMarginBottom,
435
+ )
436
+ }
437
+ if (compassGravity == null) compassGravity = uiSettings.compassGravity
438
+ if (compassMargins == null) {
439
+ compassMargins =
440
+ intArrayOf(
441
+ uiSettings.compassMarginLeft,
442
+ uiSettings.compassMarginTop,
443
+ uiSettings.compassMarginRight,
444
+ uiSettings.compassMarginBottom,
445
+ )
446
+ }
447
+
396
448
  mapStyle?.let { style ->
397
449
  mapLibreMap.setStyle(
398
450
  if (ConvertUtils.isJSONValid(style)) {
@@ -535,6 +587,13 @@ open class MLRNMapView(
535
587
  markerViewManager!!.restoreViews()
536
588
  }
537
589
  }
590
+
591
+ if (changed) {
592
+ handler.post {
593
+ updateUISettings()
594
+ updateScaleBar()
595
+ }
596
+ }
538
597
  }
539
598
 
540
599
  override fun onMapClick(latLng: LatLng): Boolean {
@@ -889,9 +948,10 @@ open class MLRNMapView(
889
948
  scaleBarPlugin = ScaleBarPlugin(this, map)
890
949
  }
891
950
 
951
+ val viewInsets = getSystemInsetsForView()
892
952
  val options = ScaleBarOptions(context)
893
- scaleBarMarginTop?.let { options.setMarginTop(it) }
894
- scaleBarMarginLeft?.let { options.setMarginLeft(it) }
953
+ options.setMarginTop((scaleBarMarginTop ?: (8 * displayDensity)) + viewInsets.top)
954
+ options.setMarginLeft((scaleBarMarginLeft ?: (10 * displayDensity)) + viewInsets.left)
895
955
  scaleBarPlugin!!.create(options)
896
956
  scaleBarPlugin!!.isEnabled = scaleBarEnabled ?: false
897
957
 
@@ -1066,6 +1126,48 @@ open class MLRNMapView(
1066
1126
  mapLibreMap!!.getStyle(onStyleLoaded)
1067
1127
  }
1068
1128
 
1129
+ private fun getSystemInsetsForView(): Insets {
1130
+ val sysInsets =
1131
+ windowInsets?.getInsets(
1132
+ WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout(),
1133
+ )
1134
+ ?: return Insets.NONE
1135
+ if (!isAttachedToWindow || width == 0 || height == 0) return Insets.NONE
1136
+
1137
+ val location = IntArray(2)
1138
+ getLocationOnScreen(location)
1139
+ val screenWidth = rootView.width
1140
+ val screenHeight = rootView.height
1141
+
1142
+ return Insets.of(
1143
+ if (location[0] <= 0) sysInsets.left else 0,
1144
+ if (location[1] <= 0) sysInsets.top else 0,
1145
+ if (location[0] + width >= screenWidth) sysInsets.right else 0,
1146
+ if (location[1] + height >= screenHeight) sysInsets.bottom else 0,
1147
+ )
1148
+ }
1149
+
1150
+ private fun marginsWithInsets(
1151
+ margins: IntArray,
1152
+ gravity: Int,
1153
+ ): IntArray {
1154
+ val viewInsets = getSystemInsetsForView()
1155
+ if (viewInsets == Insets.NONE) return margins
1156
+
1157
+ val absGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection)
1158
+ val isLeft = (absGravity and Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT
1159
+ val isRight = (absGravity and Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.RIGHT
1160
+ val isTop = (absGravity and Gravity.VERTICAL_GRAVITY_MASK) == Gravity.TOP
1161
+ val isBottom = (absGravity and Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM
1162
+
1163
+ return intArrayOf(
1164
+ margins[0] + if (isLeft) viewInsets.left else 0,
1165
+ margins[1] + if (isTop) viewInsets.top else 0,
1166
+ margins[2] + if (isRight) viewInsets.right else 0,
1167
+ margins[3] + if (isBottom) viewInsets.bottom else 0,
1168
+ )
1169
+ }
1170
+
1069
1171
  private fun updateUISettings() {
1070
1172
  if (mapLibreMap == null) {
1071
1173
  return
@@ -1113,19 +1215,13 @@ open class MLRNMapView(
1113
1215
  uiSettings.attributionGravity = attributionGravity!!
1114
1216
  }
1115
1217
 
1116
- if (attributionMargin != null &&
1117
- (
1118
- uiSettings.attributionMarginLeft != attributionMargin!![0] || uiSettings.attributionMarginTop != attributionMargin!![1] ||
1119
- uiSettings.attributionMarginRight != attributionMargin!![2] ||
1120
- uiSettings.attributionMarginBottom != attributionMargin!![3]
1121
- )
1122
- ) {
1123
- uiSettings.setAttributionMargins(
1124
- attributionMargin!![0],
1125
- attributionMargin!![1],
1126
- attributionMargin!![2],
1127
- attributionMargin!![3],
1128
- )
1218
+ if (attributionMargin != null) {
1219
+ val adjusted = marginsWithInsets(attributionMargin!!, attributionGravity ?: uiSettings.attributionGravity)
1220
+ if (uiSettings.attributionMarginLeft != adjusted[0] || uiSettings.attributionMarginTop != adjusted[1] ||
1221
+ uiSettings.attributionMarginRight != adjusted[2] || uiSettings.attributionMarginBottom != adjusted[3]
1222
+ ) {
1223
+ uiSettings.setAttributionMargins(adjusted[0], adjusted[1], adjusted[2], adjusted[3])
1224
+ }
1129
1225
  }
1130
1226
 
1131
1227
  if (logoEnabled != null && uiSettings.isLogoEnabled != logoEnabled) {
@@ -1136,19 +1232,13 @@ open class MLRNMapView(
1136
1232
  uiSettings.logoGravity = logoGravity!!
1137
1233
  }
1138
1234
 
1139
- if (logoMargins != null &&
1140
- (
1141
- uiSettings.logoMarginLeft != logoMargins!![0] || uiSettings.logoMarginTop != logoMargins!![1] ||
1142
- uiSettings.logoMarginRight != logoMargins!![2] ||
1143
- uiSettings.logoMarginBottom != logoMargins!![3]
1144
- )
1145
- ) {
1146
- uiSettings.setLogoMargins(
1147
- logoMargins!![0],
1148
- logoMargins!![1],
1149
- logoMargins!![2],
1150
- logoMargins!![3],
1151
- )
1235
+ if (logoMargins != null) {
1236
+ val adjusted = marginsWithInsets(logoMargins!!, logoGravity ?: uiSettings.logoGravity)
1237
+ if (uiSettings.logoMarginLeft != adjusted[0] || uiSettings.logoMarginTop != adjusted[1] ||
1238
+ uiSettings.logoMarginRight != adjusted[2] || uiSettings.logoMarginBottom != adjusted[3]
1239
+ ) {
1240
+ uiSettings.setLogoMargins(adjusted[0], adjusted[1], adjusted[2], adjusted[3])
1241
+ }
1152
1242
  }
1153
1243
 
1154
1244
  if (compassEnabled != null && uiSettings.isCompassEnabled != compassEnabled) {
@@ -1159,19 +1249,13 @@ open class MLRNMapView(
1159
1249
  uiSettings.compassGravity = compassGravity!!
1160
1250
  }
1161
1251
 
1162
- if (compassMargins != null &&
1163
- (
1164
- uiSettings.compassMarginLeft != compassMargins!![0] || uiSettings.compassMarginTop != compassMargins!![1] ||
1165
- uiSettings.compassMarginRight != compassMargins!![2] ||
1166
- uiSettings.compassMarginBottom != compassMargins!![3]
1167
- )
1168
- ) {
1169
- uiSettings.setCompassMargins(
1170
- compassMargins!![0],
1171
- compassMargins!![1],
1172
- compassMargins!![2],
1173
- compassMargins!![3],
1174
- )
1252
+ if (compassMargins != null) {
1253
+ val adjusted = marginsWithInsets(compassMargins!!, compassGravity ?: uiSettings.compassGravity)
1254
+ if (uiSettings.compassMarginLeft != adjusted[0] || uiSettings.compassMarginTop != adjusted[1] ||
1255
+ uiSettings.compassMarginRight != adjusted[2] || uiSettings.compassMarginBottom != adjusted[3]
1256
+ ) {
1257
+ uiSettings.setCompassMargins(adjusted[0], adjusted[1], adjusted[2], adjusted[3])
1258
+ }
1175
1259
  }
1176
1260
  if (compassHiddenFacingNorth != null) {
1177
1261
  uiSettings.setCompassFadeFacingNorth(compassHiddenFacingNorth!!)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maplibre/maplibre-react-native",
3
3
  "description": "React Native library for creating maps with MapLibre Native for Android & iOS",
4
- "version": "11.0.0-beta.20",
4
+ "version": "11.0.0-beta.21",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "provenance": true