mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 10:22:05 -05:00
WindowServer: Use early return to reduce nesting
This commit is contained in:
parent
14f9a29502
commit
97508844dc
1 changed files with 27 additions and 24 deletions
|
@ -125,33 +125,36 @@ void Menu::redraw()
|
|||
|
||||
Window& Menu::ensure_menu_window()
|
||||
{
|
||||
if (m_menu_window)
|
||||
return *m_menu_window;
|
||||
|
||||
int width = this->content_width();
|
||||
if (!m_menu_window) {
|
||||
Gfx::Point next_item_location(frame_thickness(), frame_thickness());
|
||||
for (auto& item : m_items) {
|
||||
int height = 0;
|
||||
if (item.type() == MenuItem::Text)
|
||||
height = item_height();
|
||||
else if (item.type() == MenuItem::Separator)
|
||||
height = 8;
|
||||
item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
|
||||
next_item_location.move_by(0, height);
|
||||
}
|
||||
|
||||
int window_height_available = Screen::the().height() - MenuManager::the().menubar_rect().height() - frame_thickness() * 2;
|
||||
int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
|
||||
int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
|
||||
int window_height = min(max_window_height, content_height);
|
||||
if (window_height < content_height) {
|
||||
m_scrollable = true;
|
||||
m_max_scroll_offset = item_count() - window_height / item_height() + 2;
|
||||
}
|
||||
|
||||
auto window = Window::construct(*this, WindowType::Menu);
|
||||
window->set_rect(0, 0, width, window_height);
|
||||
m_menu_window = move(window);
|
||||
draw();
|
||||
Gfx::Point next_item_location(frame_thickness(), frame_thickness());
|
||||
for (auto& item : m_items) {
|
||||
int height = 0;
|
||||
if (item.type() == MenuItem::Text)
|
||||
height = item_height();
|
||||
else if (item.type() == MenuItem::Separator)
|
||||
height = 8;
|
||||
item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
|
||||
next_item_location.move_by(0, height);
|
||||
}
|
||||
|
||||
int window_height_available = Screen::the().height() - MenuManager::the().menubar_rect().height() - frame_thickness() * 2;
|
||||
int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
|
||||
int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
|
||||
int window_height = min(max_window_height, content_height);
|
||||
if (window_height < content_height) {
|
||||
m_scrollable = true;
|
||||
m_max_scroll_offset = item_count() - window_height / item_height() + 2;
|
||||
}
|
||||
|
||||
auto window = Window::construct(*this, WindowType::Menu);
|
||||
window->set_rect(0, 0, width, window_height);
|
||||
m_menu_window = move(window);
|
||||
draw();
|
||||
|
||||
return *m_menu_window;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue