diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-05 18:40:36 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-05 18:40:36 +0200 |
commit | 3155a2e12881823c1c5353bee6c92b39ac5fe4fe (patch) | |
tree | 8111dfcc485da685a8e76c0376d0b7a234f90d7b | |
parent | 9fbac66a91e831d46adb1deee472feeb61e438be (diff) | |
download | serenity-3155a2e12881823c1c5353bee6c92b39ac5fe4fe.zip |
WindowServer: Add a WSButton class and make the window close buttons use it.
-rw-r--r-- | Servers/WindowServer/Makefile | 1 | ||||
-rw-r--r-- | Servers/WindowServer/WSButton.cpp | 31 | ||||
-rw-r--r-- | Servers/WindowServer/WSButton.h | 32 | ||||
-rw-r--r-- | Servers/WindowServer/WSWindowFrame.cpp | 94 | ||||
-rw-r--r-- | Servers/WindowServer/WSWindowFrame.h | 4 |
5 files changed, 112 insertions, 50 deletions
diff --git a/Servers/WindowServer/Makefile b/Servers/WindowServer/Makefile index ebed131563..5a39579bf1 100644 --- a/Servers/WindowServer/Makefile +++ b/Servers/WindowServer/Makefile @@ -23,6 +23,7 @@ WINDOWSERVER_OBJS = \ WSClipboard.o \ WSCursor.o \ WSWindowFrame.o \ + WSButton.o \ main.o APP = WindowServer diff --git a/Servers/WindowServer/WSButton.cpp b/Servers/WindowServer/WSButton.cpp new file mode 100644 index 0000000000..66baffd8bd --- /dev/null +++ b/Servers/WindowServer/WSButton.cpp @@ -0,0 +1,31 @@ +#include <WindowServer/WSButton.h> +#include <WindowServer/WSMessage.h> +#include <SharedGraphics/Painter.h> +#include <SharedGraphics/StylePainter.h> +#include <SharedGraphics/CharacterBitmap.h> + +WSButton::WSButton(Retained<CharacterBitmap>&& bitmap, Function<void()>&& on_click_handler) + : on_click(move(on_click_handler)) + , m_bitmap(move(bitmap)) +{ +} + +WSButton::~WSButton() +{ +} + +void WSButton::paint(Painter& painter) +{ + StylePainter::paint_button(painter, m_rect, ButtonStyle::Normal, m_pressed); + auto x_location = m_rect.center(); + x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2)); + painter.draw_bitmap(x_location, *m_bitmap, Color::Black); +} + +void WSButton::on_mouse_event(const WSMouseEvent& event) +{ + if (event.type() == WSMessage::MouseDown) { + if (on_click) + on_click(); + } +} diff --git a/Servers/WindowServer/WSButton.h b/Servers/WindowServer/WSButton.h new file mode 100644 index 0000000000..5cd54587d0 --- /dev/null +++ b/Servers/WindowServer/WSButton.h @@ -0,0 +1,32 @@ +#pragma once + +#include <SharedGraphics/Rect.h> +#include <AK/Function.h> +#include <AK/Retained.h> + +class CharacterBitmap; +class Painter; +class WSMouseEvent; + +class WSButton { +public: + WSButton(Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler); + ~WSButton(); + + Rect rect() const { return m_rect; } + void set_rect(const Rect& rect) { m_rect = rect; } + + void paint(Painter&); + + void on_mouse_event(const WSMouseEvent&); + + Function<void()> on_click; + + bool is_visible() const { return m_visible; } + +private: + Rect m_rect; + Retained<CharacterBitmap> m_bitmap; + bool m_pressed { false }; + bool m_visible { true }; +}; diff --git a/Servers/WindowServer/WSWindowFrame.cpp b/Servers/WindowServer/WSWindowFrame.cpp index 1e42dca3ac..32eb907499 100644 --- a/Servers/WindowServer/WSWindowFrame.cpp +++ b/Servers/WindowServer/WSWindowFrame.cpp @@ -2,14 +2,37 @@ #include <WindowServer/WSWindowManager.h> #include <WindowServer/WSWindow.h> #include <WindowServer/WSMessage.h> +#include <WindowServer/WSButton.h> #include <SharedGraphics/CharacterBitmap.h> #include <SharedGraphics/Painter.h> #include <SharedGraphics/Font.h> #include <SharedGraphics/StylePainter.h> +static const char* s_close_button_bitmap_data = { + "## ##" + "### ###" + " ###### " + " #### " + " ## " + " #### " + " ###### " + "### ###" + "## ##" +}; + +static CharacterBitmap* s_close_button_bitmap; +static const int s_close_button_bitmap_width = 8; +static const int s_close_button_bitmap_height = 9; + WSWindowFrame::WSWindowFrame(WSWindow& window) : m_window(window) { + if (!s_close_button_bitmap) + s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref(); + + m_buttons.append(make<WSButton>(*s_close_button_bitmap, [this] { + m_window.on_message(WSMessage(WSMessage::WindowCloseRequest)); + })); } WSWindowFrame::~WSWindowFrame() @@ -56,19 +79,6 @@ static inline Rect title_bar_text_rect(const Rect& window) }; } -static inline Rect close_button_rect_for_window(const Rect& window_rect) -{ - auto titlebar_inner_rect = title_bar_text_rect(window_rect); - int close_button_margin = 1; - int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2; - return Rect { - titlebar_inner_rect.right() - close_button_size + 1, - titlebar_inner_rect.top() + close_button_margin, - close_button_size, - close_button_size - 1 - }; -} - static inline Rect border_window_rect(const Rect& window) { auto titlebar_rect = title_bar_rect(window); @@ -98,22 +108,6 @@ static inline Rect outer_window_rect(const WSWindow& window) return outer_window_rect(window.rect()); } -static const char* s_close_button_bitmap_data = { - "## ##" - "### ###" - " ###### " - " #### " - " ## " - " #### " - " ###### " - "### ###" - "## ##" -}; - -static CharacterBitmap* s_close_button_bitmap; -static const int s_close_button_bitmap_width = 8; -static const int s_close_button_bitmap_height = 9; - 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()); @@ -136,7 +130,6 @@ void WSWindowFrame::paint(Painter& painter) auto titlebar_inner_rect = title_bar_text_rect(window.rect()); auto outer_rect = outer_window_rect(window); auto border_rect = border_window_rect(window.rect()); - auto close_button_rect = close_button_rect_for_window(window.rect()); auto titlebar_title_rect = titlebar_inner_rect; titlebar_title_rect.set_width(Font::default_bold_font().width(window.title())); @@ -177,9 +170,11 @@ void WSWindowFrame::paint(Painter& painter) middle_border_color = Color::MidGray; } + auto leftmost_button_rect = m_buttons.is_empty() ? Rect() : m_buttons.last()->rect(); + painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2); for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) { - painter.draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_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(outer_rect, border_color); @@ -189,14 +184,9 @@ void WSWindowFrame::paint(Painter& painter) painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect()); - if (!s_close_button_bitmap) - s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref(); - - StylePainter::paint_button(painter, close_button_rect, ButtonStyle::Normal, false, false); - - auto x_location = close_button_rect.center(); - x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2)); - painter.draw_bitmap(x_location, *s_close_button_bitmap, Color::Black); + for (auto& button : m_buttons) { + button->paint(painter); + } } Rect WSWindowFrame::rect() const @@ -214,6 +204,17 @@ Rect WSWindowFrame::rect() const void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect) { + int window_button_width = 15; + int window_button_height = 15; + int x = title_bar_text_rect(new_rect).right() + 1;; + for (auto& button : m_buttons) { + x -= window_button_width; + Rect rect { x, 0, window_button_width, window_button_height }; + rect.center_vertically_within(title_bar_rect(new_rect)); + rect.set_y(rect.y() - 1); + button->set_rect(rect); + } + auto& wm = WSWindowManager::the(); wm.invalidate(outer_window_rect(old_rect)); wm.invalidate(outer_window_rect(new_rect)); @@ -228,19 +229,12 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event) if (title_bar_rect(m_window.rect()).contains(event.position())) { if (event.type() == WSMessage::MouseDown) wm.move_to_front_and_make_active(m_window); - if (close_button_rect_for_window(m_window.rect()).contains(event.position())) { - handle_close_button_mouse_event(event); - return; + + for (auto& button : m_buttons) { + if (button->rect().contains(event.position())) + return button->on_mouse_event(event); } if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) wm.start_window_drag(m_window, event); } } - -void WSWindowFrame::handle_close_button_mouse_event(const WSMouseEvent& event) -{ - if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) { - m_window.on_message(WSMessage(WSMessage::WindowCloseRequest)); - return; - } -} diff --git a/Servers/WindowServer/WSWindowFrame.h b/Servers/WindowServer/WSWindowFrame.h index 9eb47d82e7..270de4c2b1 100644 --- a/Servers/WindowServer/WSWindowFrame.h +++ b/Servers/WindowServer/WSWindowFrame.h @@ -1,7 +1,10 @@ #pragma once +#include <AK/Vector.h> + class Painter; class Rect; +class WSButton; class WSMouseEvent; class WSWindow; @@ -19,4 +22,5 @@ private: void handle_close_button_mouse_event(const WSMouseEvent&); WSWindow& m_window; + Vector<OwnPtr<WSButton>> m_buttons; }; |