17
17
* Authored by: Marco Trevisan (Treviño) <3v1n0@ubuntu.com>
20
#include "GLibSource.h"
20
#include <unordered_set>
21
22
#include "AppmenuIndicator.h"
23
#include "ConnectionManager.h"
25
27
namespace indicator
31
const Indicator::Entries empty_entries_;
28
34
struct AppmenuIndicator::Impl
30
36
Impl(AppmenuIndicator* parent)
32
// When the active window has changed we might need to emit an updated signal
33
parent->active_window.changed.connect([this, parent] (unsigned long) {
34
update_wait_.reset(new glib::Timeout(250, [parent] {
35
parent->updated.emit();
40
parent->updated.connect([this] { update_wait_.reset(); });
38
connections_.Add(parent->on_entry_added.connect([this] (Entry::Ptr const& entry) {
39
window_entries_[entry->parent_window()].push_back(entry);
42
connections_.Add(parent->on_entry_removed.connect([this] (Entry::Ptr const& entry) {
43
auto it = window_entries_.find(entry->parent_window());
45
if (it != window_entries_.end())
47
auto& entries = it->second;
48
entries.erase(std::remove(entries.begin(), entries.end(), entry), entries.end());
51
window_entries_.erase(it);
43
glib::Source::UniquePtr update_wait_;
56
connection::Manager connections_;
57
std::unordered_map<uint32_t, Indicator::Entries> window_entries_;
46
60
AppmenuIndicator::AppmenuIndicator(std::string const& name)
49
62
, impl_(new AppmenuIndicator::Impl(this))
52
65
AppmenuIndicator::~AppmenuIndicator()
68
void AppmenuIndicator::Sync(Indicator::Entries const& entries)
70
std::unordered_set<uint32_t> changed_windows;
71
connection::Wrapper added_conn(on_entry_added.connect([this, &changed_windows] (Entry::Ptr const& entry) {
72
changed_windows.insert(entry->parent_window());
75
connection::Wrapper rm_conn(on_entry_removed.connect([this, &changed_windows] (Entry::Ptr const& entry) {
76
changed_windows.insert(entry->parent_window());
79
Indicator::Sync(entries);
81
for (uint32_t win : changed_windows)
82
updated_win.emit(win);
85
Indicator::Entries const& AppmenuIndicator::GetEntriesForWindow(uint32_t parent_window) const
87
auto it = impl_->window_entries_.find(parent_window);
89
if (it != impl_->window_entries_.end())
92
return empty_entries_;
55
95
void AppmenuIndicator::ShowAppmenu(unsigned int xid, int x, int y) const
57
97
on_show_appmenu.emit(xid, x, y);