diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-03 18:18:14 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-03 18:18:14 +0000 |
commit | 2d31666a58e746b7a1d415c99e5f68ad9256d236 (patch) | |
tree | 144c99d4b80df0f0f9a3ded83f9d21a8b36f17cc /chrome/views | |
parent | 90d6958fe2374a00d3c8583cf4d3b8a509ae8e90 (diff) | |
download | chromium_src-2d31666a58e746b7a1d415c99e5f68ad9256d236.zip chromium_src-2d31666a58e746b7a1d415c99e5f68ad9256d236.tar.gz chromium_src-2d31666a58e746b7a1d415c99e5f68ad9256d236.tar.bz2 |
Minor cleanup to OneShotTimer and RepeatingTimer: moves more of the member variables into the Task subclass.
Also included in this change: deprecate MessageLoop::timer_manager(), and change consumers over to use OneShotTimer or RepeatingTimer.
R=beng
BUG=1346553
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1684 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/views')
-rw-r--r-- | chrome/views/chrome_menu.cc | 85 | ||||
-rw-r--r-- | chrome/views/chrome_menu.h | 56 | ||||
-rw-r--r-- | chrome/views/repeat_controller.cc | 34 | ||||
-rw-r--r-- | chrome/views/repeat_controller.h | 25 | ||||
-rw-r--r-- | chrome/views/throbber.cc | 34 | ||||
-rw-r--r-- | chrome/views/throbber.h | 22 |
6 files changed, 80 insertions, 176 deletions
diff --git a/chrome/views/chrome_menu.cc b/chrome/views/chrome_menu.cc index a1f9306..360f6c1 100644 --- a/chrome/views/chrome_menu.cc +++ b/chrome/views/chrome_menu.cc @@ -181,36 +181,16 @@ static void ScrollToVisible(View* view) { // MenuScrollTask -------------------------------------------------------------- // MenuScrollTask is used when the SubmenuView does not all fit on screen and -// the mouse is over the scroll up/down buttons. MenuScrollTask schedules itself -// with the TimerManager. When Run is invoked MenuScrollTask scrolls +// the mouse is over the scroll up/down buttons. MenuScrollTask schedules +// itself with a RepeatingTimer. When Run is invoked MenuScrollTask scrolls // appropriately. -class MenuScrollTask : public Task { +class MenuScrollTask { public: MenuScrollTask() : submenu_(NULL) { pixels_per_second_ = pref_menu_height * 20; } - virtual ~MenuScrollTask() { - StopScrolling(); - } - - virtual void Run() { - DCHECK(submenu_); - gfx::Rect vis_rect = submenu_->GetVisibleBounds(); - const int delta_y = static_cast<int>( - (Time::Now() - start_scroll_time_).InMilliseconds() * - pixels_per_second_ / 1000); - int target_y = start_y_; - if (is_scrolling_up_) - target_y = std::max(0, target_y - delta_y); - else - target_y = std::min(submenu_->GetHeight() - vis_rect.height(), - target_y + delta_y); - submenu_->ScrollRectToVisible(vis_rect.x(), target_y, vis_rect.width(), - vis_rect.height()); - } - void Update(const MenuController::MenuPart& part) { if (!part.is_scroll()) { StopScrolling(); @@ -227,18 +207,15 @@ public: submenu_ = new_menu; is_scrolling_up_ = new_is_up; - if (!scrolling_timer_.get()) { - scrolling_timer_.reset(new Timer(kScrollTimerMS, this, true)); - TimerManager* tm = MessageLoop::current()->timer_manager(); - tm->StartTimer(scrolling_timer_.get()); + if (!scrolling_timer_.IsRunning()) { + scrolling_timer_.Start(TimeDelta::FromMilliseconds(kScrollTimerMS), this, + &MenuScrollTask::Run); } } void StopScrolling() { - if (scrolling_timer_.get()) { - TimerManager* tm = MessageLoop::current()->timer_manager(); - tm->StopTimer(scrolling_timer_.get()); - scrolling_timer_.reset(NULL); + if (scrolling_timer_.IsRunning()) { + scrolling_timer_.Stop(); submenu_ = NULL; } } @@ -247,6 +224,22 @@ public: SubmenuView* submenu() const { return submenu_; } private: + void Run() { + DCHECK(submenu_); + gfx::Rect vis_rect = submenu_->GetVisibleBounds(); + const int delta_y = static_cast<int>( + (Time::Now() - start_scroll_time_).InMilliseconds() * + pixels_per_second_ / 1000); + int target_y = start_y_; + if (is_scrolling_up_) + target_y = std::max(0, target_y - delta_y); + else + target_y = std::min(submenu_->GetHeight() - vis_rect.height(), + target_y + delta_y); + submenu_->ScrollRectToVisible(vis_rect.x(), target_y, vis_rect.width(), + vis_rect.height()); + } + // SubmenuView being scrolled. SubmenuView* submenu_; @@ -254,7 +247,7 @@ public: bool is_scrolling_up_; // Timer to periodically scroll. - scoped_ptr<Timer> scrolling_timer_; + base::RepeatingTimer<MenuScrollTask> scrolling_timer_; // Time we started scrolling at. Time start_scroll_time_; @@ -2088,13 +2081,7 @@ MenuController::MenuController(bool blocking) showing_(false), exit_all_(false), did_capture_(false), -#pragma warning(suppress: 4355) // Okay to pass "this" here. - show_task_(this), result_(NULL), - show_timer_(NULL), -#pragma warning(suppress: 4355) // Okay to pass "this" here. - cancel_all_task_(this), - cancel_all_timer_(NULL), drop_target_(NULL), owner_(NULL), possible_drag_(false), @@ -2384,31 +2371,21 @@ void MenuController::BuildMenuItemPath(MenuItemView* item, } void MenuController::StartShowTimer() { - StopShowTimer(); - show_timer_ = MessageLoop::current()->timer_manager()-> - StartTimer(kShowDelay, &show_task_, false); + show_timer_.Start(TimeDelta::FromMilliseconds(kShowDelay), this, + &MenuController::CommitPendingSelection); } void MenuController::StopShowTimer() { - if (show_timer_) { - MessageLoop::current()->timer_manager()->StopTimer(show_timer_); - delete show_timer_; - show_timer_ = NULL; - } + show_timer_.Stop(); } void MenuController::StartCancelAllTimer() { - StopCancelAllTimer(); - cancel_all_timer_ = MessageLoop::current()->timer_manager()-> - StartTimer(kCloseOnExitTime, &cancel_all_task_, false); + cancel_all_timer_.Start(TimeDelta::FromMilliseconds(kCloseOnExitTime), + this, &MenuController::CancelAll); } void MenuController::StopCancelAllTimer() { - if (cancel_all_timer_) { - MessageLoop::current()->timer_manager()->StopTimer(cancel_all_timer_); - delete cancel_all_timer_; - cancel_all_timer_ = NULL; - } + cancel_all_timer_.Stop(); } gfx::Rect MenuController::CalculateMenuBounds(MenuItemView* item, diff --git a/chrome/views/chrome_menu.h b/chrome/views/chrome_menu.h index 1377155..a017b3d 100644 --- a/chrome/views/chrome_menu.h +++ b/chrome/views/chrome_menu.h @@ -17,8 +17,6 @@ #include "chrome/views/view.h" #include "skia/include/SkBitmap.h" -class Timer; - namespace ChromeViews { class HWNDViewContainer; @@ -595,7 +593,6 @@ class MenuController : public MessageLoopForUI::Dispatcher { public: friend class MenuHostRootView; friend class MenuItemView; - friend class ShowSubmenusTask; friend class MenuScrollTask; // If a menu is currently active, this returns the controller for it. @@ -629,6 +626,9 @@ class MenuController : public MessageLoopForUI::Dispatcher { // as well. This immediatley hides all menus. void Cancel(bool all); + // An alternative to Cancel(true) that can be used with a OneShotTimer. + void CancelAll() { return Cancel(true); } + // Various events, forwarded from the submenu. // // NOTE: the coordinates of the events are in that of the @@ -649,42 +649,6 @@ class MenuController : public MessageLoopForUI::Dispatcher { void OnDragExitedScrollButton(SubmenuView* source); private: - // As the mouse moves around submenus are not opened immediately. Instead - // they open after this timer fires. - class ShowSubmenusTask : public Task { - public: - explicit ShowSubmenusTask(MenuController* controller) - : controller_(controller) {} - - virtual void Run() { - controller_->CommitPendingSelection(); - } - - private: - MenuController* controller_; - - DISALLOW_EVIL_CONSTRUCTORS(ShowSubmenusTask); - }; - - // Task used to invoke Cancel(true). This is used during drag and drop - // to hide the menu after the mouse moves out of the of the menu. This is - // necessitated by the lack of an ability to detect when the drag has - // completed from the drop side. - class CancelAllTask : public Task { - public: - explicit CancelAllTask(MenuController* controller) - : controller_(controller) {} - - virtual void Run() { - controller_->Cancel(true); - } - - private: - MenuController* controller_; - - DISALLOW_EVIL_CONSTRUCTORS(CancelAllTask); - }; - // Tracks selection information. struct State { State() : item(NULL), submenu_open(false) {} @@ -914,13 +878,15 @@ class MenuController : public MessageLoopForUI::Dispatcher { // MenuController to restore the state when the nested run returns. std::list<State> menu_stack_; - // Used to comming pending to state. - ShowSubmenusTask show_task_; - Timer* show_timer_; + // As the mouse moves around submenus are not opened immediately. Instead + // they open after this timer fires. + base::OneShotTimer<MenuController> show_timer_; - // Used to cancel all menus. - CancelAllTask cancel_all_task_; - Timer* cancel_all_timer_; + // Used to invoke CancelAll(). This is used during drag and drop to hide the + // menu after the mouse moves out of the of the menu. This is necessitated by + // the lack of an ability to detect when the drag has completed from the drop + // side. + base::OneShotTimer<MenuController> cancel_all_timer_; // Drop target. MenuItemView* drop_target_; diff --git a/chrome/views/repeat_controller.cc b/chrome/views/repeat_controller.cc index cf41dea..96aa923 100644 --- a/chrome/views/repeat_controller.cc +++ b/chrome/views/repeat_controller.cc @@ -15,45 +15,29 @@ static const int kRepeatDelay = 50; // RepeatController, public: RepeatController::RepeatController(RepeatCallback* callback) - : timer_(NULL), - callback_(callback) { + : callback_(callback) { } RepeatController::~RepeatController() { - DestroyTimer(); } void RepeatController::Start() { - DCHECK(!timer_); // The first timer is slightly longer than subsequent repeats. - timer_ = MessageLoop::current()->timer_manager()->StartTimer( - kInitialRepeatDelay, this, false); + timer_.Start(TimeDelta::FromMilliseconds(kInitialRepeatDelay), this, + &RepeatController::Run); } void RepeatController::Stop() { - DestroyTimer(); -} - -void RepeatController::Run() { - DestroyTimer(); - - // TODO(beng): (Cleanup) change this to just Run() when base rolls forward. - callback_->RunWithParams(Tuple0()); - timer_ = MessageLoop::current()->timer_manager()->StartTimer( - kRepeatDelay, this, true); + timer_.Stop(); } /////////////////////////////////////////////////////////////////////////////// // RepeatController, private: -void RepeatController::DestroyTimer() { - if (!timer_) - return; - - MessageLoop::current()->timer_manager()->StopTimer(timer_); - delete timer_; - timer_ = NULL; -} - +void RepeatController::Run() { + timer_.Start(TimeDelta::FromMilliseconds(kRepeatDelay), this, + &RepeatController::Run); + callback_->Run(); } +} // namespace ChromeViews diff --git a/chrome/views/repeat_controller.h b/chrome/views/repeat_controller.h index 69c539a..92434f6 100644 --- a/chrome/views/repeat_controller.h +++ b/chrome/views/repeat_controller.h @@ -2,11 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_VIEWS_REPEAT_CONTROLLER_H__ -#define CHROME_VIEWS_REPEAT_CONTROLLER_H__ +#ifndef CHROME_VIEWS_REPEAT_CONTROLLER_H_ +#define CHROME_VIEWS_REPEAT_CONTROLLER_H_ -#include "base/message_loop.h" -#include "base/task.h" +#include "base/timer.h" namespace ChromeViews { @@ -20,7 +19,7 @@ namespace ChromeViews { // associated action. // /////////////////////////////////////////////////////////////////////////////// -class RepeatController : public Task { +class RepeatController { public: typedef Callback0::Type RepeatCallback; @@ -34,24 +33,20 @@ class RepeatController : public Task { // Stop repeating. void Stop(); - // Task implementation: - void Run(); - private: RepeatController(); - // Stop and delete the timer. - void DestroyTimer(); + // Called when the timer expires. + void Run(); // The current timer. - Timer* timer_; + base::OneShotTimer<RepeatController> timer_; scoped_ptr<RepeatCallback> callback_; - DISALLOW_EVIL_CONSTRUCTORS(RepeatController); + DISALLOW_COPY_AND_ASSIGN(RepeatController); }; -} - -#endif // #ifndef CHROME_VIEWS_REPEAT_CONTROLLER_H__ +} // namespace ChromeViews +#endif // #ifndef CHROME_VIEWS_REPEAT_CONTROLLER_H_ diff --git a/chrome/views/throbber.cc b/chrome/views/throbber.cc index 6697d80..eee8a02 100644 --- a/chrome/views/throbber.cc +++ b/chrome/views/throbber.cc @@ -4,8 +4,6 @@ #include "chrome/views/throbber.h" -#include "base/message_loop.h" -#include "base/timer.h" #include "chrome/app/theme/theme_resources.h" #include "chrome/common/gfx/chrome_canvas.h" #include "chrome/common/logging_chrome.h" @@ -40,8 +38,8 @@ void Throbber::Start() { start_time_ = GetTickCount(); last_time_recorded_ = start_time_; - timer_ = MessageLoop::current()->timer_manager()->StartTimer( - frame_time_ms_ - 10, this, true); + timer_.Start( + TimeDelta::FromMilliseconds(frame_time_ms_ - 10), this, &Throbber::Run); running_ = true; @@ -52,8 +50,7 @@ void Throbber::Stop() { if (!running_) return; - MessageLoop::current()->timer_manager()->StopTimer(timer_); - timer_ = NULL; + timer_.Stop(); running_ = false; SchedulePaint(); // Important if we're not painting while stopped @@ -111,19 +108,15 @@ static const int kStopDelay = 50; SmoothedThrobber::SmoothedThrobber(int frame_time_ms) - : Throbber(frame_time_ms, /* paint_while_stopped= */ false), - start_delay_factory_(this), - end_delay_factory_(this) { + : Throbber(frame_time_ms, /* paint_while_stopped= */ false) { } void SmoothedThrobber::Start() { - end_delay_factory_.RevokeAll(); + stop_timer_.Stop(); - if (!running_ && start_delay_factory_.empty()) { - MessageLoop::current()->PostDelayedTask(FROM_HERE, - start_delay_factory_.NewRunnableMethod( - &SmoothedThrobber::StartDelayOver), - kStartDelay); + if (!running_ && !start_timer_.IsRunning()) { + start_timer_.Start(TimeDelta::FromMilliseconds(kStartDelay), this, + &SmoothedThrobber::StartDelayOver); } } @@ -132,15 +125,12 @@ void SmoothedThrobber::StartDelayOver() { } void SmoothedThrobber::Stop() { - TimerManager* timer_manager = MessageLoop::current()->timer_manager(); - if (!running_) - start_delay_factory_.RevokeAll(); + start_timer_.Stop(); - end_delay_factory_.RevokeAll(); - MessageLoop::current()->PostDelayedTask(FROM_HERE, - end_delay_factory_.NewRunnableMethod(&SmoothedThrobber::StopDelayOver), - kStopDelay); + stop_timer_.Stop(); + stop_timer_.Start(TimeDelta::FromMilliseconds(kStopDelay), this, + &SmoothedThrobber::StopDelayOver); } void SmoothedThrobber::StopDelayOver() { diff --git a/chrome/views/throbber.h b/chrome/views/throbber.h index 812cae1..3c42fde 100644 --- a/chrome/views/throbber.h +++ b/chrome/views/throbber.h @@ -8,19 +8,14 @@ #define CHROME_VIEWS_THROBBER_H__ #include "base/basictypes.h" -#include "base/task.h" +#include "base/timer.h" #include "chrome/views/view.h" class SkBitmap; -namespace base { -class Timer; -} - namespace ChromeViews { -class Throbber : public ChromeViews::View, - public Task { +class Throbber : public ChromeViews::View { public: // |frame_time_ms| is the amount of time that should elapse between frames // (in milliseconds) @@ -37,14 +32,13 @@ class Throbber : public ChromeViews::View, virtual void GetPreferredSize(CSize *out); virtual void Paint(ChromeCanvas* canvas); - // implemented from Task - virtual void Run(); - protected: // Specifies whether the throbber is currently animating or not bool running_; private: + void Run(); + bool paint_while_stopped_; int frame_count_; int last_frame_drawn_; @@ -52,7 +46,7 @@ class Throbber : public ChromeViews::View, DWORD last_time_recorded_; SkBitmap* frames_; int frame_time_ms_; - base::Timer* timer_; + base::RepeatingTimer<Throbber> timer_; DISALLOW_EVIL_CONSTRUCTORS(Throbber); }; @@ -77,10 +71,8 @@ class SmoothedThrobber : public Throbber { // This function stops the actual throbbing. void StopDelayOver(); - // Method factory for delaying throbber startup. - ScopedRunnableMethodFactory<SmoothedThrobber> start_delay_factory_; - // Method factory for delaying throbber shutdown. - ScopedRunnableMethodFactory<SmoothedThrobber> end_delay_factory_; + base::OneShotTimer<SmoothedThrobber> start_timer_; + base::OneShotTimer<SmoothedThrobber> stop_timer_; DISALLOW_EVIL_CONSTRUCTORS(SmoothedThrobber); }; |