Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/sidebar/Deck.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <algorithm>
21
22
#include <sfx2/sidebar/Deck.hxx>
23
#include <sidebar/DeckDescriptor.hxx>
24
#include <sidebar/DeckLayouter.hxx>
25
#include <sidebar/DeckTitleBar.hxx>
26
#include <sidebar/PanelTitleBar.hxx>
27
#include <sfx2/sidebar/Panel.hxx>
28
#include <sfx2/sidebar/SidebarDockingWindow.hxx>
29
#include <sfx2/sidebar/Theme.hxx>
30
#include <sfx2/viewsh.hxx>
31
32
#include <vcl/event.hxx>
33
#include <vcl/weld/ScrolledWindow.hxx>
34
#include <comphelper/lok.hxx>
35
#include <tools/json_writer.hxx>
36
37
using namespace css;
38
39
namespace sfx2::sidebar {
40
41
Deck::Deck(const DeckDescriptor& rDeckDescriptor, SidebarDockingWindow* pParentWindow,
42
           const std::function<void()>& rCloserAction)
43
0
    : InterimItemWindow(pParentWindow, u"sfx/ui/deck.ui"_ustr, u"Deck"_ustr)
44
0
    , msId(rDeckDescriptor.msId)
45
0
    , mnMinimalWidth(0)
46
0
    , mnScrolledWindowExtraWidth(0)
47
0
    , mnMinimalHeight(0)
48
0
    , maPanels()
49
0
    , mxParentWindow(pParentWindow)
50
0
    , mxTitleBar(new DeckTitleBar(rDeckDescriptor.msTitle, *m_xBuilder,
51
0
                 rDeckDescriptor.msHelpId, rCloserAction))
52
0
    , mxVerticalScrollBar(m_xBuilder->weld_scrolled_window(u"scrolledwindow"_ustr))
53
0
    , mxContents(m_xBuilder->weld_box(u"contents"_ustr))
54
0
{
55
0
    SetStyle(GetStyle() | WB_DIALOGCONTROL);
56
57
0
    m_xContainer->set_background(Theme::GetColor(Theme::Color_DeckBackground));
58
59
0
    mxVerticalScrollBar->vadjustment_set_step_increment(10);
60
0
    mxVerticalScrollBar->vadjustment_set_page_increment(100);
61
62
    // tdf#142458 Measure the preferred width of an empty ScrolledWindow
63
    // to add to the width of the union of panel widths when calculating
64
    // the minimal width of the deck
65
0
    mxVerticalScrollBar->set_hpolicy(VclPolicyType::NEVER);
66
0
    mxVerticalScrollBar->set_vpolicy(VclPolicyType::NEVER);
67
0
    mnScrolledWindowExtraWidth = mxVerticalScrollBar->get_preferred_size().Width();
68
0
    mxVerticalScrollBar->set_hpolicy(VclPolicyType::AUTOMATIC);
69
0
    mxVerticalScrollBar->set_vpolicy(VclPolicyType::AUTOMATIC);
70
0
}
Unexecuted instantiation: sfx2::sidebar::Deck::Deck(sfx2::sidebar::DeckDescriptor const&, sfx2::sidebar::SidebarDockingWindow*, std::__1::function<void ()> const&)
Unexecuted instantiation: sfx2::sidebar::Deck::Deck(sfx2::sidebar::DeckDescriptor const&, sfx2::sidebar::SidebarDockingWindow*, std::__1::function<void ()> const&)
71
72
Deck::~Deck()
73
0
{
74
0
    disposeOnce();
75
0
}
76
77
void Deck::dispose()
78
0
{
79
0
    SharedPanelContainer aPanels;
80
0
    aPanels.swap(maPanels);
81
82
    // We have to explicitly trigger the destruction of panels.
83
    // Otherwise that is done by one of our base class destructors
84
    // without updating maPanels.
85
0
    for (auto& rpPanel : aPanels)
86
0
        rpPanel.reset();
87
88
0
    maPanels.clear();
89
0
    mxTitleBar.reset();
90
0
    mxContents.reset();
91
0
    mxVerticalScrollBar.reset();
92
93
0
    mxParentWindow.reset();
94
95
0
    InterimItemWindow::dispose();
96
0
}
97
98
DeckTitleBar* Deck::GetTitleBar() const
99
0
{
100
0
    return mxTitleBar.get();
101
0
}
102
103
tools::Rectangle Deck::GetContentArea() const
104
0
{
105
0
    const Size aWindowSize (GetSizePixel());
106
0
    const int nBorderSize (Theme::GetInteger(Theme::Int_DeckBorderSize));
107
0
    if (aWindowSize.IsEmpty())
108
0
        return tools::Rectangle();
109
110
0
    return tools::Rectangle(
111
0
        Theme::GetInteger(Theme::Int_DeckLeftPadding) + nBorderSize,
112
0
        Theme::GetInteger(Theme::Int_DeckTopPadding) + nBorderSize,
113
0
        aWindowSize.Width() - 1 - Theme::GetInteger(Theme::Int_DeckRightPadding) - nBorderSize,
114
0
        aWindowSize.Height() - 1 - Theme::GetInteger(Theme::Int_DeckBottomPadding) - nBorderSize);
115
0
}
116
117
void Deck::DataChanged(const DataChangedEvent&)
118
0
{
119
0
    for (auto& rpPanel : maPanels)
120
0
        rpPanel->DataChanged();
121
122
0
    RequestLayoutInternal();
123
0
}
124
125
/**
126
 * This container may contain existing panels that are
127
 * being re-used, and new ones too.
128
 */
129
void Deck::ResetPanels(SharedPanelContainer&& rPanelContainer)
130
0
{
131
0
    SharedPanelContainer aHiddens;
132
133
    // First hide old panels we don't need just now.
134
0
    for (auto& rpPanel : maPanels)
135
0
    {
136
0
        const bool bFound = std::ranges::any_of(rPanelContainer,
137
0
            [&rpPanel](const auto& rOther) { return rpPanel.get() == rOther.get(); });
138
0
        if (!bFound) // this one didn't survive.
139
0
        {
140
0
            rpPanel->SetLurkMode(true);
141
0
            aHiddens.push_back(rpPanel);
142
0
        }
143
0
    }
144
145
0
    maPanels = std::move(rPanelContainer);
146
147
    // Hidden ones always at the end
148
0
    maPanels.insert(std::end(maPanels), std::begin(aHiddens), std::end(aHiddens));
149
150
0
    RequestLayoutInternal();
151
0
}
152
153
void Deck::RequestLayoutInternal()
154
0
{
155
0
    mnMinimalWidth = 0;
156
0
    mnMinimalHeight = 0;
157
158
0
    DeckLayouter::LayoutDeck(mxParentWindow.get(), GetContentArea(),
159
0
                             mnMinimalWidth, mnMinimalHeight, maPanels,
160
0
                             *GetTitleBar(), *mxVerticalScrollBar);
161
162
0
    if (mnMinimalWidth)
163
0
    {
164
        // tdf#142458 at this point mnMinimalWidth contains the width required
165
        // by the panels, but extra space may be needed by the scrolledwindow
166
        // that will contain the panels
167
0
        mnMinimalWidth += mnScrolledWindowExtraWidth;
168
0
    }
169
0
}
170
171
void Deck::RequestLayout()
172
0
{
173
0
    RequestLayoutInternal();
174
175
0
    if (!comphelper::LibreOfficeKit::isActive())
176
0
        return;
177
178
0
    bool bChangeNeeded = false;
179
0
    Size aParentSize = mxParentWindow->GetSizePixel();
180
181
0
    if (mnMinimalHeight > 0 && (mnMinimalHeight != aParentSize.Height() || GetSizePixel().Height() != mnMinimalHeight))
182
0
    {
183
0
        aParentSize.setHeight(mnMinimalHeight);
184
0
        bChangeNeeded = true;
185
0
    }
186
187
0
    if (bChangeNeeded)
188
0
    {
189
0
        mxParentWindow->SetSizePixel(aParentSize);
190
0
        setPosSizePixel(0, 0, aParentSize.Width(), aParentSize.Height());
191
0
    }
192
0
    else if (aParentSize != GetSizePixel()) //Sync parent & child sizes
193
0
        setPosSizePixel(0, 0, aParentSize.Width(), aParentSize.Height());
194
0
}
195
196
weld::Widget* Deck::GetPanelParentWindow()
197
0
{
198
0
    return mxContents.get();
199
0
}
200
201
std::shared_ptr<Panel> Deck::GetPanel(std::u16string_view panelId)
202
0
{
203
0
    for (const auto& pPanel : maPanels)
204
0
    {
205
0
        if(pPanel->GetId() == panelId)
206
0
        {
207
0
            return pPanel;
208
0
        }
209
0
    }
210
0
    return nullptr;
211
212
0
}
213
214
void Deck::ShowPanel(const Panel& rPanel)
215
0
{
216
0
    if (!mxVerticalScrollBar || mxVerticalScrollBar->get_vpolicy() == VclPolicyType::NEVER)
217
0
        return;
218
219
    // Get vertical extent of the panel.
220
0
    tools::Rectangle aExtents;
221
0
    if (!rPanel.get_extents(aExtents))
222
0
        return;
223
224
0
    auto nPanelTop = aExtents.Top();
225
0
    auto nPanelBottom = aExtents.Bottom() - 1;
226
227
    // Determine what the new thumb position should be like.
228
    // When the whole panel does not fit then make its top visible
229
    // and it off at the bottom.
230
0
    sal_Int32 nNewThumbPos(mxVerticalScrollBar->vadjustment_get_value());
231
0
    if (nPanelBottom >= nNewThumbPos + mxVerticalScrollBar->vadjustment_get_page_size())
232
0
        nNewThumbPos = nPanelBottom - mxVerticalScrollBar->vadjustment_get_page_size();
233
0
    if (nPanelTop < nNewThumbPos)
234
0
        nNewThumbPos = nPanelTop;
235
236
0
    mxVerticalScrollBar->vadjustment_set_value(nNewThumbPos);
237
0
}
238
239
} // end of namespace sfx2::sidebar
240
241
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */