Offlin33er commited on
Commit
dcf5d65
·
verified ·
1 Parent(s): 4d05ad5

Match GV 147 API: 4-arg onLocationChange, onCanGoBack tracking

Browse files
app/src/main/java/dev/nova/browser/TabManager.kt CHANGED
@@ -1,6 +1,7 @@
1
  package dev.nova.browser
2
 
3
  import androidx.compose.runtime.mutableStateListOf
 
4
  import androidx.compose.runtime.mutableStateOf
5
  import org.mozilla.geckoview.GeckoResult
6
  import org.mozilla.geckoview.GeckoRuntime
@@ -10,6 +11,10 @@ import org.mozilla.geckoview.WebExtension
10
  /**
11
  * Nova's tab model. One GeckoSession per tab; the UI activates a tab by
12
  * calling GeckoView.setSession() on the active tab's session.
 
 
 
 
13
  */
14
  class TabManager(private val runtime: GeckoRuntime) {
15
 
@@ -19,6 +24,10 @@ class TabManager(private val runtime: GeckoRuntime) {
19
  val tabs = mutableStateListOf<Tab>()
20
  val activeTabId = mutableStateOf<Int?>(null)
21
 
 
 
 
 
22
  /** Installed built-in extension, wired in once available. */
23
  var devExtension: WebExtension? = null
24
 
@@ -40,18 +49,29 @@ class TabManager(private val runtime: GeckoRuntime) {
40
  }
41
 
42
  fun open(url: String): Tab {
 
43
  val session = GeckoSession()
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  session.progressDelegate = object : GeckoSession.ProgressDelegate {
46
  override fun onProgressChange(session: GeckoSession, progress: Int) {
47
  // Hook for the loading bar (M2 milestone).
48
  }
49
  }
50
- session.navigationDelegate = object : GeckoSession.NavigationDelegate {
51
- override fun onLocationChange(session: GeckoSession, url: String?) {
52
- // Hook for syncing the URL bar (M2 milestone).
53
- }
54
- }
55
 
56
  session.open(runtime)
57
 
@@ -60,7 +80,7 @@ class TabManager(private val runtime: GeckoRuntime) {
60
  session.webExtensionController.setMessageDelegate(ext, sessionMessageDelegate, "browser")
61
  }
62
 
63
- val tab = Tab(nextId++, session)
64
  tabs.add(tab)
65
  activeTabId.value = tab.id
66
  session.loadUri(url)
@@ -76,6 +96,8 @@ class TabManager(private val runtime: GeckoRuntime) {
76
  if (index == -1) return
77
  tabs[index].session.close()
78
  tabs.removeAt(index)
 
 
79
  if (activeTabId.value == id) {
80
  activeTabId.value = tabs.lastOrNull()?.id
81
  }
@@ -84,6 +106,10 @@ class TabManager(private val runtime: GeckoRuntime) {
84
  fun activeSession(): GeckoSession? =
85
  activeTabId.value?.let { id -> tabs.firstOrNull { it.id == id }?.session }
86
 
 
 
 
 
87
  /** Called once the built-in extension finishes installing. */
88
  fun wireExtension(ext: WebExtension) {
89
  devExtension = ext
 
1
  package dev.nova.browser
2
 
3
  import androidx.compose.runtime.mutableStateListOf
4
+ import androidx.compose.runtime.mutableStateMapOf
5
  import androidx.compose.runtime.mutableStateOf
6
  import org.mozilla.geckoview.GeckoResult
7
  import org.mozilla.geckoview.GeckoRuntime
 
11
  /**
12
  * Nova's tab model. One GeckoSession per tab; the UI activates a tab by
13
  * calling GeckoView.setSession() on the active tab's session.
14
+ *
15
+ * GeckoView does not expose canGoBack()/canGoForward() as polling methods —
16
+ * navigation state arrives via NavigationDelegate callbacks, so we track it
17
+ * here per tab.
18
  */
19
  class TabManager(private val runtime: GeckoRuntime) {
20
 
 
24
  val tabs = mutableStateListOf<Tab>()
25
  val activeTabId = mutableStateOf<Int?>(null)
26
 
27
+ /** Navigation state per tab id, fed by NavigationDelegate callbacks. */
28
+ val canGoBackStates = mutableStateMapOf<Int, Boolean>()
29
+ val lastUrls = mutableStateMapOf<Int, String>()
30
+
31
  /** Installed built-in extension, wired in once available. */
32
  var devExtension: WebExtension? = null
33
 
 
49
  }
50
 
51
  fun open(url: String): Tab {
52
+ val tabId = nextId++
53
  val session = GeckoSession()
54
 
55
+ session.navigationDelegate = object : GeckoSession.NavigationDelegate {
56
+ // GV 147 signature: (session, url, perms, hasUserGesture)
57
+ override fun onLocationChange(
58
+ session: GeckoSession,
59
+ url: String?,
60
+ perms: List<GeckoSession.PermissionDelegate.ContentPermission>,
61
+ hasUserGesture: Boolean,
62
+ ) {
63
+ if (url != null) lastUrls[tabId] = url
64
+ }
65
+
66
+ override fun onCanGoBack(session: GeckoSession, canGoBack: Boolean) {
67
+ canGoBackStates[tabId] = canGoBack
68
+ }
69
+ }
70
  session.progressDelegate = object : GeckoSession.ProgressDelegate {
71
  override fun onProgressChange(session: GeckoSession, progress: Int) {
72
  // Hook for the loading bar (M2 milestone).
73
  }
74
  }
 
 
 
 
 
75
 
76
  session.open(runtime)
77
 
 
80
  session.webExtensionController.setMessageDelegate(ext, sessionMessageDelegate, "browser")
81
  }
82
 
83
+ val tab = Tab(tabId, session)
84
  tabs.add(tab)
85
  activeTabId.value = tab.id
86
  session.loadUri(url)
 
96
  if (index == -1) return
97
  tabs[index].session.close()
98
  tabs.removeAt(index)
99
+ canGoBackStates.remove(id)
100
+ lastUrls.remove(id)
101
  if (activeTabId.value == id) {
102
  activeTabId.value = tabs.lastOrNull()?.id
103
  }
 
106
  fun activeSession(): GeckoSession? =
107
  activeTabId.value?.let { id -> tabs.firstOrNull { it.id == id }?.session }
108
 
109
+ /** Whether the active tab can navigate back (delegate-tracked). */
110
+ fun canGoBack(): Boolean =
111
+ activeTabId.value?.let { canGoBackStates[it] == true } ?: false
112
+
113
  /** Called once the built-in extension finishes installing. */
114
  fun wireExtension(ext: WebExtension) {
115
  devExtension = ext