Skip to content

Commit

Permalink
WindowServer: Use early return to reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonbooth authored and awesomekling committed Feb 11, 2020
1 parent 14f9a29 commit 9750884
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions Servers/WindowServer/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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);
}

auto window = Window::construct(*this, WindowType::Menu);
window->set_rect(0, 0, width, window_height);
m_menu_window = move(window);
draw();
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;
}

Expand Down

0 comments on commit 9750884

Please sign in to comment.