/
event_queue_with_resize_display.h
91 lines (77 loc) · 2.96 KB
/
event_queue_with_resize_display.h
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
// LAF OS Library
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef OS_COMMON_EVENT_QUEUE_WITH_RESIZE_DISPLAY_INCLUDED
#define OS_COMMON_EVENT_QUEUE_WITH_RESIZE_DISPLAY_INCLUDED
#pragma once
#include "base/time.h"
#include "os/event.h"
#include "os/event_queue.h"
#include <queue>
namespace os {
#pragma push_macro("None")
#undef None // Undefine the X11 None macro
// Auxiliary class to handle the ResizeDisplay event in a special way.
// We can use the setResizeDisplayEvent() function in this class to
// change a pending resize event to be sent in a near future (after
// 100ms). If we receive other setResizeDisplayEvent() calls, only
// one ResizeEvent will be generated (the last one).
//
// This class is useful in platforms like X11 where we cannot know
// when the resize operation has started and finished with information
// from the OS. We only receive a sequence of ConfigureNotify events,
// but we never know when it's the last resize event (we can guess
// that is the last resize event when queueEvent() is called with a
// non-Event::None kind of event).
//
// On Windows, we use this event queue behavior just in case we don't
// receive a WM_EXITSIZEMOVE message (I don't have information if this
// can happen, but the timer might be a good solution just in case we
// don't receive the message).
class EventQueueWithResizeDisplay : public EventQueue {
public:
// This should be called at the beginning of getEvent() member.
void checkResizeDisplayEvent(bool& canWait) {
if (m_resizeEvent.type() != Event::None) {
canWait = false;
// Wait 100ms to enqueue the event
if (base::current_tick() - m_resizeEventTick > 100)
enqueueResizeDisplayEvent();
}
}
void queueEvent(const Event& ev) override {
// If we are enqueuing another event and we have a pending
// ResizeDisplay event, we first enqueue the ResizeDisplay event,
// because it means that the resize operation is over.
if (m_resizeEvent.type() != Event::None)
enqueueResizeDisplayEvent();
m_events.push(ev);
}
// Sets the ResizeDisplay to be sent in the near time (150ms in the
// future). This is used to avoid sending a lot of ResizeDisplay
// events while the user is resizing the window.
//
// Returns true if this is the first ResizeDisplay event of a
// sequence of resize events.
bool setResizeDisplayEvent(const Event& resizeEvent) {
bool isNewEvent = (m_resizeEvent.type() == Event::None);
m_resizeEvent = resizeEvent;
m_resizeEventTick = base::current_tick();
return isNewEvent;
}
void enqueueResizeDisplayEvent() {
m_events.push(m_resizeEvent);
m_resizeEvent.setType(Event::None);
m_resizeEventTick = 0;
}
protected:
std::queue<Event> m_events;
Event m_resizeEvent;
base::tick_t m_resizeEventTick = 0;
};
#pragma pop_macro("None")
} // namespace os
#endif