Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
feat(ui): handle actions::SELECT_* in EntryCore
Browse files Browse the repository at this point in the history
  • Loading branch information
yvt committed Apr 22, 2020
1 parent fa966fd commit 2b5d30b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tcw3/examples/tcw3_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ impl WndListener for MyWndListener {
macos("Super+V")
),
(1, windows("Ctrl+Q"), gtk("Ctrl+Q"), macos("Super+Q")),
(
// `SELECT_WORD` is not in the default key bindings of any
// target platforms
pal::actions::SELECT_WORD,
windows("Ctrl+W"),
gtk("Ctrl+W"),
macos("Super+W")
),
]);
}

Expand Down
29 changes: 27 additions & 2 deletions tcw3/src/ui/views/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,10 @@ impl ViewListener for EntryCoreListener {
fn validate_action(&self, _: pal::Wm, _: HViewRef<'_>, action: ActionId) -> ActionStatus {
let mut status = ActionStatus::empty();
match action {
actions::SELECT_ALL => {
actions::SELECT_ALL
| actions::SELECT_LINE
| actions::SELECT_PARAGRAPH
| actions::SELECT_WORD => {
status |= ActionStatus::VALID | ActionStatus::ENABLED;
}
actions::COPY | actions::CUT => {
Expand All @@ -449,9 +452,31 @@ impl ViewListener for EntryCoreListener {

fn perform_action(&self, wm: pal::Wm, view: HViewRef<'_>, action: ActionId) {
match action {
actions::SELECT_ALL => {
actions::SELECT_ALL | actions::SELECT_LINE | actions::SELECT_PARAGRAPH => {
log::trace!("Handling a 'select all' command (SELECT_ALL, etc.)");
update_sel_range(wm, view, RcBorrow::from(&self.inner), &mut |state| {
log::trace!("... original sel_range = {:?}", state.sel_range);
state.sel_range = [0, state.text.len()];
log::trace!("... new sel_range = {:?}", state.sel_range);
});
}
actions::SELECT_WORD => {
log::trace!("Handling SELECT_WORD");
update_sel_range(wm, view, RcBorrow::from(&self.inner), &mut |state| {
state.ensure_text_layout(&self.inner.style_elem);
let layout = &state.text_layout_info.as_ref().unwrap().text_layout;
let [mut start, mut end] = state.sel_range;
log::trace!("... original sel_range = {:?}", state.sel_range);
if start > end {
std::mem::swap(&mut start, &mut end);
}

// Expand the selection to a word
let start = layout.next_word(layout.next_char(start, true), false);
let end = layout.next_word(layout.next_char(end, false), true);

state.sel_range = [start, end];
log::trace!("... new sel_range = {:?}", state.sel_range);
});
}
actions::COPY => {
Expand Down

0 comments on commit 2b5d30b

Please sign in to comment.