From cff777db642a6ae1396bc6c72a6155b2459177ba Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 29 Aug 2023 18:10:11 +0300 Subject: [PATCH] lint and clippy --- core/tauri-macros/src/menu.rs | 4 ++ core/tauri-runtime-wry/src/lib.rs | 19 +++--- core/tauri/Cargo.toml | 4 +- core/tauri/src/lib.rs | 1 + core/tauri/src/menu/plugin.rs | 3 +- core/tauri/src/resources/plugin.rs | 4 ++ examples/api/src/vite-env.d.ts | 4 ++ examples/api/svelte.config.js | 6 +- tooling/api/src/internal/index.ts | 16 +++-- tooling/api/src/menu.ts | 96 +++++++++++++++--------------- tooling/api/src/tray.ts | 23 ++++--- 11 files changed, 103 insertions(+), 77 deletions(-) diff --git a/core/tauri-macros/src/menu.rs b/core/tauri-macros/src/menu.rs index 7e93da475bb..4f947b83d87 100644 --- a/core/tauri-macros/src/menu.rs +++ b/core/tauri-macros/src/menu.rs @@ -1,3 +1,7 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; use syn::{ diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index f19c7435af2..2e9752c9e68 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -94,6 +94,7 @@ use std::{ fmt, ops::Deref, path::PathBuf, + rc::Rc, sync::{ mpsc::{channel, Sender}, Arc, Mutex, Weak, @@ -227,7 +228,7 @@ impl Context { pub struct DispatcherMainThreadContext { pub window_target: EventLoopWindowTarget>, pub web_context: WebContextStore, - pub windows: Arc>>, + pub windows: Rc>>, } impl std::fmt::Debug for DispatcherMainThreadContext { @@ -1544,7 +1545,7 @@ impl Dispatch for WryDispatcher { #[derive(Clone)] enum WindowHandle { Webview { - inner: Arc, + inner: Rc, context_store: WebContextStore, // the key of the WebContext if it's not shared context_key: Option, @@ -1560,7 +1561,7 @@ impl Drop for WindowHandle { context_key, } = self { - if Arc::get_mut(inner).is_some() { + if Rc::get_mut(inner).is_some() { context_store.lock().unwrap().remove(context_key); } } @@ -1816,7 +1817,7 @@ impl Wry { let main_thread_id = current_thread().id(); let web_context = WebContextStore::default(); - let windows = Arc::new(RefCell::new(HashMap::default())); + let windows = Rc::new(RefCell::new(HashMap::default())); let webview_id_map = WebviewIdStore::default(); let context = Context { @@ -2056,11 +2057,11 @@ impl Runtime for Wry { pub struct EventLoopIterationContext<'a, T: UserEvent> { pub callback: &'a mut (dyn FnMut(RunEvent) + 'static), pub webview_id_map: WebviewIdStore, - pub windows: Arc>>, + pub windows: Rc>>, } struct UserMessageContext { - windows: Arc>>, + windows: Rc>>, webview_id_map: WebviewIdStore, } @@ -2529,7 +2530,7 @@ fn handle_event_loop( fn on_close_requested<'a, T: UserEvent>( callback: &'a mut (dyn FnMut(RunEvent) + 'static), window_id: WebviewId, - windows: Arc>>, + windows: Rc>>, ) { let (tx, rx) = channel(); let windows_ref = windows.borrow(); @@ -2557,7 +2558,7 @@ fn on_close_requested<'a, T: UserEvent>( } } -fn on_window_close(window_id: WebviewId, windows: Arc>>) { +fn on_window_close(window_id: WebviewId, windows: Rc>>) { if let Some(window_wrapper) = windows.borrow_mut().get_mut(&window_id) { window_wrapper.inner = None; } @@ -2803,7 +2804,7 @@ fn create_webview( Ok(WindowWrapper { label, inner: Some(WindowHandle::Webview { - inner: Arc::new(webview), + inner: Rc::new(webview), context_store: web_context_store.clone(), context_key: if automation_enabled { None diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index e1710215141..95cfe0f81fe 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -69,8 +69,8 @@ png = { version = "0.17", optional = true } ico = { version = "0.3.0", optional = true } [target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\", target_os = \"windows\", target_os = \"macos\"))".dependencies] -muda = { version = "0.8", default-features = false, features = [ "serde" ]} -tray-icon = { version = "0.8", default-features = false, features = [ "serde" ], optional = true } +muda = { path = "../../../muda", default-features = false, features = [ "serde" ]} +tray-icon = { path = "../../../tray-icon", default-features = false, features = [ "serde" ], optional = true } [target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] gtk = { version = "0.16", features = [ "v3_24" ] } diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 513b3d34c98..7f507b5ad3c 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -954,6 +954,7 @@ macro_rules! run_main_thread { let (tx, rx) = channel(); let self_ = $self.clone(); let task = move || { + #[allow(clippy::redundant_closure_call)] let _ = tx.send($ex(self_)); }; $self.app_handle.run_on_main_thread(Box::new(task))?; diff --git a/core/tauri/src/menu/plugin.rs b/core/tauri/src/menu/plugin.rs index 5158064b6ea..fb798756ecc 100644 --- a/core/tauri/src/menu/plugin.rs +++ b/core/tauri/src/menu/plugin.rs @@ -56,6 +56,7 @@ impl From for super::AboutMetadata { } } +#[allow(clippy::large_enum_variant)] #[derive(Deserialize)] enum Predefined { Separator, @@ -202,7 +203,7 @@ fn new( builder = builder.enabled(enabled); } if let Some(native_icon) = options.native_icon { - builder = builder.native_icon(native_icon.into()); + builder = builder.native_icon(native_icon); } if let Some(icon) = options.icon { builder = builder.icon(icon.into()); diff --git a/core/tauri/src/resources/plugin.rs b/core/tauri/src/resources/plugin.rs index 3103034b0d5..1b8932e2197 100644 --- a/core/tauri/src/resources/plugin.rs +++ b/core/tauri/src/resources/plugin.rs @@ -1,3 +1,7 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + use crate::{ command, plugin::{Builder, TauriPlugin}, diff --git a/examples/api/src/vite-env.d.ts b/examples/api/src/vite-env.d.ts index 4078e7476a2..c4b24aee8df 100644 --- a/examples/api/src/vite-env.d.ts +++ b/examples/api/src/vite-env.d.ts @@ -1,2 +1,6 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + /// /// diff --git a/examples/api/svelte.config.js b/examples/api/svelte.config.js index b0683fd24d7..15df6c2154c 100644 --- a/examples/api/svelte.config.js +++ b/examples/api/svelte.config.js @@ -1,7 +1,11 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + import { vitePreprocess } from '@sveltejs/vite-plugin-svelte' export default { // Consult https://svelte.dev/docs#compile-time-svelte-preprocess // for more information about preprocessors - preprocess: vitePreprocess(), + preprocess: vitePreprocess() } diff --git a/tooling/api/src/internal/index.ts b/tooling/api/src/internal/index.ts index cdb967d6645..d0639fc1a03 100644 --- a/tooling/api/src/internal/index.ts +++ b/tooling/api/src/internal/index.ts @@ -1,3 +1,7 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + import { invoke } from '../tauri' /** @@ -22,7 +26,7 @@ export class Resource { * Destroys and cleans up this resource from memory. * **You should not call any method on this object anymore and should drop any reference to it.** */ - async close() { + async close(): Promise { return invoke('plugin:resources|close', { rid: this.rid }) @@ -30,16 +34,20 @@ export class Resource { } /** Extends a base class by other specifed classes */ -export function applyMixins(baseClass: any, extendedClasses: any | any[]) { +export function applyMixins( + baseClass: { prototype: any }, + extendedClasses: any | any[] +): void { ;(Array.isArray(extendedClasses) ? extendedClasses : [extendedClasses] - ).forEach((extendedClass) => { + ).forEach((extendedClass: { prototype: any }) => { Object.getOwnPropertyNames(extendedClass.prototype).forEach((name) => { Object.defineProperty( baseClass.prototype, name, - Object.getOwnPropertyDescriptor(extendedClass.prototype, name) || + // eslint-disable-next-line + Object.getOwnPropertyDescriptor(extendedClass.prototype, name) ?? Object.create(null) ) }) diff --git a/tooling/api/src/menu.ts b/tooling/api/src/menu.ts index a830aab45bb..6ca2cce7404 100644 --- a/tooling/api/src/menu.ts +++ b/tooling/api/src/menu.ts @@ -22,12 +22,15 @@ interface MenuEvent { declare global { interface Window { __TAURI_MENU__?: { - handlers: { [key: string]: (() => void)[] } + handlers: Record void>> } } } -async function addEventListener(id: string, handler: () => void) { +async function addEventListener( + id: string, + handler: () => void +): Promise { if (!window.__TAURI_MENU__) { window.__TAURI_MENU__ = { handlers: {} } const unlisten = await listen( @@ -45,13 +48,16 @@ async function addEventListener(id: string, handler: () => void) { } ) - window.addEventListener('unload', () => unlisten()) + window.addEventListener('unload', unlisten) } + // eslint-disable-next-line if (!window.__TAURI_MENU__.handlers[id]) { + // eslint-disable-next-line window.__TAURI_MENU__.handlers[id] = [] } + // eslint-disable-next-line window.__TAURI_MENU__.handlers[id].push(handler) } @@ -201,14 +207,16 @@ function itemFromKind([rid, id, kind]: [number, string, ItemKind]): async function newMenu(kind: ItemKind, opts?: any): Promise<[number, string]> { let handler: null | (() => void) = null - let items: null | [number, string][] = null + let items: null | Array<[number, string]> = null if (opts) { if ('action' in opts) { - handler = opts.action as () => void + // eslint-disable-next-line + handler = opts.action } if ('items' in opts) { - // @ts-expect-error + // @ts-expect-error items should all have rid and kind accessors + // eslint-disable-next-line items = opts.items.map((i) => [i.rid, i.kind]) } } @@ -218,7 +226,7 @@ async function newMenu(kind: ItemKind, opts?: any): Promise<[number, string]> { options: opts ? { ...opts, items } : undefined }).then((ret) => { if (handler) { - addEventListener(ret[1], handler) + void addEventListener(ret[1], handler) } return ret }) @@ -241,19 +249,9 @@ class MenuItemBase extends Resource { this.#id = id this.#kind = kind } - - async close() { - return invoke('plugin:resources|close', { - rid: this.rid - }) - } } class MenuBase extends MenuItemBase { - constructor(rid: number, id: string, kind: ItemKind) { - super(rid, id, kind) - } - async append< T extends | Submenu @@ -261,7 +259,7 @@ class MenuBase extends MenuItemBase { | PredefinedMenuItem | CheckMenuItem | IconMenuItem - >(items: T | T[]) { + >(items: T | T[]): Promise { return invoke('plugin:menu|append', { rid: this.rid, kind: this.kind, @@ -279,7 +277,7 @@ class MenuBase extends MenuItemBase { | PredefinedMenuItem | CheckMenuItem | IconMenuItem - >(items: T | T[]) { + >(items: T | T[]): Promise { return invoke('plugin:menu|prepend', { rid: this.rid, kind: this.kind, @@ -297,7 +295,7 @@ class MenuBase extends MenuItemBase { | PredefinedMenuItem | CheckMenuItem | IconMenuItem - >(items: T | T[], position: number) { + >(items: T | T[], position: number): Promise { return invoke('plugin:menu|insert', { rid: this.rid, kind: this.kind, @@ -311,7 +309,7 @@ class MenuBase extends MenuItemBase { async remove( item: Submenu | MenuItem | PredefinedMenuItem | CheckMenuItem | IconMenuItem - ) { + ): Promise { return invoke('plugin:menu|remove', { rid: this.rid, kind: this.kind, @@ -337,9 +335,11 @@ class MenuBase extends MenuItemBase { } async items(): Promise< - (Submenu | MenuItem | PredefinedMenuItem | CheckMenuItem | IconMenuItem)[] + Array< + Submenu | MenuItem | PredefinedMenuItem | CheckMenuItem | IconMenuItem + > > { - return invoke<[number, string, ItemKind][]>('plugin:menu|append', { + return invoke>('plugin:menu|append', { rid: this.rid, kind: this.kind }).then((i) => i.map(itemFromKind)) @@ -364,7 +364,7 @@ class MenuBase extends MenuItemBase { // TODO change to logical position // TODO use window type after migrating window back to core - async popup(window: string, position?: [number, number]) { + async popup(window: string, position?: [number, number]): Promise { return invoke('plugin:menu|popup', { rid: this.rid, kind: this.kind, @@ -376,13 +376,9 @@ class MenuBase extends MenuItemBase { interface MenuOptions { id?: string - items?: ( - | Submenu - | MenuItem - | PredefinedMenuItem - | CheckMenuItem - | IconMenuItem - )[] + items?: Array< + Submenu | MenuItem | PredefinedMenuItem | CheckMenuItem | IconMenuItem + > } class Menu extends MenuBase { @@ -390,11 +386,11 @@ class Menu extends MenuBase { super(rid, id, 'Menu') } - static async new(opts?: MenuOptions) { + static async new(opts?: MenuOptions): Promise { return newMenu('Menu', opts).then(([rid, id]) => new Menu(rid, id)) } - static async default() { + static async default(): Promise { return invoke<[number, string]>('plugin:menu|default').then( ([rid, id]) => new Menu(rid, id) ) @@ -420,7 +416,7 @@ class MenuItemBase2 extends MenuItemBase { return invoke('plugin:menu|text', { rid: this.rid, kind: this.kind }) } - async setText(text: string) { + async setText(text: string): Promise { return invoke('plugin:menu|set_text', { rid: this.rid, kind: this.kind, @@ -434,7 +430,7 @@ class MenuItemBase3 extends MenuItemBase2 { return invoke('plugin:menu|is_enabled', { rid: this.rid, kind: this.kind }) } - async setEnabled(enabled: boolean) { + async setEnabled(enabled: boolean): Promise { return invoke('plugin:menu|set_enabled', { rid: this.rid, kind: this.kind, @@ -444,7 +440,7 @@ class MenuItemBase3 extends MenuItemBase2 { } class MenuItemBase4 extends MenuItemBase3 { - async setAccelerator(accelerator: string | null) { + async setAccelerator(accelerator: string | null): Promise { return invoke('plugin:menu|set_accelerator', { rid: this.rid, kind: this.kind, @@ -466,7 +462,7 @@ class MenuItem extends MenuItemBase4 { super(rid, id, 'MenuItem') } - static async new(opts: MenuItemOptions) { + static async new(opts: MenuItemOptions): Promise { return newMenu('MenuItem', opts).then(([rid, id]) => new MenuItem(rid, id)) } } @@ -480,17 +476,17 @@ class Submenu extends MenuBase { super(rid, id, 'Submenu') } - static async new(opts: SubmenuOptions) { + static async new(opts: SubmenuOptions): Promise { return newMenu('Submenu', opts).then(([rid, id]) => new Submenu(rid, id)) } - async setAsWindowsMenuForNSApp() { + async setAsWindowsMenuForNSApp(): Promise { return invoke('plugin:menu|set_as_windows_menu_for_nsapp', { rid: this.rid }) } - async setAsHelpMenuForNSApp() { + async setAsHelpMenuForNSApp(): Promise { return invoke('plugin:menu|set_as_help_menu_for_nsapp', { rid: this.rid }) @@ -499,7 +495,7 @@ class Submenu extends MenuBase { applyMixins(Submenu, MenuItemBase3) -type PredefinedMenuItemOptions = { +interface PredefinedMenuItemOptions { text?: string item: | 'Separator' @@ -540,8 +536,12 @@ class PredefinedMenuItem extends MenuItemBase2 { super(rid, id, 'MenuItem') } - static async new(opts?: PredefinedMenuItemOptions) { - return newMenu('MenuItem', opts).then(([rid, id]) => new MenuItem(rid, id)) + static async new( + opts?: PredefinedMenuItemOptions + ): Promise { + return newMenu('MenuItem', opts).then( + ([rid, id]) => new PredefinedMenuItem(rid, id) + ) } } @@ -554,7 +554,7 @@ class CheckMenuItem extends MenuItemBase4 { super(rid, id, 'Check') } - static async new(opts: MenuItemOptions) { + static async new(opts: MenuItemOptions): Promise { return newMenu('Check', opts).then( ([rid, id]) => new CheckMenuItem(rid, id) ) @@ -564,7 +564,7 @@ class CheckMenuItem extends MenuItemBase4 { return invoke('plugin:menu|is_checked', { rid: this.rid }) } - async setChecked(checked: boolean) { + async setChecked(checked: boolean): Promise { return invoke('plugin:menu|set_checked', { rid: this.rid, checked @@ -581,15 +581,15 @@ class IconMenuItem extends MenuItemBase4 { super(rid, id, 'Icon') } - static async new(opts: IconMenuItemOptions) { + static async new(opts: IconMenuItemOptions): Promise { return newMenu('Icon', opts).then(([rid, id]) => new IconMenuItem(rid, id)) } - async setIcon(icon: string | Uint8Array | null) { + async setIcon(icon: string | Uint8Array | null): Promise { return invoke('plugin:menu|set_icon', { rid: this.rid, icon }) } - async setNativeIcon(icon: NativeIcon | null) { + async setNativeIcon(icon: NativeIcon | null): Promise { return invoke('plugin:menu|set_native_icon', { rid: this.rid, icon }) } } diff --git a/tooling/api/src/tray.ts b/tooling/api/src/tray.ts index 0b86d1fc5ab..1f354690820 100644 --- a/tooling/api/src/tray.ts +++ b/tooling/api/src/tray.ts @@ -2,10 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -import { Menu } from './menu' -import { TauriEvent, UnlistenFn, listen } from './event' +import type { Menu, Submenu } from './menu' +import { TauriEvent, listen, type UnlistenFn } from './event' import { Resource } from './internal' -import { Submenu } from './menu' import { invoke } from './tauri' /** @@ -138,7 +137,7 @@ export class TrayIcon extends Resource { } /** Sets a new tray icon. If `null` is provided, it will remove the icon. */ - async setIcon(icon: string | Uint8Array | null) { + async setIcon(icon: string | Uint8Array | null): Promise { return invoke('plugin:tray|set_icon', { rid: this.rid, icon }) } @@ -149,7 +148,7 @@ export class TrayIcon extends Resource { * * - **Linux**: once a menu is set it cannot be removed so `null` has no effect */ - async setMenu(menu: Menu | Submenu | null) { + async setMenu(menu: Menu | Submenu | null): Promise { if (menu) { // @ts-expect-error we only need the rid and kind menu = [menu.rid, menu.kind] @@ -164,7 +163,7 @@ export class TrayIcon extends Resource { * * - **Linux:** Unsupported */ - async setTooltip(tooltip: string | null) { + async setTooltip(tooltip: string | null): Promise { return invoke('plugin:tray|set_tooltip', { rid: this.rid, tooltip }) } @@ -180,12 +179,12 @@ export class TrayIcon extends Resource { * on the user's panel. This may not be shown in all visualizations. * - **Windows:** Unsupported */ - async setTitle(title: string | null) { + async setTitle(title: string | null): Promise { return invoke('plugin:tray|set_title', { rid: this.rid, title }) } /** Show or hide this tray icon. */ - async setVisible(visible: boolean) { + async setVisible(visible: boolean): Promise { return invoke('plugin:tray|set_visible', { rid: this.rid, visible }) } @@ -195,12 +194,12 @@ export class TrayIcon extends Resource { * On Linux, we need to write the icon to the disk and usually it will * be `$XDG_RUNTIME_DIR/tray-icon` or `$TEMP/tray-icon`. */ - async setTempDirPath(path: string | null) { + async setTempDirPath(path: string | null): Promise { return invoke('plugin:tray|set_temp_dir_path', { rid: this.rid, path }) } /** Sets the current icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc). **macOS only** */ - async setIconAsTemplate(asTemplate: boolean) { + async setIconAsTemplate(asTemplate: boolean): Promise { return invoke('plugin:tray|set_icon_as_template', { rid: this.rid, asTemplate @@ -208,7 +207,7 @@ export class TrayIcon extends Resource { } /** Disable or enable showing the tray menu on left click. **macOS only**. */ - async setMenuOnLeftClick(onLeft: boolean) { + async setMenuOnLeftClick(onLeft: boolean): Promise { return invoke('plugin:tray|set_show_menu_on_left_click', { rid: this.rid, onLeft @@ -240,7 +239,7 @@ export class TrayIcon extends Resource { return listen( TauriEvent.TRAY, (e) => { - if (e.payload.id == this.id) { + if (e.payload.id === this.id) { handler(e.payload) } },