Skip to content

Commit 720bb45

Browse files
committed
- Create an item entry without the "icon" field when the icon name is undefined. (fixes #34)
1 parent 298e930 commit 720bb45

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

obmenu-generator

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Name: obmenu-generator
2525
# License: GPLv3
2626
# Created: 25 March 2011
27-
# Latest edit: 23 June 2020
27+
# Latest edit: 29 November 2020
2828
# https://github.com/trizen/obmenu-generator
2929

3030
use 5.014;
@@ -469,18 +469,21 @@ if ($with_icons) {
469469
my %fast_cache;
470470

471471
sub check_icon {
472-
$fast_cache{$_[0] // return ''} //= ($cache_db{$_[0]} //= get_icon_path($_[0]));
472+
$fast_cache{$_[0] // return undef} //= ($cache_db{$_[0]} //= get_icon_path($_[0]));
473473
}
474474
}
475475

476476
sub begin_category {
477-
$with_icons
478-
? <<"MENU_WITH_ICON"
479-
<menu id="${\rand()}" icon="${\check_icon($_[1])}" label="$_[0]">
477+
if ($with_icons and defined(my $icon_path = check_icon($_[1]))) {
478+
<<"MENU_WITH_ICON";
479+
<menu id="${\rand()}" icon="$icon_path" label="$_[0]">
480480
MENU_WITH_ICON
481-
: <<"MENU";
481+
}
482+
else {
483+
<<"MENU";
482484
<menu id="${\rand()}" label="$_[0]">
483485
MENU
486+
}
484487
}
485488

486489
my %categories;
@@ -575,13 +578,16 @@ ITEM
575578
}
576579
elsif (exists $schema->{item}) {
577580
my ($command, $label, $icon) = @{$schema->{item}};
578-
$generated_menu .= $with_icons
579-
? <<"ITEM_WITH_ICON"
580-
<item label="$label" icon="${\check_icon($icon)}"><action name="Execute"><command><![CDATA[$command]]></command></action></item>
581+
if ($with_icons and defined(my $icon_path = check_icon($icon))) {
582+
$generated_menu .= <<"ITEM_WITH_ICON";
583+
<item label="$label" icon="$icon_path"><action name="Execute"><command><![CDATA[$command]]></command></action></item>
581584
ITEM_WITH_ICON
582-
: <<"ITEM";
585+
}
586+
else {
587+
$generated_menu .= <<"ITEM";
583588
<item label="$label"><action name="Execute"><command><![CDATA[$command]]></command></action></item>
584589
ITEM
590+
}
585591
}
586592
elsif (exists $schema->{sep}) {
587593
$generated_menu .=
@@ -603,13 +609,16 @@ ITEM
603609
}
604610
elsif (exists $schema->{exit}) {
605611
my ($label, $icon) = @{$schema->{exit}};
606-
$generated_menu .= $with_icons
607-
? <<"EXIT_WITH_ICON"
608-
<item label="$label" icon="${\check_icon($icon)}"><action name="Exit"/></item>
612+
if ($with_icons and defined(my $icon_path = check_icon($icon))) {
613+
$generated_menu .= <<"EXIT_WITH_ICON";
614+
<item label="$label" icon="$icon_path"><action name="Exit"/></item>
609615
EXIT_WITH_ICON
610-
: <<"EXIT";
616+
}
617+
else {
618+
$generated_menu .= <<"EXIT";
611619
<item label="$label"><action name="Exit"/></item>
612620
EXIT
621+
}
613622
}
614623
elsif (exists $schema->{raw}) {
615624
$generated_menu .= qq[ $schema->{raw}\n];
@@ -620,21 +629,29 @@ EXIT
620629
}
621630
elsif (exists $schema->{pipe}) {
622631
my ($command, $label, $icon) = @{$schema->{pipe}};
623-
$generated_menu .= $with_icons
624-
? <<"PIPE_WITH_ICON"
625-
<menu id="${\rand()}" label="$label" execute="$command" icon="${\check_icon($icon)}"/>
632+
if ($with_icons and defined(my $icon_path = check_icon($icon))) {
633+
$generated_menu .= <<"PIPE_WITH_ICON";
634+
<menu id="${\rand()}" label="$label" execute="$command" icon="$icon_path"/>
626635
PIPE_WITH_ICON
627-
: <<"PIPE";
636+
}
637+
else {
638+
$generated_menu .= <<"PIPE";
628639
<menu id="${\rand()}" label="$label" execute="$command"/>
629640
PIPE
641+
}
630642
}
631643
elsif (exists $schema->{obgenmenu}) {
632644
my ($name, $icon) = ref($schema->{obgenmenu}) eq 'ARRAY' ? @{$schema->{obgenmenu}} : $schema->{obgenmenu};
633-
$generated_menu .= ($with_icons ? <<"MENU_WITH_ICON" : <<"MENU");
634-
<menu id="${\rand()}" label="$name" icon="${\check_icon($icon)}">
645+
if ($with_icons and defined(my $icon_path = check_icon($icon))) {
646+
$generated_menu .= <<"MENU_WITH_ICON";
647+
<menu id="${\rand()}" label="$name" icon="$icon_path">
635648
MENU_WITH_ICON
649+
}
650+
else {
651+
$generated_menu .= <<"MENU";
636652
<menu id="${\rand()}" label="$name">
637653
MENU
654+
}
638655

639656
$generated_menu .= ($with_icons ? <<"ITEMS_WITH_ICONS" : <<"ITEMS");
640657
<item label="Menu Schema" icon="${\check_icon('text-x-generic')}"><action name="Execute"><command><![CDATA[$CONFIG{editor} $schema_file]]></command></action></item>

0 commit comments

Comments
 (0)