Skip to content

Commit

Permalink
feat: implement ipc to retain single instance
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Sep 23, 2024
1 parent e0121dc commit d229a9d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
28 changes: 28 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ arboard = { version = "3.4.0", features = ["wayland-data-control", "wl-clipboard
chrono = "0.4.38"
display-info = { version = "0.5.1"}
iced = { version = "0.13", features = ["advanced", "canvas", "multi-window", "image", "tokio", "svg"] }
interprocess = { version = "2.2.1", features = ["tokio"] }
rdev = { git = "https://github.com/rustdesk-org/rdev", branch = "master"}
rfd = { version = "0.14", features = ["gtk3", "tokio"], default-features = false }
serde = { version = "1.0", features = ["derive"] }
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod windows;

use std::collections::BTreeMap;

use assets::{FONT_BOLD, FONT_MEDIUM, ICON, MEDIUM};
use assets::{APPNAME, FONT_BOLD, FONT_MEDIUM, ICON, MEDIUM};
use entities::{
app::{App, AppEvent},
config::{Config, ConfigureWindow},
Expand All @@ -27,13 +27,19 @@ use iced::{
},
Size, Subscription, Task,
};
use interprocess::local_socket::{traits::Stream, GenericNamespaced, ToNsName};
use style::Element;
use utils::{
capture::{fullscreen::capture_fullscreen, window::capture_window},
key_listener::global_key_listener, tray_icon::{tray_icon, tray_icon_listener, tray_menu_listener},
capture::{fullscreen::capture_fullscreen, window::capture_window}, ipc::ipc, key_listener::global_key_listener, tray_icon::{tray_icon, tray_icon_listener, tray_menu_listener}
};

pub fn main() -> Result<(), iced::Error> {

let name = APPNAME.to_ns_name::<GenericNamespaced>().unwrap();

if let Ok(_) = interprocess::local_socket::Stream::connect(name) {
return Ok(())
};
daemon(App::title, App::update, App::view)
.font(FONT_MEDIUM)
.font(FONT_BOLD)
Expand Down Expand Up @@ -92,7 +98,7 @@ impl App {
ConfigureWindow::new(&self.config)
)
);
open_task.discard()
open_task.discard().chain(gain_focus(id))
} else {
Task::none()
}
Expand Down Expand Up @@ -204,6 +210,8 @@ impl App {

let tray_menu_listener = Subscription::run(tray_menu_listener);

Subscription::batch([window_events, app_key_listener, global_key_listener, tray_icon_listener, tray_menu_listener])
let ipc = Subscription::run(ipc);

Subscription::batch([window_events, app_key_listener, global_key_listener, tray_icon_listener, tray_menu_listener, ipc])
}
}
26 changes: 26 additions & 0 deletions src/utils/ipc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use iced::{futures::{SinkExt, Stream}, stream};
use interprocess::local_socket::{traits::tokio::Listener, GenericNamespaced, ListenerOptions, ToNsName};

use crate::{assets::APPNAME, entities::app::AppEvent};

pub fn ipc() -> impl Stream<Item = AppEvent> {
stream::channel(
10,
|mut output| async move {

let name = APPNAME.to_ns_name::<GenericNamespaced>().unwrap();

let listner_opts = ListenerOptions::new().name(name);

let listener = listner_opts.create_tokio().unwrap();


loop {
if let Ok(_stream) = listener.accept().await {
output.send(AppEvent::OpenConfigureWindow).await.unwrap();
}
}

}
)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use iced::Point;

pub mod capture;
pub mod config;
pub mod ipc;
pub mod key_listener;
pub mod tray_icon;

Expand Down

0 comments on commit d229a9d

Please sign in to comment.