Skip to content

Commit

Permalink
Add Sonner for notifications and implement window management features
Browse files Browse the repository at this point in the history
  • Loading branch information
akash2061 committed Nov 3, 2024
1 parent 17dc69b commit ea3a52a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 7 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"@tauri-apps/api": "^2.0.3",
"@tauri-apps/plugin-updater": "^2.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"sonner": "^1.6.1"
},
"devDependencies": {
"@tauri-apps/cli": "^2",
Expand All @@ -23,4 +24,4 @@
"tailwindcss": "^3.4.14",
"vite": "^5.3.1"
}
}
}
5 changes: 3 additions & 2 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 @@ -23,3 +23,4 @@ tauri-plugin-shell = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
rodio = "0.19.0"
tokio = "1.41.0"
29 changes: 27 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rodio::{Decoder, OutputStream, Sink};
use std::io::Cursor;
use tauri::{AppHandle, Builder};
use tauri::{AppHandle, Builder, Manager};

#[tauri::command]
async fn play_bell_sound(_app_handle: AppHandle) {
Expand All @@ -14,11 +14,36 @@ async fn play_bell_sound(_app_handle: AppHandle) {
sink.append(source);
sink.sleep_until_end();
}
#[tauri::command]
async fn bring_window_to_front(app: AppHandle) {
if let Some(window) = app.get_window("main") {
// Unminimize the window if it's minimized
if let Ok(is_minimized) = window.is_minimized() {
if is_minimized {
window.unminimize().unwrap();
window.set_always_on_top(true).unwrap();
window.set_focus().unwrap();
}
}

// Set window to always on top and focus it
window.set_always_on_top(true).unwrap();
window.set_focus().unwrap();

// Optionally, remove the always-on-top after a short delay
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
window.set_always_on_top(false).unwrap();
});
}
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
Builder::default()
.invoke_handler(tauri::generate_handler![play_bell_sound,])
.invoke_handler(tauri::generate_handler![
play_bell_sound,
bring_window_to_front
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2 changes: 2 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"shadow": false,
"decorations": false,
"center": true,
"focus": true,
"alwaysOnTop": false,
"windowEffects": {
"effects": [
"blur"
Expand Down
2 changes: 2 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./App.css";
import Timer from "./components/Timer";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { Toaster } from 'sonner';

function App() {
const minimizeWebview = async () => {
Expand All @@ -27,6 +28,7 @@ function App() {
</div>
</div>
<Timer />
<Toaster richColors position="top-center" />
</div>
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/components/Timer.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect } from 'react';
import { invoke } from '@tauri-apps/api/core';
import { toast } from 'sonner';

const Timer = () => {
const [timeLeft, setTimeLeft] = useState(5); // Testing == 5seconds
Expand All @@ -15,6 +16,16 @@ const Timer = () => {
}, 1000);
return () => clearInterval(timer);
} else {
if (timerName !== 'Pomodoro Timer') {
toast.success(`${timerName} is done!`, {
duration: 3000
});
} else {
toast.success('Time to take a break!', {
duration: 3000
});
}
invoke('bring_window_to_front');
invoke('play_bell_sound');
}
}, [timeLeft]);
Expand Down
11 changes: 11 additions & 0 deletions src/components/win.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Foo
</body>
</html>

0 comments on commit ea3a52a

Please sign in to comment.