blob: d51241afca352f5dd1a6b9c6b157be60f1b839f9 [file] [log] [blame]
[email protected]53520b1b2012-05-07 21:43:371// 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]0039db92012-05-09 04:11:455#ifndef UI_APP_LIST_PAGINATION_MODEL_H_
6#define UI_APP_LIST_PAGINATION_MODEL_H_
[email protected]53520b1b2012-05-07 21:43:377
[email protected]53520b1b2012-05-07 21:43:378#include "base/basictypes.h"
[email protected]891217c2012-06-11 22:05:199#include "base/compiler_specific.h"
10#include "base/memory/scoped_ptr.h"
[email protected]53520b1b2012-05-07 21:43:3711#include "base/observer_list.h"
[email protected]891217c2012-06-11 22:05:1912#include "ui/app_list/app_list_export.h"
13#include "ui/base/animation/animation_delegate.h"
14
15namespace ui {
16class SlideAnimation;
17}
[email protected]53520b1b2012-05-07 21:43:3718
[email protected]0039db92012-05-09 04:11:4519namespace app_list {
[email protected]53520b1b2012-05-07 21:43:3720
21class 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]891217c2012-06-11 22:05:1926class APP_LIST_EXPORT PaginationModel : public ui::AnimationDelegate {
[email protected]53520b1b2012-05-07 21:43:3727 public:
[email protected]891217c2012-06-11 22:05:1928 // 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]d5fa3c42012-07-05 03:28:3535 bool Equals(const Transition& rhs) const {
[email protected]891217c2012-06-11 22:05:1936 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]53520b1b2012-05-07 21:43:3750 PaginationModel();
[email protected]891217c2012-06-11 22:05:1951 virtual ~PaginationModel();
[email protected]53520b1b2012-05-07 21:43:3752
53 void SetTotalPages(int total_pages);
[email protected]891217c2012-06-11 22:05:1954
55 // Selects a page. |animate| is true if the transition should be animated.
56 void SelectPage(int page, bool animate);
57
[email protected]fc613912012-06-19 23:16:1558 // Selects a page by relative |delta|.
59 void SelectPageRelative(int delta, bool animate);
60
[email protected]891217c2012-06-11 22:05:1961 void SetTransition(const Transition& transition);
62 void SetTransitionDuration(int duration_ms);
[email protected]53520b1b2012-05-07 21:43:3763
[email protected]fc613912012-06-19 23:16:1564 // 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]fc6fb7fa2012-08-30 09:53:2873 // Finishes the current scroll transition if |cancel| is false. Otherwise,
74 // reverses it.
75 void EndScroll(bool cancel);
[email protected]fc613912012-06-19 23:16:1576
[email protected]fab6c3b92012-09-21 02:36:4577 // Returns true if current transition is being reverted.
78 bool IsRevertingCurrentTransition() const;
79
[email protected]53520b1b2012-05-07 21:43:3780 void AddObserver(PaginationModelObserver* observer);
81 void RemoveObserver(PaginationModelObserver* observer);
82
[email protected]891217c2012-06-11 22:05:1983 int total_pages() const { return total_pages_; }
84 int selected_page() const { return selected_page_; }
85 const Transition& transition() const { return transition_; }
[email protected]53520b1b2012-05-07 21:43:3786
[email protected]fc613912012-06-19 23:16:1587 bool is_valid_page(int page) const {
88 return page >= 0 && page < total_pages_;
89 }
90
[email protected]fc613912012-06-19 23:16:1591 bool has_transition() const {
92 return transition_.target_page != -1 || transition_.progress != 0;
93 }
94
[email protected]6b28dd22012-06-22 04:29:3195 private:
96 void NotifySelectedPageChanged(int old_selected, int new_selected);
97 void NotifyTransitionChanged();
98
[email protected]fc613912012-06-19 23:16:1599 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]891217c2012-06-11 22:05:19111 void StartTranstionAnimation(int target_page);
[email protected]fc613912012-06-19 23:16:15112 void CreateTransitionAnimation();
[email protected]fc6fb7fa2012-08-30 09:53:28113 void ResetTransitionAnimation();
[email protected]891217c2012-06-11 22:05:19114
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]53520b1b2012-05-07 21:43:37119 int total_pages_;
120 int selected_page_;
[email protected]891217c2012-06-11 22:05:19121
122 Transition transition_;
123
[email protected]891217c2012-06-11 22:05:19124 // 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]53520b1b2012-05-07 21:43:37132 ObserverList<PaginationModelObserver> observers_;
133
134 DISALLOW_COPY_AND_ASSIGN(PaginationModel);
135};
136
[email protected]0039db92012-05-09 04:11:45137} // namespace app_list
[email protected]53520b1b2012-05-07 21:43:37138
[email protected]0039db92012-05-09 04:11:45139#endif // UI_APP_LIST_PAGINATION_MODEL_H_