OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ |
6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 | 12 |
13 namespace views { | 13 namespace views { |
14 class View; | 14 class View; |
15 } | 15 } |
16 | 16 |
17 namespace payments { | 17 namespace payments { |
18 | 18 |
| 19 class PaymentRequest; |
| 20 |
19 // A control representing a list of selectable items in the PaymentRequest | 21 // A control representing a list of selectable items in the PaymentRequest |
20 // dialog. These lists enforce that only one of their elements be selectable at | 22 // dialog. These lists enforce that only one of their elements be selectable at |
21 // a time and that "incomplete" items (for example, a credit card with no known | 23 // a time and that "incomplete" items (for example, a credit card with no known |
22 // expriration date) behave differently when selected. Most of the time, this | 24 // expiration date) behave differently when selected. Most of the time, this |
23 // behavior is to show an editor screen. | 25 // behavior is to show an editor screen. |
24 class PaymentRequestItemList { | 26 class PaymentRequestItemList { |
25 public: | 27 public: |
26 // Represents an item in the item list. | 28 // Represents an item in the item list. |
27 class Item { | 29 class Item { |
28 public: | 30 public: |
29 Item(); | 31 // Creates an item that will be owned by |list| with the initial state set |
| 32 // to |selected|. |
| 33 Item(PaymentRequest* request, PaymentRequestItemList* list, bool selected); |
30 virtual ~Item(); | 34 virtual ~Item(); |
31 | 35 |
32 // Gets the view associated with this item. It's owned by this object so | 36 // Gets the view associated with this item. It's owned by this object so |
33 // that it can listen to any changes to the underlying model and update the | 37 // that it can listen to any changes to the underlying model and update the |
34 // view. | 38 // view. |
35 views::View* GetItemView(); | 39 views::View* GetItemView(); |
36 | 40 |
| 41 bool selected() const { return selected_; } |
| 42 // Changes the selected state of this item to |selected| and calls |
| 43 // SelectedStateChanged. |
| 44 void SetSelected(bool selected); |
| 45 |
| 46 // Returns a pointer to the PaymentRequestItemList that owns this object. |
| 47 PaymentRequestItemList* list() { return list_; } |
| 48 |
| 49 // Returns a pointer to the PaymentRequest object associated with this |
| 50 // instance of the UI. |
| 51 PaymentRequest* request() { return request_; } |
| 52 |
37 protected: | 53 protected: |
38 // Creates and returns the view associated with this list item. | 54 // Creates and returns the view associated with this list item. |
39 virtual std::unique_ptr<views::View> CreateItemView() = 0; | 55 virtual std::unique_ptr<views::View> CreateItemView() = 0; |
40 | 56 |
| 57 // Called when the selected state of this item changes. Subclasses may |
| 58 // assume that they are the only selected item in |list| when this is |
| 59 // called. This could be called before CreateItemView so subclasses should |
| 60 // be aware that their views might not exist yet. |
| 61 virtual void SelectedStateChanged() = 0; |
| 62 |
41 private: | 63 private: |
42 std::unique_ptr<views::View> item_view_; | 64 std::unique_ptr<views::View> item_view_; |
| 65 PaymentRequest* request_; |
| 66 PaymentRequestItemList* list_; |
| 67 bool selected_; |
43 | 68 |
44 DISALLOW_COPY_AND_ASSIGN(Item); | 69 DISALLOW_COPY_AND_ASSIGN(Item); |
45 }; | 70 }; |
46 | 71 |
47 PaymentRequestItemList(); | 72 PaymentRequestItemList(); |
48 ~PaymentRequestItemList(); | 73 ~PaymentRequestItemList(); |
49 | 74 |
50 // Adds an item to this list. | 75 // Adds an item to this list. |item->list()| should return this object. |
51 void AddItem(std::unique_ptr<Item> item); | 76 void AddItem(std::unique_ptr<Item> item); |
52 | 77 |
53 // Creates and returns the UI representation of this list. It iterates over | 78 // Creates and returns the UI representation of this list. It iterates over |
54 // the items it contains, creates their associated views, and adds them to the | 79 // the items it contains, creates their associated views, and adds them to the |
55 // hierarchy. | 80 // hierarchy. |
56 std::unique_ptr<views::View> CreateListView(); | 81 std::unique_ptr<views::View> CreateListView(); |
57 | 82 |
| 83 // Deselects the currently selected item and selects |item| instead. |
| 84 void SelectItem(Item* item); |
| 85 |
58 private: | 86 private: |
| 87 // Unselects the currently selected item. This is private so that the list can |
| 88 // use it when selecting a new item while avoiding consumers of this class |
| 89 // putting the list in a state where no item is selected. |
| 90 void UnselectSelectedItem(); |
| 91 |
59 std::vector<std::unique_ptr<Item>> items_; | 92 std::vector<std::unique_ptr<Item>> items_; |
| 93 Item* selected_item_; |
60 | 94 |
61 DISALLOW_COPY_AND_ASSIGN(PaymentRequestItemList); | 95 DISALLOW_COPY_AND_ASSIGN(PaymentRequestItemList); |
62 }; | 96 }; |
63 | 97 |
64 } // namespace payments | 98 } // namespace payments |
65 | 99 |
66 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ | 100 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_PAYMENT_REQUEST_ITEM_LIST_H_ |
OLD | NEW |