Skip to content

Commit

Permalink
feat: migrate V1 codebase for V2
Browse files Browse the repository at this point in the history
Signed-off-by: Martichou <[email protected]>
  • Loading branch information
Martichou committed Jun 24, 2024
1 parent ba4f16f commit a3226fc
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 49 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"rust-analyzer.linkedProjects": [
"./core_lib/Cargo.toml",
"./app/legacy/src-tauri/Cargo.toml",
"./app/main/src-tauri/Cargo.toml",
],
}
10 changes: 7 additions & 3 deletions app/main/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ rqs_lib = { path = "../../../core_lib", features = ["experimental"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sys_metrics = "0.2"
tauri = { version = "2.0.0-beta", features = [ "devtools", "tray-icon", "native-tls-vendored"] }
tauri = { version = "2.0.0-beta", features = [ "devtools", "tray-icon", "native-tls-vendored", "image-png"] }
tauri-plugin-autostart = "2.0.0-beta"
tauri-plugin-process = "2.0.0-beta"
tauri-plugin-notification = "2.0.0-beta"
tauri-plugin-single-instance = "2.0.0-beta"
tauri-plugin-store = "2.0.0-beta"
time = { version = "0.3", features = ["formatting"] }
tokio = { version = "1.25", features = ["macros", "rt", "rt-multi-thread", "net", "sync", "time", "io-util"] }
tokio-util = { version = "0.7", features = ["rt"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = ["tauri/custom-protocol"]

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion app/main/src-tauri/gen/schemas/acl-manifests.json

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions app/main/src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,55 @@
"menu:deny-text"
]
},
{
"description": "notification:default -> Allows requesting permission, checking permission state and sending notifications",
"type": "string",
"enum": [
"notification:default"
]
},
{
"description": "notification:allow-is-permission-granted -> Enables the is_permission_granted command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-is-permission-granted"
]
},
{
"description": "notification:allow-notify -> Enables the notify command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-notify"
]
},
{
"description": "notification:allow-request-permission -> Enables the request_permission command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-request-permission"
]
},
{
"description": "notification:deny-is-permission-granted -> Denies the is_permission_granted command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-is-permission-granted"
]
},
{
"description": "notification:deny-notify -> Denies the notify command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-notify"
]
},
{
"description": "notification:deny-request-permission -> Denies the request_permission command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-request-permission"
]
},
{
"description": "path:default -> Default permissions for the plugin.",
"type": "string",
Expand Down
49 changes: 49 additions & 0 deletions app/main/src-tauri/gen/schemas/linux-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,55 @@
"menu:deny-text"
]
},
{
"description": "notification:default -> Allows requesting permission, checking permission state and sending notifications",
"type": "string",
"enum": [
"notification:default"
]
},
{
"description": "notification:allow-is-permission-granted -> Enables the is_permission_granted command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-is-permission-granted"
]
},
{
"description": "notification:allow-notify -> Enables the notify command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-notify"
]
},
{
"description": "notification:allow-request-permission -> Enables the request_permission command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:allow-request-permission"
]
},
{
"description": "notification:deny-is-permission-granted -> Denies the is_permission_granted command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-is-permission-granted"
]
},
{
"description": "notification:deny-notify -> Denies the notify command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-notify"
]
},
{
"description": "notification:deny-request-permission -> Denies the request_permission command without any pre-configured scope.",
"type": "string",
"enum": [
"notification:deny-request-permission"
]
},
{
"description": "path:default -> Default permissions for the plugin.",
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion app/main/src-tauri/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::time::SystemTime;

use fern::colors::{Color, ColoredLevelConfig};
use tauri::AppHandle;
use tauri::Manager;
use time::OffsetDateTime;

use crate::store::get_logging_level;
Expand Down Expand Up @@ -58,7 +59,7 @@ pub fn set_up_logging(app_handle: &AppHandle) -> Result<(), anyhow::Error> {
.level_for("polling", log::LevelFilter::Error)
.chain(std::io::stdout());

if let Some(path) = app_handle.path_resolver().app_log_dir() {
if let Ok(path) = app_handle.path().app_log_dir() {
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
Expand Down
83 changes: 41 additions & 42 deletions app/main/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use std::sync::{Arc, Mutex};
use rqs_lib::channel::{ChannelDirection, ChannelMessage};
use rqs_lib::{EndpointInfo, SendInfo, State, Visibility, RQS};
use tauri::{
AppHandle, CustomMenuItem, GlobalWindowEvent, Manager, SystemTray, SystemTrayEvent,
SystemTrayMenu, SystemTrayMenuItem,
image::Image,
menu::{MenuBuilder, MenuItemBuilder},
tray::TrayIconBuilder,
AppHandle, Manager, Window, WindowEvent,
};
use tauri_plugin_autostart::MacosLauncher;
use tokio::sync::{broadcast, mpsc, watch};
Expand Down Expand Up @@ -42,19 +44,13 @@ async fn main() -> Result<(), anyhow::Error> {
// Define tauri async runtime to be tokio
tauri::async_runtime::set(tokio::runtime::Handle::current());

// Configure System Tray
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("rqs", "RQuickShare").disabled())
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("show".to_string(), "Show"))
.add_item(CustomMenuItem::new("quit".to_string(), "Quit"));

// Build and run Tauri app
tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
println!("{}, {argv:?}, {cwd}", app.package_info().name);
}))
Expand All @@ -74,6 +70,35 @@ async fn main() -> Result<(), anyhow::Error> {
set_up_logging(&app.app_handle())?;
debug!("Starting setup of RQuickShare app");

// Initialize system Tray
let show = MenuItemBuilder::with_id("show", "Show").build(app)?;
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?;
let _tray = TrayIconBuilder::new()
.icon(Image::from_bytes(include_bytes!("../icons/icon.png")).unwrap())
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
"show" => {
if let Some(webview_window) = app.get_webview_window("main") {
let _ = webview_window.show();
let _ = webview_window.set_focus();
}
}
"quit" => {
tokio::task::block_in_place(|| {
tauri::async_runtime::block_on(async move {
let state: tauri::State<'_, AppState> = app.state();
let _ = state.rqs.lock().unwrap().stop().await;

app.app_handle().cleanup_before_exit();
std::process::exit(0);
});
});
}
_ => (),
})
.build(app)?;

// Initialize default values for the store
init_default(&app.app_handle());

Expand Down Expand Up @@ -108,8 +133,6 @@ async fn main() -> Result<(), anyhow::Error> {
spawn_receiver_tasks(&app.app_handle());
Ok(())
})
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(handle_system_tray_event)
.on_window_event(handle_window_event)
.build(tauri::generate_context!())
.expect("error while building tauri application")
Expand Down Expand Up @@ -216,41 +239,17 @@ fn spawn_receiver_tasks(app_handle: &AppHandle) {
});
}

fn handle_system_tray_event(app: &tauri::AppHandle, event: SystemTrayEvent) {
if let SystemTrayEvent::MenuItemClick { id, .. } = event {
match id.as_str() {
"show" => {
if let Some(webview_window) = app.get_window("main") {
let _ = webview_window.show();
let _ = webview_window.set_focus();
}
}
"quit" => {
tokio::task::block_in_place(|| {
tauri::async_runtime::block_on(async move {
let state: tauri::State<'_, AppState> = app.state();
let _ = state.rqs.lock().unwrap().stop().await;

app.app_handle().exit(0);
});
});
}
_ => {}
}
}
}

fn handle_window_event(event: GlobalWindowEvent) {
if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() {
if !get_realclose(&event.window().app_handle()) {
fn handle_window_event(w: &Window, event: &WindowEvent) {
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
if !get_realclose(w.app_handle()) {
trace!("Prevent close");
event.window().hide().unwrap();
w.hide().unwrap();
api.prevent_close();
} else {
trace!("Real close");
tokio::task::block_in_place(|| {
tauri::async_runtime::block_on(async move {
let app_handle = event.window().app_handle();
let app_handle = w.app_handle();
let state: tauri::State<'_, AppState> = app_handle.state();
let _ = state.rqs.lock().unwrap().stop().await;

Expand All @@ -267,12 +266,12 @@ fn rs2js_channelmessage<R: tauri::Runtime>(message: ChannelMessage, manager: &im
}

info!("rs2js_channelmessage: {:?}", &message);
manager.emit_all("rs2js_channelmessage", &message).unwrap();
manager.emit("rs2js_channelmessage", &message).unwrap();
}

fn rs2js_endpointinfo<R: tauri::Runtime>(message: EndpointInfo, manager: &impl Manager<R>) {
info!("rs2js_endpointinfo: {:?}", &message);
manager.emit_all("rs2js_endpointinfo", &message).unwrap();
manager.emit("rs2js_endpointinfo", &message).unwrap();
}

#[tauri::command]
Expand Down
2 changes: 1 addition & 1 deletion app/main/src-tauri/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn set_visibility(app_handle: &AppHandle, v: Visibility) -> Result<(), anyho
|store| store.insert("visibility".to_owned(), JsonValue::Number((v as u8).into())),
)?;

app_handle.emit_all("visibility_updated", ())?;
app_handle.emit("visibility_updated", ())?;
Ok(())
}

Expand Down
15 changes: 14 additions & 1 deletion fmt.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

for dir in core_lib app/legacy/src-tauri; do
for dir in core_lib app/legacy/src-tauri app/main/src-tauri; do
find "$dir" -name '*.rs' -not -path "*/target/*" -exec rustfmt {} +
done

Expand All @@ -15,4 +15,17 @@ if git diff --name-only HEAD | grep '^app/legacy/src/' >/dev/null; then
pnpm lint --fix
else
echo "No changes detected in /app/legacy/src."
fi

# Check if there are changes in the /app/main/src directory
if git diff --name-only HEAD | grep '^app/main/src/' >/dev/null; then
echo "Changes detected in /app/main/src. Executing script..."

# Navigate to the app/main/src-tauri directory
cd app/main/src-tauri

# Run your commands
pnpm lint --fix
else
echo "No changes detected in /app/main/src."
fi

0 comments on commit a3226fc

Please sign in to comment.