Skip to content

Commit

Permalink
Dialog methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
smh0505 committed Sep 12, 2024
1 parent bc20494 commit bdea5d4
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 17 deletions.
14 changes: 14 additions & 0 deletions file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tauri + Vue + Typescript App</title>
</head>

<body>
<div id="file"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sea-orm = { version = "1.0.1", features = [
"macros",
] }
sea-orm-migration = "1.0.1"
chrono = "0.4.38"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
18 changes: 18 additions & 0 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ impl From<sea_orm::DbErr> for BackendError {
}
}
}

impl From<tauri::Error> for BackendError {
fn from(value: tauri::Error) -> Self {
Self {
kind: String::from("tauri::Error"),
message: value.to_string(),
}
}
}

impl From<window_shadows::Error> for BackendError {
fn from(value: window_shadows::Error) -> Self {
Self {
kind: String::from("window_shadows::Error"),
message: value.to_string(),
}
}
}
44 changes: 38 additions & 6 deletions src-tauri/src/game.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::path::Path;

use chrono::Local;
use sea_orm::*;

use crate::{
Expand All @@ -6,18 +9,19 @@ use crate::{
AppState, Response,
};

pub struct GameQuery;
struct GameQuery;

impl GameQuery {
async fn create(db: &DbConn, form_data: game::Model) -> Result<game::ActiveModel, DbErr> {
game::ActiveModel {
let model = game::ActiveModel {
name: Set(form_data.name.to_owned()),
directory: Set(form_data.directory.to_owned()),
installed: Set(form_data.installed.to_owned()),
playtime: Set(form_data.playtime.to_owned()),
..Default::default()
}
.save(db)
.await
};
Game::insert(model.clone()).exec(db).await?;
Ok(model)
}

async fn read_page(
Expand All @@ -31,13 +35,41 @@ impl GameQuery {
let num_pages = paginator.num_pages().await?;
paginator.fetch_page(page - 1).await.map(|p| (p, num_pages))
}

async fn delete(db: &DbConn, id: i32) -> Result<DeleteResult, DbErr> {
let game: game::ActiveModel = Game::find_by_id(id)
.one(db)
.await?
.ok_or(DbErr::Custom(format!("Cannot find game with id {}", id)))
.map(Into::into)?;
game.delete(db).await
}
}

#[tauri::command]
pub async fn add_game(
state: tauri::State<'_, AppState>,
form: game::Model,
file: &str,
) -> Result<Response, BackendError> {
let dir = Path::new(file);
let form = game::Model {
id: 0,
name: dir
.file_stem()
.unwrap()
.to_os_string()
.into_string()
.unwrap(),
caption: None,
description: None,
developer: None,
image: None,
directory: dir.to_str().unwrap().to_owned(),
executive: None,
installed: Local::now().naive_local(),
playtime: 0,
};

GameQuery::create(&state.conn, form).await?;

Ok(Response {
Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod entities;
mod error;
mod game;
mod migration;
mod window;

use core::panic;
use migration::Migrator;
Expand Down Expand Up @@ -38,7 +39,12 @@ fn main() {
Ok(())
})
.manage(state)
.invoke_handler(tauri::generate_handler![game::add_game, game::list_games])
.invoke_handler(tauri::generate_handler![
game::add_game,
game::list_games,
window::open_file_dialog,
window::close_file_dialog
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Expand Down
34 changes: 34 additions & 0 deletions src-tauri/src/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use tauri::Manager;
use window_shadows::set_shadow;

use crate::{error::BackendError, Response};

#[tauri::command]
pub async fn open_file_dialog(handle: tauri::AppHandle) -> Result<Response, BackendError> {
let dialog = tauri::WindowBuilder::new(&handle, "file", tauri::WindowUrl::App("file.html".into()))
.decorations(false)
.build()?;

set_shadow(dialog, true)?;

Ok(Response {
kind: "success".to_owned(),
message: "Window::OpenFileDialog successful".to_owned(),
})
}

#[tauri::command]
pub async fn close_file_dialog(handle: tauri::AppHandle) -> Result<Response, BackendError> {
handle
.get_window("file")
.ok_or(BackendError {
kind: "tauri::Error".to_owned(),
message: "the window with the label `file` not found".to_owned(),
})?
.close()?;

Ok(Response {
kind: "success".to_owned(),
message: "Window::CloseFileDialog successful".to_owned(),
})
}
31 changes: 23 additions & 8 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
<template>
<main :data-theme="theme ? 'dark' : 'light'" class="w-screen h-screen">
<Titlebar :theme="theme" @set-theme="setTheme"></Titlebar>
<button class="btn btn-primary" @click="openDialog">Open Window</button>
<button class="btn btn-primary" @click="closeDialog">Close Window</button>
</main>
</template>

<script setup lang="ts">
import Titlebar from './components/Titlebar.vue';
import { onMounted, ref } from 'vue';
import { invoke } from "@tauri-apps/api/tauri";
import Titlebar from "./components/Titlebar.vue";
import { onMounted, ref } from "vue";
const theme = ref(false)
const theme = ref(false);
function setTheme(value: boolean) {
theme.value = value
if (theme.value) localStorage.setItem("theme", "dark")
else localStorage.setItem("theme", "light")
theme.value = value;
if (theme.value) localStorage.setItem("theme", "dark");
else localStorage.setItem("theme", "light");
}
function openDialog() {
invoke("open_file_dialog")
.then((res) => console.log(res))
.catch((err) => console.error(err));
}
function closeDialog() {
invoke("close_file_dialog")
.then((res) => console.log(res))
.catch((err) => console.error(err));
}
onMounted(() => {
theme.value = localStorage.getItem("theme") === "dark"
})
theme.value = localStorage.getItem("theme") === "dark";
});
</script>
15 changes: 15 additions & 0 deletions src/File.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<main :data-theme="theme ? 'dark' : 'light'" class="w-screen h-screen">
Hello world!
</main>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
const theme = ref(false);
onMounted(() => {
theme.value = localStorage.getItem("theme") === "dark";
})
</script>
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createApp } from "vue";
import App from "./App.vue";
import "./style.css"
import File from "./File.vue";
import "./style.css";

createApp(App).mount("#app");
if (document.querySelector("#app")) createApp(App).mount("#app");
if (document.querySelector("#file")) createApp(File).mount("#file");
9 changes: 9 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ export default defineConfig(async () => ({
ignored: ["**/src-tauri/**"],
},
},

build: {
rollupOptions: {
input: {
index: "./index.html",
openfile: "./file.html",
},
},
},
}));

0 comments on commit bdea5d4

Please sign in to comment.