Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WindowServer: More natural mouse menu navigation #1097

Merged
merged 2 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Libraries/LibDraw/Triangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020, Shannon Booth <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <LibDraw/Point.h>

class Triangle {
public:
Triangle(Point a, Point b, Point c)
: m_a(a)
, m_b(b)
, m_c(c)
{
m_det = (m_b.x() - m_a.x()) * (m_c.y() - m_a.y()) - (m_b.y() - m_a.y()) * (m_c.x() - m_a.x());
}

Point a() const { return m_a; }
Point b() const { return m_b; }
Point c() const { return m_c; }

bool contains(Point p) const
{
int x = p.x();
int y = p.y();

int ax = m_a.x();
int bx = m_b.x();
int cx = m_c.x();

int ay = m_a.y();
int by = m_b.y();
int cy = m_c.y();

if (m_det * ((bx - ax) * (y - ay) - (by - ay) * (x - ax)) <= 0)
return false;
if (m_det * ((cx - bx) * (y - by) - (cy - by) * (x - bx)) <= 0)
return false;
if (m_det * ((ax - cx) * (y - cy) - (ay - cy) * (x - cx)) <= 0)
return false;
return true;
}

String to_string() const { return String::format("(%s,%s,%s)", m_a.to_string().characters(), m_b.to_string().characters(), m_c.to_string().characters()); }

private:
int m_det;
Point m_a;
Point m_b;
Point m_c;
};

inline const LogStream& operator<<(const LogStream& stream, const Triangle& value)
{
return stream << value.to_string();
}
20 changes: 19 additions & 1 deletion Servers/WindowServer/WSMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2020, Shannon Booth <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -37,6 +38,7 @@
#include <LibDraw/GraphicsBitmap.h>
#include <LibDraw/Painter.h>
#include <LibDraw/StylePainter.h>
#include <LibDraw/Triangle.h>
#include <WindowServer/WSClientConnection.h>
#include <WindowServer/WindowClientEndpoint.h>

Expand Down Expand Up @@ -279,7 +281,23 @@ void WSMenu::event(CEvent& event)
{
if (event.type() == WSEvent::MouseMove) {
ASSERT(menu_window());
int index = item_index_at(static_cast<const WSMouseEvent&>(event).position());
auto mouse_event = static_cast<const WSMouseEvent&>(event);

if (hovered_item() && hovered_item()->is_submenu()) {

auto item = *hovered_item();
auto submenu_top_left = item.rect().location() + Point { item.rect().width(), 0 };
auto submenu_bottom_left = submenu_top_left + Point { 0, item.submenu()->height() };

auto safe_hover_triangle = Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
m_last_position_in_hover = mouse_event.position();

// Don't update the hovered item if mouse is moving towards a submenu
if (safe_hover_triangle.contains(mouse_event.position()))
return;
}

int index = item_index_at(mouse_event.position());
if (m_hovered_item_index == index)
return;
m_hovered_item_index = index;
Expand Down
2 changes: 1 addition & 1 deletion Servers/WindowServer/WSMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class WSMenu final : public CObject {

WeakPtr<WSWindow> m_window_menu_of;
bool m_is_window_menu_open = { false };

Point m_last_position_in_hover;
int m_theme_index_at_last_paint { -1 };
int m_hovered_item_index { -1 };
bool m_in_submenu { false };
Expand Down