#include "Mouse.h" #include "Client.h" #include #include #include #include static SharedPtr g_mouse_cursor; static Mouse* s_mouse; Mouse::Mouse(ui::Canvas& screen) { m_position.x = screen.width / 2; m_position.y = screen.height / 2; m_screen_rect = screen.rect(); g_mouse_cursor = ui::Image::load("/usr/share/cursors/default.tga").value_or({}); s_mouse = this; } Mouse& Mouse::the() { check(s_mouse); return *s_mouse; } void Mouse::draw(ui::Canvas& screen) { if (!g_mouse_cursor) return; auto canvas = screen.subcanvas(ui::Rect { m_position, g_mouse_cursor->width(), g_mouse_cursor->height() }); canvas.fill(g_mouse_cursor->pixels(), g_mouse_cursor->width()); } void Mouse::update(const moon::MousePacket& packet) { m_position.x += packet.xdelta; m_position.y -= packet.ydelta; m_position = m_screen_rect.normalize(m_position); if (m_dragging_window && !(packet.buttons & moon::MouseButton::Left)) { os::println("Stopped drag: window at (%d,%d,%d,%d) with offset (%d,%d)", m_dragging_window->surface.pos.x, m_dragging_window->surface.pos.y, m_dragging_window->surface.width, m_dragging_window->surface.height, this->m_initial_drag_position.x, this->m_initial_drag_position.y); m_dragging_window = nullptr; } if (m_dragging_window) { m_dragging_window->surface.pos = ui::Point { m_position.x - m_initial_drag_position.x, m_position.y - m_initial_drag_position.y }; m_dragging_window->surface = m_dragging_window->surface.normalized(); } else if ((packet.buttons & moon::MouseButton::Left) && !m_dragging_window) { // Iterate from the end of the list, since windows at the beginning are stacked at the bottom and windows at the // top are at the end. for (Window* window = g_windows.last().value_or(nullptr); window; window = g_windows.previous(window).value_or(nullptr)) { if (window->surface.absolute(window->close_button).contains(m_position)) { ui::WindowCloseRequest request; request.window = window->id; auto& client = *window->client; os::IPC::send_async(client.conn, request); break; } else if (window->surface.absolute(window->titlebar).contains(m_position)) { m_dragging_window = window; m_initial_drag_position = window->surface.relative(m_position); os::println("Started drag: window at (%d,%d,%d,%d) with offset (%d,%d)", window->surface.pos.x, window->surface.pos.y, window->surface.width, window->surface.height, m_initial_drag_position.x, m_initial_drag_position.y); window->focus(); break; } else if (window->surface.absolute(window->contents).contains(m_position)) { window->focus(); break; // We don't want to continue iterating, otherwise this would take into account windows whose // titlebar is underneath another window's contents! } } } Window* new_active_window = nullptr; for (Window* window = g_windows.last().value_or(nullptr); window; window = g_windows.previous(window).value_or(nullptr)) { auto titlebar = window->surface.absolute(window->titlebar); auto contents = window->surface.absolute(window->contents); if (titlebar.contains(m_position)) break; if (contents.contains(m_position)) { ui::MouseEventRequest request; request.window = window->id; request.position = contents.relative(m_position); request.buttons = packet.buttons; os::IPC::send_async(window->client->conn, request); new_active_window = window; break; } } if (m_active_window != new_active_window) { if (m_active_window) { ui::MouseLeaveRequest request; request.window = m_active_window->id; os::IPC::send_async(m_active_window->client->conn, request); } m_active_window = new_active_window; } } void Mouse::window_did_close(Window* window) { if (m_dragging_window == window) { m_dragging_window = nullptr; } if (m_active_window == window) { m_active_window = nullptr; } }