~brandontschaefer/unity/bump-to-new-nux-abi

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2014 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
 *              William Hua <william.hua@canonical.com>
 */

#include <gtk/gtk.h>
#include <NuxCore/Logger.h>
#include <UnityCore/GLibSignal.h>
#include <UnityCore/GLibWrapper.h>
#include <UnityCore/DBusIndicators.h>
#include <unordered_map>

#include "MenuManager.h"
#include "WindowManager.h"

namespace unity
{
namespace menu
{
namespace
{
DECLARE_LOGGER(logger, "unity.menu.manager");

const std::string SETTINGS_NAME = "com.canonical.Unity";
const std::string LIM_KEY = "integrated-menus";
const std::string ALWAYS_SHOW_MENUS_KEY = "always-show-menus";
}

using namespace indicator;

struct Manager::Impl : sigc::trackable
{
  Impl(Manager* parent, Indicators::Ptr const& indicators, key::Grabber::Ptr const& grabber)
    : parent_(parent)
    , indicators_(indicators)
    , key_grabber_(grabber)
    , show_now_window_(0)
    , settings_(g_settings_new(SETTINGS_NAME.c_str()))
  {
    for (auto const& indicator : indicators_->GetIndicators())
      AddIndicator(indicator);

    GrabMnemonicsForActiveWindow();

    parent_->show_menus.changed.connect(sigc::mem_fun(this, &Impl::ShowMenus));
    indicators_->on_object_added.connect(sigc::mem_fun(this, &Impl::AddIndicator));
    indicators_->on_object_removed.connect(sigc::mem_fun(this, &Impl::RemoveIndicator));
    indicators_->on_entry_activate_request.connect(sigc::mem_fun(this, &Impl::ActivateRequest));
    indicators_->icon_paths_changed.connect(sigc::mem_fun(this, &Impl::IconPathsChanged));
    WindowManager::Default().window_focus_changed.connect(sigc::hide(sigc::mem_fun(this, &Impl::GrabMnemonicsForActiveWindow)));

    signals_.Add<void, GSettings*, const gchar*>(settings_, "changed::" + LIM_KEY, [this] (GSettings*, const gchar*) {
      parent_->integrated_menus = g_settings_get_boolean(settings_, LIM_KEY.c_str());
    });
    signals_.Add<void, GSettings*, const gchar*>(settings_, "changed::" + ALWAYS_SHOW_MENUS_KEY, [this] (GSettings*, const gchar*) {
      parent_->always_show_menus = g_settings_get_boolean(settings_, ALWAYS_SHOW_MENUS_KEY.c_str());
    });

    parent_->integrated_menus = g_settings_get_boolean(settings_, LIM_KEY.c_str());
    parent_->always_show_menus = g_settings_get_boolean(settings_, ALWAYS_SHOW_MENUS_KEY.c_str());
  }

  ~Impl()
  {
    if (appmenu_)
      RemoveIndicator(appmenu_);
  }

  void AddIndicator(Indicator::Ptr const& indicator)
  {
    if (!indicator->IsAppmenu())
      return;

    appmenu_connections_.Clear();
    appmenu_ = std::static_pointer_cast<AppmenuIndicator>(indicator);

    for (auto const& entry : appmenu_->GetEntries())
      GrabEntryMnemonics(entry);

    appmenu_connections_.Add(appmenu_->on_entry_added.connect(sigc::mem_fun(this, &Impl::GrabEntryMnemonics)));
    appmenu_connections_.Add(appmenu_->on_entry_removed.connect(sigc::mem_fun(this, &Impl::UngrabEntryMnemonics)));

    parent_->appmenu_added();
  }

  void RemoveIndicator(Indicator::Ptr const& indicator)
  {
    if (indicator != appmenu_)
      return;

    appmenu_connections_.Clear();

    for (auto const& entry : appmenu_->GetEntries())
      UngrabEntryMnemonics(entry);

    appmenu_.reset();
    parent_->appmenu_removed();
  }

  void GrabMnemonicsForActiveWindow()
  {
    if (!appmenu_)
      return;

    UngrabMnemonics();
    auto active_window = WindowManager::Default().GetActiveWindow();

    for (auto const& entry : appmenu_->GetEntriesForWindow(active_window))
      GrabEntryMnemonics(entry);
  }

  void GrabEntryMnemonics(indicator::Entry::Ptr const& entry)
  {
    if (entry->parent_window() != WindowManager::Default().GetActiveWindow())
      return;

    gunichar mnemonic;

    if (pango_parse_markup(entry->label().c_str(), -1, '_', nullptr, nullptr, &mnemonic, nullptr) && mnemonic)
    {
      auto key = gdk_keyval_to_lower(gdk_unicode_to_keyval(mnemonic));
      glib::String accelerator(gtk_accelerator_name(key, GDK_MOD1_MASK));
      auto action = std::make_shared<CompAction>();
      auto const& id = entry->id();

      action->keyFromString(accelerator);
      action->setState(CompAction::StateInitKey);
      action->setInitiate([this, id] (CompAction* action, CompAction::State, CompOption::Vector&) {
        LOG_DEBUG(logger) << "pressed \"" << action->keyToString() << "\"";
        return parent_->key_activate_entry.emit(id);
      });

      entry_actions_.insert({entry, action});
      key_grabber_->AddAction(*action);
    }
  }

  void UngrabEntryMnemonics(indicator::Entry::Ptr const& entry)
  {
    auto it = entry_actions_.find(entry);

    if (it != entry_actions_.end())
    {
      key_grabber_->RemoveAction(*it->second);
      entry_actions_.erase(it);
    }
  }

  void UngrabMnemonics()
  {
    for (auto it = entry_actions_.begin(); it != entry_actions_.end();)
    {
      key_grabber_->RemoveAction(*it->second);
      it = entry_actions_.erase(it);
    }
  }

  void ActivateRequest(std::string const& entry_id)
  {
    parent_->key_activate_entry.emit(entry_id);
  }

  void SetShowNowForWindow(Window xid, bool show)
  {
    if (!appmenu_)
      return;

    show_now_window_ = show ? xid : 0;

    for (auto const& entry : appmenu_->GetEntriesForWindow(xid))
      entry->set_show_now(show);
  }

  void ShowMenus(bool show)
  {
    if (!appmenu_)
      return;

    auto& wm = WindowManager::Default();

    if (show)
    {
      active_win_conn_ = wm.window_focus_changed.connect([this] (Window new_active) {
        SetShowNowForWindow(show_now_window_, false);
        SetShowNowForWindow(new_active, true);
      });
    }
    else
    {
      active_win_conn_->disconnect();
    }

    SetShowNowForWindow(wm.GetActiveWindow(), show);
  }

  void IconPathsChanged()
  {
    auto const& icon_paths = indicators_->IconPaths();
    std::vector<const gchar*> gicon_paths(icon_paths.size());

    for (auto const& path : icon_paths)
      gicon_paths.push_back(path.c_str());

    gtk_icon_theme_set_search_path(gtk_icon_theme_get_default(), gicon_paths.data(), gicon_paths.size());
  }

  Manager* parent_;
  Indicators::Ptr indicators_;
  AppmenuIndicator::Ptr appmenu_;
  key::Grabber::Ptr key_grabber_;
  Window show_now_window_;
  connection::Manager appmenu_connections_;
  connection::Wrapper active_win_conn_;
  glib::Object<GSettings> settings_;
  glib::SignalManager signals_;
  std::unordered_map<indicator::Entry::Ptr, std::shared_ptr<CompAction>> entry_actions_;
};

Manager::Manager(Indicators::Ptr const& indicators, key::Grabber::Ptr const& grabber)
  : integrated_menus(false)
  , show_menus_wait(180)
  , always_show_menus(false)
  , fadein(100)
  , fadeout(120)
  , discovery(2)
  , discovery_fadein(200)
  , discovery_fadeout(300)
  , impl_(new Impl(this, indicators, grabber))
{}

Manager::~Manager()
{}

bool Manager::HasAppMenu() const
{
  return impl_->appmenu_ && !impl_->appmenu_->GetEntries().empty();
}

Indicators::Ptr const& Manager::Indicators() const
{
  return impl_->indicators_;
}

AppmenuIndicator::Ptr const& Manager::AppMenu() const
{
  return impl_->appmenu_;
}

key::Grabber::Ptr const& Manager::KeyGrabber() const
{
  return impl_->key_grabber_;
}

} // menu namespace
} // unity namespace