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.
|
|
|
|
*/
|
|
|
|
|
2019-07-18 11:15:00 +03:00
|
|
|
#include <LibDraw/Font.h>
|
2019-08-26 19:54:44 +03:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
2019-07-18 11:15:00 +03:00
|
|
|
#include <LibDraw/StylePainter.h>
|
2019-06-07 12:47:19 +03:00
|
|
|
#include <WindowServer/WSEvent.h>
|
|
|
|
#include <WindowServer/WSScreen.h>
|
|
|
|
#include <WindowServer/WSWindowManager.h>
|
|
|
|
#include <WindowServer/WSWindowSwitcher.h>
|
2019-05-12 05:15:25 +03:00
|
|
|
|
|
|
|
static WSWindowSwitcher* s_the;
|
|
|
|
|
|
|
|
WSWindowSwitcher& WSWindowSwitcher::the()
|
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
2019-03-03 17:17:05 +03:00
|
|
|
|
|
|
|
WSWindowSwitcher::WSWindowSwitcher()
|
|
|
|
{
|
2019-05-12 05:15:25 +03:00
|
|
|
s_the = this;
|
2019-03-03 17:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WSWindowSwitcher::~WSWindowSwitcher()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSWindowSwitcher::set_visible(bool visible)
|
|
|
|
{
|
|
|
|
if (m_visible == visible)
|
|
|
|
return;
|
|
|
|
m_visible = visible;
|
2019-12-27 13:34:40 +03:00
|
|
|
WSWindowManager::the().recompute_occlusions();
|
2019-03-06 12:03:10 +03:00
|
|
|
if (m_switcher_window)
|
|
|
|
m_switcher_window->set_visible(visible);
|
|
|
|
if (!m_visible)
|
2019-03-03 17:17:05 +03:00
|
|
|
return;
|
2019-03-06 12:03:10 +03:00
|
|
|
refresh();
|
2019-03-03 17:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WSWindow* WSWindowSwitcher::selected_window()
|
|
|
|
{
|
|
|
|
if (m_selected_index < 0 || m_selected_index >= m_windows.size())
|
|
|
|
return nullptr;
|
|
|
|
return m_windows[m_selected_index].ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSWindowSwitcher::on_key_event(const WSKeyEvent& event)
|
|
|
|
{
|
2019-04-14 06:23:37 +03:00
|
|
|
if (event.type() == WSEvent::KeyUp) {
|
2019-03-03 17:17:05 +03:00
|
|
|
if (event.key() == Key_Logo) {
|
2019-12-29 05:06:04 +03:00
|
|
|
if (auto* window = selected_window()) {
|
|
|
|
window->set_minimized(false);
|
2019-03-24 15:09:46 +03:00
|
|
|
WSWindowManager::the().move_to_front_and_make_active(*window);
|
2019-12-29 05:06:04 +03:00
|
|
|
}
|
2019-03-03 17:17:05 +03:00
|
|
|
WSWindowManager::the().set_highlight_window(nullptr);
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-01-01 20:07:53 +03:00
|
|
|
|
|
|
|
if (event.key() == Key_LeftShift || event.key() == Key_RightShift)
|
|
|
|
return;
|
2019-03-03 17:17:05 +03:00
|
|
|
if (event.key() != Key_Tab) {
|
2019-03-10 04:19:55 +03:00
|
|
|
WSWindowManager::the().set_highlight_window(nullptr);
|
2019-03-03 17:17:05 +03:00
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT(!m_windows.is_empty());
|
2020-01-01 20:07:53 +03:00
|
|
|
|
|
|
|
if (!event.shift()) {
|
|
|
|
m_selected_index = (m_selected_index + 1) % m_windows.size();
|
|
|
|
} else {
|
|
|
|
m_selected_index = (m_selected_index - 1) % m_windows.size();
|
|
|
|
if (m_selected_index < 0)
|
|
|
|
m_selected_index = m_windows.size() - 1;
|
|
|
|
}
|
2019-03-03 17:17:05 +03:00
|
|
|
ASSERT(m_selected_index < m_windows.size());
|
|
|
|
auto* highlight_window = m_windows.at(m_selected_index).ptr();
|
|
|
|
ASSERT(highlight_window);
|
|
|
|
WSWindowManager::the().set_highlight_window(highlight_window);
|
2019-03-06 12:03:10 +03:00
|
|
|
draw();
|
2019-03-03 17:17:05 +03:00
|
|
|
WSWindowManager::the().invalidate(m_rect);
|
|
|
|
}
|
|
|
|
|
2019-03-06 12:03:10 +03:00
|
|
|
void WSWindowSwitcher::draw()
|
2019-03-03 17:17:05 +03:00
|
|
|
{
|
2019-12-29 02:47:49 +03:00
|
|
|
auto palette = WSWindowManager::the().palette();
|
2019-03-06 12:03:10 +03:00
|
|
|
Painter painter(*m_switcher_window->backing_store());
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect({ {}, m_rect.size() }, palette.window());
|
|
|
|
painter.draw_rect({ {}, m_rect.size() }, palette.threed_shadow2());
|
2019-03-03 17:17:05 +03:00
|
|
|
for (int index = 0; index < m_windows.size(); ++index) {
|
|
|
|
auto& window = *m_windows.at(index);
|
|
|
|
Rect item_rect {
|
|
|
|
padding(),
|
|
|
|
padding() + index * item_height(),
|
|
|
|
m_rect.width() - padding() * 2,
|
|
|
|
item_height()
|
|
|
|
};
|
|
|
|
Color text_color;
|
|
|
|
Color rect_text_color;
|
|
|
|
if (index == m_selected_index) {
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(item_rect, palette.selection());
|
|
|
|
text_color = palette.selection_text();
|
|
|
|
rect_text_color = palette.threed_shadow1();
|
2019-03-03 17:17:05 +03:00
|
|
|
} else {
|
2019-12-24 22:57:54 +03:00
|
|
|
text_color = palette.window_text();
|
|
|
|
rect_text_color = palette.threed_shadow2();
|
2019-03-03 17:17:05 +03:00
|
|
|
}
|
2019-04-23 23:01:33 +03:00
|
|
|
item_rect.shrink(item_padding(), 0);
|
2019-05-12 05:15:25 +03:00
|
|
|
Rect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } };
|
|
|
|
if (window.backing_store()) {
|
|
|
|
painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect());
|
2019-12-24 22:57:54 +03:00
|
|
|
StylePainter::paint_frame(painter, thumbnail_rect.inflated(4, 4), palette, FrameShape::Container, FrameShadow::Sunken, 2);
|
2019-05-12 05:15:25 +03:00
|
|
|
}
|
|
|
|
Rect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } };
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(icon_rect, palette.window());
|
2019-05-12 05:15:25 +03:00
|
|
|
painter.blit(icon_rect.location(), window.icon(), window.icon().rect());
|
|
|
|
painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.title(), WSWindowManager::the().window_title_font(), TextAlignment::CenterLeft, text_color);
|
2019-03-03 17:17:05 +03:00
|
|
|
painter.draw_text(item_rect, window.rect().to_string(), TextAlignment::CenterRight, rect_text_color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 12:03:10 +03:00
|
|
|
void WSWindowSwitcher::refresh()
|
2019-03-03 17:17:05 +03:00
|
|
|
{
|
2019-05-13 01:08:56 +03:00
|
|
|
auto& wm = WSWindowManager::the();
|
2019-03-03 17:17:05 +03:00
|
|
|
WSWindow* selected_window = nullptr;
|
|
|
|
if (m_selected_index > 0 && m_windows[m_selected_index])
|
|
|
|
selected_window = m_windows[m_selected_index].ptr();
|
2019-05-13 01:08:56 +03:00
|
|
|
if (!selected_window)
|
|
|
|
selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window();
|
2019-03-03 17:17:05 +03:00
|
|
|
m_windows.clear();
|
|
|
|
m_selected_index = 0;
|
|
|
|
int window_count = 0;
|
2019-03-06 13:03:10 +03:00
|
|
|
int longest_title_width = 0;
|
2019-12-29 05:06:04 +03:00
|
|
|
wm.for_each_window_of_type_from_front_to_back(WSWindowType::Normal, [&](WSWindow& window) {
|
2019-03-03 17:17:05 +03:00
|
|
|
++window_count;
|
2019-05-13 01:08:56 +03:00
|
|
|
longest_title_width = max(longest_title_width, wm.font().width(window.title()));
|
2019-03-03 17:17:05 +03:00
|
|
|
if (selected_window == &window)
|
|
|
|
m_selected_index = m_windows.size();
|
|
|
|
m_windows.append(window.make_weak_ptr());
|
|
|
|
return IterationDecision::Continue;
|
2019-06-07 12:47:19 +03:00
|
|
|
},
|
|
|
|
true);
|
2019-03-03 17:17:05 +03:00
|
|
|
if (m_windows.is_empty()) {
|
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-06 13:03:10 +03:00
|
|
|
int space_for_window_rect = 180;
|
2019-05-12 05:15:25 +03:00
|
|
|
m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_rect + padding() * 2 + item_padding() * 2);
|
2019-03-03 17:17:05 +03:00
|
|
|
m_rect.set_height(window_count * item_height() + padding() * 2);
|
2019-05-24 20:32:46 +03:00
|
|
|
m_rect.center_within(WSScreen::the().rect());
|
2019-03-06 12:03:10 +03:00
|
|
|
if (!m_switcher_window)
|
2019-09-22 01:17:53 +03:00
|
|
|
m_switcher_window = WSWindow::construct(*this, WSWindowType::WindowSwitcher);
|
2019-03-06 12:03:10 +03:00
|
|
|
m_switcher_window->set_rect(m_rect);
|
|
|
|
draw();
|
|
|
|
}
|
2019-05-12 05:15:25 +03:00
|
|
|
|
|
|
|
void WSWindowSwitcher::refresh_if_needed()
|
|
|
|
{
|
|
|
|
if (m_visible) {
|
|
|
|
refresh();
|
|
|
|
WSWindowManager::the().invalidate(m_rect);
|
|
|
|
}
|
|
|
|
}
|