Debian Codegraph @

src/app_model/types/_icon.py

raw
1 from typing import Any, Optional, TypedDict, Union
2
3 from pydantic import Field, model_validator
4
5 from ._base import _BaseModel
6
7
8 class Icon(_BaseModel):
9 """Icons used to represent commands, or submenus.
10
11 May provide both a light and dark variant. If only one is provided, it is used
12 in all theme types.
13 """
14
15 dark: Optional[str] = Field(
16 default=None,
17 description="Icon path when a dark theme is used. These may be "
18 "[iconify keys](https://icon-sets.iconify.design), such as "
19 "`fa6-solid:arrow-down`, or "
20 "[superqt.fonticon](https://pyapp-kit.github.io/superqt/utilities/fonticon/)"
21 " keys, such as `fa6s.arrow_down`",
22 )
23 light: Optional[str] = Field(
24 default=None,
25 description="Icon path when a light theme is used. These may be "
26 "[iconify keys](https://icon-sets.iconify.design), such as "
27 "`fa6-solid:arrow-down`, or "
28 "[superqt.fonticon](https://pyapp-kit.github.io/superqt/utilities/fonticon/)"
29 " keys, such as `fa6s.arrow_down`",
30 )
31
32 @classmethod
33 def _validate(cls, v: Any) -> "Icon":
34 """Validate icon."""
35 # if a single string is passed, use it for both light and dark.
36 if isinstance(v, Icon):
37 return v
38 if isinstance(v, str):
39 v = {"dark": v, "light": v}
40 return cls(**v)
41
42 # for v2
43 @model_validator(mode="before")
44 @classmethod
45 def _model_val(cls, v: dict) -> dict:
46 if isinstance(v, str):
47 v = {"dark": v, "light": v}
48 return v
49
50
51 class IconDict(TypedDict):
52 """Icon dictionary."""
53
54 dark: Optional[str]
55 light: Optional[str]
56
57
58 IconOrDict = Union[Icon, IconDict]