Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

12.2 KB
Raw Blame
use crate::connection::ConnectionOps;
use crate::macos::menu::RepresentedItem;
use crate::macos::{nsstring, nsstring_to_str};
use crate::menu::{Menu, MenuItem};
use crate::{ApplicationEvent, Connection, DockMenuItem};
use cocoa::appkit::{NSApp, NSApplicationTerminateReply, NSImage, NSImageNameApplicationIcon};
use cocoa::base::{id, nil};
use cocoa::foundation::{NSDictionary, NSInteger, NSUInteger};
use config::keyassignment::KeyAssignment;
use config::WindowCloseConfirmation;
use objc::declare::ClassDecl;
use objc::rc::StrongPtr;
use objc::runtime::{Class, Object, Sel, BOOL, NO, YES};
use objc::*;
use std::cell::RefCell;
use std::path::{Path, PathBuf};

const CLS_NAME: &str = "WezTermAppDelegate";

thread_local! {
    static DOCK_MENU_ITEMS: RefCell<Option<Box<dyn Fn() -> Vec<DockMenuItem>>>> =
        RefCell::new(None);
}

/// Have the Dock menu list the entries `items` gives, asked each time it
/// opens; choosing one sends `ApplicationEvent::DockMenuItem`.
pub fn set_dock_menu_items(items: impl Fn() -> Vec<DockMenuItem> + 'static) {
    DOCK_MENU_ITEMS.with(|slot| *slot.borrow_mut() = Some(Box::new(items)));
}

#[link(name = "AppKit", kind = "framework")]
extern "C" {
    static NSAboutPanelOptionApplicationName: id;
    static NSAboutPanelOptionApplicationIcon: id;
    static NSAboutPanelOptionVersion: id;
}

extern "C" fn application_should_terminate(
    _self: &mut Object,
    _sel: Sel,
    _app: *mut Object,
) -> u64 {
    log::debug!("application termination requested");
    unsafe {
        match config::configuration().window_close_confirmation {
            WindowCloseConfirmation::NeverPrompt => terminate_now(),
            WindowCloseConfirmation::AlwaysPrompt => {
                let alert: id = msg_send![class!(NSAlert), alloc];
                let alert: id = msg_send![alert, init];
                let message_text = nsstring(&thinkterm_i18n::tr("quit-confirm-title"));
                let info_text = nsstring(&thinkterm_i18n::tr("quit-confirm-detail"));
                let cancel = nsstring(&thinkterm_i18n::tr("quit-confirm-cancel"));
                let ok = nsstring(&thinkterm_i18n::tr("quit-confirm-ok"));

                let () = msg_send![alert, setMessageText: message_text];
                let () = msg_send![alert, setInformativeText: info_text];
                let () = msg_send![alert, addButtonWithTitle: cancel];
                let () = msg_send![alert, addButtonWithTitle: ok];
                #[allow(non_upper_case_globals)]
                const NSModalResponseCancel: NSInteger = 1000;
                #[allow(non_upper_case_globals, dead_code)]
                const NSModalResponseOK: NSInteger = 1001;
                let result: NSInteger = msg_send![alert, runModal];
                log::info!("alert result is {result}");

                if result == NSModalResponseCancel {
                    NSApplicationTerminateReply::NSTerminateCancel as u64
                } else {
                    terminate_now()
                }
            }
        }
    }
}

fn terminate_now() -> u64 {
    if let Some(conn) = Connection::get() {
        // AppKit exits inside `terminate:` once this answers: the loop below
        // is asked to stop, but `run` never returns to the code after it.
        conn.dispatch_app_event(ApplicationEvent::WillTerminate);
        conn.terminate_message_loop();
    }
    NSApplicationTerminateReply::NSTerminateNow as u64
}

fn thinkterm_icon_path() -> Option<PathBuf> {
    let exe = std::env::current_exe().ok()?;
    let exe_dir = exe.parent()?;
    let mut candidates = vec![exe_dir.join("ThinkTerm.icns")];

    if let Some(contents_dir) = exe_dir.parent() {
        candidates.push(contents_dir.join("Resources").join("ThinkTerm.icns"));
    }

    if let Some(repo_dir) = PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent() {
        candidates.push(repo_dir.join("assets").join("icon").join("ThinkTerm.icns"));
    }

    candidates.into_iter().find(|path| path.exists())
}

fn load_thinkterm_icon(path: &Path) -> Option<StrongPtr> {
    let path_string = path.to_string_lossy();

    unsafe {
        let ns_image = cocoa::appkit::NSImage::initWithContentsOfFile_(
            NSImage::alloc(nil),
            *nsstring(path_string.as_ref()),
        );
        if ns_image == nil {
            None
        } else {
            let ns_image = StrongPtr::new(ns_image);
            let _: BOOL = msg_send![*ns_image, setName:NSImageNameApplicationIcon];
            Some(ns_image)
        }
    }
}

extern "C" fn application_will_finish_launching(
    _self: &mut Object,
    _sel: Sel,
    _notif: *mut Object,
) {
    log::debug!("application_will_finish_launching");
}

extern "C" fn application_did_finish_launching(this: &mut Object, _sel: Sel, _notif: *mut Object) {
    log::debug!("application_did_finish_launching");
    unsafe {
        (*this).set_ivar("launched", YES);
    }
}

extern "C" fn application_open_untitled_file(
    this: &mut Object,
    _sel: Sel,
    _app: *mut Object,
) -> BOOL {
    let launched: BOOL = unsafe { *this.get_ivar("launched") };
    log::debug!("application_open_untitled_file launched={launched}");
    if let Some(conn) = Connection::get() {
        if launched == YES {
            conn.dispatch_app_event(ApplicationEvent::PerformKeyAssignment(
                KeyAssignment::SpawnWindow,
            ));
        }
        return YES;
    }
    NO
}

extern "C" fn wezterm_perform_key_assignment(
    _self: &mut Object,
    _sel: Sel,
    menu_item: *mut Object,
) {
    let menu_item = crate::os::macos::menu::MenuItem::with_menu_item(menu_item);
    // Safe because weztermPerformKeyAssignment: is only used with KeyAssignment
    let action = menu_item.get_represented_item();
    log::debug!("wezterm_perform_key_assignment {action:?}",);
    match action {
        Some(RepresentedItem::KeyAssignment(action)) => {
            if let Some(conn) = Connection::get() {
                conn.dispatch_app_event(ApplicationEvent::PerformKeyAssignment(action));
            }
        }
        Some(RepresentedItem::ContextMenuAction(_)) => {}
        Some(RepresentedItem::DockMenuItem(_)) => {}
        None => {}
    }
}

extern "C" fn thinkterm_choose_dock_menu_item(
    _self: &mut Object,
    _sel: Sel,
    menu_item: *mut Object,
) {
    let menu_item = crate::os::macos::menu::MenuItem::with_menu_item(menu_item);
    if let Some(RepresentedItem::DockMenuItem(id)) = menu_item.get_represented_item() {
        if let Some(conn) = Connection::get() {
            conn.dispatch_app_event(ApplicationEvent::DockMenuItem(id));
        }
    }
}

extern "C" fn thinkterm_order_front_standard_about_panel(
    _self: &mut Object,
    _sel: Sel,
    _sender: *mut Object,
) {
    unsafe {
        let app = NSApp();
        let app_name = nsstring("ThinkTerm");
        // The panel renders these as "Version {ApplicationVersion} ({Version})".
        // ApplicationVersion is left to default from CFBundleShortVersionString
        // so the marketing version has a single source; the parenthesised slot
        // carries the build stamp, which is what makes a screenshot of this
        // window enough to identify the exact commit a user is running.
        let build_version = nsstring(config::wezterm_version());
        let current_icon: id = msg_send![app, applicationIconImage];
        let loaded_icon = if current_icon == nil {
            thinkterm_icon_path().and_then(|path| load_thinkterm_icon(&path))
        } else {
            None
        };

        let mut keys = vec![NSAboutPanelOptionApplicationName, NSAboutPanelOptionVersion];
        let mut objects = vec![*app_name, *build_version];

        let app_icon = if current_icon != nil {
            current_icon
        } else if let Some(icon) = loaded_icon.as_ref() {
            **icon
        } else {
            nil
        };
        if app_icon != nil {
            if current_icon == nil {
                let () = msg_send![app, setApplicationIconImage: app_icon];
            }
            keys.push(NSAboutPanelOptionApplicationIcon);
            objects.push(app_icon);
        }

        let options = NSDictionary::dictionaryWithObjects_forKeys_count_(
            nil,
            objects.as_ptr(),
            keys.as_ptr(),
            objects.len() as NSUInteger,
        );
        let () = msg_send![app, orderFrontStandardAboutPanelWithOptions: options];
    }
}

extern "C" fn application_open_file(
    this: &mut Object,
    _sel: Sel,
    _app: *mut Object,
    file_name: *mut Object,
) {
    let launched: BOOL = unsafe { *this.get_ivar("launched") };
    if launched == YES {
        let file_name = unsafe { nsstring_to_str(file_name) }.to_string();
        if let Some(conn) = Connection::get() {
            log::debug!("application_open_file {file_name}");
            conn.dispatch_app_event(ApplicationEvent::OpenCommandScript(file_name));
        }
    }
}

extern "C" fn application_dock_menu(
    _self: &mut Object,
    _sel: Sel,
    _app: *mut Object,
) -> *mut Object {
    let dock_menu = Menu::new_with_title("");
    let new_window_item = MenuItem::new_with(
        &thinkterm_i18n::tr("command-new-window"),
        Some(sel!(weztermPerformKeyAssignment:)),
        "",
    );
    new_window_item
        .set_represented_item(RepresentedItem::KeyAssignment(KeyAssignment::SpawnWindow));
    dock_menu.add_item(&new_window_item);
    let items = DOCK_MENU_ITEMS.with(|slot| {
        slot.borrow()
            .as_ref()
            .map(|items| items())
            .unwrap_or_default()
    });
    if !items.is_empty() {
        dock_menu.add_item(&MenuItem::new_separator());
    }
    for item in items {
        let menu_item =
            MenuItem::new_with(&item.title, Some(sel!(thinktermChooseDockMenuItem:)), "");
        menu_item.set_checked(item.checked);
        menu_item.set_represented_item(RepresentedItem::DockMenuItem(item.id));
        dock_menu.add_item(&menu_item);
    }
    dock_menu.autorelease()
}

fn get_class() -> &'static Class {
    Class::get(CLS_NAME).unwrap_or_else(|| {
        let mut cls = ClassDecl::new(CLS_NAME, class!(NSObject))
            .expect("Unable to register application delegate class");

        cls.add_ivar::<BOOL>("launched");

        unsafe {
            cls.add_method(
                sel!(applicationShouldTerminate:),
                application_should_terminate as extern "C" fn(&mut Object, Sel, *mut Object) -> u64,
            );
            cls.add_method(
                sel!(applicationWillFinishLaunching:),
                application_will_finish_launching as extern "C" fn(&mut Object, Sel, *mut Object),
            );
            cls.add_method(
                sel!(applicationDidFinishLaunching:),
                application_did_finish_launching as extern "C" fn(&mut Object, Sel, *mut Object),
            );
            cls.add_method(
                sel!(application:openFile:),
                application_open_file as extern "C" fn(&mut Object, Sel, *mut Object, *mut Object),
            );
            cls.add_method(
                sel!(applicationDockMenu:),
                application_dock_menu
                    as extern "C" fn(&mut Object, Sel, *mut Object) -> *mut Object,
            );
            cls.add_method(
                sel!(weztermPerformKeyAssignment:),
                wezterm_perform_key_assignment as extern "C" fn(&mut Object, Sel, *mut Object),
            );
            cls.add_method(
                sel!(thinktermChooseDockMenuItem:),
                thinkterm_choose_dock_menu_item as extern "C" fn(&mut Object, Sel, *mut Object),
            );
            cls.add_method(
                sel!(thinktermOrderFrontStandardAboutPanel:),
                thinkterm_order_front_standard_about_panel
                    as extern "C" fn(&mut Object, Sel, *mut Object),
            );
            cls.add_method(
                sel!(applicationOpenUntitledFile:),
                application_open_untitled_file
                    as extern "C" fn(&mut Object, Sel, *mut Object) -> BOOL,
            );
        }

        cls.register()
    })
}

pub fn create_app_delegate() -> StrongPtr {
    let cls = get_class();
    unsafe {
        let delegate: *mut Object = msg_send![cls, alloc];
        let delegate: *mut Object = msg_send![delegate, init];
        (*delegate).set_ivar("launched", NO);
        StrongPtr::new(delegate)
    }
}