mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
WindowServer: Add a WSButton class and make the window close buttons use it.
This commit is contained in:
parent
9fbac66a91
commit
3155a2e128
5 changed files with 112 additions and 50 deletions
|
@ -23,6 +23,7 @@ WINDOWSERVER_OBJS = \
|
||||||
WSClipboard.o \
|
WSClipboard.o \
|
||||||
WSCursor.o \
|
WSCursor.o \
|
||||||
WSWindowFrame.o \
|
WSWindowFrame.o \
|
||||||
|
WSButton.o \
|
||||||
main.o
|
main.o
|
||||||
|
|
||||||
APP = WindowServer
|
APP = WindowServer
|
||||||
|
|
31
Servers/WindowServer/WSButton.cpp
Normal file
31
Servers/WindowServer/WSButton.cpp
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
32
Servers/WindowServer/WSButton.h
Normal file
32
Servers/WindowServer/WSButton.h
Normal file
|
@ -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 };
|
||||||
|
};
|
|
@ -2,14 +2,37 @@
|
||||||
#include <WindowServer/WSWindowManager.h>
|
#include <WindowServer/WSWindowManager.h>
|
||||||
#include <WindowServer/WSWindow.h>
|
#include <WindowServer/WSWindow.h>
|
||||||
#include <WindowServer/WSMessage.h>
|
#include <WindowServer/WSMessage.h>
|
||||||
|
#include <WindowServer/WSButton.h>
|
||||||
#include <SharedGraphics/CharacterBitmap.h>
|
#include <SharedGraphics/CharacterBitmap.h>
|
||||||
#include <SharedGraphics/Painter.h>
|
#include <SharedGraphics/Painter.h>
|
||||||
#include <SharedGraphics/Font.h>
|
#include <SharedGraphics/Font.h>
|
||||||
#include <SharedGraphics/StylePainter.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)
|
WSWindowFrame::WSWindowFrame(WSWindow& window)
|
||||||
: m_window(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()
|
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)
|
static inline Rect border_window_rect(const Rect& window)
|
||||||
{
|
{
|
||||||
auto titlebar_rect = title_bar_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());
|
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)
|
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());
|
//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 titlebar_inner_rect = title_bar_text_rect(window.rect());
|
||||||
auto outer_rect = outer_window_rect(window);
|
auto outer_rect = outer_window_rect(window);
|
||||||
auto border_rect = border_window_rect(window.rect());
|
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;
|
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()));
|
||||||
|
@ -177,9 +170,11 @@ 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();
|
||||||
|
|
||||||
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() - 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(border_rect, middle_border_color);
|
||||||
painter.draw_rect(outer_rect, 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());
|
painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
|
||||||
|
|
||||||
if (!s_close_button_bitmap)
|
for (auto& button : m_buttons) {
|
||||||
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();
|
button->paint(painter);
|
||||||
|
}
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect WSWindowFrame::rect() const
|
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)
|
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();
|
auto& wm = WSWindowManager::the();
|
||||||
wm.invalidate(outer_window_rect(old_rect));
|
wm.invalidate(outer_window_rect(old_rect));
|
||||||
wm.invalidate(outer_window_rect(new_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 (title_bar_rect(m_window.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);
|
||||||
if (close_button_rect_for_window(m_window.rect()).contains(event.position())) {
|
|
||||||
handle_close_button_mouse_event(event);
|
for (auto& button : m_buttons) {
|
||||||
return;
|
if (button->rect().contains(event.position()))
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
class Painter;
|
class Painter;
|
||||||
class Rect;
|
class Rect;
|
||||||
|
class WSButton;
|
||||||
class WSMouseEvent;
|
class WSMouseEvent;
|
||||||
class WSWindow;
|
class WSWindow;
|
||||||
|
|
||||||
|
@ -19,4 +22,5 @@ private:
|
||||||
void handle_close_button_mouse_event(const WSMouseEvent&);
|
void handle_close_button_mouse_event(const WSMouseEvent&);
|
||||||
|
|
||||||
WSWindow& m_window;
|
WSWindow& m_window;
|
||||||
|
Vector<OwnPtr<WSButton>> m_buttons;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue