개발/Programming RUST

tauri 개발 따라해보기 11

슈케르 2024. 12. 18. 19:51
728x90

STEP17

이제 거의 마지막이다.

search.rs에서 loc_more = vec![""]  를 봤으면 눈치챘겠지만 그냥 비워두고 있었다.

귀찮아서...

builder 생성시 옵션으로 추가해야하는 값이다. 그냥 비워두게되면 앱 실행시 먹통을

경험하게 될것이다. 왜 RUST compile과정에서 걸러지지 않는지... 

 

그리고, result field에서 더블 클릭시 해당 파일 또는 디렉토리 위치를 파일 탐색기로 열어주는

기능까지 마무리 하려고 한다.

 

1) loc_more

index.html 추가 코드

<button type="button" class="collapsible">more ...  &#8597;</button>      
      <div class="foldcontent" name="extensionDiv">
          <input id="id-input-more1" placeholder="location add..." size="50%"/>
          <button type="button" id="id-button-more1">...</button>
          <div class="small" style="float: right;">
              <label for="name-depth">depth</label>
              <input id="id-input-depth" size="5px"/> &nbsp;&nbsp;
          </div>
          <br>
          <input id="id-input-more2" placeholder="location add..." size="50%"/>
          <button type="button" id="id-button-more2">...</button>
          <div class="small" style="float: right;">
              <label for="name-limit">max count</label>
              <input id="id-input-limit" size="5px"/> &nbsp;&nbsp;
          </div>
          <br>
          <input id="id-input-more3" placeholder="location add..." size="50%"/>
          <button type="button" id="id-button-more3">...</button>
      </div>      
      <br>
      <div style="text-align: left;">
        <label">double click! ==> copy to clipboard or open dialog</label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input class="check2" type="checkbox" id="id-check-opendialog" name="name-check-dialog" checked />
        <label for="name-check-dialog">open folder</label>
      </div>

 

styles.css는 생략한다. github의 소스를 참고.

 

main.js 에서 double click 시 dialog를 열어주는 부분의 구현부는 아래와 같다.

loc_more의 값은 총 3개 허용하도록 하였으니 필요에 따라서 늘리거나 줄여서 사용하면 될것이다.

resultOut.addEventListener("dblclick", (event) => {
  if (resultOut.options.length > 0) {
    var result = event.target.value;
    console.log(result);
  //open dialog
  if (inputCheckDialog.checked == true) {
    invoke("rust_open_dir_dialog_standalone", { path: result });
    toastOn("Open Explorer...", 2); //2 seconds
  } else {
    //copy to clipboard
    navigator.clipboard.writeText(result).then(function() {
    console.log('Async: Copying to clipboard was successful!');
    toastOn("Clipboard copyed OK !", 2);
  }, function(err) {
      console.error('Async: Could not copy text: ', err);
      toastOn("Clipboard copyed FAILED !", 2);
    });
  }
}
});

위 소스에서 "rust_open_dir_dialog_standalone" 의 의미는 dialog를 열되 fastS 앱에 종속되지 않도록

dialog (파일 탐색기)를 열겠다는 뜻이다. 

checkbox 가 unchecked 상태면 clipboard에 경로를 저장한다.

 

search.rs 에서 두가지 형태의 dialog open 코드를 비교해보자

 

rust_open_dir_dialog는 파일/폴더 탐색을 위한 위치 설정시 사용하고 있고,

rust_open_dir_dialog_standalone은 파일/폴더 탐색 후 결과물의 위치를 탐색기로 열기 위한 목적이다.

#[tauri::command]
pub fn rust_open_dir_dialog() -> String {
    //open dialog and directory path return
    let files = FileDialog::new()    
    .set_directory("/")
    .pick_folder();

    match files {
        Some(file) => {
            return format!("{}", file.display().to_string());
        },
        None => "No file selected".to_string(),
    }
}
#[tauri::command]
pub fn rust_open_dir_dialog_standalone(path: &str) {
    let path = PathBuf::from(path);
    let real_dir_path;
    if path.is_dir() {
        println!("is dir");
        real_dir_path = path.to_str().unwrap().to_string();
    } else {
        println!("is not dir");
        real_dir_path = path.parent().unwrap().to_str().unwrap().to_string();
    }
    println!("{}", real_dir_path);
    Command::new("explorer")
        .arg(real_dir_path)
        .spawn()
        .expect("failed to execute process");
}

 

결과물이다.~~

 

 

음.. 

이제 다 짰다.. 내가 쓰기에 별로 불편함은 없다. 잘 돌아가고 빠르다.

 

하지만...

실행할때마다 console에서 cargo tauri dev를 실행하자니 또 번거롭다.

다음 글에서는 배포/설치를 다뤄보고 완전히 마무리를 하겠다.

 

github: https://github.com/kchhero/suker_rust_project/tree/master/fastS

commit : 22d77e5707873d3f01e607f466aca99dbba1764a

728x90

'개발 > Programming RUST' 카테고리의 다른 글

build.rs  (1) 2025.06.02
tauri 개발 따라해보기 12  (0) 2024.12.18
tauri 개발 따라해보기 10  (0) 2024.12.18
tauri 개발 따라해보기 9  (0) 2024.12.18
tauri 개발 따라해보기 8  (0) 2024.12.18