summaryrefslogtreecommitdiff
path: root/dash/previews/TabIterator.cpp
blob: 795472201f111714f776a708070d8c9aa4c3a111 (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
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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Nick Dedekind <[email protected]>
 *              Manuel de la Pena <[email protected]>
 *
 */

#include "TabIterator.h"

namespace unity
{
namespace dash
{
namespace previews
{

void TabIterator::Remove(nux::InputArea* area)
{
  areas_.remove(area);
}

void TabIterator::Prepend(nux::InputArea* area)
{
  Remove(area);
  areas_.push_front(area);
}

void TabIterator::Append(nux::InputArea* area)
{
  Remove(area);
  areas_.push_back(area);
}

void TabIterator::Insert(nux::InputArea* area, unsigned index)
{
  Remove(area);
  std::list<nux::InputArea*>::iterator it = areas_.begin();
  if (index < areas_.size())
  {
    std::advance(it, index);
    areas_.insert(it, area);
  }
  else
    areas_.push_back(area);
}

void TabIterator::InsertBefore(nux::InputArea* area, nux::InputArea* after)
{
  Remove(area);
  std::list<nux::InputArea*>::iterator it = std::find(areas_.begin(), areas_.end(),
    after);
  areas_.insert(it, area);
}

void TabIterator::InsertAfter(nux::InputArea* area, nux::InputArea* before)
{
  Remove(area);
  std::list<nux::InputArea*>::iterator it = std::find(areas_.begin(), areas_.end(),
    before);

  if (it != areas_.end())
    ++it;

  areas_.insert(it, area);
}

std::list<nux::InputArea*> const& TabIterator::GetTabAreas() const { return areas_; }

nux::InputArea* TabIterator::DefaultFocus() const
{
  if (areas_.empty())
    return nullptr;

  return *areas_.begin();
}

nux::InputArea* TabIterator::FindKeyFocusArea(unsigned int key_symbol,
                                              unsigned long x11_key_code,
                                              unsigned long special_keys_state)
{
  if (areas_.empty())
    return nullptr;

  nux::InputArea* current_focus_area = nux::GetWindowCompositor().GetKeyFocusArea();
  auto it = std::find(areas_.begin(), areas_.end(), current_focus_area);
  if (it != areas_.end())
    return current_focus_area;

  return *areas_.begin();
}

nux::Area* TabIterator::KeyNavIteration(nux::KeyNavDirection direction)
{
  if (areas_.empty())
    return nullptr;

  if (direction != nux::KEY_NAV_TAB_PREVIOUS && direction != nux::KEY_NAV_TAB_NEXT)
  {
    return nullptr;
  }

  nux::InputArea* current_focus_area = nux::GetWindowCompositor().GetKeyFocusArea();

  if (current_focus_area)
  {
    auto it = std::find(areas_.begin(), areas_.end(), current_focus_area);
    if (direction == nux::KEY_NAV_TAB_PREVIOUS)
    {
      if (it == areas_.begin())
        return *areas_.rbegin();
      else
      {
        it--;
        if (it == areas_.begin())
          return *areas_.rbegin();
        return *it;
      }
    }
    else if (direction == nux::KEY_NAV_TAB_NEXT)
    {
      if (it == areas_.end())
      {
        return *areas_.begin();
      }
      else
      {
        it++;
        if (it == areas_.end())
        {
          return *areas_.begin();
        }
        return *it;
      }
    }
  }
  else
  {
    if (direction == nux::KEY_NAV_TAB_PREVIOUS)
    {
      return *areas_.rbegin();
    }
    else if (direction == nux::KEY_NAV_TAB_NEXT)
    {
      return *areas_.begin();
    }
  }

  return nullptr;
}

} // previews

} // dash

} // unity