LibreOffice Module vcl (master) 1
IconThemeSelector.cxx
Go to the documentation of this file.
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
10#include <comphelper/lok.hxx>
11
12#include <IconThemeSelector.hxx>
13
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>
19
20#include <algorithm>
21
22namespace vcl {
23
24namespace {
25
26 class SameTheme
27 {
28 private:
29 const OUString& m_rThemeId;
30 public:
31 explicit SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
32 bool operator()(const vcl::IconThemeInfo &rInfo)
33 {
34 return m_rThemeId == rInfo.GetThemeId();
35 }
36 };
37
38bool icon_theme_is_in_installed_themes(const OUString& theme,
39 const std::vector<IconThemeInfo>& installedThemes)
40{
41 return std::any_of(installedThemes.begin(), installedThemes.end(),
42 SameTheme(theme));
43}
44
45} // end anonymous namespace
46
48 : mUseHighContrastTheme(false)
49 , mPreferDarkIconTheme(false)
50{
51}
52
53/*static*/ OUString
54IconThemeSelector::GetIconThemeForDesktopEnvironment(const OUString& desktopEnvironment, bool bPreferDarkIconTheme)
55{
57 {
58 if (!bPreferDarkIconTheme)
59 return "colibre";
60 else
61 return "colibre_dark";
62 }
63
64#ifdef _WIN32
65 (void)desktopEnvironment;
66 if (!bPreferDarkIconTheme)
67 return "colibre";
68 else
69 return "colibre_dark";
70#else
71 OUString r;
72 if ( desktopEnvironment.equalsIgnoreAsciiCase("plasma5") ||
73 desktopEnvironment.equalsIgnoreAsciiCase("lxqt") ) {
74 if (!bPreferDarkIconTheme)
75 r = "breeze";
76 else
77 r = "breeze_dark";
78 }
79 else if ( desktopEnvironment.equalsIgnoreAsciiCase("macosx") ) {
80 if (!bPreferDarkIconTheme)
81 r = "sukapura";
82 else
83 r = "sukapura_dark";
84 }
85 else if ( desktopEnvironment.equalsIgnoreAsciiCase("gnome") ||
86 desktopEnvironment.equalsIgnoreAsciiCase("mate") ||
87 desktopEnvironment.equalsIgnoreAsciiCase("unity") ) {
88 if (!bPreferDarkIconTheme)
89 r = "elementary";
90 else
91 r = "sifr_dark";
92 } else
93 {
94 if (!bPreferDarkIconTheme)
96 else
98 }
99 return r;
100#endif // _WIN32
101}
102
103OUString
105 const std::vector<IconThemeInfo>& installedThemes,
106 const OUString& desktopEnvironment) const
107{
108 if (!mPreferredIconTheme.isEmpty()) {
109 if (icon_theme_is_in_installed_themes(mPreferredIconTheme, installedThemes)) {
110 return mPreferredIconTheme;
111 }
112 }
113
114 OUString themeForDesktop = GetIconThemeForDesktopEnvironment(desktopEnvironment, mPreferDarkIconTheme);
115 if (icon_theme_is_in_installed_themes(themeForDesktop, installedThemes)) {
116 return themeForDesktop;
117 }
118
119 return ReturnFallback(installedThemes);
120}
121
122OUString
124 const std::vector<IconThemeInfo>& installedThemes,
125 const OUString& theme) const
126{
128 const Color aCol(Application::GetSettings().GetStyleSettings().GetWindowColor());
129 const OUString name(aCol.IsDark() ? OUString(IconThemeInfo::HIGH_CONTRAST_ID_DARK)
131 if (icon_theme_is_in_installed_themes(name, installedThemes)) {
132 return name;
133 }
134 }
135
136 if (icon_theme_is_in_installed_themes(theme, installedThemes)) {
137 return theme;
138 }
139
140 return ReturnFallback(installedThemes);
141}
142
143void
145{
147}
148
149bool
150IconThemeSelector::SetPreferredIconTheme(const OUString& theme, bool bDarkIconTheme)
151{
152 // lower case theme name, and (tdf#120175) replace - with _
153 // see icon-themes/README
154 OUString sIconTheme = theme.toAsciiLowerCase().replace('-','_');
155
156 const bool bChanged = mPreferredIconTheme != sIconTheme || mPreferDarkIconTheme != bDarkIconTheme;
157 if (bChanged)
158 {
159 mPreferredIconTheme = sIconTheme;
160 mPreferDarkIconTheme = bDarkIconTheme;
161 }
162 return bChanged;
163}
164
165bool
167{
168 if (this == &other) {
169 return true;
170 }
172 return false;
173 }
175 return false;
176 }
178 return false;
179 }
180 return true;
181}
182
183bool
185{
186 return !(*this == other);
187}
188
189/*static*/ OUString
190IconThemeSelector::ReturnFallback(const std::vector<IconThemeInfo>& installedThemes)
191{
192 if (!installedThemes.empty()) {
193 return installedThemes.front().GetThemeId();
194 }
195 else {
197 }
198}
199
200} /* namespace vcl */
201
202/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & m_rThemeId
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:655
bool IsDark() const
This class provides information about an icon theme.
const OUString & GetThemeId() const
static constexpr OUStringLiteral HIGH_CONTRAST_ID_BRIGHT
The name of the icon theme to use for high contrast mode.
static constexpr OUStringLiteral HIGH_CONTRAST_ID_DARK
This class helps to choose an icon theme from a list of installed themes.
static constexpr OUStringLiteral FALLBACK_LIGHT_ICON_THEME_ID
The name of the icon themes which are used as fallbacks.
bool SetPreferredIconTheme(const OUString &, bool bDarkIconTheme)
Returns true if the PreferredIconTheme was changed.
bool operator==(const vcl::IconThemeSelector &) const
bool operator!=(const vcl::IconThemeSelector &) const
static OUString GetIconThemeForDesktopEnvironment(const OUString &desktopEnvironment, bool bPreferDarkIconTheme)
static constexpr OUStringLiteral FALLBACK_DARK_ICON_THEME_ID
static OUString ReturnFallback(const std::vector< IconThemeInfo > &installedThemes)
Return the first element of the themes, or the fallback if the vector is empty.
OUString SelectIconTheme(const std::vector< IconThemeInfo > &installedThemes, const OUString &theme) const
Select an icon theme from the list of installed themes.
OUString SelectIconThemeForDesktopEnvironment(const std::vector< IconThemeInfo > &installedThemes, const OUString &desktopEnvironment) const
Select the standard icon theme for a desktop environment from a list of installed themes.
float v
const char * name