Coverage for fastblocks / adapters / icons / materialicons.py: 24%

190 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

1"""Material Icons adapter for FastBlocks with multiple themes.""" 

2 

3from contextlib import suppress 

4from typing import Any 

5from uuid import UUID 

6 

7from oneiric.core.resolution import Resolver 

8 

9# Oneiric resolver for dependency injection 

10depends = Resolver() 

11 

12from ._base import IconsBase, IconsBaseSettings 

13from ._utils import ( 

14 add_accessibility_attributes, 

15 build_attr_string, 

16 process_animations, 

17 process_semantic_colors, 

18 process_state_attributes, 

19 process_transformations, 

20) 

21 

22 

23class MaterialIconsSettings(IconsBaseSettings): 

24 """Settings for Material Icons adapter.""" 

25 

26 # Required ACB 0.19.0+ metadata 

27 MODULE_ID: UUID = UUID("01937d86-cf0b-e4bc-0d3e-2c3d4e5f6071") # Static UUID7 

28 MODULE_STATUS: str = "stable" 

29 

30 # Material Icons configuration 

31 version: str = "latest" 

32 base_url: str = "https://fonts.googleapis.com" 

33 default_theme: str = "filled" # filled, outlined, round, sharp, two-tone 

34 default_size: str = "24px" 

35 

36 # Available themes 

37 enabled_themes: list[str] = ["filled", "outlined", "round", "sharp", "two-tone"] 

38 

39 # Icon mapping for common names 

40 icon_aliases: dict[str, str] = { 

41 "home": "home", 

42 "user": "person", 

43 "settings": "settings", 

44 "search": "search", 

45 "menu": "menu", 

46 "close": "close", 

47 "check": "check", 

48 "error": "error", 

49 "info": "info", 

50 "success": "check_circle", 

51 "warning": "warning", 

52 "edit": "edit", 

53 "delete": "delete", 

54 "save": "save", 

55 "download": "download", 

56 "upload": "upload", 

57 "email": "email", 

58 "phone": "phone", 

59 "location": "location_on", 

60 "calendar": "event", 

61 "clock": "schedule", 

62 "heart": "favorite", 

63 "star": "star", 

64 "share": "share", 

65 "link": "link", 

66 "copy": "content_copy", 

67 "cut": "content_cut", 

68 "paste": "content_paste", 

69 "undo": "undo", 

70 "redo": "redo", 

71 "refresh": "refresh", 

72 "logout": "logout", 

73 "login": "login", 

74 "plus": "add", 

75 "minus": "remove", 

76 "eye": "visibility", 

77 "eye-off": "visibility_off", 

78 "lock": "lock", 

79 "unlock": "lock_open", 

80 "arrow-up": "keyboard_arrow_up", 

81 "arrow-down": "keyboard_arrow_down", 

82 "arrow-left": "keyboard_arrow_left", 

83 "arrow-right": "keyboard_arrow_right", 

84 } 

85 

86 # Size presets 

87 size_presets: dict[str, str] = { 

88 "xs": "16px", 

89 "sm": "20px", 

90 "md": "24px", 

91 "lg": "28px", 

92 "xl": "32px", 

93 "2xl": "40px", 

94 "3xl": "48px", 

95 "4xl": "56px", 

96 "5xl": "64px", 

97 } 

98 

99 # Color palette 

100 material_colors: dict[str, str] = { 

101 "red": "#f44336", 

102 "pink": "#e91e63", 

103 "purple": "#9c27b0", 

104 "deep-purple": "#673ab7", 

105 "indigo": "#3f51b5", 

106 "blue": "#2196f3", 

107 "light-blue": "#03a9f4", 

108 "cyan": "#00bcd4", 

109 "teal": "#009688", 

110 "green": "#4caf50", 

111 "light-green": "#8bc34a", 

112 "lime": "#cddc39", 

113 "yellow": "#ffeb3b", 

114 "amber": "#ffc107", 

115 "orange": "#ff9800", 

116 "deep-orange": "#ff5722", 

117 "brown": "#795548", 

118 "grey": "#9e9e9e", 

119 "blue-grey": "#607d8b", 

120 } 

121 

122 

123class MaterialIcons(IconsBase): 

124 """Material Icons adapter with multiple themes and comprehensive icon set.""" 

125 

126 # Required ACB 0.19.0+ metadata 

127 MODULE_ID: UUID = UUID("01937d86-cf0b-e4bc-0d3e-2c3d4e5f6071") # Static UUID7 

128 MODULE_STATUS: str = "stable" 

129 

130 def __init__(self) -> None: 

131 """Initialize Material Icons adapter.""" 

132 super().__init__() 

133 self.settings: MaterialIconsSettings | None = None 

134 

135 # Register with Oneiric resolver (fail gracefully if not supported) 

136 with suppress(Exception): 

137 # Register with Oneiric resolver (fail gracefully if not supported) 

138 pass 

139 with suppress(Exception): 

140 depends.set(self) 

141 

142 def get_stylesheet_links(self) -> list[str]: 

143 """Get Material Icons stylesheet links.""" 

144 if not self.settings: 

145 self.settings = MaterialIconsSettings() 

146 

147 links = [] 

148 

149 # Material Icons CSS from Google Fonts 

150 for theme in self.settings.enabled_themes: 

151 if theme == "filled": 

152 # Base Material Icons (filled is default) 

153 css_url = f"{self.settings.base_url}/icon?family=Material+Icons" 

154 else: 

155 # Themed variants 

156 theme_name = theme.replace("-", "+").title() 

157 css_url = ( 

158 f"{self.settings.base_url}/icon?family=Material+Icons+{theme_name}" 

159 ) 

160 

161 links.append(f'<link rel="stylesheet" href="{css_url}">') 

162 

163 # Custom Material Icons CSS 

164 material_css = self._generate_material_css() 

165 links.append(f"<style>{material_css}</style>") 

166 

167 return links 

168 

169 def _generate_material_css(self) -> str: 

170 """Generate Material Icons-specific CSS.""" 

171 if not self.settings: 

172 self.settings = MaterialIconsSettings() 

173 

174 return f""" 

175/* Material Icons Base Styles */ 

176.material-icons {{ 

177 font-family: 'Material Icons'; 

178 font-weight: normal; 

179 font-style: normal; 

180 font-size: {self.settings.default_size}; 

181 line-height: 1; 

182 letter-spacing: normal; 

183 text-transform: none; 

184 display: inline-block; 

185 white-space: nowrap; 

186 word-wrap: normal; 

187 direction: ltr; 

188 -webkit-font-feature-settings: 'liga'; 

189 -webkit-font-smoothing: antialiased; 

190 vertical-align: -0.125em; 

191}} 

192 

193/* Theme-specific font families */ 

194.material-icons-outlined {{ 

195 font-family: 'Material Icons Outlined'; 

196}} 

197 

198.material-icons-round {{ 

199 font-family: 'Material Icons Round'; 

200}} 

201 

202.material-icons-sharp {{ 

203 font-family: 'Material Icons Sharp'; 

204}} 

205 

206.material-icons-two-tone {{ 

207 font-family: 'Material Icons Two Tone'; 

208}} 

209 

210/* Size variants */ 

211.material-icons-xs {{ font-size: 16px; }} 

212.material-icons-sm {{ font-size: 20px; }} 

213.material-icons-md {{ font-size: 24px; }} 

214.material-icons-lg {{ font-size: 28px; }} 

215.material-icons-xl {{ font-size: 32px; }} 

216.material-icons-2xl {{ font-size: 40px; }} 

217.material-icons-3xl {{ font-size: 48px; }} 

218.material-icons-4xl {{ font-size: 56px; }} 

219.material-icons-5xl {{ font-size: 64px; }} 

220 

221/* Density variants */ 

222.material-icons-dense {{ 

223 font-size: 20px; 

224}} 

225 

226.material-icons-comfortable {{ 

227 font-size: 24px; 

228}} 

229 

230.material-icons-compact {{ 

231 font-size: 18px; 

232}} 

233 

234/* Rotation and transformation */ 

235.material-icons-rotate-90 {{ transform: rotate(90deg); }} 

236.material-icons-rotate-180 {{ transform: rotate(180deg); }} 

237.material-icons-rotate-270 {{ transform: rotate(270deg); }} 

238.material-icons-flip-horizontal {{ transform: scaleX(-1); }} 

239.material-icons-flip-vertical {{ transform: scaleY(-1); }} 

240 

241/* Animation support */ 

242.material-icons-spin {{ 

243 animation: material-spin 2s linear infinite; 

244}} 

245 

246.material-icons-pulse {{ 

247 animation: material-pulse 2s ease-in-out infinite alternate; 

248}} 

249 

250.material-icons-bounce {{ 

251 animation: material-bounce 1s ease-in-out infinite; 

252}} 

253 

254.material-icons-shake {{ 

255 animation: material-shake 0.82s cubic-bezier(.36,.07,.19,.97) both; 

256}} 

257 

258.material-icons-flip {{ 

259 animation: material-flip 2s linear infinite; 

260}} 

261 

262@keyframes material-spin {{ 

263 0% {{ transform: rotate(0deg); }} 

264 100% {{ transform: rotate(360deg); }} 

265}} 

266 

267@keyframes material-pulse {{ 

268 from {{ opacity: 1; }} 

269 to {{ opacity: 0.25; }} 

270}} 

271 

272@keyframes material-bounce {{ 

273 0%, 100% {{ transform: translateY(0); }} 

274 50% {{ transform: translateY(-25%); }} 

275}} 

276 

277@keyframes material-shake {{ 

278 10%, 90% {{ transform: translate3d(-1px, 0, 0); }} 

279 20%, 80% {{ transform: translate3d(2px, 0, 0); }} 

280 30%, 50%, 70% {{ transform: translate3d(-4px, 0, 0); }} 

281 40%, 60% {{ transform: translate3d(4px, 0, 0); }} 

282}} 

283 

284@keyframes material-flip {{ 

285 0% {{ transform: rotateY(0); }} 

286 50% {{ transform: rotateY(180deg); }} 

287 100% {{ transform: rotateY(360deg); }} 

288}} 

289 

290/* Material Design color utilities */ 

291{self._generate_material_color_classes()} 

292 

293/* Interactive states */ 

294.material-icons-interactive {{ 

295 cursor: pointer; 

296 transition: all 0.2s ease; 

297 border-radius: 50%; 

298 padding: 4px; 

299}} 

300 

301.material-icons-interactive:hover {{ 

302 background-color: rgba(0, 0, 0, 0.04); 

303 transform: scale(1.1); 

304}} 

305 

306.material-icons-interactive:active {{ 

307 background-color: rgba(0, 0, 0, 0.08); 

308 transform: scale(0.95); 

309}} 

310 

311/* States */ 

312.material-icons-disabled {{ 

313 opacity: 0.38; 

314 cursor: not-allowed; 

315}} 

316 

317.material-icons-inactive {{ 

318 opacity: 0.54; 

319}} 

320 

321.material-icons-loading {{ 

322 opacity: 0.6; 

323}} 

324 

325/* Button integration */ 

326.btn .material-icons {{ 

327 margin-right: 8px; 

328 vertical-align: -0.125em; 

329}} 

330 

331.btn .material-icons:last-child {{ 

332 margin-right: 0; 

333 margin-left: 8px; 

334}} 

335 

336.btn .material-icons:only-child {{ 

337 margin: 0; 

338}} 

339 

340.btn-sm .material-icons {{ 

341 font-size: 20px; 

342}} 

343 

344.btn-lg .material-icons {{ 

345 font-size: 28px; 

346}} 

347 

348/* Floating Action Button */ 

349.fab {{ 

350 display: inline-flex; 

351 align-items: center; 

352 justify-content: center; 

353 width: 56px; 

354 height: 56px; 

355 border-radius: 50%; 

356 border: none; 

357 box-shadow: 0 3px 5px -1px rgba(0,0,0,.2), 0 6px 10px 0 rgba(0,0,0,.14), 0 1px 18px 0 rgba(0,0,0,.12); 

358 cursor: pointer; 

359 transition: all 0.3s ease; 

360}} 

361 

362.fab:hover {{ 

363 box-shadow: 0 5px 5px -3px rgba(0,0,0,.2), 0 8px 10px 1px rgba(0,0,0,.14), 0 3px 14px 2px rgba(0,0,0,.12); 

364 transform: translateY(-2px); 

365}} 

366 

367.fab-mini {{ 

368 width: 40px; 

369 height: 40px; 

370}} 

371 

372.fab-extended {{ 

373 width: auto; 

374 height: 48px; 

375 border-radius: 24px; 

376 padding: 0 16px; 

377}} 

378 

379/* Badge integration */ 

380.badge .material-icons {{ 

381 font-size: 16px; 

382 margin-right: 4px; 

383 vertical-align: baseline; 

384}} 

385 

386/* Navigation integration */ 

387.nav-link .material-icons {{ 

388 margin-right: 8px; 

389 font-size: 20px; 

390}} 

391 

392/* List integration */ 

393.list-group-item .material-icons {{ 

394 margin-right: 16px; 

395 color: rgba(0, 0, 0, 0.54); 

396}} 

397 

398/* Input group integration */ 

399.input-group-text .material-icons {{ 

400 color: rgba(0, 0, 0, 0.54); 

401}} 

402 

403/* Alert integration */ 

404.alert .material-icons {{ 

405 margin-right: 8px; 

406 font-size: 20px; 

407}} 

408 

409/* Card integration */ 

410.card-title .material-icons {{ 

411 margin-right: 8px; 

412}} 

413 

414/* Toolbar integration */ 

415.toolbar .material-icons {{ 

416 color: rgba(255, 255, 255, 0.87); 

417}} 

418 

419/* Dark theme support */ 

420@media (prefers-color-scheme: dark) {{ 

421 .material-icons-interactive:hover {{ 

422 background-color: rgba(255, 255, 255, 0.08); 

423 }} 

424 

425 .material-icons-interactive:active {{ 

426 background-color: rgba(255, 255, 255, 0.12); 

427 }} 

428 

429 .list-group-item .material-icons {{ 

430 color: rgba(255, 255, 255, 0.7); 

431 }} 

432 

433 .input-group-text .material-icons {{ 

434 color: rgba(255, 255, 255, 0.7); 

435 }} 

436}} 

437 

438/* Accessibility */ 

439.material-icons[aria-hidden="false"] {{ 

440 position: relative; 

441}} 

442 

443.material-icons[aria-hidden="false"]:focus {{ 

444 outline: 2px solid #1976d2; 

445 outline-offset: 2px; 

446}} 

447""" 

448 

449 def _generate_material_color_classes(self) -> str: 

450 """Generate Material Design color classes.""" 

451 if not self.settings: 

452 self.settings = MaterialIconsSettings() 

453 

454 css = "/* Material Design Colors */\n" 

455 for name, color in self.settings.material_colors.items(): 

456 css += f".material-icons-{name} {{ color: {color}; }}\n" 

457 

458 # Add semantic colors 

459 css += """ 

460.material-icons-primary { color: var(--primary-color, #1976d2); } 

461.material-icons-secondary { color: var(--secondary-color, #424242); } 

462.material-icons-success { color: var(--success-color, #4caf50); } 

463.material-icons-warning { color: var(--warning-color, #ff9800); } 

464.material-icons-danger { color: var(--danger-color, #f44336); } 

465.material-icons-info { color: var(--info-color, #2196f3); } 

466.material-icons-light { color: var(--light-color, #fafafa); } 

467.material-icons-dark { color: var(--dark-color, #212121); } 

468.material-icons-muted { color: var(--muted-color, #757575); } 

469""" 

470 

471 return css 

472 

473 def get_icon_class(self, icon_name: str, theme: str | None = None) -> str: 

474 """Get Material Icons class with theme support.""" 

475 if not self.settings: 

476 self.settings = MaterialIconsSettings() 

477 

478 # Use default theme if not specified 

479 if not theme: 

480 theme = self.settings.default_theme 

481 

482 # Validate theme 

483 if theme not in self.settings.enabled_themes: 

484 theme = self.settings.default_theme 

485 

486 # Build class name based on theme 

487 if theme == "filled": 

488 return "material-icons" 

489 

490 return f"material-icons-{theme.replace('_', '-')}" 

491 

492 def get_icon_tag( 

493 self, 

494 icon_name: str, 

495 **attributes: Any, 

496 ) -> str: 

497 """Generate Material Icons tag with full customization.""" 

498 # Extract theme and size from attributes 

499 theme = attributes.pop("theme", None) 

500 size = attributes.pop("size", None) 

501 

502 if not self.settings: 

503 self.settings = MaterialIconsSettings() 

504 

505 # Resolve icon aliases 

506 resolved_name = icon_name 

507 if icon_name in self.settings.icon_aliases: 

508 resolved_name = self.settings.icon_aliases[icon_name] 

509 

510 # Get base icon class 

511 icon_class = self.get_icon_class(icon_name, theme) 

512 

513 # Add size class or custom size 

514 if size: 

515 if size in self.settings.size_presets: 

516 icon_class += f" material-icons-{size}" 

517 else: 

518 attributes["style"] = ( 

519 f"font-size: {size}; {attributes.get('style', '')}" 

520 ) 

521 

522 # Add custom classes 

523 if "class" in attributes: 

524 icon_class += f" {attributes.pop('class')}" 

525 

526 # Process attributes using shared utilities 

527 transform_classes, attributes = process_transformations( 

528 attributes, "material-icons" 

529 ) 

530 animation_classes, attributes = process_animations( 

531 attributes, ["spin", "pulse", "bounce", "shake", "flip"], "material-icons" 

532 ) 

533 

534 # Extended semantic colors including material design colors 

535 semantic_colors = [ 

536 "primary", 

537 "secondary", 

538 "success", 

539 "warning", 

540 "danger", 

541 "info", 

542 "light", 

543 "dark", 

544 "muted", 

545 *list(self.settings.material_colors.keys()), 

546 ] 

547 color_class, attributes = process_semantic_colors( 

548 attributes, semantic_colors, "material-icons" 

549 ) 

550 state_classes, attributes = process_state_attributes( 

551 attributes, "material-icons" 

552 ) 

553 

554 # Handle density (Material Design specific feature) 

555 if "density" in attributes: 

556 density = attributes.pop("density") 

557 if density in ("dense", "comfortable", "compact"): 

558 icon_class += f" material-icons-{density}" 

559 

560 # Combine all classes 

561 icon_class += ( 

562 transform_classes + animation_classes + color_class + state_classes 

563 ) 

564 

565 # Build attributes and add accessibility 

566 attrs = {"class": icon_class} | attributes 

567 attrs = add_accessibility_attributes(attrs) 

568 

569 # Generate tag 

570 attr_string = build_attr_string(attrs) 

571 return f"<span {attr_string}>{resolved_name}</span>" 

572 

573 def get_fab_tag( 

574 self, 

575 icon_name: str, 

576 variant: str = "regular", 

577 theme: str | None = None, 

578 **attributes: Any, 

579 ) -> str: 

580 """Generate Material Design Floating Action Button.""" 

581 if not self.settings: 

582 self.settings = MaterialIconsSettings() 

583 

584 # Resolve icon name 

585 resolved_name = icon_name 

586 if icon_name in self.settings.icon_aliases: 

587 resolved_name = self.settings.icon_aliases[icon_name] 

588 

589 # Get icon tag 

590 icon_tag = self.get_icon_tag(resolved_name, theme=theme, size="md") 

591 

592 # Build FAB classes 

593 fab_class = "fab" 

594 if variant == "mini": 

595 fab_class += " fab-mini" 

596 elif variant == "extended": 

597 fab_class += " fab-extended" 

598 

599 # Add custom classes 

600 if "class" in attributes: 

601 fab_class += f" {attributes.pop('class')}" 

602 

603 # Handle extended FAB with text 

604 text = attributes.pop("text", "") 

605 if variant == "extended" and text: 

606 content = f"{icon_tag} {text}" 

607 else: 

608 content = icon_tag 

609 

610 # Build attributes 

611 attrs = {"class": fab_class} | attributes 

612 attr_string = " ".join(f'{k}="{v}"' for k, v in attrs.items()) 

613 

614 return f"<button {attr_string}>{content}</button>" 

615 

616 @staticmethod 

617 def get_available_icons() -> dict[str, list[str]]: 

618 """Get list of available Material Icons by category.""" 

619 return { 

620 "action": [ 

621 "home", 

622 "search", 

623 "settings", 

624 "favorite", 

625 "star", 

626 "bookmark", 

627 "help", 

628 "info", 

629 "check_circle", 

630 "done", 

631 "thumb_up", 

632 "thumb_down", 

633 ], 

634 "communication": [ 

635 "email", 

636 "phone", 

637 "chat", 

638 "message", 

639 "comment", 

640 "forum", 

641 "contact_mail", 

642 "contact_phone", 

643 "textsms", 

644 "call", 

645 ], 

646 "content": [ 

647 "add", 

648 "remove", 

649 "clear", 

650 "create", 

651 "edit", 

652 "delete_forever", 

653 "content_copy", 

654 "content_cut", 

655 "content_paste", 

656 "save", 

657 "undo", 

658 "redo", 

659 ], 

660 "editor": [ 

661 "format_bold", 

662 "format_italic", 

663 "format_underlined", 

664 "format_color_text", 

665 "format_align_left", 

666 "format_align_center", 

667 "format_align_right", 

668 "format_list_bulleted", 

669 ], 

670 "file": [ 

671 "folder", 

672 "folder_open", 

673 "insert_drive_file", 

674 "cloud", 

675 "cloud_download", 

676 "cloud_upload", 

677 "attachment", 

678 "file_download", 

679 "file_upload", 

680 ], 

681 "hardware": [ 

682 "computer", 

683 "phone_android", 

684 "phone_iphone", 

685 "tablet", 

686 "laptop", 

687 "desktop_windows", 

688 "keyboard", 

689 "mouse", 

690 "headset", 

691 "speaker", 

692 ], 

693 "image": [ 

694 "image", 

695 "photo", 

696 "photo_camera", 

697 "video_camera", 

698 "movie", 

699 "music_note", 

700 "palette", 

701 "brush", 

702 "color_lens", 

703 "gradient", 

704 ], 

705 "maps": [ 

706 "location_on", 

707 "location_off", 

708 "my_location", 

709 "navigation", 

710 "map", 

711 "place", 

712 "directions", 

713 "directions_car", 

714 "directions_walk", 

715 "local_taxi", 

716 ], 

717 "navigation": [ 

718 "menu", 

719 "close", 

720 "arrow_back", 

721 "arrow_forward", 

722 "arrow_upward", 

723 "arrow_downward", 

724 "chevron_left", 

725 "chevron_right", 

726 "expand_less", 

727 "expand_more", 

728 "fullscreen", 

729 ], 

730 "notification": [ 

731 "notifications", 

732 "notifications_off", 

733 "notification_important", 

734 "alarm", 

735 "alarm_on", 

736 "alarm_off", 

737 "event", 

738 "event_available", 

739 "schedule", 

740 ], 

741 "social": [ 

742 "person", 

743 "people", 

744 "group", 

745 "public", 

746 "school", 

747 "domain", 

748 "cake", 

749 "mood", 

750 "mood_bad", 

751 "sentiment_satisfied", 

752 "party_mode", 

753 ], 

754 "toggle": [ 

755 "check_box", 

756 "check_box_outline_blank", 

757 "radio_button_checked", 

758 "radio_button_unchecked", 

759 "star", 

760 "star_border", 

761 "favorite", 

762 "favorite_border", 

763 "visibility", 

764 "visibility_off", 

765 ], 

766 "av": [ 

767 "play_arrow", 

768 "pause", 

769 "stop", 

770 "fast_forward", 

771 "fast_rewind", 

772 "skip_next", 

773 "skip_previous", 

774 "volume_up", 

775 "volume_down", 

776 "volume_off", 

777 ], 

778 } 

779 

780 

781# Template filter registration for FastBlocks 

782def _register_material_basic_filters(env: Any) -> None: 

783 """Register basic Material Icons filters.""" 

784 

785 @env.filter("material_icon") # type: ignore[misc] 

786 def material_icon_filter( 

787 icon_name: str, 

788 theme: str | None = None, 

789 size: str | None = None, 

790 **attributes: Any, 

791 ) -> str: 

792 """Template filter for Material Icons.""" 

793 icons = depends.get_sync("icons") 

794 if isinstance(icons, MaterialIcons): 

795 return icons.get_icon_tag(icon_name, theme=theme, size=size, **attributes) 

796 return f"<!-- {icon_name} -->" 

797 

798 @env.filter("material_class") # type: ignore[misc] 

799 def material_class_filter(icon_name: str, theme: str | None = None) -> str: 

800 """Template filter for Material Icons classes.""" 

801 icons = depends.get_sync("icons") 

802 if isinstance(icons, MaterialIcons): 

803 return icons.get_icon_class(icon_name, theme) 

804 return "material-icons" 

805 

806 @env.global_("materialicons_stylesheet_links") # type: ignore[misc] 

807 def materialicons_stylesheet_links() -> str: 

808 """Global function for Material Icons stylesheet links.""" 

809 icons = depends.get_sync("icons") 

810 if isinstance(icons, MaterialIcons): 

811 return "\n".join(icons.get_stylesheet_links()) 

812 return "" 

813 

814 

815def _register_material_fab_functions(env: Any) -> None: 

816 """Register Material Design FAB functions.""" 

817 

818 @env.global_("material_fab") # type: ignore[misc] 

819 def material_fab( 

820 icon_name: str, 

821 variant: str = "regular", 

822 theme: str | None = None, 

823 **attributes: Any, 

824 ) -> str: 

825 """Generate Material Design Floating Action Button.""" 

826 icons = depends.get_sync("icons") 

827 if isinstance(icons, MaterialIcons): 

828 return icons.get_fab_tag(icon_name, variant, theme, **attributes) 

829 return f"<button class='fab'>{icon_name}</button>" 

830 

831 

832def _register_material_button_functions(env: Any) -> None: 

833 """Register Material Design button functions.""" 

834 

835 @env.global_("material_button") # type: ignore[misc] 

836 def material_button( 

837 text: str, 

838 icon: str | None = None, 

839 theme: str | None = None, 

840 icon_position: str = "left", 

841 **attributes: Any, 

842 ) -> str: 

843 """Generate button with Material Icon.""" 

844 icons = depends.get_sync("icons") 

845 if not isinstance(icons, MaterialIcons): 

846 return f"<button>{text}</button>" 

847 

848 btn_class = attributes.pop("class", "btn btn-primary") 

849 

850 # Build button content 

851 content = "" 

852 if icon: 

853 icon_tag = icons.get_icon_tag(icon, theme=theme, size="sm") 

854 

855 if icon_position == "left": 

856 content = f"{icon_tag} {text}" 

857 elif icon_position == "right": 

858 content = f"{text} {icon_tag}" 

859 elif icon_position == "only": 

860 content = icon_tag 

861 else: 

862 content = text 

863 else: 

864 content = text 

865 

866 # Build button attributes 

867 btn_attrs = {"class": btn_class} | attributes 

868 attr_string = " ".join(f'{k}="{v}"' for k, v in btn_attrs.items()) 

869 

870 return f"<button {attr_string}>{content}</button>" 

871 

872 

873def _register_material_chip_functions(env: Any) -> None: 

874 """Register Material Design chip functions.""" 

875 

876 @env.global_("material_chip") # type: ignore[misc] 

877 def material_chip( 

878 text: str, 

879 icon: str | None = None, 

880 theme: str | None = None, 

881 deletable: bool = False, 

882 **attributes: Any, 

883 ) -> str: 

884 """Generate Material Design chip with icon.""" 

885 icons = depends.get_sync("icons") 

886 if not isinstance(icons, MaterialIcons): 

887 return f"<div class='chip'>{text}</div>" 

888 

889 chip_class = attributes.pop("class", "chip") 

890 

891 # Build chip content 

892 content = "" 

893 if icon: 

894 icon_tag = icons.get_icon_tag( 

895 icon, theme=theme, size="sm", class_="chip-icon" 

896 ) 

897 content += icon_tag 

898 

899 content += f"<span class='chip-text'>{text}</span>" 

900 

901 if deletable: 

902 delete_icon = icons.get_icon_tag( 

903 "close", theme=theme, size="sm", class_="chip-delete" 

904 ) 

905 content += delete_icon 

906 

907 # Build chip attributes 

908 chip_attrs = {"class": chip_class} | attributes 

909 attr_string = " ".join(f'{k}="{v}"' for k, v in chip_attrs.items()) 

910 

911 return f"<div {attr_string}>{content}</div>" 

912 

913 

914def register_materialicons_filters(env: Any) -> None: 

915 """Register Material Icons filters for Jinja2 templates.""" 

916 _register_material_basic_filters(env) 

917 _register_material_fab_functions(env) 

918 _register_material_button_functions(env) 

919 _register_material_chip_functions(env) 

920 

921 

922IconsSettings = MaterialIconsSettings 

923Icons = MaterialIcons 

924 

925# Register with Oneiric resolver (fail gracefully if not supported) 

926with suppress(Exception): 

927 depends.set(Icons, "materialicons") 

928 

929# ACB 0.19.0+ compatibility 

930__all__ = [ 

931 "MaterialIcons", 

932 "MaterialIconsSettings", 

933 "register_materialicons_filters", 

934 "Icons", 

935 "IconsSettings", 

936]