File: geometry_cache.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download | duplicates (4)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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.

#include "ui/gfx/x/geometry_cache.h"

#include <memory>

#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/event.h"
#include "ui/gfx/x/xproto.h"

namespace x11 {

GeometryCache::GeometryCache(Connection* connection,
                             Window window,
                             BoundsChangedCallback bounds_changed_callback)
    : connection_(connection),
      window_(window),
      bounds_changed_callback_(bounds_changed_callback) {
  scoped_observation_.Observe(connection_);
  window_events_ =
      connection_->ScopedSelectEvent(window_, EventMask::StructureNotify);

  parent_future_ = connection_->QueryTree(window_);
  parent_future_.OnResponse(base::BindOnce(&GeometryCache::OnQueryTreeResponse,
                                           weak_ptr_factory_.GetWeakPtr()));
  geometry_future_ = connection_->GetGeometry(window_);
  geometry_future_.OnResponse(base::BindOnce(
      &GeometryCache::OnGetGeometryResponse, weak_ptr_factory_.GetWeakPtr()));
}

GeometryCache::~GeometryCache() = default;

gfx::Rect GeometryCache::GetBoundsPx() {
  if (!have_parent_) {
    parent_future_.DispatchNow();
  }
  CHECK(have_parent_);
  if (!have_geometry_) {
    geometry_future_.DispatchNow();
  }
  CHECK(have_geometry_);

  if (!parent_) {
    return geometry_;
  }
  auto parent_bounds = parent_->GetBoundsPx();
  gfx::Vector2d offset(parent_bounds.x(), parent_bounds.y());
  return geometry_ + offset;
}

void GeometryCache::OnQueryTreeResponse(QueryTreeResponse response) {
  if (have_parent_) {
    return;
  }
  OnParentChanged(response ? response->parent : Window::None,
                  geometry_.origin());
}

void GeometryCache::OnGetGeometryResponse(GetGeometryResponse response) {
  if (have_geometry_) {
    return;
  }
  OnGeometryChanged(response ? gfx::Rect(response->x, response->y,
                                         response->width, response->height)
                             : gfx::Rect());
}

void GeometryCache::OnParentChanged(Window parent, const gfx::Point& position) {
  have_parent_ = true;
  if (parent == Window::None) {
    parent_.reset();
  } else if (!parent_ || parent_->window_ != parent) {
    parent_ = std::make_unique<GeometryCache>(
        connection_, parent,
        base::BindRepeating(&GeometryCache::OnParentGeometryChanged,
                            weak_ptr_factory_.GetWeakPtr()));
  }

  geometry_.set_origin(position);

  NotifyGeometryChanged();
}

void GeometryCache::OnGeometryChanged(const gfx::Rect& geometry) {
  have_geometry_ = true;
  geometry_ = geometry;

  NotifyGeometryChanged();
}

void GeometryCache::OnPositionChanged(const gfx::Point& origin) {
  geometry_.set_origin(origin);
  NotifyGeometryChanged();
}

bool GeometryCache::Ready() const {
  return have_geometry_ && have_parent_ && (!parent_ || parent_->Ready());
}

void GeometryCache::OnParentGeometryChanged(
    const std::optional<gfx::Rect>& old_parent_bounds,
    const gfx::Rect& new_parent_bounds) {
  NotifyGeometryChanged();
}

void GeometryCache::NotifyGeometryChanged() {
  if (!Ready()) {
    return;
  }

  auto geometry = GetBoundsPx();
  if (last_notified_geometry_ == geometry) {
    return;
  }

  auto old_geometry = last_notified_geometry_;
  last_notified_geometry_ = geometry;
  bounds_changed_callback_.Run(old_geometry, geometry);
}

void GeometryCache::OnEvent(const Event& xevent) {
  // Ignore client events.
  if (xevent.send_event()) {
    return;
  }

  if (auto* configure = xevent.As<ConfigureNotifyEvent>()) {
    if (configure->window == window_) {
      OnGeometryChanged(gfx::Rect(configure->x, configure->y, configure->width,
                                  configure->height));
    }
  } else if (auto* reparent = xevent.As<ReparentNotifyEvent>()) {
    if (reparent->window == window_) {
      OnParentChanged(reparent->parent, gfx::Point(reparent->x, reparent->y));
    }
  } else if (auto* gravity = xevent.As<GravityNotifyEvent>()) {
    if (gravity->window == window_) {
      OnPositionChanged(gfx::Point(gravity->x, gravity->y));
    }
  }
}

}  // namespace x11