简体   繁体   English

如何正确地将方法句柄移动到模块中?

[英]How do you properly move the handle of a method into a module?

Questions问题

I'm new to Rust我是 Rust 的新用户

  1. I'm trying to understand how do you move the handle (closure) of a method into a module (separate file) in order to split large chunks of code into something manageable?我试图了解如何将方法的句柄(闭包)移动到模块(单独的文件)中,以便将大块代码拆分为可管理的代码? In particular, in the code below, I want to move the handle of .on_system_tray_event() method into the system_tray module.特别是,在下面的代码中,我想将.on_system_tray_event()方法的句柄移动到system_tray模块中。

  2. Did I move the handle of .system_tray() method into the system_tray module correctly or is there a better way to do this?我是否将.system_tray()方法的句柄正确地移动到system_tray模块中,或者是否有更好的方法来执行此操作?

Code代码

src/main.rs

use tauri::{SystemTrayEvent, Manager};

mod system_tray;

fn main() {
  tauri::Builder::default()
    .system_tray(system_tray::get_system_tray())
    .on_system_tray_event(|app, event| match event {
      SystemTrayEvent::LeftClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a left click");
      }
      SystemTrayEvent::RightClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a right click");
      }
      SystemTrayEvent::DoubleClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a double click");
      }
      SystemTrayEvent::MenuItemClick { id, .. } => {
        match id.as_str() {
          "quit" => {
            std::process::exit(0);
          }
          "hide" => {
            let window = app.get_window("main").unwrap();
            window.hide().unwrap();
          }
          _ => {}
        }
      }
      _ => {}
    })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

src/system_tray.rs

use tauri::{SystemTray, CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};

pub fn get_system_tray () -> SystemTray {
  let quit = CustomMenuItem::new("quit".to_string(), "Quit");
  let hide = CustomMenuItem::new("hide".to_string(), "Hide");
  let tray_menu = SystemTrayMenu::new()
    .add_item(quit)
    .add_native_item(SystemTrayMenuItem::Separator)
    .add_item(hide);
  
  let system_tray = SystemTray::new().with_menu(tray_menu);
  return system_tray
}

pub on_system_tray_event_handler () {
  ...
}

The handle in on_system_tray_event is just a closure. on_system_tray_event中的句柄只是一个闭包。 So just cut and paste everything after |app, event|所以只需在|app, event|之后剪切并粘贴所有内容即可into its own function, then call that function. Short and sweet:进入它自己的 function,然后调用它 function。简短而甜蜜:

some_function.some_handle_stuff(|x, y| A_BIG_BLOCK_FO_CODE);

becomes成为

some_function.some_handle_stuff(|x, y| a_function(x, y));

fn a_function(x: some_type, y: another_type) {
  A_BIG_BLOCK_OF_CODE
}

Now just make sure the argument types and return types make sense and you're good to go. Then as a next step you can move that function into a different module.现在只需确保参数类型和返回类型有意义,并且您可以使用 go。然后作为下一步,您可以将 function 移动到不同的模块中。

EDIT: Note, this doesn't work if the BIG_BLOCK_OF_CODE for the closure "captures the environment".编辑:请注意,如果闭包的 BIG_BLOCK_OF_CODE “捕获环境”,这将不起作用。 On quick glance, that doesn't seem to be the case here.乍一看,这里似乎并非如此。 Otherwise you'll need to add the salient parts of the environment to the function arguments.否则,您需要将环境的显着部分添加到 function arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM