[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 0039db9 | 2012-05-09 04:11:45 | [diff] [blame] | 5 | #ifndef UI_APP_LIST_PAGINATION_MODEL_H_ |
| 6 | #define UI_APP_LIST_PAGINATION_MODEL_H_ |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 7 | |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 8 | #include "base/basictypes.h" |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 11 | #include "base/observer_list.h" |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 12 | #include "ui/app_list/app_list_export.h" |
| 13 | #include "ui/base/animation/animation_delegate.h" |
| 14 | |
| 15 | namespace ui { |
| 16 | class SlideAnimation; |
| 17 | } |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 18 | |
[email protected] | 0039db9 | 2012-05-09 04:11:45 | [diff] [blame] | 19 | namespace app_list { |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 20 | |
| 21 | class PaginationModelObserver; |
| 22 | |
| 23 | // A simple pagination model that consists of two numbers: the total pages and |
| 24 | // the currently selected page. The model is a single selection model that at |
| 25 | // the most one page can become selected at any time. |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 26 | class APP_LIST_EXPORT PaginationModel : public ui::AnimationDelegate { |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 27 | public: |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 28 | // Holds info for transition animation and touch scroll. |
| 29 | struct Transition { |
| 30 | Transition(int target_page, double progress) |
| 31 | : target_page(target_page), |
| 32 | progress(progress) { |
| 33 | } |
| 34 | |
[email protected] | d5fa3c4 | 2012-07-05 03:28:35 | [diff] [blame] | 35 | bool Equals(const Transition& rhs) const { |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 36 | return target_page == rhs.target_page && progress == rhs.progress; |
| 37 | } |
| 38 | |
| 39 | // Target page for the transition or -1 if there is no target page. For |
| 40 | // page switcher, this is the target selected page. For touch scroll, |
| 41 | // this is usually the previous or next page (or -1 when there is no |
| 42 | // previous or next page). |
| 43 | int target_page; |
| 44 | |
| 45 | // A [0, 1] progress indicates how much of the current page is being |
| 46 | // transitioned. |
| 47 | double progress; |
| 48 | }; |
| 49 | |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 50 | PaginationModel(); |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 51 | virtual ~PaginationModel(); |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 52 | |
| 53 | void SetTotalPages(int total_pages); |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 54 | |
| 55 | // Selects a page. |animate| is true if the transition should be animated. |
| 56 | void SelectPage(int page, bool animate); |
| 57 | |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 58 | // Selects a page by relative |delta|. |
| 59 | void SelectPageRelative(int delta, bool animate); |
| 60 | |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 61 | void SetTransition(const Transition& transition); |
| 62 | void SetTransitionDuration(int duration_ms); |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 63 | |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 64 | // Starts a scroll transition. If there is a running transition animation, |
| 65 | // cancels it but keeps the transition info. |
| 66 | void StartScroll(); |
| 67 | |
| 68 | // Updates transition progress from |delta|. |delta| > 0 means transit to |
| 69 | // previous page (moving pages to the right). |delta| < 0 means transit |
| 70 | // to next page (moving pages to the left). |
| 71 | void UpdateScroll(double delta); |
| 72 | |
[email protected] | fc6fb7fa | 2012-08-30 09:53:28 | [diff] [blame] | 73 | // Finishes the current scroll transition if |cancel| is false. Otherwise, |
| 74 | // reverses it. |
| 75 | void EndScroll(bool cancel); |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 76 | |
[email protected] | fab6c3b9 | 2012-09-21 02:36:45 | [diff] [blame] | 77 | // Returns true if current transition is being reverted. |
| 78 | bool IsRevertingCurrentTransition() const; |
| 79 | |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 80 | void AddObserver(PaginationModelObserver* observer); |
| 81 | void RemoveObserver(PaginationModelObserver* observer); |
| 82 | |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 83 | int total_pages() const { return total_pages_; } |
| 84 | int selected_page() const { return selected_page_; } |
| 85 | const Transition& transition() const { return transition_; } |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 86 | |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 87 | bool is_valid_page(int page) const { |
| 88 | return page >= 0 && page < total_pages_; |
| 89 | } |
| 90 | |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 91 | bool has_transition() const { |
| 92 | return transition_.target_page != -1 || transition_.progress != 0; |
| 93 | } |
| 94 | |
[email protected] | 6b28dd2 | 2012-06-22 04:29:31 | [diff] [blame] | 95 | private: |
| 96 | void NotifySelectedPageChanged(int old_selected, int new_selected); |
| 97 | void NotifyTransitionChanged(); |
| 98 | |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 99 | void clear_transition() { |
| 100 | SetTransition(Transition(-1, 0)); |
| 101 | } |
| 102 | |
| 103 | // Calculates a target page number by combining current page and |delta|. |
| 104 | // When there is no transition, current page is the currently selected page. |
| 105 | // If there is a transition, current page is the transition target page or the |
| 106 | // pending transition target page. When current page + |delta| goes beyond |
| 107 | // valid range and |selected_page_| is at the range ends, invalid page number |
| 108 | // -1 or |total_pages_| is returned to indicate the situation. |
| 109 | int CalculateTargetPage(int delta) const; |
| 110 | |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 111 | void StartTranstionAnimation(int target_page); |
[email protected] | fc61391 | 2012-06-19 23:16:15 | [diff] [blame] | 112 | void CreateTransitionAnimation(); |
[email protected] | fc6fb7fa | 2012-08-30 09:53:28 | [diff] [blame] | 113 | void ResetTransitionAnimation(); |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 114 | |
| 115 | // ui::AnimationDelegate overrides: |
| 116 | virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE; |
| 117 | virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE; |
| 118 | |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 119 | int total_pages_; |
| 120 | int selected_page_; |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 121 | |
| 122 | Transition transition_; |
| 123 | |
[email protected] | 891217c | 2012-06-11 22:05:19 | [diff] [blame] | 124 | // Pending selected page when SelectedPage is called during a transition. If |
| 125 | // multiple SelectPage is called while a transition is in progress, only the |
| 126 | // last target page is remembered here. |
| 127 | int pending_selected_page_; |
| 128 | |
| 129 | scoped_ptr<ui::SlideAnimation> transition_animation_; |
| 130 | int transition_duration_ms_; // Transition duration in millisecond. |
| 131 | |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 132 | ObserverList<PaginationModelObserver> observers_; |
| 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(PaginationModel); |
| 135 | }; |
| 136 | |
[email protected] | 0039db9 | 2012-05-09 04:11:45 | [diff] [blame] | 137 | } // namespace app_list |
[email protected] | 53520b1b | 2012-05-07 21:43:37 | [diff] [blame] | 138 | |
[email protected] | 0039db9 | 2012-05-09 04:11:45 | [diff] [blame] | 139 | #endif // UI_APP_LIST_PAGINATION_MODEL_H_ |