mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 23:08:11 +00:00
WindowServer: Make WSWindowFrame and WSButton deal in relative coordinates.
This was a bit painful to get right. The code is a lot more pleasant to deal with now that all coordinates are relative to their local system instead of being absolute screen coordinates.
This commit is contained in:
parent
2ac2d79a8e
commit
0d60c56b51
6 changed files with 67 additions and 79 deletions
|
@ -16,8 +16,10 @@ WSButton::~WSButton()
|
||||||
|
|
||||||
void WSButton::paint(Painter& painter)
|
void WSButton::paint(Painter& painter)
|
||||||
{
|
{
|
||||||
StylePainter::paint_button(painter, m_rect, ButtonStyle::Normal, m_pressed);
|
PainterStateSaver saver(painter);
|
||||||
auto x_location = m_rect.center();
|
painter.translate(relative_rect().location());
|
||||||
|
StylePainter::paint_button(painter, rect(), ButtonStyle::Normal, m_pressed);
|
||||||
|
auto x_location = rect().center();
|
||||||
x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
|
x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
|
||||||
painter.draw_bitmap(x_location, *m_bitmap, Color::Black);
|
painter.draw_bitmap(x_location, *m_bitmap, Color::Black);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,10 @@ public:
|
||||||
WSButton(Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler);
|
WSButton(Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler);
|
||||||
~WSButton();
|
~WSButton();
|
||||||
|
|
||||||
Rect rect() const { return m_rect; }
|
Rect relative_rect() const { return m_relative_rect; }
|
||||||
void set_rect(const Rect& rect) { m_rect = rect; }
|
void set_relative_rect(const Rect& rect) { m_relative_rect = rect; }
|
||||||
|
|
||||||
|
Rect rect() const { return { { }, m_relative_rect.size() }; }
|
||||||
|
|
||||||
void paint(Painter&);
|
void paint(Painter&);
|
||||||
|
|
||||||
|
@ -25,7 +27,7 @@ public:
|
||||||
bool is_visible() const { return m_visible; }
|
bool is_visible() const { return m_visible; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Rect m_rect;
|
Rect m_relative_rect;
|
||||||
Retained<CharacterBitmap> m_bitmap;
|
Retained<CharacterBitmap> m_bitmap;
|
||||||
bool m_pressed { false };
|
bool m_pressed { false };
|
||||||
bool m_visible { true };
|
bool m_visible { true };
|
||||||
|
|
|
@ -579,6 +579,8 @@ public:
|
||||||
unsigned buttons() const { return m_buttons; }
|
unsigned buttons() const { return m_buttons; }
|
||||||
unsigned modifiers() const { return m_modifiers; }
|
unsigned modifiers() const { return m_modifiers; }
|
||||||
|
|
||||||
|
WSMouseEvent translated(const Point& delta) const { return WSMouseEvent(type(), m_position.translated(delta), m_buttons, m_button, m_modifiers); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Point m_position;
|
Point m_position;
|
||||||
unsigned m_buttons { 0 };
|
unsigned m_buttons { 0 };
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#include <SharedGraphics/Font.h>
|
#include <SharedGraphics/Font.h>
|
||||||
#include <SharedGraphics/StylePainter.h>
|
#include <SharedGraphics/StylePainter.h>
|
||||||
|
|
||||||
|
static const int window_titlebar_height = 17;
|
||||||
|
|
||||||
static const char* s_close_button_bitmap_data = {
|
static const char* s_close_button_bitmap_data = {
|
||||||
"## ##"
|
"## ##"
|
||||||
"### ###"
|
"### ###"
|
||||||
|
@ -39,26 +41,19 @@ WSWindowFrame::~WSWindowFrame()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int window_titlebar_height = 18;
|
|
||||||
|
|
||||||
static inline Rect menu_window_rect(const Rect& rect)
|
static inline Rect menu_window_rect(const Rect& rect)
|
||||||
{
|
{
|
||||||
return rect.inflated(2, 2);
|
return rect.inflated(2, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Rect title_bar_rect(const Rect& window)
|
Rect WSWindowFrame::title_bar_rect() const
|
||||||
{
|
{
|
||||||
return {
|
return { 2, 2, m_window.width() + 2, window_titlebar_height };
|
||||||
window.x() - 1,
|
|
||||||
window.y() - window_titlebar_height,
|
|
||||||
window.width() + 2,
|
|
||||||
window_titlebar_height
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Rect title_bar_icon_rect(const Rect& window)
|
Rect WSWindowFrame::title_bar_icon_rect() const
|
||||||
{
|
{
|
||||||
auto titlebar_rect = title_bar_rect(window);
|
auto titlebar_rect = title_bar_rect();
|
||||||
return {
|
return {
|
||||||
titlebar_rect.x() + 2,
|
titlebar_rect.x() + 2,
|
||||||
titlebar_rect.y(),
|
titlebar_rect.y(),
|
||||||
|
@ -67,10 +62,10 @@ static inline Rect title_bar_icon_rect(const Rect& window)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Rect title_bar_text_rect(const Rect& window)
|
Rect WSWindowFrame::title_bar_text_rect() const
|
||||||
{
|
{
|
||||||
auto titlebar_rect = title_bar_rect(window);
|
auto titlebar_rect = title_bar_rect();
|
||||||
auto titlebar_icon_rect = title_bar_icon_rect(window);
|
auto titlebar_icon_rect = title_bar_icon_rect();
|
||||||
return {
|
return {
|
||||||
titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2,
|
titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2,
|
||||||
titlebar_rect.y(),
|
titlebar_rect.y(),
|
||||||
|
@ -79,38 +74,15 @@ static inline Rect title_bar_text_rect(const Rect& window)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Rect border_window_rect(const Rect& window)
|
Rect WSWindowFrame::middle_border_rect() const
|
||||||
{
|
{
|
||||||
auto titlebar_rect = title_bar_rect(window);
|
return { 1, 1, m_window.width() + 4, m_window.height() + 4 + window_titlebar_height };
|
||||||
return { titlebar_rect.x() - 1,
|
|
||||||
titlebar_rect.y() - 1,
|
|
||||||
titlebar_rect.width() + 2,
|
|
||||||
window_titlebar_height + window.height() + 3
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline Rect outer_window_rect(const Rect& window)
|
|
||||||
{
|
|
||||||
auto rect = border_window_rect(window);
|
|
||||||
rect.inflate(2, 2);
|
|
||||||
return rect;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline Rect outer_window_rect(const WSWindow& window)
|
|
||||||
{
|
|
||||||
if (window.type() == WSWindowType::Menu)
|
|
||||||
return menu_window_rect(window.rect());
|
|
||||||
if (window.type() == WSWindowType::WindowSwitcher)
|
|
||||||
return window.rect();
|
|
||||||
if (window.type() == WSWindowType::Taskbar)
|
|
||||||
return window.rect();
|
|
||||||
ASSERT(window.type() == WSWindowType::Normal);
|
|
||||||
return outer_window_rect(window.rect());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSWindowFrame::paint(Painter& painter)
|
void WSWindowFrame::paint(Painter& painter)
|
||||||
{
|
{
|
||||||
//printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
|
PainterStateSaver saver(painter);
|
||||||
|
painter.translate(rect().location());
|
||||||
|
|
||||||
if (m_window.type() == WSWindowType::Menu) {
|
if (m_window.type() == WSWindowType::Menu) {
|
||||||
painter.draw_rect(menu_window_rect(m_window.rect()), Color::LightGray);
|
painter.draw_rect(menu_window_rect(m_window.rect()), Color::LightGray);
|
||||||
|
@ -125,18 +97,17 @@ void WSWindowFrame::paint(Painter& painter)
|
||||||
|
|
||||||
auto& window = m_window;
|
auto& window = m_window;
|
||||||
|
|
||||||
auto titlebar_rect = title_bar_rect(window.rect());
|
auto titlebar_rect = title_bar_rect();
|
||||||
auto titlebar_icon_rect = title_bar_icon_rect(window.rect());
|
auto titlebar_icon_rect = title_bar_icon_rect();
|
||||||
auto titlebar_inner_rect = title_bar_text_rect(window.rect());
|
auto titlebar_inner_rect = title_bar_text_rect();
|
||||||
auto outer_rect = outer_window_rect(window);
|
Rect outer_rect = { { }, rect().size() };
|
||||||
auto border_rect = border_window_rect(window.rect());
|
|
||||||
|
|
||||||
auto titlebar_title_rect = titlebar_inner_rect;
|
auto titlebar_title_rect = titlebar_inner_rect;
|
||||||
titlebar_title_rect.set_width(Font::default_bold_font().width(window.title()));
|
titlebar_title_rect.set_width(Font::default_bold_font().width(window.title()));
|
||||||
|
|
||||||
Rect inner_border_rect {
|
Rect inner_border_rect {
|
||||||
window.x() - 1,
|
2,
|
||||||
window.y() - 1,
|
2 + window_titlebar_height,
|
||||||
window.width() + 2,
|
window.width() + 2,
|
||||||
window.height() + 2
|
window.height() + 2
|
||||||
};
|
};
|
||||||
|
@ -170,13 +141,13 @@ void WSWindowFrame::paint(Painter& painter)
|
||||||
middle_border_color = Color::MidGray;
|
middle_border_color = Color::MidGray;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto leftmost_button_rect = m_buttons.is_empty() ? Rect() : m_buttons.last()->rect();
|
auto leftmost_button_rect = m_buttons.is_empty() ? Rect() : m_buttons.last()->relative_rect();
|
||||||
|
|
||||||
painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
|
painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
|
||||||
for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
|
for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
|
||||||
painter.draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { leftmost_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
|
painter.draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { leftmost_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
|
||||||
}
|
}
|
||||||
painter.draw_rect(border_rect, middle_border_color);
|
painter.draw_rect(middle_border_rect(), middle_border_color);
|
||||||
painter.draw_rect(outer_rect, border_color);
|
painter.draw_rect(outer_rect, border_color);
|
||||||
painter.draw_rect(inner_border_rect, border_color);
|
painter.draw_rect(inner_border_rect, border_color);
|
||||||
|
|
||||||
|
@ -189,35 +160,42 @@ void WSWindowFrame::paint(Painter& painter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Rect frame_rect_for_window_type(WSWindowType type, const Rect& rect)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case WSWindowType::Menu:
|
||||||
|
return menu_window_rect(rect);
|
||||||
|
case WSWindowType::Normal:
|
||||||
|
return { rect.x() - 3, rect.y() - window_titlebar_height - 3, rect.width() + 6, rect.height() + 6 + window_titlebar_height };
|
||||||
|
case WSWindowType::WindowSwitcher:
|
||||||
|
return rect;
|
||||||
|
case WSWindowType::Taskbar:
|
||||||
|
return rect;
|
||||||
|
default:
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Rect WSWindowFrame::rect() const
|
Rect WSWindowFrame::rect() const
|
||||||
{
|
{
|
||||||
if (m_window.type() == WSWindowType::Menu)
|
return frame_rect_for_window_type(m_window.type(), m_window.rect());
|
||||||
return menu_window_rect(m_window.rect());
|
|
||||||
if (m_window.type() == WSWindowType::Normal)
|
|
||||||
return outer_window_rect(m_window);
|
|
||||||
if (m_window.type() == WSWindowType::WindowSwitcher)
|
|
||||||
return m_window.rect();
|
|
||||||
if (m_window.type() == WSWindowType::Taskbar)
|
|
||||||
return m_window.rect();
|
|
||||||
ASSERT_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect)
|
void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect)
|
||||||
{
|
{
|
||||||
int window_button_width = 15;
|
int window_button_width = 15;
|
||||||
int window_button_height = 15;
|
int window_button_height = 15;
|
||||||
int x = title_bar_text_rect(new_rect).right() + 1;;
|
int x = title_bar_text_rect().right() + 1;;
|
||||||
for (auto& button : m_buttons) {
|
for (auto& button : m_buttons) {
|
||||||
x -= window_button_width;
|
x -= window_button_width;
|
||||||
Rect rect { x, 0, window_button_width, window_button_height };
|
Rect rect { x, 0, window_button_width, window_button_height };
|
||||||
rect.center_vertically_within(title_bar_rect(new_rect));
|
rect.center_vertically_within(title_bar_text_rect());
|
||||||
rect.set_y(rect.y() - 1);
|
button->set_relative_rect(rect);
|
||||||
button->set_rect(rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& wm = WSWindowManager::the();
|
auto& wm = WSWindowManager::the();
|
||||||
wm.invalidate(outer_window_rect(old_rect));
|
wm.invalidate(frame_rect_for_window_type(m_window.type(), old_rect));
|
||||||
wm.invalidate(outer_window_rect(new_rect));
|
wm.invalidate(frame_rect_for_window_type(m_window.type(), new_rect));
|
||||||
wm.notify_rect_changed(m_window, old_rect, new_rect);
|
wm.notify_rect_changed(m_window, old_rect, new_rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,15 +204,15 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
|
||||||
auto& wm = WSWindowManager::the();
|
auto& wm = WSWindowManager::the();
|
||||||
if (m_window.type() != WSWindowType::Normal)
|
if (m_window.type() != WSWindowType::Normal)
|
||||||
return;
|
return;
|
||||||
if (title_bar_rect(m_window.rect()).contains(event.position())) {
|
if (title_bar_rect().contains(event.position())) {
|
||||||
if (event.type() == WSMessage::MouseDown)
|
if (event.type() == WSMessage::MouseDown)
|
||||||
wm.move_to_front_and_make_active(m_window);
|
wm.move_to_front_and_make_active(m_window);
|
||||||
|
|
||||||
for (auto& button : m_buttons) {
|
for (auto& button : m_buttons) {
|
||||||
if (button->rect().contains(event.position()))
|
if (button->relative_rect().contains(event.position()))
|
||||||
return button->on_mouse_event(event);
|
return button->on_mouse_event(event);
|
||||||
}
|
}
|
||||||
if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left)
|
if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left)
|
||||||
wm.start_window_drag(m_window, event);
|
wm.start_window_drag(m_window, event.translated(rect().location()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,10 @@ public:
|
||||||
void notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect);
|
void notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handle_close_button_mouse_event(const WSMouseEvent&);
|
Rect title_bar_rect() const;
|
||||||
|
Rect title_bar_icon_rect() const;
|
||||||
|
Rect title_bar_text_rect() const;
|
||||||
|
Rect middle_border_rect() const;
|
||||||
|
|
||||||
WSWindow& m_window;
|
WSWindow& m_window;
|
||||||
Vector<OwnPtr<WSButton>> m_buttons;
|
Vector<OwnPtr<WSButton>> m_buttons;
|
||||||
|
|
|
@ -621,7 +621,7 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
|
||||||
continue;
|
continue;
|
||||||
ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
|
ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
|
||||||
windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
|
windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
|
||||||
window->on_message(WSMouseEvent(event.type(), event.position().translated(-window->position()), event.buttons(), event.button(), event.modifiers()));
|
window->on_message(event.translated(-window->position()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (menubar_rect().contains(event.position())) {
|
if (menubar_rect().contains(event.position())) {
|
||||||
|
@ -640,7 +640,8 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
|
||||||
}
|
}
|
||||||
|
|
||||||
for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
|
for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
|
||||||
if (!window.frame().rect().contains(event.position()))
|
auto window_frame_rect = window.frame().rect();
|
||||||
|
if (!window_frame_rect.contains(event.position()))
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
// First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
|
// First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
|
||||||
// In those cases, the event is swallowed by the window manager.
|
// In those cases, the event is swallowed by the window manager.
|
||||||
|
@ -660,12 +661,12 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
|
||||||
move_to_front_and_make_active(window);
|
move_to_front_and_make_active(window);
|
||||||
event_window = &window;
|
event_window = &window;
|
||||||
if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window))
|
if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window))
|
||||||
window.on_message(WSMouseEvent(event.type(), event.position().translated(-window.position()), event.buttons(), event.button(), event.modifiers()));
|
window.on_message(event.translated(-window.position()));
|
||||||
return IterationDecision::Abort;
|
return IterationDecision::Abort;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We are hitting the frame, pass the event along to WSWindowFrame.
|
// We are hitting the frame, pass the event along to WSWindowFrame.
|
||||||
window.frame().on_mouse_event(event);
|
window.frame().on_mouse_event(event.translated(-window_frame_rect.location()));
|
||||||
return IterationDecision::Abort;
|
return IterationDecision::Abort;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue