<commit_before>// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/app_list/views/app_list_main_view.h"

#include <algorithm>

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_folder_item.h"
#include "ui/app_list/app_list_item.h"
#include "ui/app_list/app_list_model.h"
#include "ui/app_list/app_list_switches.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/pagination_model.h"
#include "ui/app_list/search_box_model.h"
#include "ui/app_list/views/app_list_folder_view.h"
#include "ui/app_list/views/app_list_item_view.h"
#include "ui/app_list/views/apps_container_view.h"
#include "ui/app_list/views/apps_grid_view.h"
#include "ui/app_list/views/contents_switcher_view.h"
#include "ui/app_list/views/contents_view.h"
#include "ui/app_list/views/search_box_view.h"
#include "ui/views/border.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"

namespace app_list {

namespace {

// Inner padding space in pixels of bubble contents.
const int kInnerPadding = 1;

// The maximum allowed time to wait for icon loading in milliseconds.
const int kMaxIconLoadingWaitTimeInMs = 50;

// The padding around the search box in the experimental app list.
const int kSearchBoxViewPadding = 24;
const int kSearchBoxViewPaddingBottom = 12;

// A view that holds another view and takes its preferred size. This is used for
// wrapping the search box view so it still gets laid out while hidden. This is
// a separate class so it can notify the main view on search box visibility
// change.
class SearchBoxContainerView : public views::View {
 public:
  SearchBoxContainerView(AppListMainView* host, SearchBoxView* search_box)
      : host_(host), search_box_(search_box) {
    SetLayoutManager(new views::FillLayout());
    AddChildView(search_box);
  }
  virtual ~SearchBoxContainerView() {}

 private:
  // Overridden from views::View:
  virtual void ChildVisibilityChanged(views::View* child) OVERRIDE {
    DCHECK_EQ(search_box_, child);
    host_->NotifySearchBoxVisibilityChanged();
  }

  AppListMainView* host_;
  SearchBoxView* search_box_;

  DISALLOW_COPY_AND_ASSIGN(SearchBoxContainerView);
};

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// AppListMainView::IconLoader

class AppListMainView::IconLoader : public AppListItemObserver {
 public:
  IconLoader(AppListMainView* owner,
             AppListItem* item,
             float scale)
      : owner_(owner),
        item_(item) {
    item_->AddObserver(this);

    // Triggers icon loading for given |scale_factor|.
    item_->icon().GetRepresentation(scale);
  }

  virtual ~IconLoader() {
    item_->RemoveObserver(this);
  }

 private:
  // AppListItemObserver overrides:
  virtual void ItemIconChanged() OVERRIDE {
    owner_->OnItemIconLoaded(this);
    // Note that IconLoader is released here.
  }

  AppListMainView* owner_;
  AppListItem* item_;

  DISALLOW_COPY_AND_ASSIGN(IconLoader);
};

////////////////////////////////////////////////////////////////////////////////
// AppListMainView:

AppListMainView::AppListMainView(AppListViewDelegate* delegate,
                                 int initial_apps_page,
                                 gfx::NativeView parent)
    : delegate_(delegate),
      model_(delegate->GetModel()),
      search_box_view_(NULL),
      contents_view_(NULL),
      contents_switcher_view_(NULL),
      weak_ptr_factory_(this) {
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
                                        kInnerPadding,
                                        kInnerPadding,
                                        kInnerPadding));

  search_box_view_ = new SearchBoxView(this, delegate);
  views::View* container = new SearchBoxContainerView(this, search_box_view_);
  if (switches::IsExperimentalAppListEnabled()) {
    container->SetBorder(
        views::Border::CreateEmptyBorder(kSearchBoxViewPadding,
                                         kSearchBoxViewPadding,
                                         kSearchBoxViewPaddingBottom,
                                         kSearchBoxViewPadding));
  }
  AddChildView(container);
  AddContentsViews();

  // Switch the apps grid view to the specified page.
  app_list::PaginationModel* pagination_model = GetAppsPaginationModel();
  if (pagination_model->is_valid_page(initial_apps_page))
    pagination_model->SelectPage(initial_apps_page, false);

  // Starts icon loading early.
  PreloadIcons(parent);
}

void AppListMainView::AddContentsViews() {
  contents_view_ = new ContentsView(this);
  if (app_list::switches::IsExperimentalAppListEnabled()) {
    contents_switcher_view_ = new ContentsSwitcherView(contents_view_);
    contents_view_->SetContentsSwitcherView(contents_switcher_view_);
  }
  contents_view_->InitNamedPages(model_, delegate_);
  AddChildView(contents_view_);
  if (contents_switcher_view_)
    AddChildView(contents_switcher_view_);

  search_box_view_->set_contents_view(contents_view_);

  contents_view_->SetPaintToLayer(true);
  contents_view_->SetFillsBoundsOpaquely(false);
  contents_view_->layer()->SetMasksToBounds(true);

  delegate_->StartSearch();
}

AppListMainView::~AppListMainView() {
  pending_icon_loaders_.clear();
}

void AppListMainView::ShowAppListWhenReady() {
  if (pending_icon_loaders_.empty()) {
    icon_loading_wait_timer_.Stop();
    GetWidget()->Show();
    return;
  }

  if (icon_loading_wait_timer_.IsRunning())
    return;

  icon_loading_wait_timer_.Start(
      FROM_HERE,
      base::TimeDelta::FromMilliseconds(kMaxIconLoadingWaitTimeInMs),
      this, &AppListMainView::OnIconLoadingWaitTimer);
}

void AppListMainView::ResetForShow() {
  contents_view_->apps_container_view()->ResetForShowApps();
  // We clear the search when hiding so when app list appears it is not showing
  // search results.
  search_box_view_->ClearSearch();
}
<commit_msg>Reset the experimental app list to the start page on dismiss.<commit_after>// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/app_list/views/app_list_main_view.h"

#include <algorithm>

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_folder_item.h"
#include "ui/app_list/app_list_item.h"
#include "ui/app_list/app_list_model.h"
#include "ui/app_list/app_list_switches.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/pagination_model.h"
#include "ui/app_list/search_box_model.h"
#include "ui/app_list/views/app_list_folder_view.h"
#include "ui/app_list/views/app_list_item_view.h"
#include "ui/app_list/views/apps_container_view.h"
#include "ui/app_list/views/apps_grid_view.h"
#include "ui/app_list/views/contents_switcher_view.h"
#include "ui/app_list/views/contents_view.h"
#include "ui/app_list/views/search_box_view.h"
#include "ui/views/border.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"

namespace app_list {

namespace {

// Inner padding space in pixels of bubble contents.
const int kInnerPadding = 1;

// The maximum allowed time to wait for icon loading in milliseconds.
const int kMaxIconLoadingWaitTimeInMs = 50;

// The padding around the search box in the experimental app list.
const int kSearchBoxViewPadding = 24;
const int kSearchBoxViewPaddingBottom = 12;

// A view that holds another view and takes its preferred size. This is used for
// wrapping the search box view so it still gets laid out while hidden. This is
// a separate class so it can notify the main view on search box visibility
// change.
class SearchBoxContainerView : public views::View {
 public:
  SearchBoxContainerView(AppListMainView* host, SearchBoxView* search_box)
      : host_(host), search_box_(search_box) {
    SetLayoutManager(new views::FillLayout());
    AddChildView(search_box);
  }
  virtual ~SearchBoxContainerView() {}

 private:
  // Overridden from views::View:
  virtual void ChildVisibilityChanged(views::View* child) OVERRIDE {
    DCHECK_EQ(search_box_, child);
    host_->NotifySearchBoxVisibilityChanged();
  }

  AppListMainView* host_;
  SearchBoxView* search_box_;

  DISALLOW_COPY_AND_ASSIGN(SearchBoxContainerView);
};

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// AppListMainView::IconLoader

class AppListMainView::IconLoader : public AppListItemObserver {
 public:
  IconLoader(AppListMainView* owner,
             AppListItem* item,
             float scale)
      : owner_(owner),
        item_(item) {
    item_->AddObserver(this);

    // Triggers icon loading for given |scale_factor|.
    item_->icon().GetRepresentation(scale);
  }

  virtual ~IconLoader() {
    item_->RemoveObserver(this);
  }

 private:
  // AppListItemObserver overrides:
  virtual void ItemIconChanged() OVERRIDE {
    owner_->OnItemIconLoaded(this);
    // Note that IconLoader is released here.
  }

  AppListMainView* owner_;
  AppListItem* item_;

  DISALLOW_COPY_AND_ASSIGN(IconLoader);
};

////////////////////////////////////////////////////////////////////////////////
// AppListMainView:

AppListMainView::AppListMainView(AppListViewDelegate* delegate,
                                 int initial_apps_page,
                                 gfx::NativeView parent)
    : delegate_(delegate),
      model_(delegate->GetModel()),
      search_box_view_(NULL),
      contents_view_(NULL),
      contents_switcher_view_(NULL),
      weak_ptr_factory_(this) {
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
                                        kInnerPadding,
                                        kInnerPadding,
                                        kInnerPadding));

  search_box_view_ = new SearchBoxView(this, delegate);
  views::View* container = new SearchBoxContainerView(this, search_box_view_);
  if (switches::IsExperimentalAppListEnabled()) {
    container->SetBorder(
        views::Border::CreateEmptyBorder(kSearchBoxViewPadding,
                                         kSearchBoxViewPadding,
                                         kSearchBoxViewPaddingBottom,
                                         kSearchBoxViewPadding));
  }
  AddChildView(container);
  AddContentsViews();

  // Switch the apps grid view to the specified page.
  app_list::PaginationModel* pagination_model = GetAppsPaginationModel();
  if (pagination_model->is_valid_page(initial_apps_page))
    pagination_model->SelectPage(initial_apps_page, false);

  // Starts icon loading early.
  PreloadIcons(parent);
}

void AppListMainView::AddContentsViews() {
  contents_view_ = new ContentsView(this);
  if (app_list::switches::IsExperimentalAppListEnabled()) {
    contents_switcher_view_ = new ContentsSwitcherView(contents_view_);
    contents_view_->SetContentsSwitcherView(contents_switcher_view_);
  }
  contents_view_->InitNamedPages(model_, delegate_);
  AddChildView(contents_view_);
  if (contents_switcher_view_)
    AddChildView(contents_switcher_view_);

  search_box_view_->set_contents_view(contents_view_);

  contents_view_->SetPaintToLayer(true);
  contents_view_->SetFillsBoundsOpaquely(false);
  contents_view_->layer()->SetMasksToBounds(true);

  delegate_->StartSearch();
}

AppListMainView::~AppListMainView() {
  pending_icon_loaders_.clear();
}

void AppListMainView::ShowAppListWhenReady() {
  if (pending_icon_loaders_.empty()) {
    icon_loading_wait_timer_.Stop();
    GetWidget()->Show();
    return;
  }

  if (icon_loading_wait_timer_.IsRunning())
    return;

  icon_loading_wait_timer_.Start(
      FROM_HERE,
      base::TimeDelta::FromMilliseconds(kMaxIconLoadingWaitTimeInMs),
      this, &AppListMainView::OnIconLoadingWaitTimer);
}

void AppListMainView::ResetForShow() {
  if (switches::IsExperimentalAppListEnabled()) {
    contents_view_->SetActivePage(contents_view_->GetPageIndexForNamedPage(
        ContentsView::NAMED_PAGE_START));
  }
  contents_view_->apps_container_view()->ResetForShowApps();
  // We clear the search when hiding so when app list appears it is not showing
  // search results.
  search_box_view_->ClearSearch();
}

{'fraction_non_alphanumeric': 0.12449424214130096, 'fraction_numerical': 0.0017117958294428882, 'mean_word_length': 3.512991573033708, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4427069', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3790, 'n_words': 756}