From ba0460c981d8178859d4987f3554401545228a71 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 20 Mar 2018 16:07:39 -0700 Subject: Factor out a menu class for screen ui Also consolidate the duplicate codes to draw the menu in ScreenRecoveryUI and WearRecoveryUI. This helps us to support text icons as menu in the future. Bug: 74397117 Test: Check the menu under recovery on bullhead and a wear device. Change-Id: Iba9b646c3828670f0e78a7e07d1a94a44e96bb0b (cherry picked from commit 2b3f80068ece1040ba7c923afe1e70b705535ad5) --- screen_ui.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 4 deletions(-) (limited to 'screen_ui.h') diff --git a/screen_ui.h b/screen_ui.h index f05761c42..c1222a576 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -28,6 +29,70 @@ // From minui/minui.h. struct GRSurface; +// This class maintains the menu selection and display of the screen ui. +class Menu { + public: + Menu(bool scrollable, size_t max_items, size_t max_length); + + bool scrollable() const { + return scrollable_; + } + + int selection() const { + return selection_; + } + + // Returns count of menu items. + size_t ItemsCount() const; + // Returns the index of the first menu item. + size_t MenuStart() const; + // Returns the index of the last menu item + 1. + size_t MenuEnd() const; + + // Menu example: + // info: Android Recovery + // .... + // help messages: Swipe up/down to move + // Swipe left/right to select + // empty line (horizontal rule): + // menu headers: Select file to view + // menu items: /cache/recovery/last_log + // /cache/recovery/last_log.1 + // /cache/recovery/last_log.2 + // ... + const char* const* text_headers() const; + std::string TextItem(size_t index) const; + + // Checks if the menu items fit vertically on the screen. Returns true and set the + // |cur_selection_str| if the items exceed the screen limit. + bool ItemsOverflow(std::string* cur_selection_str) const; + + // Starts the menu with |headers| and |items| in text. Sets the default selection to + // |initial_selection|. + void Start(const char* const* headers, const char* const* items, int initial_selection); + + // Sets the current selection to |sel|. Handle the overflow cases depending on if the menu is + // scrollable. + int Select(int sel); + + private: + // The menu is scrollable to display more items. Used on wear devices who have smaller screens. + const bool scrollable_; + // The max number of menu items to fit vertically on a screen. + const size_t max_display_items_; + // The length of each item to fit horizontally on a screen. + const size_t max_item_length_; + + // Internal storage for the menu headers and items in text. + const char* const* text_headers_; + std::vector text_items_; + + // The first item to display on the screen. + size_t menu_start_; + // Current menu selection. + int selection_; +}; + // Implementation of RecoveryUI appropriate for devices with a screen // (shows an icon + a progress bar, text logging, menu, etc.) class ScreenRecoveryUI : public RecoveryUI { @@ -44,6 +109,7 @@ class ScreenRecoveryUI : public RecoveryUI { }; ScreenRecoveryUI(); + explicit ScreenRecoveryUI(bool scrollable_menu); bool Init(const std::string& locale) override; @@ -101,6 +167,7 @@ class ScreenRecoveryUI : public RecoveryUI { virtual void draw_background_locked(); virtual void draw_foreground_locked(); virtual void draw_screen_locked(); + virtual void draw_menu_and_text_buffer_locked(const char* const* help_message); virtual void update_screen_locked(); virtual void update_progress_locked(); @@ -184,10 +251,8 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_text; bool show_text_ever; // has show_text ever been true? - std::vector menu_; - const char* const* menu_headers_; - bool show_menu; - int menu_items, menu_sel; + bool scrollable_menu_; + std::unique_ptr menu_; // An alternate text screen, swapped with 'text_' when we're viewing a log file. char** file_viewer_text_; -- cgit v1.2.3