2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-11 23:46:49 +03:00
|
|
|
#include "ClientConnection.h"
|
2020-02-15 02:10:34 +03:00
|
|
|
#include <AK/Badge.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/CharacterBitmap.h>
|
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
#include <LibGfx/Painter.h>
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2020-02-06 22:03:37 +03:00
|
|
|
#include <WindowServer/Button.h>
|
|
|
|
#include <WindowServer/Compositor.h>
|
|
|
|
#include <WindowServer/Event.h>
|
|
|
|
#include <WindowServer/Window.h>
|
|
|
|
#include <WindowServer/WindowFrame.h>
|
|
|
|
#include <WindowServer/WindowManager.h>
|
|
|
|
|
|
|
|
namespace WindowServer {
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2019-04-05 19:40:36 +03:00
|
|
|
static const char* s_close_button_bitmap_data = {
|
|
|
|
"## ##"
|
|
|
|
"### ###"
|
|
|
|
" ###### "
|
|
|
|
" #### "
|
|
|
|
" #### "
|
|
|
|
" ###### "
|
|
|
|
"### ###"
|
|
|
|
"## ##"
|
2020-03-22 21:39:12 +03:00
|
|
|
" "
|
2019-04-05 19:40:36 +03:00
|
|
|
};
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
static Gfx::CharacterBitmap* s_close_button_bitmap;
|
2019-04-05 19:40:36 +03:00
|
|
|
static const int s_close_button_bitmap_width = 8;
|
|
|
|
static const int s_close_button_bitmap_height = 9;
|
|
|
|
|
2019-04-05 23:32:00 +03:00
|
|
|
static const char* s_minimize_button_bitmap_data = {
|
|
|
|
" "
|
|
|
|
" "
|
2019-04-30 16:09:40 +03:00
|
|
|
" "
|
2019-04-05 23:32:00 +03:00
|
|
|
" ###### "
|
|
|
|
" #### "
|
|
|
|
" ## "
|
|
|
|
" "
|
|
|
|
" "
|
2019-04-30 16:09:40 +03:00
|
|
|
" "
|
2019-04-05 23:32:00 +03:00
|
|
|
};
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
static Gfx::CharacterBitmap* s_minimize_button_bitmap;
|
2019-04-05 23:32:00 +03:00
|
|
|
static const int s_minimize_button_bitmap_width = 8;
|
|
|
|
static const int s_minimize_button_bitmap_height = 9;
|
|
|
|
|
2019-05-12 22:32:02 +03:00
|
|
|
static const char* s_maximize_button_bitmap_data = {
|
|
|
|
" "
|
|
|
|
" "
|
|
|
|
" "
|
|
|
|
" ## "
|
|
|
|
" #### "
|
|
|
|
" ###### "
|
|
|
|
" "
|
|
|
|
" "
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
static Gfx::CharacterBitmap* s_maximize_button_bitmap;
|
2019-05-12 22:32:02 +03:00
|
|
|
static const int s_maximize_button_bitmap_width = 8;
|
|
|
|
static const int s_maximize_button_bitmap_height = 9;
|
|
|
|
|
|
|
|
static const char* s_unmaximize_button_bitmap_data = {
|
|
|
|
" "
|
|
|
|
" ## "
|
|
|
|
" #### "
|
|
|
|
" ###### "
|
|
|
|
" "
|
|
|
|
" ###### "
|
|
|
|
" #### "
|
|
|
|
" ## "
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
static Gfx::CharacterBitmap* s_unmaximize_button_bitmap;
|
2019-05-12 22:32:02 +03:00
|
|
|
static const int s_unmaximize_button_bitmap_width = 8;
|
|
|
|
static const int s_unmaximize_button_bitmap_height = 9;
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
WindowFrame::WindowFrame(Window& window)
|
2019-04-05 16:54:56 +03:00
|
|
|
: m_window(window)
|
|
|
|
{
|
2019-04-05 19:40:36 +03:00
|
|
|
if (!s_close_button_bitmap)
|
2020-02-06 13:56:38 +03:00
|
|
|
s_close_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
|
2019-04-05 19:40:36 +03:00
|
|
|
|
2019-04-05 23:32:00 +03:00
|
|
|
if (!s_minimize_button_bitmap)
|
2020-02-06 13:56:38 +03:00
|
|
|
s_minimize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_minimize_button_bitmap_data, s_minimize_button_bitmap_width, s_minimize_button_bitmap_height).leak_ref();
|
2019-04-05 23:32:00 +03:00
|
|
|
|
2019-05-12 22:32:02 +03:00
|
|
|
if (!s_maximize_button_bitmap)
|
2020-02-06 13:56:38 +03:00
|
|
|
s_maximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_maximize_button_bitmap_data, s_maximize_button_bitmap_width, s_maximize_button_bitmap_height).leak_ref();
|
2019-05-12 22:32:02 +03:00
|
|
|
|
|
|
|
if (!s_unmaximize_button_bitmap)
|
2020-02-06 13:56:38 +03:00
|
|
|
s_unmaximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_unmaximize_button_bitmap_data, s_unmaximize_button_bitmap_width, s_unmaximize_button_bitmap_height).leak_ref();
|
2019-05-12 22:32:02 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
m_buttons.append(make<Button>(*this, *s_close_button_bitmap, [this](auto&) {
|
2019-06-21 12:03:43 +03:00
|
|
|
m_window.request_close();
|
2019-04-05 19:40:36 +03:00
|
|
|
}));
|
2019-04-05 23:32:00 +03:00
|
|
|
|
2019-05-13 01:48:54 +03:00
|
|
|
if (window.is_resizable()) {
|
2020-02-06 22:03:37 +03:00
|
|
|
auto button = make<Button>(*this, *s_maximize_button_bitmap, [this](auto&) {
|
2019-05-13 01:48:54 +03:00
|
|
|
m_window.set_maximized(!m_window.is_maximized());
|
2019-06-02 16:35:00 +03:00
|
|
|
});
|
|
|
|
m_maximize_button = button.ptr();
|
|
|
|
m_buttons.append(move(button));
|
2019-05-13 01:48:54 +03:00
|
|
|
}
|
2019-05-12 22:33:25 +03:00
|
|
|
|
2020-01-04 14:12:02 +03:00
|
|
|
if (window.is_minimizable()) {
|
2020-02-06 22:03:37 +03:00
|
|
|
auto button = make<Button>(*this, *s_minimize_button_bitmap, [this](auto&) {
|
2020-01-04 14:12:02 +03:00
|
|
|
m_window.set_minimized(true);
|
|
|
|
});
|
|
|
|
m_minimize_button = button.ptr();
|
|
|
|
m_buttons.append(move(button));
|
|
|
|
}
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
WindowFrame::~WindowFrame()
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
|
2019-06-02 16:35:00 +03:00
|
|
|
{
|
|
|
|
ASSERT(m_maximize_button);
|
|
|
|
m_maximize_button->set_bitmap(maximized ? *s_unmaximize_button_bitmap : *s_maximize_button_bitmap);
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect WindowFrame::title_bar_rect() const
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2020-07-17 03:27:55 +03:00
|
|
|
auto window_titlebar_height = WindowManager::the().palette().window_title_height();
|
2020-03-30 18:00:23 +03:00
|
|
|
if (m_window.type() == WindowType::Notification)
|
|
|
|
return { m_window.width() + 3, 3, window_titlebar_height, m_window.height() };
|
2020-04-23 15:37:13 +03:00
|
|
|
return { 4, 4, m_window.width(), window_titlebar_height };
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect WindowFrame::title_bar_icon_rect() const
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2019-04-05 22:33:34 +03:00
|
|
|
auto titlebar_rect = title_bar_rect();
|
2019-04-05 16:54:56 +03:00
|
|
|
return {
|
2020-06-27 21:46:35 +03:00
|
|
|
titlebar_rect.x() + 2,
|
2019-04-18 03:11:23 +03:00
|
|
|
titlebar_rect.y() + 2,
|
2019-04-05 16:54:56 +03:00
|
|
|
16,
|
|
|
|
titlebar_rect.height(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect WindowFrame::title_bar_text_rect() const
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2019-04-05 22:33:34 +03:00
|
|
|
auto titlebar_rect = title_bar_rect();
|
|
|
|
auto titlebar_icon_rect = title_bar_icon_rect();
|
2019-04-05 16:54:56 +03:00
|
|
|
return {
|
2020-07-04 20:21:57 +03:00
|
|
|
titlebar_rect.x() + 3 + titlebar_icon_rect.width() + 2,
|
2019-04-05 16:54:56 +03:00
|
|
|
titlebar_rect.y(),
|
2020-07-04 20:21:57 +03:00
|
|
|
titlebar_rect.width() - 5 - titlebar_icon_rect.width() - 2,
|
2019-04-05 16:54:56 +03:00
|
|
|
titlebar_rect.height()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
WindowFrame::FrameColors WindowFrame::compute_frame_colors() const
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2020-03-30 18:00:23 +03:00
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
auto palette = wm.palette();
|
|
|
|
if (&m_window == wm.m_highlight_window)
|
|
|
|
return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2() };
|
|
|
|
if (&m_window == wm.m_move_window)
|
|
|
|
return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2() };
|
2020-07-15 04:17:00 +03:00
|
|
|
if (wm.is_active_window_or_accessory(m_window))
|
2020-03-30 18:00:23 +03:00
|
|
|
return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2() };
|
|
|
|
return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2() };
|
|
|
|
}
|
2019-04-08 19:58:44 +03:00
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
|
|
|
|
{
|
2020-02-06 22:03:37 +03:00
|
|
|
auto palette = WindowManager::the().palette();
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect outer_rect = { {}, rect().size() };
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
auto titlebar_rect = title_bar_rect();
|
|
|
|
painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2());
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
int stripe_top = m_buttons.last().relative_rect().bottom() + 4;
|
|
|
|
int stripe_bottom = m_window.height() - 3;
|
|
|
|
if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) {
|
2020-07-17 03:27:55 +03:00
|
|
|
for (int i = 2; i <= palette.window_title_height() - 2; i += 2) {
|
|
|
|
painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.window_title_stripes());
|
2020-03-30 18:00:23 +03:00
|
|
|
}
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
2020-03-30 18:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
|
|
|
|
{
|
|
|
|
auto palette = WindowManager::the().palette();
|
|
|
|
auto& window = m_window;
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect outer_rect = { {}, rect().size() };
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
|
2019-05-25 00:37:23 +03:00
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
auto titlebar_rect = title_bar_rect();
|
|
|
|
auto titlebar_icon_rect = title_bar_icon_rect();
|
|
|
|
auto titlebar_inner_rect = title_bar_text_rect();
|
|
|
|
auto titlebar_title_rect = titlebar_inner_rect;
|
|
|
|
titlebar_title_rect.set_width(Gfx::Font::default_bold_font().width(window.title()));
|
|
|
|
|
|
|
|
auto [title_color, border_color, border_color2] = compute_frame_colors();
|
|
|
|
|
|
|
|
auto& wm = WindowManager::the();
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
|
2020-04-23 18:47:34 +03:00
|
|
|
painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
|
2019-05-25 05:17:21 +03:00
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
|
2019-04-05 19:40:36 +03:00
|
|
|
|
2019-04-05 16:54:56 +03:00
|
|
|
painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
|
2019-09-06 20:07:52 +03:00
|
|
|
|
|
|
|
int stripe_left = titlebar_title_rect.right() + 4;
|
|
|
|
int stripe_right = leftmost_button_rect.left() - 3;
|
|
|
|
if (stripe_left && stripe_right && stripe_left < stripe_right) {
|
|
|
|
for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
|
2020-07-17 03:27:55 +03:00
|
|
|
painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, palette.window_title_stripes());
|
2019-09-06 20:07:52 +03:00
|
|
|
}
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
2019-05-11 02:31:10 +03:00
|
|
|
|
2020-06-11 23:46:49 +03:00
|
|
|
String title_text;
|
|
|
|
|
|
|
|
if (window.client() && window.client()->is_unresponsive()) {
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(window.title());
|
|
|
|
builder.append(" (Not responding)");
|
|
|
|
title_text = builder.to_string();
|
|
|
|
} else {
|
|
|
|
title_text = window.title();
|
|
|
|
}
|
|
|
|
|
2019-09-06 20:07:52 +03:00
|
|
|
auto clipped_title_rect = titlebar_title_rect;
|
|
|
|
clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
|
|
|
|
if (!clipped_title_rect.is_empty()) {
|
2020-07-17 03:27:55 +03:00
|
|
|
painter.draw_text(clipped_title_rect.translated(1, 2), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, palette.window_title_shadow(), Gfx::TextElision::Right);
|
2019-09-06 20:07:52 +03:00
|
|
|
// FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
|
2020-06-11 23:46:49 +03:00
|
|
|
painter.draw_text(clipped_title_rect.translated(0, 1), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
|
2019-09-06 20:07:52 +03:00
|
|
|
}
|
2019-04-05 16:54:56 +03:00
|
|
|
|
|
|
|
painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
|
2020-03-30 18:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowFrame::paint(Gfx::Painter& painter)
|
|
|
|
{
|
2020-05-02 00:26:32 +03:00
|
|
|
if (m_window.is_frameless())
|
|
|
|
return;
|
|
|
|
|
2020-03-30 18:00:23 +03:00
|
|
|
Gfx::PainterStateSaver saver(painter);
|
|
|
|
painter.translate(rect().location());
|
|
|
|
|
|
|
|
if (m_window.type() == WindowType::Notification)
|
|
|
|
paint_notification_frame(painter);
|
|
|
|
else if (m_window.type() == WindowType::Normal)
|
|
|
|
paint_normal_frame(painter);
|
|
|
|
else
|
|
|
|
return;
|
2019-04-05 16:54:56 +03:00
|
|
|
|
2019-04-05 19:40:36 +03:00
|
|
|
for (auto& button : m_buttons) {
|
2019-07-24 10:04:57 +03:00
|
|
|
button.paint(painter);
|
2019-04-05 19:40:36 +03:00
|
|
|
}
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
|
2019-04-05 22:33:34 +03:00
|
|
|
{
|
2020-05-02 00:26:32 +03:00
|
|
|
if (window.is_frameless())
|
|
|
|
return rect;
|
|
|
|
|
2019-05-25 00:37:23 +03:00
|
|
|
auto type = window.type();
|
2020-07-17 03:27:55 +03:00
|
|
|
auto window_titlebar_height = WindowManager::the().palette().window_title_height();
|
2019-05-25 00:37:23 +03:00
|
|
|
|
2019-04-05 22:33:34 +03:00
|
|
|
switch (type) {
|
2020-02-06 22:03:37 +03:00
|
|
|
case WindowType::Normal:
|
2020-03-30 18:00:23 +03:00
|
|
|
return {
|
2020-04-23 15:37:13 +03:00
|
|
|
rect.x() - 4,
|
2020-05-02 00:19:00 +03:00
|
|
|
rect.y() - window_titlebar_height - 6,
|
2020-04-23 15:37:13 +03:00
|
|
|
rect.width() + 8,
|
2020-05-02 00:19:00 +03:00
|
|
|
rect.height() + 10 + window_titlebar_height
|
2020-03-30 18:00:23 +03:00
|
|
|
};
|
|
|
|
case WindowType::Notification:
|
|
|
|
return {
|
|
|
|
rect.x() - 3,
|
|
|
|
rect.y() - 3,
|
|
|
|
rect.width() + 6 + window_titlebar_height,
|
|
|
|
rect.height() + 6
|
|
|
|
};
|
2019-04-05 22:33:34 +03:00
|
|
|
default:
|
2019-04-18 02:45:52 +03:00
|
|
|
return rect;
|
2019-04-05 22:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
static Gfx::IntRect frame_rect_for_window(Window& window)
|
2019-05-25 00:37:23 +03:00
|
|
|
{
|
|
|
|
return frame_rect_for_window(window, window.rect());
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect WindowFrame::rect() const
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2019-05-25 00:37:23 +03:00
|
|
|
return frame_rect_for_window(m_window);
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void WindowFrame::invalidate_title_bar()
|
2019-04-05 22:53:45 +03:00
|
|
|
{
|
2020-05-19 20:14:20 +03:00
|
|
|
Compositor::the().invalidate(title_bar_rect().translated(rect().location()));
|
2019-04-05 22:53:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2020-07-17 03:27:55 +03:00
|
|
|
auto palette = WindowManager::the().palette();
|
|
|
|
int window_button_width = palette.window_title_button_width();
|
|
|
|
int window_button_height = palette.window_title_button_height();
|
2020-03-30 18:00:23 +03:00
|
|
|
int pos;
|
|
|
|
if (m_window.type() == WindowType::Notification)
|
|
|
|
pos = title_bar_rect().top() + 2;
|
|
|
|
else
|
|
|
|
pos = title_bar_text_rect().right() + 1;
|
2019-07-24 10:04:57 +03:00
|
|
|
|
2019-04-05 19:40:36 +03:00
|
|
|
for (auto& button : m_buttons) {
|
2020-03-30 18:00:23 +03:00
|
|
|
if (m_window.type() == WindowType::Notification) {
|
2020-07-17 03:27:55 +03:00
|
|
|
// The button height & width have to be equal or it leaks out of its area
|
|
|
|
Gfx::IntRect rect { 0, pos, window_button_height, window_button_height };
|
2020-03-30 18:00:23 +03:00
|
|
|
rect.center_horizontally_within(title_bar_rect());
|
|
|
|
button.set_relative_rect(rect);
|
2020-07-17 03:27:55 +03:00
|
|
|
pos += window_button_height;
|
2020-03-30 18:00:23 +03:00
|
|
|
} else {
|
|
|
|
pos -= window_button_width;
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
|
2020-03-30 18:00:23 +03:00
|
|
|
rect.center_vertically_within(title_bar_text_rect());
|
|
|
|
button.set_relative_rect(rect);
|
|
|
|
}
|
2019-04-05 19:40:36 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
auto& wm = WindowManager::the();
|
2019-05-25 00:37:23 +03:00
|
|
|
wm.invalidate(frame_rect_for_window(m_window, old_rect));
|
|
|
|
wm.invalidate(frame_rect_for_window(m_window, new_rect));
|
2019-04-05 16:54:56 +03:00
|
|
|
wm.notify_rect_changed(m_window, old_rect, new_rect);
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
void WindowFrame::on_mouse_event(const MouseEvent& event)
|
2019-04-05 16:54:56 +03:00
|
|
|
{
|
2019-05-17 22:33:44 +03:00
|
|
|
ASSERT(!m_window.is_fullscreen());
|
|
|
|
|
2020-01-25 12:39:09 +03:00
|
|
|
if (m_window.is_blocked_by_modal_window())
|
|
|
|
return;
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
auto& wm = WindowManager::the();
|
2020-03-30 18:00:23 +03:00
|
|
|
if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
|
2019-04-05 16:54:56 +03:00
|
|
|
return;
|
2019-05-16 21:29:42 +03:00
|
|
|
|
2020-07-07 21:09:18 +03:00
|
|
|
if (m_window.type() == WindowType::Normal && title_bar_icon_rect().contains(event.position())) {
|
2020-07-15 19:48:58 +03:00
|
|
|
if (event.type() == Event::MouseDown)
|
|
|
|
wm.move_to_front_and_make_active(m_window);
|
2020-07-07 21:09:18 +03:00
|
|
|
if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right)) {
|
|
|
|
// Manually start a potential double click. Since we're opening
|
|
|
|
// a menu, we will only receive the MouseDown event, so we
|
|
|
|
// need to record that fact. If the user subsequently clicks
|
|
|
|
// on the same area, the menu will get closed, and we will
|
|
|
|
// receive a MouseUp event, but because windows have changed
|
|
|
|
// we don't get a MouseDoubleClick event. We can however record
|
|
|
|
// this click, and when we receive the MouseUp event check if
|
|
|
|
// it would have been considered a double click, if it weren't
|
|
|
|
// for the fact that we opened and closed a window in the meanwhile
|
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
wm.start_menu_doubleclick(m_window, event);
|
|
|
|
|
|
|
|
m_window.popup_window_menu(title_bar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
|
|
|
|
return;
|
|
|
|
} else if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
|
|
|
|
// Since the MouseDown event opened a menu, another MouseUp
|
|
|
|
// from the second click outside the menu wouldn't be considered
|
|
|
|
// a double click, so let's manually check if it would otherwise
|
|
|
|
// have been be considered to be one
|
|
|
|
auto& wm = WindowManager::the();
|
|
|
|
if (wm.is_menu_doubleclick(m_window, event)) {
|
|
|
|
// It is a double click, so perform activate the default item
|
|
|
|
m_window.window_menu_activate_default();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-06-21 12:03:43 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:27:33 +03:00
|
|
|
// This is slightly hackish, but expand the title bar rect by two pixels downwards,
|
2019-05-16 21:29:42 +03:00
|
|
|
// so that mouse events between the title bar and window contents don't act like
|
|
|
|
// mouse events on the border.
|
|
|
|
auto adjusted_title_bar_rect = title_bar_rect();
|
2020-04-23 20:27:33 +03:00
|
|
|
adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 2);
|
2019-05-16 21:29:42 +03:00
|
|
|
|
|
|
|
if (adjusted_title_bar_rect.contains(event.position())) {
|
2019-04-07 00:20:06 +03:00
|
|
|
wm.clear_resize_candidate();
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (event.type() == Event::MouseDown)
|
2019-04-05 16:54:56 +03:00
|
|
|
wm.move_to_front_and_make_active(m_window);
|
2019-04-05 19:40:36 +03:00
|
|
|
|
|
|
|
for (auto& button : m_buttons) {
|
2019-07-24 10:04:57 +03:00
|
|
|
if (button.relative_rect().contains(event.position()))
|
|
|
|
return button.on_mouse_event(event.translated(-button.relative_rect().location()));
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
2020-02-06 22:03:37 +03:00
|
|
|
if (event.type() == Event::MouseDown) {
|
2020-03-30 18:00:23 +03:00
|
|
|
if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) {
|
2020-07-07 21:09:18 +03:00
|
|
|
auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
|
|
|
|
m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
|
2020-01-04 16:14:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-03-30 18:00:23 +03:00
|
|
|
if (m_window.is_movable() && event.button() == MouseButton::Left)
|
2020-01-04 16:14:36 +03:00
|
|
|
wm.start_window_move(m_window, event.translated(rect().location()));
|
|
|
|
}
|
2019-04-07 00:20:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
|
2019-04-07 00:20:06 +03:00
|
|
|
constexpr ResizeDirection direction_for_hot_area[3][3] = {
|
|
|
|
{ ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
|
|
|
|
{ ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
|
|
|
|
{ ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
|
|
|
|
};
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect outer_rect = { {}, rect().size() };
|
2019-04-07 00:20:06 +03:00
|
|
|
ASSERT(outer_rect.contains(event.position()));
|
|
|
|
int window_relative_x = event.x() - outer_rect.x();
|
|
|
|
int window_relative_y = event.y() - outer_rect.y();
|
|
|
|
int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
|
|
|
|
int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
|
|
|
|
wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
|
2020-02-06 22:03:37 +03:00
|
|
|
Compositor::the().invalidate_cursor();
|
2019-04-07 00:20:06 +03:00
|
|
|
return;
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
2019-04-07 00:20:06 +03:00
|
|
|
|
2020-02-06 22:03:37 +03:00
|
|
|
if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
|
2019-04-07 00:20:06 +03:00
|
|
|
wm.start_window_resize(m_window, event.translated(rect().location()));
|
2019-04-05 16:54:56 +03:00
|
|
|
}
|
2020-02-06 22:03:37 +03:00
|
|
|
}
|