84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
from fastcore.basics import NotStr
|
|
|
|
from myfasthtml.core.constants import ColumnType
|
|
from myfasthtml.icons.fluent import question20_regular, brain_circuit20_regular, number_symbol20_regular, \
|
|
number_row20_regular
|
|
from myfasthtml.icons.fluent_p1 import checkbox_checked20_regular, checkbox_unchecked20_regular, \
|
|
checkbox_checked20_filled, math_formula16_regular, folder20_regular, document20_regular
|
|
from myfasthtml.icons.fluent_p2 import text_field20_regular, text_bullet_list_square20_regular
|
|
from myfasthtml.icons.fluent_p3 import calendar_ltr20_regular
|
|
|
|
default_icons = {
|
|
# default type icons
|
|
None: question20_regular,
|
|
True: checkbox_checked20_regular,
|
|
False: checkbox_unchecked20_regular,
|
|
|
|
"Brain": brain_circuit20_regular,
|
|
"QuestionMark": question20_regular,
|
|
|
|
# TreeView icons
|
|
"TreeViewFolder": folder20_regular,
|
|
"TreeViewFile": document20_regular,
|
|
|
|
# Datagrid column icons
|
|
ColumnType.RowIndex: number_symbol20_regular,
|
|
ColumnType.Text: text_field20_regular,
|
|
ColumnType.Number: number_row20_regular,
|
|
ColumnType.Datetime: calendar_ltr20_regular,
|
|
ColumnType.Bool: checkbox_checked20_filled,
|
|
ColumnType.Enum: text_bullet_list_square20_regular,
|
|
ColumnType.Formula: math_formula16_regular,
|
|
}
|
|
|
|
|
|
class IconsHelper:
|
|
_icons = default_icons.copy()
|
|
|
|
@staticmethod
|
|
def get(name, package=None):
|
|
"""
|
|
Fetches and returns an icon resource based on the provided name and optional package. If the icon is not already
|
|
cached, it will attempt to dynamically load the icon from the available modules under the `myfasthtml.icons` package.
|
|
This method uses an internal caching mechanism to store previously fetched icons for future quick lookups.
|
|
|
|
:param name: The name of the requested icon.
|
|
:param package: The optional sub-package to limit the search for the requested icon. If not provided, the method will
|
|
iterate through all available modules within the `myfasthtml.icons` package.
|
|
:return: The requested icon resource if found; otherwise, returns None.
|
|
:rtype: object or None
|
|
"""
|
|
if isinstance(name, NotStr):
|
|
return name
|
|
|
|
if name in IconsHelper._icons:
|
|
return IconsHelper._icons[name]
|
|
|
|
import importlib
|
|
import pkgutil
|
|
import myfasthtml.icons as icons_pkg
|
|
|
|
_UTILITY_MODULES = {'manage_icons', 'update_icons'}
|
|
|
|
if package:
|
|
module = importlib.import_module(f"myfasthtml.icons.{package}")
|
|
icon = getattr(module, name, None)
|
|
if icon is not None:
|
|
IconsHelper._icons[name] = icon
|
|
return icon
|
|
|
|
for _, modname, _ in pkgutil.iter_modules(icons_pkg.__path__):
|
|
if modname in _UTILITY_MODULES:
|
|
continue
|
|
module = importlib.import_module(f"myfasthtml.icons.{modname}")
|
|
icon = getattr(module, name, None)
|
|
if icon is not None:
|
|
IconsHelper._icons[name] = icon
|
|
return icon
|
|
|
|
return None
|
|
|
|
@staticmethod
|
|
def reset():
|
|
IconsHelper._icons = default_icons.copy()
|