1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <comphelper/lok.hxx>
12 #include <IconThemeSelector.hxx>
14 #include <tools/color.hxx>
15 #include <vcl/IconThemeInfo.hxx>
16 #include <vcl/settings.hxx>
17 #include <vcl/svapp.hxx>
18 #include <config_mpl.h>
29 const OUString
& m_rThemeId
;
31 explicit SameTheme(const OUString
&rThemeId
) : m_rThemeId(rThemeId
) {}
32 bool operator()(const vcl::IconThemeInfo
&rInfo
)
34 return m_rThemeId
== rInfo
.GetThemeId();
38 bool icon_theme_is_in_installed_themes(const OUString
& theme
,
39 const std::vector
<IconThemeInfo
>& installedThemes
)
41 return std::any_of(installedThemes
.begin(), installedThemes
.end(),
45 } // end anonymous namespace
47 IconThemeSelector::IconThemeSelector()
48 : mUseHighContrastTheme(false)
49 , mPreferDarkIconTheme(false)
54 IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString
& desktopEnvironment
, bool bPreferDarkIconTheme
)
56 if (comphelper::LibreOfficeKit::isActive())
58 if (!bPreferDarkIconTheme
)
61 return "colibre_dark";
65 (void)desktopEnvironment
;
66 if (!bPreferDarkIconTheme
)
69 return "colibre_dark";
72 if ( desktopEnvironment
.equalsIgnoreAsciiCase("plasma5") ||
73 desktopEnvironment
.equalsIgnoreAsciiCase("lxqt") ) {
74 if (!bPreferDarkIconTheme
)
79 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("macosx") ) {
80 if (!bPreferDarkIconTheme
)
85 else if ( desktopEnvironment
.equalsIgnoreAsciiCase("gnome") ||
86 desktopEnvironment
.equalsIgnoreAsciiCase("mate") ||
87 desktopEnvironment
.equalsIgnoreAsciiCase("unity") ) {
88 if (!bPreferDarkIconTheme
)
94 if (!bPreferDarkIconTheme
)
95 r
= FALLBACK_LIGHT_ICON_THEME_ID
;
97 r
= FALLBACK_DARK_ICON_THEME_ID
;
104 IconThemeSelector::SelectIconThemeForDesktopEnvironment(
105 const std::vector
<IconThemeInfo
>& installedThemes
,
106 const OUString
& desktopEnvironment
) const
108 if (!mPreferredIconTheme
.isEmpty()) {
109 if (icon_theme_is_in_installed_themes(mPreferredIconTheme
, installedThemes
)) {
110 return mPreferredIconTheme
;
114 OUString themeForDesktop
= GetIconThemeForDesktopEnvironment(desktopEnvironment
, mPreferDarkIconTheme
);
115 if (icon_theme_is_in_installed_themes(themeForDesktop
, installedThemes
)) {
116 return themeForDesktop
;
119 return ReturnFallback(installedThemes
);
123 IconThemeSelector::SelectIconTheme(
124 const std::vector
<IconThemeInfo
>& installedThemes
,
125 const OUString
& theme
) const
127 if (mUseHighContrastTheme
) {
128 const Color
aCol(Application::GetSettings().GetStyleSettings().GetWindowColor());
129 const OUString
name(aCol
.IsDark() ? OUString(IconThemeInfo::HIGH_CONTRAST_ID_DARK
)
130 : OUString(IconThemeInfo::HIGH_CONTRAST_ID_BRIGHT
));
131 if (icon_theme_is_in_installed_themes(name
, installedThemes
)) {
136 if (icon_theme_is_in_installed_themes(theme
, installedThemes
)) {
140 return ReturnFallback(installedThemes
);
144 IconThemeSelector::SetUseHighContrastTheme(bool v
)
146 mUseHighContrastTheme
= v
;
150 IconThemeSelector::SetPreferredIconTheme(const OUString
& theme
, bool bDarkIconTheme
)
152 // lower case theme name, and (tdf#120175) replace - with _
153 // see icon-themes/README
154 OUString sIconTheme
= theme
.toAsciiLowerCase().replace('-','_');
156 const bool bChanged
= mPreferredIconTheme
!= sIconTheme
|| mPreferDarkIconTheme
!= bDarkIconTheme
;
159 mPreferredIconTheme
= sIconTheme
;
160 mPreferDarkIconTheme
= bDarkIconTheme
;
166 IconThemeSelector::operator==(const vcl::IconThemeSelector
& other
) const
168 if (this == &other
) {
171 if (mPreferredIconTheme
!= other
.mPreferredIconTheme
) {
174 if (mPreferDarkIconTheme
!= other
.mPreferDarkIconTheme
) {
177 if (mUseHighContrastTheme
!= other
.mUseHighContrastTheme
) {
184 IconThemeSelector::operator!=(const vcl::IconThemeSelector
& other
) const
186 return !(*this == other
);
190 IconThemeSelector::ReturnFallback(const std::vector
<IconThemeInfo
>& installedThemes
)
192 if (!installedThemes
.empty()) {
193 return installedThemes
.front().GetThemeId();
196 return FALLBACK_LIGHT_ICON_THEME_ID
;
200 } /* namespace vcl */
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */