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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <o3tl/safeint.hxx>
#include <sfx2/sidebar/FocusManager.hxx>
#include <sfx2/sidebar/Deck.hxx>
#include <sfx2/sidebar/Panel.hxx>
#include <sidebar/DeckTitleBar.hxx>
#include <sidebar/PanelTitleBar.hxx>
#include <sidebar/TitleBar.hxx>
#include <utility>
#include <vcl/event.hxx>
#include <vcl/weld.hxx>
namespace sfx2::sidebar {
FocusManager::FocusLocation::FocusLocation (const PanelComponent eComponent, const sal_Int32 nIndex)
: meComponent(eComponent),
mnIndex(nIndex)
{
}
FocusManager::FocusManager(std::function<void(const Panel&)> aShowPanelFunctor)
: mpDeckTitleBar(nullptr),
maShowPanelFunctor(std::move(aShowPanelFunctor))
{
}
FocusManager::~FocusManager()
{
Clear();
}
void FocusManager::GrabFocus()
{
FocusDeckTitle();
}
void FocusManager::GrabFocusPanel()
{
FocusPanel(0, false);
}
void FocusManager::Clear()
{
SetDeck(nullptr);
ClearPanels();
ClearButtons();
}
void FocusManager::ClearPanels()
{
SharedPanelContainer aPanels;
aPanels.swap(maPanels);
for (auto const& panel : aPanels)
{
if (panel->GetTitleBar())
{
UnregisterWindow(panel->GetTitleBar()->GetToolBox());
UnregisterWindow(panel->GetTitleBar()->GetExpander());
}
weld::Container* pContents = panel->GetContents();
UnregisterWindow(*pContents);
}
}
void FocusManager::ClearButtons()
{
std::vector<weld::Widget*> aButtons;
aButtons.swap(maButtons);
for (auto const& button : aButtons)
{
UnregisterWindow(*button);
}
}
void FocusManager::SetDeck(Deck* pDeck)
{
DeckTitleBar* pDeckTitleBar = pDeck ? pDeck->GetTitleBar() : nullptr;
if (mpDeckTitleBar != nullptr)
UnregisterWindow(mpDeckTitleBar->GetToolBox());
mxDeck = pDeck;
mpDeckTitleBar = pDeckTitleBar;
if (mpDeckTitleBar != nullptr)
RegisterWindow(mpDeckTitleBar->GetToolBox());
}
void FocusManager::SetPanels (const SharedPanelContainer& rPanels)
{
ClearPanels();
for (auto const& panel : rPanels)
{
if (panel->GetTitleBar())
{
RegisterWindow(panel->GetTitleBar()->GetToolBox());
RegisterWindow(panel->GetTitleBar()->GetExpander());
}
// Register also as key event listener at the panel.
weld::Container* pContents = panel->GetContents();
RegisterWindow(*pContents);
maPanels.emplace_back(panel);
}
}
void FocusManager::SetButtons(const std::vector<weld::Widget*>& rButtons)
{
ClearButtons();
for (auto const& button : rButtons)
{
RegisterWindow(*button);
maButtons.emplace_back(button);
}
}
void FocusManager::RegisterWindow(weld::Widget& rWidget)
{
UnregisterWindow(rWidget); // explicitly unset key press handler so we can reconnect without warnings
rWidget.connect_key_press(LINK(this, FocusManager, KeyInputHdl));
}
void FocusManager::UnregisterWindow(weld::Widget& rWidget)
{
rWidget.connect_key_press(Link<const KeyEvent&, bool>());
}
FocusManager::FocusLocation FocusManager::GetFocusLocation() const
{
// Check the deck title.
if (mpDeckTitleBar && mpDeckTitleBar->GetToolBox().has_focus())
return FocusLocation(PC_DeckToolBox, -1);
// Search the panels.
for (size_t nIndex = 0; nIndex < maPanels.size(); ++nIndex)
{
PanelTitleBar* pTitleBar = maPanels[nIndex]->GetTitleBar();
if (!pTitleBar)
continue;
if (pTitleBar->GetExpander().has_focus())
return FocusLocation(PC_PanelTitle, nIndex);
if (pTitleBar->GetToolBox().has_focus())
return FocusLocation(PC_PanelToolBox, nIndex);
weld::Container* pContents = maPanels[nIndex]->GetContents();
if (pContents->has_child_focus())
return FocusLocation(PC_PanelContent, nIndex);
}
// Search the buttons.
for (size_t nIndex=0; nIndex < maButtons.size(); ++nIndex)
{
if (maButtons[nIndex]->has_focus())
return FocusLocation(PC_TabBar, nIndex);
}
return FocusLocation(PC_None, -1);
}
void FocusManager::FocusDeckTitle()
{
if (mpDeckTitleBar != nullptr)
{
if (mpDeckTitleBar->GetToolBox().get_n_items() > 0)
{
weld::Toolbar& rToolBox = mpDeckTitleBar->GetToolBox();
rToolBox.grab_focus();
}
else
FocusPanel(0, false);
}
else
FocusPanel(0, false);
}
bool FocusManager::IsDeckTitleVisible() const
{
return mpDeckTitleBar != nullptr && mpDeckTitleBar->GetVisible();
}
void FocusManager::FocusPanel (
const sal_Int32 nPanelIndex,
const bool bFallbackToDeckTitle)
{
if (nPanelIndex<0 || o3tl::make_unsigned(nPanelIndex)>=maPanels.size())
{
if (bFallbackToDeckTitle)
FocusDeckTitle();
return;
}
Panel& rPanel (*maPanels[nPanelIndex]);
PanelTitleBar* pTitleBar = rPanel.GetTitleBar();
if (pTitleBar && pTitleBar->GetVisible())
{
rPanel.SetExpanded(true);
pTitleBar->GetExpander().grab_focus();
}
// Fallback to deck title should only be applicable when there is more than one panel,
// or else it will never be possible to enter the panel contents when there's a single panel
// without a titlebar and expander
else if (bFallbackToDeckTitle && maPanels.size() > 1)
{
// The panel title is not visible, fall back to the deck
// title.
// Make sure that the desk title is visible here to prevent a
// loop when both the title of panel 0 and the deck title are
// not present.
if (IsDeckTitleVisible())
FocusDeckTitle();
else
FocusPanelContent(nPanelIndex);
}
else
FocusPanelContent(nPanelIndex);
if (maShowPanelFunctor)
maShowPanelFunctor(rPanel);
}
void FocusManager::FocusPanelContent(const sal_Int32 nPanelIndex)
{
if (!maPanels[nPanelIndex]->IsExpanded())
maPanels[nPanelIndex]->SetExpanded(true);
weld::Container* pContents = maPanels[nPanelIndex]->GetContents();
pContents->child_grab_focus();
}
void FocusManager::FocusButton (const sal_Int32 nButtonIndex)
{
maButtons[nButtonIndex]->grab_focus();
}
void FocusManager::MoveFocusInsidePanel (
const FocusLocation& rFocusLocation,
const sal_Int32 nDirection)
{
const bool bHasToolBoxItem (
maPanels[rFocusLocation.mnIndex]->GetTitleBar()->GetToolBox().get_n_items() > 0);
switch (rFocusLocation.meComponent)
{
case PC_PanelTitle:
if (nDirection > 0 && bHasToolBoxItem)
maPanels[rFocusLocation.mnIndex]->GetTitleBar()->GetToolBox().grab_focus();
else
FocusPanelContent(rFocusLocation.mnIndex);
break;
case PC_PanelToolBox:
if (nDirection < 0 && bHasToolBoxItem)
maPanels[rFocusLocation.mnIndex]->GetTitleBar()->GetExpander().grab_focus();
else
FocusPanelContent(rFocusLocation.mnIndex);
break;
default: break;
}
}
bool FocusManager::HandleKeyEvent(
const vcl::KeyCode& rKeyCode,
const FocusLocation& aLocation)
{
bool bConsumed = false;
switch (rKeyCode.GetCode())
{
case KEY_ESCAPE:
switch (aLocation.meComponent)
{
case PC_TabBar:
case PC_DeckToolBox:
case PC_PanelTitle:
case PC_PanelToolBox:
{
if (mxDeck)
{
mxDeck->GrabFocusToDocument();
bConsumed = true;
}
break;
}
case PC_PanelContent:
// Return focus to tab bar sidebar settings button or panel title.
if ((!IsDeckTitleVisible() && maPanels.size() == 1) ||
(!maPanels[aLocation.mnIndex]->GetTitleBar()->GetVisible()))
FocusButton(0);
else
FocusPanel(aLocation.mnIndex, true);
bConsumed = true;
break;
default:
break;
}
return bConsumed;
case KEY_RETURN:
switch (aLocation.meComponent)
{
case PC_PanelTitle:
// Enter the panel.
FocusPanelContent(aLocation.mnIndex);
bConsumed = true;
break;
default:
break;
}
return bConsumed;
case KEY_TAB:
{
const sal_Int32 nDirection (
rKeyCode.IsShift()
? -1
: +1);
switch (aLocation.meComponent)
{
case PC_PanelTitle:
case PC_PanelToolBox:
if (rKeyCode.IsShift())
break;
MoveFocusInsidePanel(aLocation, nDirection);
bConsumed = true;
break;
case PC_DeckToolBox:
{
// Moves to the first deck activation button that is visible and sensitive
sal_Int32 nIndex(0);
sal_Int32 nButtons(maButtons.size());
if (nButtons > 1)
{
nIndex = 1;
// Finds the next visible button that is sensitive
while((!maButtons[nIndex]->get_visible() ||
!maButtons[nIndex]->get_sensitive()) && ++nIndex < nButtons);
// Wrap to the menu button when going past the last button
if (nIndex >= nButtons)
nIndex = 0;
}
FocusButton(nIndex);
bConsumed = true;
}
break;
case PC_TabBar:
if (rKeyCode.IsShift())
{
if (IsDeckTitleVisible())
FocusDeckTitle();
else
FocusPanel(0, true);
}
else
FocusPanel(0, true);
bConsumed = true;
break;
default:
break;
}
break;
}
case KEY_LEFT:
case KEY_UP:
switch (aLocation.meComponent)
{
case PC_PanelTitle:
case PC_PanelToolBox:
// Go to previous panel or the deck title.
if (aLocation.mnIndex > 0)
FocusPanel(aLocation.mnIndex-1, true);
else if (IsDeckTitleVisible())
FocusDeckTitle();
else
{
// Set focus to the last visible sensitive button.
sal_Int32 nIndex(maButtons.size()-1);
while((!maButtons[nIndex]->get_visible() ||
!maButtons[nIndex]->get_sensitive()) && --nIndex > 0);
FocusButton(nIndex);
}
bConsumed = true;
break;
case PC_TabBar:
{
if (rKeyCode.GetCode() == KEY_LEFT)
break;
sal_Int32 nIndex;
if (aLocation.mnIndex <= 0)
nIndex = maButtons.size() - 1;
else
nIndex = aLocation.mnIndex - 1;
// Finds the previous visible sensitive button
while((!maButtons[nIndex]->get_visible() ||
!maButtons[nIndex]->get_sensitive()) && --nIndex > 0);
FocusButton(nIndex);
bConsumed = true;
}
break;
default:
break;
}
break;
case KEY_RIGHT:
case KEY_DOWN:
switch(aLocation.meComponent)
{
case PC_PanelTitle:
case PC_PanelToolBox:
// Go to next panel.
if (aLocation.mnIndex < static_cast<sal_Int32>(maPanels.size())-1)
FocusPanel(aLocation.mnIndex+1, false);
else
FocusButton(0);
bConsumed = true;
break;
case PC_TabBar:
{
if (rKeyCode.GetCode() == KEY_RIGHT)
break;
sal_Int32 nButtons(maButtons.size());
sal_Int32 nIndex = aLocation.mnIndex + 1;
if (nIndex >= nButtons)
nIndex = 0;
// Finds the next visible sensitive button
while((!maButtons[nIndex]->get_visible() ||
!maButtons[nIndex]->get_sensitive()) && ++nIndex < nButtons);
// Wrap to the menu button when going past the last button
if (nIndex >= nButtons)
nIndex = 0;
FocusButton(nIndex);
bConsumed = true;
}
break;
default:
break;
}
break;
}
return bConsumed;
}
IMPL_LINK(FocusManager, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
{
return HandleKeyEvent(rKeyEvent.GetKeyCode(), GetFocusLocation());
}
} // end of namespace sfx2::sidebar
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|