summaryrefslogtreecommitdiff
path: root/chromium/ui/views/widget/desktop_aura/desktop_screen_linux.cc
blob: 0eba478b86fe7008d9e8ce700dadc511b1bd3809 (plain)
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
// Copyright 2020 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/views/widget/desktop_aura/desktop_screen.h"

#include "base/notreached.h"

#if defined(USE_X11)
#include "ui/views/widget/desktop_aura/desktop_screen_x11.h"
#endif

#if defined(USE_OZONE)
#include "ui/base/ui_base_features.h"
#include "ui/ozone/public/platform_screen.h"
#include "ui/views/linux_ui/device_scale_factor_observer.h"
#include "ui/views/linux_ui/linux_ui.h"
#include "ui/views/widget/desktop_aura/desktop_screen_ozone.h"
#endif

namespace views {

#if defined(USE_OZONE)
// Listens to device scale factor changes that can be provided via "external" to
// Ozone sources such as toolkits, etc, and provides Ozone with new values.
class DesktopScreenOzoneLinux : public DesktopScreenOzone,
                                public DeviceScaleFactorObserver {
 public:
  DesktopScreenOzoneLinux() {
    auto* linux_ui = LinuxUI::instance();
    if (linux_ui) {
      display_scale_factor_observer_.Observe(linux_ui);

      // Send current scale factor as starting to observe doesn't actually
      // result in getting a OnDeviceScaleFactorChanged call.
      SetDeviceScaleFactorToPlatformScreen(linux_ui->GetDeviceScaleFactor());
    }
  }
  ~DesktopScreenOzoneLinux() override = default;

 private:
  // DeviceScaleFactorObserver:
  void OnDeviceScaleFactorChanged() override {
    SetDeviceScaleFactorToPlatformScreen(
        LinuxUI::instance()->GetDeviceScaleFactor());
  }

  void SetDeviceScaleFactorToPlatformScreen(float scale_factor) {
    platform_screen()->SetDeviceScaleFactor(scale_factor);
  }

  base::ScopedObservation<LinuxUI,
                          DeviceScaleFactorObserver,
                          &LinuxUI::AddDeviceScaleFactorObserver,
                          &LinuxUI::RemoveDeviceScaleFactorObserver>
      display_scale_factor_observer_{this};
};

#endif

std::unique_ptr<display::Screen> CreateDesktopScreen() {
#if defined(USE_OZONE)
  if (features::IsUsingOzonePlatform())
    return std::make_unique<DesktopScreenOzoneLinux>();
#endif
#if defined(USE_X11)
  auto screen = std::make_unique<DesktopScreenX11>();
  screen->Init();
  return screen;
#else
  NOTREACHED();
  return nullptr;
#endif
}

}  // namespace views