C.W.K.
Stream
Lesson 01 of 06 · published

창 만들기: WebviewWindow

~12 min · tauri, windows, webviewwindow, rust

Level 0웹 관광객
0 XP0/56 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete
"설정 속 창은 실행할 때 얻는 창이야. 코드 속 창은 네가 정하는 언제든 얻는 창이고."

창 만드는 두 가지 길

창을 tauri.conf.json에 정적으로 선언하거나(시작할 때 열림), Rust에서 WebviewWindowBuilder로 런타임에 만들 수 있어. 정적 경로는 메인 창에 완벽하고, 런타임 경로는 필요할 때 여는 창용이야 — 사용자가 Preferences 클릭하면 뜨는 설정 창, 두 번째 문서 창, 팝업. 둘 다 같은 WebviewWindow 타입을 만들어.

어떤 URL을 보여줘?

창은 뭘 그릴지 알아야 하고, 그게 WebviewUrl이야. WebviewUrl::App("index.html".into())(또는 라우트 경로)는 번들된 프론트엔드를 로드해 — 일반적인 경우. WebviewUrl::External(url)은 창을 원격 사이트로 가리키는데, 조심히 다뤄야 해(원격 콘텐츠 신뢰는 security 트랙에서). WebviewUrl::default()는 앱 진입점을 로드해. 네가 만드는 창은 대부분 라우트랑 App을 써서, 새 창이 네 UI의 특정 화면을 보여줘.

빌더 패턴

WebviewWindowBuilder는 설정을 체이닝해 — title, 크기, resizable — 그리고 .build()로 끝나, 살아있는 창(또는 에러)을 반환해. 빌더를 시작하려면 AppHandle이나 &App이 필요해서, 창 생성은 보통 그걸 손에 쥔 setup, command, 이벤트 핸들러에서 일어나.

Code

런타임에 창 만들기, 멱등하게·rust
use tauri::{WebviewWindowBuilder, WebviewUrl, Manager};

// 필요할 때 설정 창 열기 (예: command에서).
#[tauri::command]
fn open_settings(app: tauri::AppHandle) -> Result<(), String> {
    // 이미 있으면 복제하지 말고 포커스만.
    if let Some(win) = app.get_webview_window("settings") {
        let _ = win.set_focus();
        return Ok(());
    }
    WebviewWindowBuilder::new(
        &app,
        "settings",                                  // label (신원)
        WebviewUrl::App("index.html#/settings".into()), // 번들 UI의 라우트
    )
    .title("Settings")
    .inner_size(520.0, 640.0)
    .resizable(false)
    .build()
    .map_err(|e| e.to_string())?;
    Ok(())
}

External links

Exercise

프론트엔드의 라우트(예: 해시 라우트 #/settings)를 가리키는 두 번째 창을 만드는 open_settings command를 더하고, 멱등하게 해: 창이 이미 있으면 복제 말고 포커스. 버튼을 엮어 invoke해. 멀티윈도우 앱의 토대인 필요시 창 생성을 만든 거야.
Hint
build 전에 if let Some(win) = app.get_webview_window("settings") { win.set_focus(); return Ok(()); }로 가드해. 라우트엔 WebviewUrl::App 써. label "settings"가 가드를 작동시켜.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.