WindowServer: Convert Vector<OwnPtr> to NonnullOwnPtrVector.

This commit is contained in:
Andreas Kling 2019-07-24 09:04:57 +02:00
parent a635e62e6a
commit 90ea4918d6
Notes: sideshowbarker 2024-07-19 13:04:34 +09:00
4 changed files with 38 additions and 42 deletions

View File

@ -50,14 +50,14 @@ int WSMenu::width() const
int widest_text = 0;
int widest_shortcut = 0;
for (auto& item : m_items) {
if (item->type() != WSMenuItem::Text)
if (item.type() != WSMenuItem::Text)
continue;
int text_width = font().width(item->text());
if (!item->shortcut_text().is_empty()) {
int shortcut_width = font().width(item->shortcut_text());
int text_width = font().width(item.text());
if (!item.shortcut_text().is_empty()) {
int shortcut_width = font().width(item.shortcut_text());
widest_shortcut = max(shortcut_width, widest_shortcut);
}
if (item->is_checkable())
if (item.is_checkable())
text_width += s_checked_bitmap_width + s_checked_bitmap_padding;
widest_text = max(widest_text, text_width);
@ -74,7 +74,7 @@ int WSMenu::height() const
{
if (m_items.is_empty())
return 0;
return (m_items.last()->rect().bottom() - 1) + frame_thickness() * 2;
return (m_items.last().rect().bottom() - 1) + frame_thickness() * 2;
}
void WSMenu::redraw()
@ -92,11 +92,11 @@ WSWindow& WSMenu::ensure_menu_window()
Point next_item_location(frame_thickness(), frame_thickness());
for (auto& item : m_items) {
int height = 0;
if (item->type() == WSMenuItem::Text)
if (item.type() == WSMenuItem::Text)
height = item_height();
else if (item->type() == WSMenuItem::Separator)
else if (item.type() == WSMenuItem::Separator)
height = 8;
item->set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
next_item_location.move_by(0, height);
}
@ -124,30 +124,30 @@ void WSMenu::draw()
s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
for (auto& item : m_items) {
if (item->type() == WSMenuItem::Text) {
if (item.type() == WSMenuItem::Text) {
Color text_color = Color::Black;
if (item == m_hovered_item) {
painter.fill_rect(item->rect(), WSWindowManager::the().menu_selection_color());
if (&item == m_hovered_item) {
painter.fill_rect(item.rect(), WSWindowManager::the().menu_selection_color());
text_color = Color::White;
}
if (!item->is_enabled())
if (!item.is_enabled())
text_color = Color::MidGray;
Rect text_rect = item->rect().translated(left_padding(), 0);
if (item->is_checkable()) {
if (item->is_checked()) {
Rect text_rect = item.rect().translated(left_padding(), 0);
if (item.is_checkable()) {
if (item.is_checked()) {
Rect checkmark_rect { text_rect.location().x(), 0, s_checked_bitmap_width, s_checked_bitmap_height };
checkmark_rect.center_vertically_within(text_rect);
painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, Color::Black);
}
text_rect.move_by(s_checked_bitmap_width + s_checked_bitmap_padding, 0);
}
painter.draw_text(text_rect, item->text(), TextAlignment::CenterLeft, text_color);
if (!item->shortcut_text().is_empty()) {
painter.draw_text(item->rect().translated(-right_padding(), 0), item->shortcut_text(), TextAlignment::CenterRight, text_color);
painter.draw_text(text_rect, item.text(), TextAlignment::CenterLeft, text_color);
if (!item.shortcut_text().is_empty()) {
painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), TextAlignment::CenterRight, text_color);
}
} else if (item->type() == WSMenuItem::Separator) {
Point p1(4, item->rect().center().y());
Point p2(width - 5, item->rect().center().y());
} else if (item.type() == WSMenuItem::Separator) {
Point p1(4, item.rect().center().y());
Point p2(width - 5, item.rect().center().y());
painter.draw_line(p1, p2, Color::MidGray);
painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), Color::White);
}
@ -205,8 +205,8 @@ void WSMenu::did_activate(WSMenuItem& item)
WSMenuItem* WSMenu::item_with_identifier(unsigned identifer)
{
for (auto& item : m_items) {
if (item->identifier() == identifer)
return item.ptr();
if (item.identifier() == identifer)
return &item;
}
return nullptr;
}
@ -214,9 +214,8 @@ WSMenuItem* WSMenu::item_with_identifier(unsigned identifer)
WSMenuItem* WSMenu::item_at(const Point& position)
{
for (auto& item : m_items) {
if (!item->rect().contains(position))
continue;
return item.ptr();
if (item.rect().contains(position))
return &item;
}
return nullptr;
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <AK/AKString.h>
#include <AK/Vector.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/WeakPtr.h>
#include <LibCore/CObject.h>
#include <LibDraw/Rect.h>
@ -28,10 +28,8 @@ public:
bool is_empty() const { return m_items.is_empty(); }
int item_count() const { return m_items.size(); }
WSMenuItem* item(int i) { return m_items[i].ptr(); }
const WSMenuItem* item(int i) const { return m_items[i].ptr(); }
void add_item(OwnPtr<WSMenuItem>&& item) { m_items.append(move(item)); }
void add_item(NonnullOwnPtr<WSMenuItem>&& item) { m_items.append(move(item)); }
String name() const { return m_name; }
@ -39,7 +37,7 @@ public:
void for_each_item(Callback callback) const
{
for (auto& item : m_items)
callback(*item);
callback(item);
}
Rect text_rect_in_menubar() const { return m_text_rect_in_menubar; }
@ -88,6 +86,6 @@ private:
Rect m_text_rect_in_menubar;
WSMenuBar* m_menubar { nullptr };
WSMenuItem* m_hovered_item { nullptr };
Vector<OwnPtr<WSMenuItem>> m_items;
NonnullOwnPtrVector<WSMenuItem> m_items;
OwnPtr<WSWindow> m_menu_window;
};

View File

@ -194,7 +194,7 @@ void WSWindowFrame::paint(Painter& painter)
painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), Color::WarmGray);
auto leftmost_button_rect = m_buttons.is_empty() ? Rect() : m_buttons.last()->relative_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);
for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
@ -208,7 +208,7 @@ void WSWindowFrame::paint(Painter& painter)
painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
for (auto& button : m_buttons) {
button->paint(painter);
button.paint(painter);
}
}
@ -248,12 +248,12 @@ void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect&
int window_button_width = 15;
int window_button_height = 15;
int x = title_bar_text_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_text_rect());
button->set_relative_rect(rect);
button.set_relative_rect(rect);
}
auto& wm = WSWindowManager::the();
@ -288,8 +288,8 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
wm.move_to_front_and_make_active(m_window);
for (auto& button : m_buttons) {
if (button->relative_rect().contains(event.position()))
return button->on_mouse_event(event.translated(-button->relative_rect().location()));
if (button.relative_rect().contains(event.position()))
return button.on_mouse_event(event.translated(-button.relative_rect().location()));
}
if (event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left)
wm.start_window_drag(m_window, event.translated(rect().location()));

View File

@ -1,8 +1,7 @@
#pragma once
#include <AK/Badge.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <AK/NonnullOwnPtrVector.h>
class Painter;
class Rect;
@ -29,6 +28,6 @@ public:
private:
WSWindow& m_window;
Vector<OwnPtr<WSButton>> m_buttons;
NonnullOwnPtrVector<WSButton> m_buttons;
WSButton* m_maximize_button { nullptr };
};