Skip to content

Commit

Permalink
Merge pull request #385 from perspect3vism/tray_message
Browse files Browse the repository at this point in the history
Tray message popup
  • Loading branch information
lucksus committed Jun 19, 2023
2 parents 9306ea7 + 9765ae2 commit 8c4965b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 6 deletions.
28 changes: 27 additions & 1 deletion ui/src-tauri/src/commands/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
extern crate remove_dir_all;
use std::time::{Duration, SystemTime};

use crate::Payload;
use crate::{config::data_path, get_main_window, util::find_and_kill_processes};

use remove_dir_all::*;

use tauri::LogicalSize;
use tauri::{LogicalSize, Manager};
use tauri::Size;
use tauri_plugin_positioner::{Position, WindowExt};

Expand Down Expand Up @@ -37,6 +40,29 @@ pub fn open_tray(app_handle: tauri::AppHandle) {
}
}

#[tauri::command]
pub fn open_tray_message(app_handle: tauri::AppHandle) {
let tray_window = app_handle.get_window("TrayMessage").unwrap();
let _ = tray_window.show();

let _ = tray_window.emit("tray_message_open", Payload {message: "".to_string()});

let seconds = Duration::from_secs(5);

let start = SystemTime::now();
loop {
std::thread::sleep(Duration::new(5, 0));

match start.elapsed() {
Ok(elapsed) if elapsed > seconds => {
let _ = tray_window.hide();
return;
}
_ => (),
}
}
}

#[tauri::command]
pub fn clear_state(app_handle: tauri::AppHandle) {
find_and_kill_processes("ad4m-host");
Expand Down
10 changes: 10 additions & 0 deletions ui/src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ pub fn app_url() -> String {
"http://127.0.0.1:3000".to_string()
}

#[cfg(feature = "custom-protocol")]
pub fn app_tray_message_url() -> String {
"index.html/tray_message".to_string()
}

#[cfg(not(feature = "custom-protocol"))]
pub fn app_tray_message_url() -> String {
"http://127.0.0.1:3000/tray_message".to_string()
}

pub fn executor_port_path() -> PathBuf {
data_path().join("executor-port")
}
8 changes: 6 additions & 2 deletions ui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ use tauri::api::dialog;
use tauri::Manager;
use crate::commands::proxy::{get_proxy, login_proxy, setup_proxy, stop_proxy};
use crate::commands::state::{get_port, request_credential};
use crate::commands::app::{close_application, close_main_window, clear_state, open_tray};
use crate::commands::app::{close_application, close_main_window, clear_state, open_tray, open_tray_message};
use crate::config::data_path;
use crate::util::create_tray_message_windows;
use crate::util::find_port;
use crate::menu::{handle_menu_event, open_logs_folder};
use crate::util::has_processes_running;
Expand Down Expand Up @@ -128,10 +129,12 @@ fn main() {
close_application,
close_main_window,
clear_state,
open_tray
open_tray,
open_tray_message
])
.setup(move |app| {
// Hides the dock icon
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);

let splashscreen = app.get_window("splashscreen").unwrap();
Expand Down Expand Up @@ -166,6 +169,7 @@ fn main() {
let url = app_url();
log::info!("Executor started on: {:?}", url);
let _ = splashscreen_clone.hide();
create_tray_message_windows(&handle);
let main = get_main_window(&handle);
main.emit("ready", Payload { message: "ad4m-executor is ready".into() }).unwrap();
}
Expand Down
24 changes: 24 additions & 0 deletions ui/src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::app_url;
use crate::config::app_tray_message_url;
use crate::config::executor_port_path;
use crate::menu::open_logs_folder;
use std::fs::remove_file;
use std::fs::File;
use std::io::prelude::*;
use std::time::Duration;
use std::time::SystemTime;
use sysinfo::Process;
use sysinfo::{ProcessExt, Signal, System, SystemExt};
use tauri::{AppHandle, Manager, WindowBuilder, WindowEvent, WindowUrl, Wry};
use tauri_plugin_positioner::Position;
use tauri_plugin_positioner::WindowExt;

pub fn find_port(start_port: u16, end_port: u16) -> u16 {
for x in start_port..end_port {
Expand Down Expand Up @@ -81,6 +86,25 @@ pub fn create_main_window(app: &AppHandle<Wry>) {
});
}


pub fn create_tray_message_windows(app: &AppHandle<Wry>) {
let url = app_tray_message_url();

let new_ad4m_window = WindowBuilder::new(app, "TrayMessage", WindowUrl::App(url.into()))
.center()
.focused(false)
.inner_size(300.0, 80.0)
.title("TrayMessage")
.visible(false);

let _ = new_ad4m_window.build();

let tray_window = app.get_window("TrayMessage").unwrap();
let _ = tray_window.move_window(Position::TopRight);
let _ = tray_window.set_decorations(false);
let _ = tray_window.set_always_on_top(true);
}

pub fn save_executor_port(port: u16) {
let _ = remove_file(executor_port_path());

Expand Down
2 changes: 2 additions & 0 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Settings from "./components/Settings";
import { appWindow } from "@tauri-apps/api/window";
import { Connect } from "./components/Connect";
import Apps from "./components/Apps";
import TrayMessage from "./components/TrayMessage";

const App = () => {
const [opened, setOpened] = useState(false);
Expand Down Expand Up @@ -45,6 +46,7 @@ const App = () => {
<div className="App">
<Routes>
<Route path="/splashscreen" element={<Splashscreen />} />
<Route path="/tray_message" element={<TrayMessage />} />
<Route
path="/login"
element={
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/Splashscreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Splashscreen() {
error.style.opacity = "1";
error.style.height = "160px";
}
}, 13000);
}, 30000);
}, []);

return (
Expand Down
35 changes: 35 additions & 0 deletions ui/src/components/TrayMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useEffect, useState } from 'react'
import { splashscreenContainer, splashscreenError, splashscreenErrorFlex } from "./styles";
import { appWindow } from '@tauri-apps/api/window';

export default function TrayMessage() {
const [count, setCount] = useState(5);

useEffect(() => {
appWindow.listen('tray_message_open', () => {
let i = 5;
const interval = setInterval(() => {
if (count === 0) {
clearInterval(interval)
} else {
i -= 1;
setCount(i)
}

}, 1000)
})
}, [])

return (
<div style={splashscreenContainer}>
<div style={{padding: "20px 20px 0 20px"}}>
<j-text size="400" variant="ingress">
Ad4m launcher is minimized, click on the tray icon to open it.
</j-text>
<j-text size="300" variant="ingress">
This popup will automatically close in {count}
</j-text>
</div>
</div>
)
}
4 changes: 2 additions & 2 deletions ui/src/context/AgentContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function AgentProvider({ children }: any) {
setLoading(false);

await invoke('close_main_window');
await invoke('open_tray');
await invoke('open_tray_message');
await invoke('login_proxy', { subdomain: agentStatus.did! });

navigate('/apps');
Expand All @@ -117,7 +117,7 @@ export function AgentProvider({ children }: any) {
handleLogin(client!, agentStatus!.isUnlocked, agentStatus!.did!);
console.log("agent status in unlock: ", agentStatus);
await invoke('close_main_window');
await invoke('open_tray');
await invoke('open_tray_message');
await invoke('login_proxy', { subdomain: agentStatus!.did });
navigate('/apps');
} else {
Expand Down

0 comments on commit 8c4965b

Please sign in to comment.