@@ -8,6 +8,8 @@ import androidx.activity.ComponentActivity
88import androidx.activity.compose.setContent
99import androidx.activity.enableEdgeToEdge
1010import androidx.compose.foundation.background
11+ import androidx.compose.foundation.clickable
12+ import androidx.compose.foundation.layout.Box
1113import androidx.compose.foundation.layout.Column
1214import androidx.compose.foundation.layout.fillMaxSize
1315import androidx.compose.foundation.layout.fillMaxWidth
@@ -20,8 +22,10 @@ import androidx.compose.material3.Text
2022import androidx.compose.runtime.Composable
2123import androidx.compose.runtime.DisposableEffect
2224import androidx.compose.runtime.LaunchedEffect
23- import androidx.compose.runtime.key
25+ import androidx.compose.runtime.getValue
26+ import androidx.compose.runtime.mutableStateOf
2427import androidx.compose.runtime.remember
28+ import androidx.compose.runtime.setValue
2529import androidx.compose.ui.Alignment
2630import androidx.compose.ui.Modifier
2731import androidx.compose.ui.graphics.Color
@@ -46,75 +50,63 @@ class ComposeWebActivity : ComponentActivity() {
4650 val context = LocalContext .current
4751 val webView = remember(context) { WebView (context) }
4852 val customWebView = remember(context) { CustomWebView (context) }
49- val geckoView = remember(context) { GeckoView (context) }
50- val customGeckoView = remember(context) { CustomGeckoView (context) }
51-
5253 Column (
5354 modifier = Modifier
5455 .fillMaxSize()
5556 .padding(innerPadding)
56- .verticalScroll(rememberScrollState())
5757 ) {
58- Text (
59- text = " android.webkit.WebView" ,
60- fontSize = 16 .sp,
61- modifier = Modifier
62- .background(Color .Yellow )
63- .align(Alignment .CenterHorizontally )
64- )
65- WebViewItem (
66- url = " https://www.google.com" ,
67- webView = webView,
58+ Column (
6859 modifier = Modifier
69- .fillMaxWidth()
70- .height(450 .dp)
71- )
60+ .weight(1f )
61+ .verticalScroll(rememberScrollState())
62+ ) {
63+ Text (
64+ text = " android.webkit.WebView" ,
65+ fontSize = 16 .sp,
66+ modifier = Modifier
67+ .background(Color .Yellow )
68+ .align(Alignment .CenterHorizontally )
69+ )
70+ WebViewItem (
71+ url = " https://www.google.com" ,
72+ webView = webView,
73+ modifier = Modifier
74+ .fillMaxWidth()
75+ .height(450 .dp)
76+ )
7277
73- Text (
74- text = " CustomWebView" ,
75- fontSize = 16 .sp,
76- modifier = Modifier
77- .background(Color .Yellow )
78- .align(Alignment .CenterHorizontally )
79- )
80- WebViewItem (
81- url = " https://www.google.com" ,
82- webView = customWebView,
83- modifier = Modifier
84- .fillMaxWidth()
85- .height(450 .dp)
86- )
78+ Text (
79+ text = " CustomWebView" ,
80+ fontSize = 16 .sp,
81+ modifier = Modifier
82+ .background(Color .Yellow )
83+ .align(Alignment .CenterHorizontally )
84+ )
85+ WebViewItem (
86+ url = " https://www.google.com" ,
87+ webView = customWebView,
88+ modifier = Modifier
89+ .fillMaxWidth()
90+ .height(450 .dp)
91+ )
92+ }
8793
88- Text (
89- text = " org.mozilla.geckoview.GeckoView" ,
90- fontSize = 16 .sp,
91- modifier = Modifier
92- .align(Alignment .CenterHorizontally )
93- .background(Color .Yellow )
94- .padding(top = 8 .dp)
95- )
96- GeckoViewItem (
94+ LazyGeckoViewItem (
95+ label = " org.mozilla.geckoview.GeckoView (device)" ,
9796 url = " https://www.google.com" ,
98- geckoView = geckoView ,
97+ geckoViewFactory = { GeckoView (it) } ,
9998 modifier = Modifier
10099 .fillMaxWidth()
101- .height(450 .dp)
100+ .height(200 .dp)
102101 )
103102
104- Text (
105- text = " CustomGeckoView" ,
106- fontSize = 16 .sp,
107- modifier = Modifier
108- .align(Alignment .CenterHorizontally )
109- .background(Color .Yellow )
110- .padding(top = 8 .dp)
111- )
112- GeckoViewItem (
103+ LazyGeckoViewItem (
104+ label = " CustomGeckoView (device)" ,
113105 url = " https://www.google.com" ,
114- geckoView = customGeckoView ,
106+ geckoViewFactory = { CustomGeckoView (it) } ,
115107 modifier = Modifier
116108 .fillMaxWidth()
117- .height(450 .dp)
109+ .height(200 .dp)
118110 )
119111 }
120112 }
@@ -152,36 +144,52 @@ fun WebViewItem(url: String, webView: WebView, modifier: Modifier = Modifier) {
152144}
153145
154146@Composable
155- fun GeckoViewItem (url : String , geckoView : GeckoView , modifier : Modifier = Modifier ) {
156- val context = LocalContext .current
157- val runtime = remember {
158- GeckoRuntime .getDefault(context.applicationContext)
159- }
160- val session = remember(runtime) {
161- GeckoSession ().apply {
162- setContentDelegate(object : ContentDelegate {})
163- open(runtime)
147+ fun LazyGeckoViewItem (
148+ label : String ,
149+ url : String ,
150+ geckoViewFactory : (android.content.Context ) -> GeckoView ,
151+ modifier : Modifier = Modifier
152+ ) {
153+ var loaded by remember { mutableStateOf(false ) }
154+
155+ Text (
156+ text = if (loaded) label else " Tap to load $label " ,
157+ fontSize = 16 .sp,
158+ modifier = Modifier
159+ .background(Color .Yellow )
160+ .padding(top = 8 .dp)
161+ .then(if (! loaded) Modifier .clickable { loaded = true } else Modifier )
162+ )
163+
164+ if (loaded) {
165+ val context = LocalContext .current
166+ val geckoView = remember(context) { geckoViewFactory(context) }
167+ val runtime = remember { GeckoRuntime .getDefault(context.applicationContext) }
168+ val session = remember(runtime) {
169+ GeckoSession ().apply {
170+ setContentDelegate(object : ContentDelegate {})
171+ open(runtime)
172+ }
164173 }
165- }
166174
167- DisposableEffect (session) {
168- onDispose {
169- session.close()
175+ DisposableEffect (session) {
176+ onDispose { session.close() }
170177 }
171- }
172178
173- key(geckoView) {
174179 AndroidView (
175180 modifier = modifier,
176- factory = { _ ->
177- geckoView.apply {
178- setSession(session)
179- }
180- }
181+ factory = { _ -> geckoView.apply { setSession(session) } }
181182 )
182- }
183183
184- LaunchedEffect (url) {
185- session.loadUri(url)
184+ LaunchedEffect (url) { session.loadUri(url) }
185+ } else {
186+ Box (
187+ modifier = modifier
188+ .background(Color .LightGray )
189+ .clickable { loaded = true },
190+ contentAlignment = Alignment .Center
191+ ) {
192+ Text (" Tap to load" , color = Color .DarkGray )
193+ }
186194 }
187195}
0 commit comments