11#include " menu_widget.h"
22#include " ../utils.h"
33#include " animator.h"
4+ #include " contextmenu.h"
45#include " hbitmap_utils.h"
56#include " menu_render.h"
67#include " nanovg.h"
7- #include " shell.h"
88#include " ui.h"
99#include < iostream>
1010#include < print>
@@ -32,16 +32,16 @@ void mb_shell::menu_item_widget::render(ui::nanovg_context ctx) {
3232 ctx.fillRoundedRect (*x + margin, *y, *width - margin * 2 , *height,
3333 roundcorner);
3434
35- if (item.icon_bitmap .has_value ()) {
35+ if (item.icon_bitmap .has_value () || item.icon_svg .has_value ()) {
36+ if (!icon_img || item.icon_updated )
37+ reload_icon_img (ctx);
38+ item.icon_updated = false ;
39+
3640 auto paintY = floor (*y + (*height - icon_width) / 2 );
37- if (!icon_img_bmp) {
38- icon_img_bmp = ui::LoadBitmapImage (ctx, item.icon_bitmap .value ());
39- }
40-
4141 auto paint =
4242 nvgImagePattern (ctx.ctx , *x + icon_padding + margin + ctx.offset_x ,
4343 paintY + ctx.offset_y , icon_width, icon_width, 0 ,
44- icon_img_bmp ->id , *opacity / 255 .f );
44+ icon_img ->id , *opacity / 255 .f );
4545
4646 ctx.beginPath ();
4747 ctx.rect (*x + icon_padding + margin, paintY, icon_width, icon_width);
@@ -579,7 +579,8 @@ void mb_shell::menu_widget::init_from_data(menu menu_data) {
579579void mb_shell::menu_widget::update_icon_width () {
580580 bool has_icon = std::ranges::any_of (children, [](auto &item) {
581581 auto i = item->template downcast <menu_item_widget>()->item ;
582- return i.icon_bitmap .has_value () || i.type == menu_item::type::toggle;
582+ return i.icon_bitmap .has_value () || i.icon_svg .has_value () ||
583+ i.type == menu_item::type::toggle;
583584 });
584585
585586 for (auto &item : children) {
@@ -590,4 +591,14 @@ void mb_shell::menu_widget::update_icon_width() {
590591 mi->icon_width = 16 ;
591592 }
592593 }
593- };
594+ };
595+ void mb_shell::menu_item_widget::reload_icon_img (ui::nanovg_context ctx) {
596+ if (item.icon_bitmap )
597+ icon_img = ui::LoadBitmapImage (ctx, (HBITMAP)item.icon_bitmap .value ());
598+ else if (item.icon_svg ) {
599+ std::string copy = item.icon_svg .value ();
600+ icon_img = ctx.imageFromSVG (nsvgParse (copy.data (), " px" , 96 ));
601+ } else {
602+ icon_img = std::nullopt ;
603+ }
604+ }
0 commit comments