slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.94k stars 568 forks source link

StandardListViewItem can't copy text #3249

Closed Sherlock-Holo closed 8 months ago

Sherlock-Holo commented 1 year ago

I am using slint with rust, when I try the StandardListView with StandardListViewItem, I found I can't copy the text in the StandardListViewItem when running the slint gui, no matter the right click or press Ctrl+C directly

I am using the native style

FloVanGH commented 1 year ago

Hi, thank you for your feedback.

Such a features is not yet implemented for the StandardListView. What you can do is to copy it from you data source, here an example in Rust

let main_window = MainWindow::new():unwrap();
main_window.set_items(VecModel::from_slice(&[StandardListViewItem { text: "Hello".into(), /* ... more items */}]));

main_window.on_copy({
    let window_handle = main_window.as_weak();
    | item_index | {
          let text_to_copy = main_window.get_items().row_data(item_index()).unwrap().text.to_string();
          // copy text to clipboard
    }
});

main_window.on_paste({
    let window_handle = main_window.as_weak();
    || {
          // paste the content of clipboard
    }
});
import { StandardListView } from "std-widgets.slint";

export component MainWindow {
    callback copy(int);
    callback paste();

    in property <[StandardListViewItem]> items <=> i-list-view.model;

    width: 600px;
    height: 400px;

    FocusScope {
           // handle keyboard events to invoke copy and paste
    }

    i-list-view := StandardListView {}
}
Sherlock-Holo commented 1 year ago

it looks a little complicated, when does StandardListViewItem support copy the text?

FloVanGH commented 1 year ago

At the moment there is nothing planned to implement that feature. In most native ui-toolkits it is not common as default behavior to copy the text of a list item element. It's possible in the web, but there can be almost every text be selected and copied. From my experience most time you want to copy a list item, you want to copy the corresponding data of the list item. Often the text is only a visual representation of the item. So to implement text copy on StandardListView as default behavior would make the other use cases more complicated.

What we want to do is to provide a better way to implement custom items for ListView so you can easier implement your custom behavior for items in Slint.

With the current implementation what could help you right now to do in Slint is something like this:

private property <string> copy-text;

    FocusScope {
        key-released(e) => {
            if (e.text == "C" && e.modifiers.control) {
                copy(i-list-view.model[i-list-view.current-item.text]);
            }
        }
    }

    i-list-view := StandardListView {}

    function copy(text: string) {
        copy-text = text;
    }

I hope that helps you a little bit.