5
5
import matplotlib
6
6
import matplotlib .style as mplstyle
7
7
import napari
8
+ from napari .utils .theme import get_theme
8
9
from matplotlib .backends .backend_qtagg import ( # type: ignore[attr-defined]
9
10
FigureCanvasQTAgg ,
10
11
NavigationToolbar2QT ,
13
14
from qtpy .QtGui import QIcon
14
15
from qtpy .QtWidgets import QLabel , QVBoxLayout , QWidget
15
16
16
- from .util import Interval , from_napari_css_get_size_of
17
+ from .util import Interval , from_napari_css_get_size_of , style_sheet_from_theme
17
18
18
19
__all__ = ["BaseNapariMPLWidget" , "NapariMPLWidget" , "SingleAxesWidget" ]
19
20
@@ -45,10 +46,11 @@ def __init__(
45
46
):
46
47
super ().__init__ (parent = parent )
47
48
self .viewer = napari_viewer
48
- self ._mpl_style_sheet_path : Optional [Path ] = None
49
+ #self._mpl_style_sheet_path: Optional[Path] = None
50
+ self .napari_theme_style_sheet = style_sheet_from_theme (get_theme (napari_viewer .theme , as_dict = False ))
49
51
50
52
# Sets figure.* style
51
- with mplstyle .context (self .mpl_style_sheet_path ):
53
+ with mplstyle .context (self .napari_theme_style_sheet ):
52
54
self .canvas = FigureCanvasQTAgg () # type: ignore[no-untyped-call]
53
55
54
56
self .canvas .figure .set_layout_engine ("constrained" )
@@ -68,23 +70,23 @@ def figure(self) -> Figure:
68
70
"""Matplotlib figure."""
69
71
return self .canvas .figure
70
72
71
- @property
72
- def mpl_style_sheet_path (self ) -> Path :
73
- """
74
- Path to the set Matplotlib style sheet.
75
- """
76
- if self ._mpl_style_sheet_path is not None :
77
- return self ._mpl_style_sheet_path
78
- elif (_CUSTOM_STYLE_PATH ).exists ():
79
- return _CUSTOM_STYLE_PATH
80
- elif self ._napari_theme_has_light_bg ():
81
- return Path (__file__ ).parent / "styles" / "light.mplstyle"
82
- else :
83
- return Path (__file__ ).parent / "styles" / "dark.mplstyle"
84
-
85
- @mpl_style_sheet_path .setter
86
- def mpl_style_sheet_path (self , path : Path ) -> None :
87
- self ._mpl_style_sheet_path = Path (path )
73
+ # @property
74
+ # def mpl_style_sheet_path(self) -> Path:
75
+ # """
76
+ # Path to the set Matplotlib style sheet.
77
+ # """
78
+ # if self._mpl_style_sheet_path is not None:
79
+ # return self._mpl_style_sheet_path
80
+ # elif (_CUSTOM_STYLE_PATH).exists():
81
+ # return _CUSTOM_STYLE_PATH
82
+ # elif self._napari_theme_has_light_bg():
83
+ # return Path(__file__).parent / "styles" / "light.mplstyle"
84
+ # else:
85
+ # return Path(__file__).parent / "styles" / "dark.mplstyle"
86
+
87
+ # @mpl_style_sheet_path.setter
88
+ # def mpl_style_sheet_path(self, path: Path) -> None:
89
+ # self._mpl_style_sheet_path = Path(path)
88
90
89
91
def add_single_axes (self ) -> None :
90
92
"""
@@ -94,13 +96,14 @@ def add_single_axes(self) -> None:
94
96
"""
95
97
# Sets axes.* style.
96
98
# Does not set any text styling set by axes.* keys
97
- with mplstyle .context (self .mpl_style_sheet_path ):
99
+ with mplstyle .context (self .napari_theme_style_sheet ):
98
100
self .axes = self .figure .add_subplot ()
99
101
100
102
def _on_napari_theme_changed (self ) -> None :
101
103
"""
102
104
Called when the napari theme is changed.
103
105
"""
106
+ self .napari_theme_style_sheet = style_sheet_from_theme (get_theme (self .napari_viewer .theme , as_dict = False ))
104
107
self ._replace_toolbar_icons ()
105
108
106
109
def _napari_theme_has_light_bg (self ) -> bool :
@@ -252,7 +255,7 @@ def _draw(self) -> None:
252
255
"""
253
256
# Clearing axes sets new defaults, so need to make sure style is applied when
254
257
# this happens
255
- with mplstyle .context (self .mpl_style_sheet_path ):
258
+ with mplstyle .context (self .napari_theme_style_sheet ):
256
259
self .clear ()
257
260
if self .n_selected_layers in self .n_layers_input and all (
258
261
isinstance (layer , self .input_layer_types ) for layer in self .layers
@@ -300,7 +303,7 @@ def clear(self) -> None:
300
303
"""
301
304
Clear the axes.
302
305
"""
303
- with mplstyle .context (self .mpl_style_sheet_path ):
306
+ with mplstyle .context (self .napari_theme_style_sheet ):
304
307
self .axes .clear ()
305
308
306
309
0 commit comments