FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/navigation/ref.py at main · pythonnative/pythonnative · GitHub
pythonnative
/
pythonnative
Public
Notifications
You must be signed in to change notification settings
Fork
23
Star
131
Code
Issues
11
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
navigation
/
ref.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
104 lines (75 loc) · 3.34 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
navigation
/
ref.py
Copy path
File metadata and controls
104 lines (75 loc) · 3.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""``NavigationRef``: navigate from outside the component tree.
```python
nav_ref = pn.NavigationRef()
@pn.component
def App() -> pn.Node:
return pn.NavigationContainer(Root, ref=nav_ref)
def on_push_notification(payload: dict[str, str]) -> None:
if nav_ref.is_ready():
nav_ref.navigate(ThreadScreen(id=payload["thread"]))
```
The container binds the root navigator's handle to ``ref.current``
when it mounts and clears it on unmount. Every proxied method raises
``RuntimeError`` while no container is mounted, so call
``is_ready()`` first from code that may run before the UI is up.
"""
from
__future__
import
annotations
from
typing
import
Optional
from
..
element
import
Element
from
.
handle
import
Navigation
from
.
screen
import
ScreenTarget
from
.
state
import
NavigationState
__all__
=
[
"NavigationRef"
]
_NOT_MOUNTED
=
"Navigation container is not mounted"
class
NavigationRef
:
"""Handle to the root navigator, usable outside components.
Create one at module level and bind it with
``NavigationContainer(ref=...)``, so push-notification handlers,
deep-link code, and services can navigate without a component in
scope. The proxied methods mirror [`Navigation`][pythonnative.Navigation]
and act from the root navigator, which reaches every statically
nested screen.
Attributes:
current: The root navigator's
[`Navigation`][pythonnative.Navigation] handle, or ``None``
while no container is mounted.
"""
__slots__
=
(
"current"
,)
def
__init__
(
self
)
->
None
:
self
.
current
:
Optional
[
Navigation
]
=
None
def
is_ready
(
self
)
->
bool
:
"""Whether a ``NavigationContainer`` has bound this ref."""
return
self
.
current
is
not
None
def
_require
(
self
)
->
Navigation
:
if
self
.
current
is
None
:
raise
RuntimeError
(
_NOT_MOUNTED
)
return
self
.
current
def
navigate
(
self
,
target
:
Element
,
/
)
->
None
:
"""Go to ``target`` (see ``Navigation.navigate``)."""
self
.
_require
().
navigate
(
target
)
def
push
(
self
,
target
:
Element
,
/
)
->
None
:
"""Push ``target`` (see ``Navigation.push``)."""
self
.
_require
().
push
(
target
)
def
replace
(
self
,
target
:
Element
,
/
)
->
None
:
"""Replace the active screen with ``target`` (see ``Navigation.replace``)."""
self
.
_require
().
replace
(
target
)
def
pop
(
self
,
count
:
int
=
1
)
->
bool
:
"""Pop ``count`` screens; returns whether anything happened."""
return
self
.
_require
().
pop
(
count
)
def
go_back
(
self
)
->
bool
:
"""Pop one screen; returns whether anything happened."""
return
self
.
_require
().
go_back
()
def
pop_to
(
self
,
target
:
ScreenTarget
,
/
)
->
None
:
"""Pop back to ``target`` (see ``Navigation.pop_to``)."""
self
.
_require
().
pop_to
(
target
)
def
pop_to_top
(
self
)
->
None
:
"""Pop every screen above the first one."""
self
.
_require
().
pop_to_top
()
def
reset
(
self
,
*
targets
:
Element
,
index
:
Optional
[
int
]
=
None
)
->
None
:
"""Replace the root navigator's history (see ``Navigation.reset``)."""
self
.
_require
().
reset
(
*
targets
,
index
=
index
)
def
get_state
(
self
)
->
NavigationState
:
"""The root navigator's current state."""
return
self
.
_require
().
get_state
()
def
__repr__
(
self
)
->
str
:
return
f"<NavigationRef
{
'ready'
if
self
.
is_ready
()
else
'unbound'
}
>"
Back
|
FazBrowse Home
|
New Git URL