Skip to content

Commit 12a48d1

Browse files
authored
fix(api): transform icon when creating icon menu item and predefined about menu item with icon (#11741)
1 parent 020ea05 commit 12a48d1

8 files changed

+67
-78
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Fix error when calling `PredefinedMenuItem.new` to create an `About` menu item that uses an `Image` instance for the about icon.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/api": "patch:bug"
3+
---
4+
5+
Fix error when calling `IconMenuItem.new` using an `Image` instance for the icon.
6+

crates/tauri/scripts/bundle.global.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"packageManager": "pnpm@9.9.0",
2929
"pnpm": {
3030
"overrides": {
31-
"rollup@>=4.0.0 <4.22.4": ">=4.22.4",
31+
"cross-spawn@>=7.0.0 <7.0.5": ">=7.0.5",
3232
"cookie@<0.7.0": ">=0.7.0"
3333
}
3434
}

packages/api/src/menu/base.ts

+40-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Channel, invoke, Resource } from '../core'
66
import { transformImage } from '../image'
77
import { CheckMenuItemOptions } from './checkMenuItem'
88
import { IconMenuItemOptions } from './iconMenuItem'
9+
import { MenuOptions } from './menu'
910
import { MenuItemOptions } from './menuItem'
1011
import { PredefinedMenuItemOptions } from './predefinedMenuItem'
1112
import { SubmenuOptions } from './submenu'
@@ -18,19 +19,16 @@ export type ItemKind =
1819
| 'Submenu'
1920
| 'Menu'
2021

21-
function injectChannel(
22-
i:
23-
| MenuItemOptions
24-
| SubmenuOptions
25-
| IconMenuItemOptions
26-
| PredefinedMenuItemOptions
27-
| CheckMenuItemOptions
28-
):
22+
type MenuItemOptionsAlias =
23+
| MenuItemOptions
2924
| SubmenuOptions
25+
| IconMenuItemOptions
3026
| PredefinedMenuItemOptions
31-
| ((MenuItemOptions | IconMenuItemOptions | CheckMenuItemOptions) & {
32-
handler?: Channel<string>
33-
}) {
27+
| CheckMenuItemOptions
28+
29+
function injectChannel(i: MenuItemOptionsAlias): MenuItemOptionsAlias & {
30+
handler?: Channel<string>
31+
} {
3432
if ('items' in i) {
3533
i.items = i.items?.map((item) =>
3634
'rid' in item ? item : injectChannel(item)
@@ -46,7 +44,13 @@ function injectChannel(
4644

4745
export async function newMenu(
4846
kind: ItemKind,
49-
opts?: unknown
47+
opts?:
48+
| MenuOptions
49+
| MenuItemOptions
50+
| SubmenuOptions
51+
| PredefinedMenuItemOptions
52+
| CheckMenuItemOptions
53+
| IconMenuItemOptions
5054
): Promise<[number, string]> {
5155
const handler = new Channel<string>()
5256

@@ -56,22 +60,30 @@ export async function newMenu(
5660
delete opts.action
5761
}
5862

63+
// about predefined menu item icon
64+
if (
65+
'item' in opts &&
66+
opts.item &&
67+
typeof opts.item === 'object' &&
68+
'About' in opts.item &&
69+
opts.item.About &&
70+
typeof opts.item.About === 'object' &&
71+
'icon' in opts.item.About &&
72+
opts.item.About.icon
73+
) {
74+
opts.item.About.icon = transformImage(opts.item.About.icon)
75+
}
76+
77+
// icon menu item icon
78+
if ('icon' in opts && opts.icon) {
79+
opts.icon = transformImage(opts.icon)
80+
}
81+
82+
// submenu items
5983
if ('items' in opts && opts.items) {
6084
function prepareItem(
61-
i:
62-
| { rid: number; kind: string }
63-
| MenuItemOptions
64-
| SubmenuOptions
65-
| IconMenuItemOptions
66-
| PredefinedMenuItemOptions
67-
| CheckMenuItemOptions
68-
):
69-
| [number, string]
70-
| SubmenuOptions
71-
| PredefinedMenuItemOptions
72-
| MenuItemOptions
73-
| IconMenuItemOptions
74-
| CheckMenuItemOptions {
85+
i: { rid: number; kind: string } | MenuItemOptionsAlias
86+
): [number, string] | MenuItemOptionsAlias {
7587
if ('rid' in i) {
7688
return [i.rid, i.kind]
7789
}
@@ -93,6 +105,8 @@ export async function newMenu(
93105
return injectChannel(i)
94106
}
95107

108+
// @ts-expect-error the `prepareItem` return doesn't exactly match
109+
// this is fine, because the difference is in `[number, string]` variant
96110
opts.items = (opts.items as []).map(prepareItem)
97111
}
98112
}

packages/api/src/menu/menu.ts

+1-29
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,12 @@ import { MenuItem } from './menuItem'
1313
import { CheckMenuItem } from './checkMenuItem'
1414
import { IconMenuItem } from './iconMenuItem'
1515
import { PredefinedMenuItem } from './predefinedMenuItem'
16-
import { Submenu } from './submenu'
16+
import { itemFromKind, Submenu } from './submenu'
1717
import { type LogicalPosition, PhysicalPosition, Position } from '../dpi'
1818
import { type Window } from '../window'
1919
import { invoke } from '../core'
2020
import { type ItemKind, MenuItemBase, newMenu } from './base'
2121

22-
function itemFromKind([rid, id, kind]: [number, string, ItemKind]):
23-
| Submenu
24-
| MenuItem
25-
| PredefinedMenuItem
26-
| CheckMenuItem
27-
| IconMenuItem {
28-
/* eslint-disable @typescript-eslint/no-unsafe-return */
29-
switch (kind) {
30-
case 'Submenu':
31-
// @ts-expect-error constructor is protected for external usage only
32-
return new Submenu(rid, id)
33-
case 'Predefined':
34-
// @ts-expect-error constructor is protected for external usage only
35-
return new PredefinedMenuItem(rid, id)
36-
case 'Check':
37-
// @ts-expect-error constructor is protected for external usage only
38-
return new CheckMenuItem(rid, id)
39-
case 'Icon':
40-
// @ts-expect-error constructor is protected for external usage only
41-
return new IconMenuItem(rid, id)
42-
case 'MenuItem':
43-
default:
44-
// @ts-expect-error constructor is protected for external usage only
45-
return new MenuItem(rid, id)
46-
}
47-
/* eslint-enable @typescript-eslint/no-unsafe-return */
48-
}
49-
5022
/** Options for creating a new menu. */
5123
export interface MenuOptions {
5224
/** Specify an id to use for the new menu. */

packages/api/src/menu/submenu.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { type ItemKind, MenuItemBase, newMenu } from './base'
1717
import { type MenuOptions } from './menu'
1818
import { Position } from '../dpi'
1919

20-
function itemFromKind([rid, id, kind]: [number, string, ItemKind]):
20+
/** @ignore */
21+
export function itemFromKind([rid, id, kind]: [number, string, ItemKind]):
2122
| Submenu
2223
| MenuItem
2324
| PredefinedMenuItem

pnpm-lock.yaml

+10-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)