Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

3.1 KB
Raw Blame
//! The right-click menu of a text box (the address bar, a rename box, the search box, a dialog's field ...): Cut, Copy, Paste and
//! Select all, as the app's own menu in the app's language. The kit's own was Windows' plain menu in English only, which looked like
//! no menu of ours at all (issue 628: "I can't copy path with mouse but works with keyboard"; a Discord request for the rename box).

use gpui_kit::assets::IconName as L;
use gpui_kit::component::input::{Copy, Cut, Input, InputState, Paste, SelectAll};
use gpui_kit::*;

use crate::fluent_menu::{Item, Menu, MenuIcon};
use crate::t;

/// `Input::new(state)` with our right-click menu instead of the kit's.
pub fn input(state: &Entity<InputState>) -> Input {
    let st = state.clone();
    Input::new(state).context_menu(move |nothing, window, cx| {
        show(&st, window, cx);
        // (the kit shows nothing for an empty menu: ours is the one on the screen)
        nothing
    })
}

/// What the menu offers for a box: whether it can be changed, and whether it has a selection that may be copied.
fn offers(editable: bool, masked: bool, selected: bool) -> (bool, bool, bool) {
    let copy = selected && !masked;
    (copy && editable, copy, editable)
}

fn show(state: &Entity<InputState>, window: &mut Window, cx: &mut App) {
    show_at(state, window.mouse_position(), window, cx);
}

pub(crate) fn show_at(state: &Entity<InputState>, at: Point<Pixels>, window: &mut Window, cx: &mut App) {
    let (editable, masked, selected) = {
        let s = state.read(cx);
        let p = s.presentation();
        (p.is_editable(), p.is_masked(), !s.selected_range().is_empty())
    };
    let (cut, copy, paste) = offers(editable, masked, selected);
    // each command goes to the box itself: it takes the keyboard back first (the menu had it)
    let act = |action: fn() -> Box<dyn Action>| {
        let st = state.clone();
        move |window: &mut Window, cx: &mut App| {
            st.update(cx, |s, cx| s.focus(window, cx));
            window.dispatch_action(action(), cx);
        }
    };
    let items = vec![
        Item::action(t!("Kes"), MenuIcon::Lucide(L::Scissors), act(|| Box::new(Cut))).hint("Ctrl+X").enabled(cut),
        Item::action(t!("Kopyala"), MenuIcon::Lucide(L::Copy), act(|| Box::new(Copy))).hint("Ctrl+C").enabled(copy),
        Item::action(t!("Yapıştır"), MenuIcon::Lucide(L::ClipboardPaste), act(|| Box::new(Paste))).hint("Ctrl+V").enabled(paste),
        Item::Separator,
        Item::action(t!("Tümünü seç"), MenuIcon::None, act(|| Box::new(SelectAll))).hint("Ctrl+A"),
    ];
    crate::fluent_menu::show(Menu { quick: Vec::new(), items }, at, window, cx);
}

#[cfg(test)]
mod tests {
    use super::offers;

    #[test]
    fn a_selection_is_cut_and_copied_where_the_box_allows_it() {
        assert_eq!(offers(true, false, true), (true, true, true));
        assert_eq!(offers(true, false, false), (false, false, true), "nothing selected: paste only");
        assert_eq!(offers(false, false, true), (false, true, false), "read-only: copy only");
        assert_eq!(offers(true, true, true), (false, false, true), "a hidden password is never copied out");
    }
}