File: quick_app_access_model.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,563 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_APP_LIST_QUICK_APP_ACCESS_MODEL_H_
#define ASH_APP_LIST_QUICK_APP_ACCESS_MODEL_H_

#include <optional>
#include <string>

#include "ash/app_list/model/app_list_item_observer.h"
#include "ash/public/cpp/app_list/app_list_controller_observer.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"

namespace gfx {
class ImageSkia;
class Size;
}  // namespace gfx

namespace ash {

class AppListItem;
class AppListController;

// The model which holds information on which app is currently set as the quick
// app. Shelf home buttons observe changes to this model and will show/hide the
// quick app button accordingly.
class QuickAppAccessModel : public AppListItemObserver,
                            public AppListControllerObserver {
 public:
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    // Called when the quick app shown state changes.
    virtual void OnQuickAppShouldShowChanged(bool show_quick_app) = 0;

    // Called when the default icon for the quick app icon changes.
    virtual void OnQuickAppIconChanged() = 0;
  };

  QuickAppAccessModel();
  QuickAppAccessModel(const QuickAppAccessModel&) = delete;
  QuickAppAccessModel& operator=(const QuickAppAccessModel&) = delete;
  ~QuickAppAccessModel() override;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Set the quick app what will be shown next to home buttons in the shelf.
  // Returns true when the quick app was changed to a valid `app_id` or reset
  // using an empty `app_id`.
  bool SetQuickApp(const std::string& app_id);

  // Set the quick app as activated and update the quick app shown state.
  void SetQuickAppActivated();

  // Returns the quick app's icon as an image, sized to 'icon_size'.
  gfx::ImageSkia GetAppIcon(gfx::Size icon_size);

  // Returns the quick app's display name.
  const std::u16string GetAppName() const;

  const std::string& quick_app_id() const { return quick_app_id_; }
  bool quick_app_should_show_state() const {
    return quick_app_should_show_state_;
  }

 private:
  // AppListItemObserver:
  void ItemDefaultIconChanged() override;
  void ItemIconVersionChanged() override;
  void ItemBeingDestroyed() override;

  // AppListControllerObserver:
  void OnAppListVisibilityChanged(bool shown, int64_t display_id) override;

  AppListItem* GetQuickAppItem() const;

  // Checks if the should show state of the quick app has changed, and notifies
  // observers when the state does change.
  void UpdateQuickAppShouldShowState();

  // Calculates and returns whether the quick app should show.
  bool ShouldShowQuickApp();

  // Reset the quick app id and other associated variables to their default
  // values.
  void ClearQuickApp();

  // The time that the icon load is requested.
  std::optional<base::TimeTicks> icon_load_start_time_;

  base::ObserverList<Observer> observers_;

  base::ScopedObservation<AppListItem, AppListItemObserver> item_observation_{
      this};
  base::ScopedObservation<AppListController, AppListControllerObserver>
      app_list_controller_observer_{this};

  // The app id for the quick app.
  std::string quick_app_id_;

  // Whether the quick app is in a state such that it should be shown.
  bool quick_app_should_show_state_ = false;
};

}  // namespace ash

#endif  // ASH_APP_LIST_QUICK_APP_ACCESS_MODEL_H_