diff --git a/crates/client/lang/en/common.txt b/crates/client/lang/en/common.txt index 2143ae1..c87fdf3 100644 --- a/crates/client/lang/en/common.txt +++ b/crates/client/lang/en/common.txt @@ -22,3 +22,4 @@ more = {count} more messages = Messages no_users = No users no_bans = No bans +confirm = Confirm diff --git a/crates/client/lang/en/community_settings.txt b/crates/client/lang/en/community_settings.txt index a5b78c0..808084d 100644 --- a/crates/client/lang/en/community_settings.txt +++ b/crates/client/lang/en/community_settings.txt @@ -5,7 +5,6 @@ voice = Voice bans = Bans logs = Logs moderation = Moderation -leave_community = Leave community delete_community = Delete community newest = Newest oldest = Oldest diff --git a/crates/client/lang/en/profile.txt b/crates/client/lang/en/profile.txt index 8286fe1..ad8287e 100644 --- a/crates/client/lang/en/profile.txt +++ b/crates/client/lang/en/profile.txt @@ -6,7 +6,6 @@ kick = Kick ban = Ban make_rank = Make {rank} delete_user = Delete user -confirm = Confirm a_call = a call in_room = in {room} edit_status = Edit status diff --git a/crates/client/lang/en/settings_page.txt b/crates/client/lang/en/settings_page.txt index 8946d52..01fb7ee 100644 --- a/crates/client/lang/en/settings_page.txt +++ b/crates/client/lang/en/settings_page.txt @@ -7,9 +7,8 @@ identity = Identity name = Name status = Status presence = Presence -log_out = Log out +danger_zone = Danger zone delete_account = Delete account -confirm = Confirm save = Save remove = Remove change_picture = Change picture diff --git a/crates/client/lang/tr/common.txt b/crates/client/lang/tr/common.txt index 786cfad..b91a54c 100644 --- a/crates/client/lang/tr/common.txt +++ b/crates/client/lang/tr/common.txt @@ -22,3 +22,4 @@ more = {count} daha messages = Mesajlar no_users = Kullanıcı yok no_bans = Yasak yok +confirm = Onayla diff --git a/crates/client/lang/tr/community_settings.txt b/crates/client/lang/tr/community_settings.txt index 1cd4e5f..0ddd023 100644 --- a/crates/client/lang/tr/community_settings.txt +++ b/crates/client/lang/tr/community_settings.txt @@ -5,7 +5,6 @@ voice = Ses bans = Yasaklar logs = Kayıtlar moderation = Moderasyon -leave_community = Topluluktan ayrıl delete_community = Topluluğu sil newest = En yeni oldest = En eski diff --git a/crates/client/lang/tr/profile.txt b/crates/client/lang/tr/profile.txt index 51cb24d..e59ddd9 100644 --- a/crates/client/lang/tr/profile.txt +++ b/crates/client/lang/tr/profile.txt @@ -6,7 +6,6 @@ kick = Çıkar ban = Yasakla make_rank = {rank} yap delete_user = Kullanıcıyı sil -confirm = Onayla a_call = bir arama in_room = {room} içinde edit_status = Durumu düzenle diff --git a/crates/client/lang/tr/settings_page.txt b/crates/client/lang/tr/settings_page.txt index fbc1579..2ce7538 100644 --- a/crates/client/lang/tr/settings_page.txt +++ b/crates/client/lang/tr/settings_page.txt @@ -7,9 +7,8 @@ identity = Kimlik name = Ad status = Durum presence = Görünürlük -log_out = Çıkış yap +danger_zone = Tehlikeli bölge delete_account = Hesabı sil -confirm = Onayla save = Kaydet remove = Kaldır change_picture = Resmi değiştir diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs index 1ea8432..b390db1 100644 --- a/crates/client/src/gui.rs +++ b/crates/client/src/gui.rs @@ -829,6 +829,34 @@ impl Yumush { cx.notify(); } + pub(super) fn armed(&self, armed: chat::Armed) -> bool { + self.chat.armed == Some(armed) + } + + pub(super) fn armed_label(&self, armed: chat::Armed, label: &'static str) -> &'static str { + if self.armed(armed) { + w().common.confirm + } else { + label + } + } + + pub(super) fn arm(&mut self, armed: chat::Armed, cx: &mut Context) -> bool { + if self.armed(armed) { + self.chat.armed = None; + return true; + } + self.chat.armed = Some(armed); + cx.notify(); + false + } + + pub(super) fn disarm(&mut self, cx: &mut Context) { + if self.chat.armed.take().is_some() { + cx.notify(); + } + } + fn logout(&mut self, window: &mut Window, cx: &mut Context) { let Some(user) = self.user.clone() else { return; @@ -890,7 +918,7 @@ impl Yumush { } self.chat.view = view; - self.chat.delete_step = chat::DeleteStep::Idle; + self.chat.armed = None; if view == View::Chat { self.chat.focus(window, cx); } diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index b892f67..3d8c939 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -82,9 +82,13 @@ pub enum View { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum DeleteStep { - Idle, - Confirm, +pub enum Armed { + LogOut, + Leave(Id), + RemoveFriend(Id), + DeleteUser(Id), + DeleteAccount, + DeleteCommunity, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -177,7 +181,7 @@ pub struct Chat { pub(super) avatar_requests: HashSet, pub(super) users: HashMap, user_requests: HashSet, - pub(super) delete_step: DeleteStep, + pub(super) armed: Option, pub(super) bans: Vec, pub(super) ban_search: Entity, pub(super) ban_sort: BanSort, @@ -186,7 +190,6 @@ pub struct Chat { pub(super) log_sort: LogSort, pub(super) friends: Friends, pub(super) invitations: Vec, - pub(super) pending_user_delete: Option, pub(super) card_menu: CardMenu, pub(super) presences: HashMap, pub(super) slowed_until: Option, @@ -459,8 +462,7 @@ impl Chat { avatar_requests: HashSet::new(), users: HashMap::new(), user_requests: HashSet::new(), - delete_step: DeleteStep::Idle, - pending_user_delete: None, + armed: None, card_menu: CardMenu::Main, presences: HashMap::new(), slowed_until: None, @@ -842,7 +844,7 @@ impl Chat { } if self.view == View::CommunitySettings { self.view = View::Chat; - self.delete_step = DeleteStep::Idle; + self.armed = None; } self.selected_community_id = community_id; self.users.extend( @@ -852,7 +854,7 @@ impl Chat { ); self.bans.clear(); self.logs.clear(); - self.pending_user_delete = None; + self.armed = None; self.messages.clear(); self.jump = None; self.pins.clear(); @@ -1259,7 +1261,7 @@ impl Chat { self.builder_invites.clear(); self.pending_invite_delete = None; self.view = View::Chat; - self.delete_step = DeleteStep::Idle; + self.armed = None; self.voice.clear(); self.server_muted.clear(); self.builder_voice_rooms.clear(); diff --git a/crates/client/src/gui/community_card.rs b/crates/client/src/gui/community_card.rs index 8fd9b2f..8a4e1a2 100644 --- a/crates/client/src/gui/community_card.rs +++ b/crates/client/src/gui/community_card.rs @@ -11,8 +11,9 @@ use gpui_kit::{ px, }; +use super::chat::Armed; use super::parts::{Ink, Text, band, small_icon, stack, words}; -use super::profile::{menu_row, yumush_action}; +use super::profile::{armed_action, menu_row, yumush_action}; use super::style::{size, space, stroke, text}; use super::{Palette, Trigger, Yumush, text}; use crate::lang::w; @@ -26,9 +27,11 @@ pub(super) fn community_popover( let community = community.clone(); let entity = entity.clone(); + let disarm = entity.clone(); Popover::new("community_card") .anchor(Anchor::TopLeft) .appearance(false) + .on_open_change(move |_, _, cx| disarm.update(cx, |yumush, cx| yumush.disarm(cx))) .trigger(Trigger::new(trigger)) .content(move |_, _, cx| { let popover = cx.entity(); @@ -164,12 +167,16 @@ pub(super) fn room_rows( |yumush, id, _, cx| yumush.toggle_mark(Mark::Mute, id, cx), )) .when(can_leave, |this| { - this.child(row( - "room_leave", - words.leave, - IconName::LogOut, + let armed = Armed::Leave(id); + this.child(menu_row( + ("room_leave", id as usize), + yumush.armed_label(armed, words.leave), + small_icon(IconName::LogOut), palette.danger, - |yumush, id, window, cx| yumush.leave_community(id, window, cx), + palette, + armed_action(armed, entity, popover, move |yumush, window, cx| { + yumush.leave_community(id, window, cx) + }), )) }) } diff --git a/crates/client/src/gui/community_settings.rs b/crates/client/src/gui/community_settings.rs index 288080d..683126c 100644 --- a/crates/client/src/gui/community_settings.rs +++ b/crates/client/src/gui/community_settings.rs @@ -20,12 +20,12 @@ use gpui_kit::{ use super::{ Palette, Yumush, avatar, - chat::View, + chat::{Armed, View}, clickable, community_icon, entity_action, millis_time, notify_error, profile::user_link, settings_page::{ - Editable, Picture, Section, delete_control, page, picked_avatar, picture_card, row, - section, small_button, text_row, with_danger, + Editable, Picture, Section, danger, delete_control, page, picked_avatar, picture_card, row, + section, small_button, text_row, }, style::{radius, size, space, text as text_token}, text, @@ -182,28 +182,22 @@ impl Yumush { moderation, )); } - let mut danger = Vec::new(); - if let Some(community) = community.filter(|_| permission::can_leave(actor)) { - danger.push(leave_control(entity, community.id, palette).into_any_element()); - } if permission::can_delete_community(actor) { - danger.push( + sections.push(danger(vec![ delete_control( "delete_community_button", w().community_settings.delete_community, - self.chat.delete_step, + self.armed(Armed::DeleteCommunity), entity, palette, Yumush::delete_community, ) .into_any_element(), - ); + ])); } if sections.is_empty() { sections.push(section(w().community_settings.general, palette, Vec::new())); } - let first = sections.remove(0); - sections.insert(0, with_danger(first, danger)); page( "community_settings_scroll", @@ -309,7 +303,7 @@ impl Yumush { } pub(super) fn delete_community(&mut self, window: &mut Window, cx: &mut Context) { - if !self.confirm_delete(cx) { + if !self.arm(Armed::DeleteCommunity, cx) { return; } let Some(community_id) = self.chat.selected_community_id else { @@ -667,22 +661,6 @@ fn log_row(entry: &LogEntry, palette: Palette, avatars: &HashMap> ) } -fn leave_control(entity: &Entity, community_id: Id, palette: Palette) -> impl IntoElement { - let entity = entity.clone(); - - small_button( - "leave_community_button", - palette, - palette.danger, - move |window, cx| { - entity.update(cx, |yumush, cx| { - yumush.leave_community(community_id, window, cx) - }); - }, - ) - .child(w().community_settings.leave_community) -} - fn ban_row( ban: &Ban, entity: &Entity, diff --git a/crates/client/src/gui/jump.rs b/crates/client/src/gui/jump.rs index 128a089..02777ed 100644 --- a/crates/client/src/gui/jump.rs +++ b/crates/client/src/gui/jump.rs @@ -14,7 +14,7 @@ use gpui_kit::{ use super::{ Palette, Yumush, avatar, builder::BuilderTab, - chat::View, + chat::{Armed, View}, community_icon, community_name, parts::{Ink, Text, band, words}, presence_avatar, @@ -333,7 +333,11 @@ impl Yumush { Glyph::Icon(IconName::Circle), ) })) - .chain([action(Place::LogOut, w().jump.log_out, IconName::LogOut)]); + .chain([action( + Place::LogOut, + self.armed_label(Armed::LogOut, w().jump.log_out), + IconName::LogOut, + )]); let mut found: Vec<_> = rooms .chain(people) .chain(pages) @@ -364,6 +368,7 @@ impl Yumush { pub(super) fn close_jump(&mut self, window: &mut Window, cx: &mut Context) { self.chat.goto.open = false; + self.chat.armed = None; window.focus(&self.focus, cx); cx.notify(); } @@ -391,6 +396,9 @@ impl Yumush { } fn pick(&mut self, place: Place, window: &mut Window, cx: &mut Context) { + if place == Place::LogOut && !self.arm(Armed::LogOut, cx) { + return; + } self.close_jump(window, cx); *self.settings.jump_usage.entry(place.key()).or_default() += 1; crate::settings::save(&self.settings); diff --git a/crates/client/src/gui/profile.rs b/crates/client/src/gui/profile.rs index aa55ab3..e97ee8b 100644 --- a/crates/client/src/gui/profile.rs +++ b/crates/client/src/gui/profile.rs @@ -25,7 +25,7 @@ use gpui_kit::{ div, px, }; -use super::chat::{CardMenu, ReportTarget, View}; +use super::chat::{Armed, CardMenu, ReportTarget, View}; use super::message_actions::report_bar; use super::parts::{ Ink, Scale, Text, Tone, Voice, band, icon_button, small_icon, stack, voice_line, words, @@ -92,7 +92,7 @@ pub(super) fn profile_popover( .on_open_change(move |_, _, cx| { notify_entity.update(cx, |yumush, cx| { yumush.chat.card_menu = CardMenu::Main; - yumush.chat.pending_user_delete = None; + yumush.chat.armed = None; yumush.open_user_volume(volume_user_id, cx); cx.notify() }) @@ -107,7 +107,7 @@ pub(super) fn profile_popover( relation: yumush.relation(user.id), card_menu: yumush.chat.card_menu, presence: yumush.presence_of(user.id), - confirming: yumush.chat.pending_user_delete == Some(user.id), + armed: yumush.chat.armed, reporting: yumush.chat.report == Some(ReportTarget::User(user.id)), reason: yumush.chat.report_reason.clone(), status: yumush.chat.status_input.clone(), @@ -144,7 +144,7 @@ struct Card { relation: Relation, card_menu: CardMenu, presence: Presence, - confirming: bool, + armed: Option, reporting: bool, reason: Entity, status: Entity, @@ -241,6 +241,7 @@ impl Card { self.viewer .as_ref() .is_some_and(|viewer| permission::is_builder(viewer.role)), + self.armed, entity, &popover, palette, @@ -554,8 +555,9 @@ impl Card { .as_ref() .is_some_and(|viewer| permission::can_delete_user(viewer.role, false, self.user.role)); if deletable { - let label = if self.confirming { - w().profile.confirm + let armed = Armed::DeleteUser(user_id); + let label = if self.armed == Some(armed) { + w().common.confirm } else { w().profile.delete_user }; @@ -567,7 +569,9 @@ impl Card { small_icon(IconName::Trash), palette.danger, palette, - delete_user_action(user_id, self.confirming, entity, popover), + armed_action(armed, entity, popover, move |yumush, window, cx| { + yumush.delete_member_account(user_id, window, cx) + }), ) .into_any_element(), ); @@ -579,6 +583,7 @@ impl Card { fn main_self_rows( builder: bool, + armed: Option, entity: &Entity, popover: &Entity, palette: Palette, @@ -614,11 +619,15 @@ fn main_self_rows( rows.push( menu_row( "profile_logout", - w().profile.log_out, + if armed == Some(Armed::LogOut) { + w().common.confirm + } else { + w().profile.log_out + }, small_icon(IconName::LogOut), palette.danger, palette, - yumush_action(entity, popover, |yumush, window, cx| { + armed_action(Armed::LogOut, entity, popover, |yumush, window, cx| { yumush.logout(window, cx) }), ) @@ -711,23 +720,19 @@ pub(super) fn yumush_action( } } -fn delete_user_action( - user_id: Id, - confirming: bool, +pub(super) fn armed_action( + armed: Armed, entity: &Entity, popover: &Entity, + run: impl Fn(&mut Yumush, &mut Window, &mut Context) + 'static, ) -> impl Fn(&mut Window, &mut App) + 'static { let entity = entity.clone(); let popover = popover.clone(); move |window, cx| { - if confirming { + if entity.update(cx, |yumush, cx| yumush.arm(armed, cx)) { popover.update(cx, |state, cx| state.dismiss(window, cx)); - entity.update(cx, |yumush, cx| { - yumush.delete_member_account(user_id, window, cx) - }); - } else { - entity.update(cx, |yumush, cx| yumush.request_member_delete(user_id, cx)); + entity.update(cx, |yumush, cx| run(yumush, window, cx)); } } } @@ -925,18 +930,12 @@ impl Yumush { }); } - pub(super) fn request_member_delete(&mut self, user_id: Id, cx: &mut Context) { - self.chat.pending_user_delete = Some(user_id); - cx.notify(); - } - pub(super) fn delete_member_account( &mut self, user_id: Id, window: &mut Window, cx: &mut Context, ) { - self.chat.pending_user_delete = None; let network = self.network.clone(); cx.spawn_in(window, async move |this, cx| { diff --git a/crates/client/src/gui/settings_page.rs b/crates/client/src/gui/settings_page.rs index 0e3dabd..37ce990 100644 --- a/crates/client/src/gui/settings_page.rs +++ b/crates/client/src/gui/settings_page.rs @@ -16,7 +16,7 @@ use gpui_kit::{ use super::{ Palette, Tooltip as _, Yumush, avatar, - chat::DeleteStep, + chat::Armed, chat::View, clickable, notify_error, parts, share_view::{AudioMode, FPS, RESOLUTIONS, codecs, share_modes}, @@ -31,9 +31,15 @@ use crate::{ pub(super) const SETTINGS_IDS: [&str; 4] = ["profile", "voice", "camera_share", "appearance"]; -pub(super) fn settings_titles() -> [&'static str; 4] { +pub(super) fn settings_titles() -> [&'static str; 5] { let w = &w().settings_page; - [w.profile, w.voice, w.camera_share, w.appearance] + [ + w.profile, + w.voice, + w.camera_share, + w.appearance, + w.danger_zone, + ] } pub(super) fn setting_rows() -> [(usize, &'static str); 25] { @@ -44,7 +50,7 @@ pub(super) fn setting_rows() -> [(usize, &'static str); 25] { (0, s.status), (0, s.presence), (0, s.change_picture), - (0, s.delete_account), + (4, s.delete_account), (1, v.input_device), (1, v.output_device), (1, v.input_volume), @@ -235,33 +241,18 @@ impl Yumush { )); } sections.push(section(settings_titles()[3], palette, appearance)); - if self.user.is_some() && !sections.is_empty() { - let profile = sections.remove(0); - sections.insert( - 0, - with_danger( - profile, - vec![ - small_button("logout_button", palette, palette.muted, { - let entity = entity.clone(); - move |window, cx| { - entity.update(cx, |yumush, cx| yumush.logout(window, cx)) - } - }) - .child(w().settings_page.log_out) - .into_any_element(), - delete_control( - "delete_account_button", - w().settings_page.delete_account, - self.chat.delete_step, - entity, - palette, - Yumush::delete_account, - ) - .into_any_element(), - ], - ), - ); + if self.user.is_some() { + sections.push(danger(vec![ + delete_control( + "delete_account_button", + w().settings_page.delete_account, + self.armed(Armed::DeleteAccount), + entity, + palette, + Yumush::delete_account, + ) + .into_any_element(), + ])); } page( @@ -342,17 +333,8 @@ impl Yumush { .detach(); } - pub(super) fn confirm_delete(&mut self, cx: &mut Context) -> bool { - if self.chat.delete_step == DeleteStep::Idle { - self.chat.delete_step = DeleteStep::Confirm; - cx.notify(); - return false; - } - true - } - pub(super) fn delete_account(&mut self, window: &mut Window, cx: &mut Context) { - if !self.confirm_delete(cx) { + if !self.arm(Armed::DeleteAccount, cx) { return; } let Some(user_id) = self.user.as_ref().map(|user| user.id) else { @@ -639,6 +621,7 @@ pub(super) fn page( move |_, cx| { entity.update(cx, |yumush, cx| { yumush.chat.section = position; + yumush.chat.armed = None; cx.notify(); }) }, @@ -759,25 +742,15 @@ pub(super) fn section(title: &'static str, palette: Palette, rows: Vec
) -> Section(title, rows_column(palette, rows)) } -pub(super) fn with_danger(section: Section, actions: Vec) -> Section { - let Section(title, body) = section; +pub(super) fn danger(actions: Vec) -> Section { Section( - title, + w().settings_page.danger_zone, div() + .ml(px(-space::SM)) .flex() - .flex_col() - .gap(px(space::XXL)) - .child(body) - .when(!actions.is_empty(), |this| { - this.child( - div() - .ml(px(-space::SM)) - .flex() - .items_center() - .gap(px(space::LG)) - .children(actions), - ) - }) + .items_center() + .gap(px(space::LG)) + .children(actions) .into_any_element(), ) } @@ -936,20 +909,18 @@ pub(super) fn small_button( pub(super) fn delete_control( id: &'static str, label: &'static str, - step: DeleteStep, + confirming: bool, entity: &Entity, palette: Palette, action: fn(&mut Yumush, &mut Window, &mut Context), ) -> impl IntoElement { let entity = entity.clone(); - let confirming = step == DeleteStep::Confirm; - small_button(id, palette, palette.danger, move |window, cx| { entity.update(cx, |yumush, cx| action(yumush, window, cx)); }) .when(confirming, |this| this.font_weight(FontWeight::MEDIUM)) .child(if confirming { - w().settings_page.confirm + w().common.confirm } else { label }) diff --git a/crates/client/src/gui/sidebar.rs b/crates/client/src/gui/sidebar.rs index 6da4725..548d608 100644 --- a/crates/client/src/gui/sidebar.rs +++ b/crates/client/src/gui/sidebar.rs @@ -23,7 +23,7 @@ use gpui_kit::{ use super::{ Palette, Tooltip as _, Yumush, avatar, - chat::{View, create_community}, + chat::{Armed, View, create_community}, clickable, community_icon, community_name, entity_action, hoverable, input_dialog, parts::{Scale, Tone, icon_button}, presence_avatar, @@ -429,20 +429,29 @@ impl Yumush { yumush.open_direct(user_id, window, cx) }) }); - right_menu(("friend_menu", user_id as usize), row, move |popover, _| { - super::parts::stack(space::XS).child(super::profile::menu_row( - ("remove_friend", user_id as usize), - w().sidebar.remove_friend, - super::parts::small_icon(IconName::UserMinus), - palette.danger, - palette, - super::profile::yumush_action( - &remove, - popover, - move |yumush, window, cx| yumush.remove_friend(user_id, window, cx), - ), - )) - }) + let armed = Armed::RemoveFriend(user_id); + right_menu( + ("friend_menu", user_id as usize), + entity, + row, + move |popover, cx| { + super::parts::stack(space::XS).child(super::profile::menu_row( + ("remove_friend", user_id as usize), + remove + .read(cx) + .armed_label(armed, w().sidebar.remove_friend), + super::parts::small_icon(IconName::UserMinus), + palette.danger, + palette, + super::profile::armed_action( + armed, + &remove, + popover, + move |yumush, window, cx| yumush.remove_friend(user_id, window, cx), + ), + )) + }, + ) .into_any_element() }) .collect() @@ -615,6 +624,7 @@ impl Yumush { let menu = entity.clone(); right_menu( ("room_menu", community_id as usize), + entity, row, move |popover, cx| { let yumush = menu.read(cx); @@ -1165,11 +1175,14 @@ fn row_button( fn right_menu( id: impl Into, + entity: &Entity, trigger: impl IntoElement, rows: impl Fn(&Entity, &App) -> Div + 'static, ) -> Popover { + let disarm = entity.clone(); Popover::new(id) .mouse_button(gpui_kit::MouseButton::Right) + .on_open_change(move |_, _, cx| disarm.update(cx, |yumush, cx| yumush.disarm(cx))) .anchor(Anchor::TopLeft) .appearance(false) .trigger(super::Trigger::new(trigger)) diff --git a/crates/client/src/instance.rs b/crates/client/src/instance.rs new file mode 100644 index 0000000..2cac342 --- /dev/null +++ b/crates/client/src/instance.rs @@ -0,0 +1,109 @@ +use tokio::sync::mpsc::{UnboundedReceiver, unbounded_channel}; + +pub const MULTI: &str = "--multi"; + +pub enum Instance { + First(UnboundedReceiver<()>), + Running, +} + +pub fn claim() -> Instance { + if std::env::args().any(|arg| arg == MULTI) { + return alone(); + } + match crate::config_dir() { + Some(dir) => claim_in(dir), + None => alone(), + } +} + +fn alone() -> Instance { + Instance::First(unbounded_channel().1) +} + +#[cfg(unix)] +fn claim_in(dir: std::path::PathBuf) -> Instance { + use std::os::unix::net::{UnixListener, UnixStream}; + + let path = dir.join("instance.sock"); + if UnixStream::connect(&path).is_ok() { + return Instance::Running; + } + let _ = std::fs::create_dir_all(&dir); + let _ = std::fs::remove_file(&path); + let listener = match UnixListener::bind(&path) { + Ok(listener) => listener, + Err(error) => { + tracing::warn!("instance: {error}"); + return alone(); + } + }; + let (sender, receiver) = unbounded_channel(); + let _ = std::thread::Builder::new() + .name("instance".to_string()) + .spawn(move || { + for stream in listener.incoming() { + if stream.is_ok() && sender.send(()).is_err() { + break; + } + } + }); + Instance::First(receiver) +} + +#[cfg(windows)] +fn claim_in(dir: std::path::PathBuf) -> Instance { + use tokio::net::windows::named_pipe::ServerOptions; + + let name = format!( + r"\\.\pipe\yumush-{}", + dir.display().to_string().replace('\\', "/") + ); + let (sender, receiver) = unbounded_channel(); + let (ready_sender, ready) = std::sync::mpsc::channel(); + let pipe = name.clone(); + let _ = std::thread::Builder::new() + .name("instance".to_string()) + .spawn(move || { + let Ok(runtime) = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + else { + let _ = ready_sender.send(false); + return; + }; + runtime.block_on(async move { + let Ok(mut server) = ServerOptions::new().first_pipe_instance(true).create(&pipe) + else { + let _ = ready_sender.send(false); + return; + }; + let _ = ready_sender.send(true); + loop { + if server.connect().await.is_ok() && sender.send(()).is_err() { + break; + } + let Ok(next) = ServerOptions::new().create(&pipe) else { + break; + }; + server = next; + } + }); + }); + if ready.recv() == Ok(true) { + return Instance::First(receiver); + } + match std::fs::OpenOptions::new() + .read(true) + .write(true) + .open(&name) + { + Ok(_) => Instance::Running, + Err(_) => alone(), + } +} + +#[cfg(not(any(unix, windows)))] +fn claim_in(_: std::path::PathBuf) -> Instance { + alone() +} diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 77d090d..c743eb2 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -9,6 +9,7 @@ pub mod avatar; pub mod embed; pub mod emoji; pub mod gui; +pub mod instance; pub mod lang; pub mod logo; pub mod marks; diff --git a/crates/client/src/main.rs b/crates/client/src/main.rs index f561576..8c2692d 100644 --- a/crates/client/src/main.rs +++ b/crates/client/src/main.rs @@ -41,6 +41,10 @@ fn main() { return; } + let client::instance::Instance::First(mut wake) = client::instance::claim() else { + return; + }; + #[cfg(target_os = "linux")] if let Err(error) = client::logo::install() { tracing::warn!("desktop entry: {error}"); @@ -83,6 +87,15 @@ fn main() { }) .detach(); + let woken = view.clone(); + cx.spawn(async move |cx| { + while wake.recv().await.is_some() { + let view = woken.clone(); + cx.update(|cx| show(view, cx)); + } + }) + .detach(); + cx.spawn(async move |cx| { while let Some(action) = tray_receiver.recv().await { let view = view.clone();