1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use tauri;
use tauri::Manager;

mod wallpaper;

pub struct Wallpaper<R: tauri::Runtime> {
  invoke_handler: Box<dyn Fn(tauri::Invoke<R>) + Send + Sync>,
}

#[tauri::command]
// invoke('plugin:wallpaper|attach')
fn attach<R: tauri::Runtime>(app_handle: tauri::AppHandle<R>, window_label: String) {
  let window = app_handle.get_window(&window_label).expect("window not found");
  Wallpaper::attach(&window);
}

#[tauri::command]
// invoke('plugin:wallpaper|detach')
fn detach<R: tauri::Runtime>(app_handle: tauri::AppHandle<R>, window_label: String) {
  let window = app_handle.get_window(&window_label).expect("window not found");
  Wallpaper::detach(&window);
}

impl<R: tauri::Runtime> Wallpaper<R> {
  pub fn init() -> Self {
    Self {
      invoke_handler: Box::new(tauri::generate_handler![
        attach,
        detach,
      ]),
    }
  }

  pub fn attach(window: &tauri::Window<R>) {
    if cfg!(target_os = "windows") {
      let hwnd = window.hwnd().unwrap();
      wallpaper::attach(hwnd);
    } else {
      panic!("attach not implemented for this platform");
    }
  }

  pub fn detach(window: &tauri::Window<R>) {
    if cfg!(target_os = "windows") {
      let hwnd = window.hwnd().unwrap();
      wallpaper::detach(hwnd);
    } else {
      panic!("detach not implemented for this platform");
    }
  }
}

impl<R: tauri::Runtime> tauri::plugin::Plugin<R> for Wallpaper<R> {
  fn name(&self) -> &'static str {
    "wallpaper"
  }

  fn extend_api(&mut self, message: tauri::Invoke<R>) {
    (self.invoke_handler)(message)
  }
}