blob: 9c1e6eca641d0f880da3c9797a0ad63c091a6b56 [file] [log] [blame]
[email protected]472de002014-07-18 22:36:131// Copyright 2014 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
oshimaa15deb02016-05-11 00:19:335#include "ui/display/display_change_notifier.h"
[email protected]472de002014-07-18 22:36:136
avic89eb8d42015-12-23 08:08:187#include <stdint.h>
8
oshimaa15deb02016-05-11 00:19:339#include "ui/display/display.h"
10#include "ui/display/display_observer.h"
[email protected]472de002014-07-18 22:36:1311
oshimaa15deb02016-05-11 00:19:3312namespace display {
[email protected]472de002014-07-18 22:36:1313
14namespace {
15
16class DisplayComparator {
17 public:
18 explicit DisplayComparator(const Display& display)
oshimaa15deb02016-05-11 00:19:3319 : display_id_(display.id()) {}
[email protected]472de002014-07-18 22:36:1320
21 bool operator()(const Display& display) const {
22 return display.id() == display_id_;
23 }
24
25 private:
avic89eb8d42015-12-23 08:08:1826 int64_t display_id_;
[email protected]472de002014-07-18 22:36:1327};
28
oshimaa15deb02016-05-11 00:19:3329} // anonymous namespace
[email protected]472de002014-07-18 22:36:1330
oshimaa15deb02016-05-11 00:19:3331DisplayChangeNotifier::DisplayChangeNotifier() {}
[email protected]472de002014-07-18 22:36:1332
oshimaa15deb02016-05-11 00:19:3333DisplayChangeNotifier::~DisplayChangeNotifier() {}
[email protected]472de002014-07-18 22:36:1334
35void DisplayChangeNotifier::AddObserver(DisplayObserver* obs) {
36 observer_list_.AddObserver(obs);
37}
38
39void DisplayChangeNotifier::RemoveObserver(DisplayObserver* obs) {
40 observer_list_.RemoveObserver(obs);
41}
42
43void DisplayChangeNotifier::NotifyDisplaysChanged(
44 const std::vector<Display>& old_displays,
45 const std::vector<Display>& new_displays) {
46 // Display present in old_displays but not in new_displays has been removed.
jdoerriedcef4b12018-10-10 12:04:3247 auto old_it = old_displays.begin();
[email protected]472de002014-07-18 22:36:1348 for (; old_it != old_displays.end(); ++old_it) {
49 if (std::find_if(new_displays.begin(), new_displays.end(),
50 DisplayComparator(*old_it)) == new_displays.end()) {
ericwilligers44bb4272016-10-19 00:15:2451 for (DisplayObserver& observer : observer_list_)
52 observer.OnDisplayRemoved(*old_it);
[email protected]472de002014-07-18 22:36:1353 }
54 }
55
56 // Display present in new_displays but not in old_displays has been added.
57 // Display present in both might have been modified.
jdoerriedcef4b12018-10-10 12:04:3258 for (auto new_it = new_displays.begin(); new_it != new_displays.end();
59 ++new_it) {
60 auto old_it = std::find_if(old_displays.begin(), old_displays.end(),
61 DisplayComparator(*new_it));
[email protected]472de002014-07-18 22:36:1362
63 if (old_it == old_displays.end()) {
ericwilligers44bb4272016-10-19 00:15:2464 for (DisplayObserver& observer : observer_list_)
65 observer.OnDisplayAdded(*new_it);
[email protected]472de002014-07-18 22:36:1366 continue;
67 }
68
69 uint32_t metrics = DisplayObserver::DISPLAY_METRIC_NONE;
70
71 if (new_it->bounds() != old_it->bounds())
72 metrics |= DisplayObserver::DISPLAY_METRIC_BOUNDS;
73
74 if (new_it->rotation() != old_it->rotation())
75 metrics |= DisplayObserver::DISPLAY_METRIC_ROTATION;
76
77 if (new_it->work_area() != old_it->work_area())
78 metrics |= DisplayObserver::DISPLAY_METRIC_WORK_AREA;
79
80 if (new_it->device_scale_factor() != old_it->device_scale_factor())
81 metrics |= DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
82
hubbe853276f2017-07-02 06:44:5983 if (new_it->color_space() != old_it->color_space())
84 metrics |= DisplayObserver::DISPLAY_METRIC_COLOR_SPACE;
85
[email protected]472de002014-07-18 22:36:1386 if (metrics != DisplayObserver::DISPLAY_METRIC_NONE) {
ericwilligers44bb4272016-10-19 00:15:2487 for (DisplayObserver& observer : observer_list_)
88 observer.OnDisplayMetricsChanged(*new_it, metrics);
[email protected]472de002014-07-18 22:36:1389 }
90 }
91}
92
oshimaa15deb02016-05-11 00:19:3393} // namespace display