From b1f5639d8cbc9b0a8525e1fcc76a4000f7852ded Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Tue, 18 Dec 2018 18:22:50 +0300 Subject: [PATCH] src: Add support for right click events The current implementation treats left mouse button clicks as a generic "mouse press" modifier, this commit extends the list of modifiers by adding a "right mouse click" one. The proper way to implement this would be to ship the coordinates of mouse key press events in each `Key` object, and pass whichever button was clicked as a codepoint value (instead of coordinates currently), but this would require more work. This commit allows: * right clicks to set the cursor of the main selection * control-right clicks to merge all the selections, and then set its cursor Fixes #843 --- src/input_handler.cc | 18 +++++++++++++++--- src/json_ui.cc | 12 ++++++++---- src/keys.cc | 12 ++++++++---- src/keys.hh | 21 ++++++++++++--------- src/ncurses_ui.cc | 8 ++++++-- 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index ade87d04..0d9710dd 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -92,9 +92,20 @@ struct MouseHandler Buffer& buffer = context.buffer(); BufferCoord cursor; auto& selections = context.selections(); - switch ((Key::Modifiers)(key.modifiers & Key::Modifiers::MouseEvent)) + const auto key_modifier = (Key::Modifiers)(key.modifiers & Key::Modifiers::MouseEvent); + switch (key_modifier) { - case Key::Modifiers::MousePress: + case Key::Modifiers::MousePressRight: + m_dragging = false; + cursor = context.window().buffer_coord(key.coord()); + if (key.modifiers & Key::Modifiers::Control) + selections = {{selections.begin()->anchor(), cursor}}; + else + selections.main() = {selections.main().anchor(), cursor}; + selections.sort_and_merge_overlapping(); + return true; + + case Key::Modifiers::MousePressLeft: m_dragging = true; m_anchor = context.window().buffer_coord(key.coord()); if (not (key.modifiers & Key::Modifiers::Control)) @@ -108,7 +119,8 @@ struct MouseHandler } return true; - case Key::Modifiers::MouseRelease: + case Key::Modifiers::MouseReleaseLeft: + case Key::Modifiers::MouseReleaseRight: if (not m_dragging) return true; m_dragging = false; diff --git a/src/json_ui.cc b/src/json_ui.cc index 32524c3b..fc021cb8 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -424,10 +424,14 @@ void JsonUI::eval_json(const Value& json) const Codepoint coord = encode_coord({params[1].as(), params[2].as()}); if (type == "move") m_on_key({Key::Modifiers::MousePos, coord}); - else if (type == "press") - m_on_key({Key::Modifiers::MousePress, coord}); - else if (type == "release") - m_on_key({Key::Modifiers::MouseRelease, coord}); + else if (type == "press_left") + m_on_key({Key::Modifiers::MousePressLeft, coord}); + else if (type == "press_right") + m_on_key({Key::Modifiers::MousePressRight, coord}); + else if (type == "release_left") + m_on_key({Key::Modifiers::MouseReleaseLeft, coord}); + else if (type == "release_right") + m_on_key({Key::Modifiers::MouseReleaseRight, coord}); else if (type == "wheel_up") m_on_key({Key::Modifiers::MouseWheelUp, coord}); else if (type == "wheel_down") diff --git a/src/keys.cc b/src/keys.cc index e4318707..e624767c 100644 --- a/src/keys.cc +++ b/src/keys.cc @@ -152,10 +152,14 @@ String key_to_str(Key key) { case Key::Modifiers::MousePos: return format("", coord.line, coord.column); - case Key::Modifiers::MousePress: - return format("", coord.line, coord.column); - case Key::Modifiers::MouseRelease: - return format("", coord.line, coord.column); + case Key::Modifiers::MousePressLeft: + return format("", coord.line, coord.column); + case Key::Modifiers::MousePressRight: + return format("", coord.line, coord.column); + case Key::Modifiers::MouseReleaseLeft: + return format("", coord.line, coord.column); + case Key::Modifiers::MouseReleaseRight: + return format("", coord.line, coord.column); case Key::Modifiers::MouseWheelDown: return ""; case Key::Modifiers::MouseWheelUp: diff --git a/src/keys.hh b/src/keys.hh index a7684f22..b0940bd6 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -21,16 +21,19 @@ struct Key Alt = 1 << 1, Shift = 1 << 2, - MousePress = 1 << 3, - MouseRelease = 1 << 4, - MousePos = 1 << 5, - MouseWheelDown = 1 << 6, - MouseWheelUp = 1 << 7, - MouseEvent = MousePress | MouseRelease | MousePos | - MouseWheelDown | MouseWheelUp, + MousePressLeft = 1 << 3, + MousePressRight = 1 << 4, + MouseReleaseLeft = 1 << 5, + MouseReleaseRight = 1 << 6, + MousePos = 1 << 7, + MouseWheelDown = 1 << 8, + MouseWheelUp = 1 << 9, + MouseEvent = MousePressLeft | MousePressRight | + MouseReleaseLeft | MouseReleaseRight | + MousePos | MouseWheelDown | MouseWheelUp, - Resize = 1 << 8, - MenuSelect = 1 << 10, + Resize = 1 << 10, + MenuSelect = 1 << 11, }; enum NamedKey : Codepoint { diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index 653f5c95..452c2e8b 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -565,9 +565,13 @@ Optional NCursesUI::get_next_key() res |= Key::Modifiers::Alt; if (BUTTON_PRESS(mask, 1)) - return res | Key::Modifiers::MousePress; + return res | Key::Modifiers::MousePressLeft; + if (BUTTON_PRESS(mask, 3)) + return res | Key::Modifiers::MousePressRight; if (BUTTON_RELEASE(mask, 1)) - return res | Key::Modifiers::MouseRelease; + return res | Key::Modifiers::MouseReleaseLeft; + if (BUTTON_RELEASE(mask, 3)) + return res | Key::Modifiers::MouseReleaseRight; if (BUTTON_PRESS(mask, m_wheel_down_button)) return res | Key::Modifiers::MouseWheelDown; if (BUTTON_PRESS(mask, m_wheel_up_button))