Added single-instance, where second instance raises the current running instance instead of creating a second conflicting instance
Moorly CI / windows-build (push) Successful in 3m2s
Moorly CI / linux-build (push) Successful in 4m7s

This commit is contained in:
Squid committed 2026-11-12 20:07:56 +01:00
1 parent 6425cfaacf
commit 4dc4401871
4 files changed
+190 -2

No files matched your search

+90 -2
View File
@@ -21,6 +21,7 @@ use iced::{
mod autostart;
mod hotkeys;
mod notifications;
mod single_instance;
#[allow(dead_code)]
mod terminal;
mod theme;
@@ -93,11 +94,21 @@ const DEFAULT_PROFILE_FOLDER: &str = "Ungrouped";
const ROOT_FOLDER_INDEX: usize = usize::MAX;
fn main() -> iced::Result {
iced::daemon(boot, update, view)
// Elect the primary before creating any window, tray, hotkeys or
// terminals. A second launch hands its activation request to the owner
// and exits here.
if !single_instance::acquire() {
return Ok(());
}
let result = iced::daemon(boot, update, view)
.title(window_title)
.theme(|state: &Moorly, _window| state.theme.iced())
.subscription(subscription)
.run()
.run();
single_instance::shutdown();
result
}
/// Opens the first window and gives it the Start tab when configured.
@@ -409,6 +420,7 @@ fn subscription(state: &Moorly) -> Subscription<Message> {
terminal_text,
hotkeys::subscription().map(Message::Hotkey),
tray::subscription().map(Message::Tray),
single_instance::subscription(),
iced::time::every(Duration::from_secs(1)).map(|_| Message::ReloadThemes),
window::events().map(|(window, event)| Message::WindowEvent(window, event)),
iced::event::listen_with(pointer_event),
@@ -5242,6 +5254,7 @@ enum Message {
ScrollTabsBy(window::Id, f32),
Hotkey(hotkeys::Event),
Tray(tray::Event),
SecondInstanceActivated,
FinishToggleWindowVisibility(Option<bool>),
FinishMinimizeToTray(window::Id, Option<bool>),
Quit,
@@ -6674,6 +6687,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
}
Message::Hotkey(event) => return handle_hotkey_event(state, event),
Message::Tray(event) => return handle_tray_event(state, event),
Message::SecondInstanceActivated => return activate_main_window(state),
Message::FinishToggleWindowVisibility(minimized) => {
return finish_toggle_window_visibility(state, minimized);
}
@@ -6690,6 +6704,7 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
Message::Quit => {
tray::shutdown();
hotkeys::shutdown();
single_instance::shutdown();
// Drop PTY sessions explicitly so their child processes are
// terminated before process::exit bypasses the state destructor.
state.terminal_sessions.clear();
@@ -6770,6 +6785,38 @@ fn reopen_main_window(state: &mut Moorly, window_state: WindowState) -> Task<Mes
Task::batch([open.map(|_| Message::Ignore), window::gain_focus(id)])
}
/// Restores and raises the main window for a second launch. Compositors that
/// refuse focus still get a user-attention request, and the window is never
/// recreated when it already exists.
fn activate_main_window(state: &mut Moorly) -> Task<Message> {
if state.main_window_hidden || state.hidden_main_window.is_some() {
let show = show_main_window(state);
let Some(window) = main_window_id(state) else {
return show;
};
return Task::batch([
show,
window::request_user_attention(window, Some(window::UserAttention::Critical)),
]);
}
let Some(window) = main_window_id(state) else {
return Task::none();
};
state.focused_window = Some(window);
// A plain (not tray) minimized window has no hidden flag; restoring and
// focusing it does not change its mode, so a maximized window stays
// maximized.
Task::batch([
window::minimize(window, false),
window::gain_focus(window),
window::request_user_attention(window, Some(window::UserAttention::Critical)),
])
}
fn toggle_main_window(state: &mut Moorly) -> Task<Message> {
if state.main_window_hidden {
return show_main_window(state);
@@ -12612,6 +12659,47 @@ mod tests {
assert!(state.terminal_sessions[&id].selection.is_none());
}
#[test]
fn second_instance_activation_restores_a_hidden_window() {
let mut state = state();
let window = test_window();
let saved = state.windows.remove(&window).unwrap();
let tabs = saved.tabs.clone();
let active_tab = saved.active_tab;
state.window_order.clear();
state.main_window = None;
state.focused_window = None;
state.main_window_hidden = true;
state.hidden_main_window = Some(saved);
let _ = update(&mut state, Message::SecondInstanceActivated);
assert!(!state.main_window_hidden);
assert!(state.hidden_main_window.is_none());
let restored = state.main_window.expect("window was restored");
assert_eq!(state.windows[&restored].tabs, tabs);
assert_eq!(state.windows[&restored].active_tab, active_tab);
assert_eq!(state.focused_window, Some(restored));
}
#[test]
fn second_instance_activation_keeps_a_visible_window() {
let mut state = state();
let window = test_window();
let tabs = window_state(&state).tabs.clone();
let active_tab = window_state(&state).active_tab;
let _ = update(&mut state, Message::SecondInstanceActivated);
assert_eq!(state.windows.len(), 1);
assert_eq!(state.main_window, Some(window));
assert_eq!(state.focused_window, Some(window));
assert_eq!(window_state(&state).tabs, tabs);
assert_eq!(window_state(&state).active_tab, active_tab);
}
#[test]
fn quick_power_shells_are_numbered_separately() {
let entries = vec![