Cleanup mouse handling a little

This commit is contained in:
Maxime Coste 2015-08-16 14:06:07 +01:00
parent 97b871d49e
commit 043ca99983
2 changed files with 21 additions and 23 deletions

View File

@ -73,43 +73,44 @@ struct MouseHandler
return false; return false;
Buffer& buffer = context.buffer(); Buffer& buffer = context.buffer();
if (key.modifiers == Key::Modifiers::MousePress) ByteCoord cursor;
switch (key.modifiers)
{ {
case Key::Modifiers::MousePress:
m_dragging = true; m_dragging = true;
m_anchor = context.window().buffer_coord(key.mouse_coord()); m_anchor = context.window().buffer_coord(key.mouse_coord());
context.selections_write_only() = SelectionList{ buffer, m_anchor }; context.selections_write_only() = SelectionList{ buffer, m_anchor };
return true; return true;
}
if (key.modifiers == Key::Modifiers::MouseRelease) case Key::Modifiers::MouseRelease:
{
if (not m_dragging) if (not m_dragging)
return true; return true;
m_dragging = false; m_dragging = false;
auto cursor = context.window().buffer_coord(key.mouse_coord()); cursor = context.window().buffer_coord(key.mouse_coord());
context.selections_write_only() = context.selections_write_only() =
SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} }; SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} };
return true; return true;
}
if (key.modifiers == Key::Modifiers::MousePos) case Key::Modifiers::MousePos:
{
if (not m_dragging) if (not m_dragging)
return true; return true;
auto cursor = context.window().buffer_coord(key.mouse_coord()); cursor = context.window().buffer_coord(key.mouse_coord());
context.selections_write_only() = context.selections_write_only() =
SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} }; SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} };
return true; return true;
}
if (key.modifiers == Key::Modifiers::MouseWheelDown) case Key::Modifiers::MouseWheelDown:
{ m_dragging = false;
wheel(context, 3); wheel(context, 3);
return true; return true;
}
if (key.modifiers == Key::Modifiers::MouseWheelUp) case Key::Modifiers::MouseWheelUp:
{ m_dragging = false;
wheel(context, -3); wheel(context, -3);
return true; return true;
default: return false;
} }
return false;
} }
private: private:

View File

@ -482,14 +482,11 @@ Key NCursesUI::get_key()
MEVENT ev; MEVENT ev;
if (getmouse(&ev) == OK) if (getmouse(&ev) == OK)
{ {
auto wheel_down_mask = NCURSES_MOUSE_MASK(m_wheel_down_button, NCURSES_BUTTON_PRESSED);
auto wheel_up_mask = NCURSES_MOUSE_MASK(m_wheel_up_button, NCURSES_BUTTON_PRESSED);
CharCoord pos{ ev.y - (m_status_on_top ? 1 : 0), ev.x }; CharCoord pos{ ev.y - (m_status_on_top ? 1 : 0), ev.x };
if ((ev.bstate & BUTTON1_PRESSED) == BUTTON1_PRESSED) return mouse_press(pos); if (BUTTON_PRESS(ev.bstate, 1)) return mouse_press(pos);
if ((ev.bstate & BUTTON1_RELEASED) == BUTTON1_RELEASED) return mouse_release(pos); if (BUTTON_RELEASE(ev.bstate, 1)) return mouse_release(pos);
if ((ev.bstate & wheel_down_mask) == wheel_down_mask) return mouse_wheel_down(pos); if (BUTTON_PRESS(ev.bstate, m_wheel_down_button)) return mouse_wheel_down(pos);
if ((ev.bstate & wheel_up_mask) == wheel_up_mask) return mouse_wheel_up(pos); if (BUTTON_PRESS(ev.bstate, m_wheel_up_button)) return mouse_wheel_up(pos);
return mouse_pos(pos); return mouse_pos(pos);
} }
} }