Skip to content

Commit d0f5bce

Browse files
committed
feat(desktop-widgets): add text opacity slider to label widget
1 parent e922b5d commit d0f5bce

5 files changed

Lines changed: 41 additions & 12 deletions

File tree

src/shell/desktop/desktop_widget_factory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ std::unique_ptr<DesktopWidget> DesktopWidgetFactory::create(
286286
auto widget = std::make_unique<DesktopLabelWidget>(
287287
getStringSetting(settings, "title", "Title"), getStringSetting(settings, "description"),
288288
getColorSpecSetting(settings, "color", colorSpecFromRole(ColorRole::OnSurface)),
289-
getBoolSetting(settings, "shadow", true)
289+
std::clamp(getFloatSetting(settings, "opacity", 1.0f), 0.0f, 1.0f), getBoolSetting(settings, "shadow", true)
290290
);
291291
applyCommonSettings(*widget, settings);
292292
widget->setContentScale(contentScale);

src/shell/desktop/desktop_widget_settings_registry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ namespace desktop_settings {
315315
add(stringSpec("title", "Title"));
316316
add(stringSpec("description"));
317317
add(colorSpec("color", "on_surface"));
318+
add(doubleSpec("opacity", 1.0, 0.0, 1.0, 0.01));
318319
add(fontFamilySpec());
319320
add(boolSpec("shadow", true));
320321
} else if (type == "button") {

src/shell/desktop/desktop_widgets_controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace {
3737
void normalizeDesktopWidgetSettings(DesktopWidgetState& widget) {
3838
clampOpacitySetting(widget, "background_opacity", 0.8);
3939

40-
if (widget.type == "sticker") {
40+
if (widget.type == "sticker" || widget.type == "label") {
4141
const auto opacityIt = widget.settings.find("opacity");
4242
if (opacityIt == widget.settings.end()) {
4343
widget.settings.insert_or_assign("opacity", 1.0);

src/shell/desktop/widgets/desktop_label_widget.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ namespace {
2020

2121
} // namespace
2222

23-
DesktopLabelWidget::DesktopLabelWidget(std::string title, std::string description, ColorSpec color, bool shadow)
24-
: m_title(std::move(title)), m_description(std::move(description)), m_color(color), m_shadow(shadow) {}
23+
DesktopLabelWidget::DesktopLabelWidget(
24+
std::string title, std::string description, ColorSpec color, float opacity, bool shadow
25+
)
26+
: m_title(std::move(title)), m_description(std::move(description)), m_color(color),
27+
m_opacity(std::clamp(opacity, 0.0f, 1.0f)), m_shadow(shadow) {}
2528

2629
void DesktopLabelWidget::create() {
2730
auto rootNode = std::make_unique<Node>();
@@ -49,6 +52,7 @@ void DesktopLabelWidget::create() {
4952
rootNode->addChild(std::move(descriptionLabel));
5053

5154
setRoot(std::move(rootNode));
55+
applyLabelColors();
5256
applyShadow();
5357
}
5458

@@ -81,17 +85,27 @@ bool DesktopLabelWidget::applySetting(
8185
if (key == "color") {
8286
if (const auto* v = std::get_if<std::string>(&value)) {
8387
m_color = colorSpecFromConfigString(*v, key);
84-
if (m_titleLabel != nullptr) {
85-
m_titleLabel->setColor(m_color);
86-
}
87-
if (m_descriptionLabel != nullptr) {
88-
m_descriptionLabel->setColor(m_color);
89-
}
88+
applyLabelColors();
9089
requestLayout();
9190
return true;
9291
}
9392
return false;
9493
}
94+
if (key == "opacity") {
95+
if (const auto* v = std::get_if<double>(&value)) {
96+
m_opacity = std::clamp(static_cast<float>(*v), 0.0f, 1.0f);
97+
applyLabelColors();
98+
applyShadow();
99+
return true;
100+
}
101+
if (const auto* v = std::get_if<std::int64_t>(&value)) {
102+
m_opacity = std::clamp(static_cast<float>(*v), 0.0f, 1.0f);
103+
applyLabelColors();
104+
applyShadow();
105+
return true;
106+
}
107+
return false;
108+
}
95109
if (key == "shadow") {
96110
if (const auto* v = std::get_if<bool>(&value)) {
97111
m_shadow = *v;
@@ -142,12 +156,24 @@ void DesktopLabelWidget::doLayout(Renderer& renderer) {
142156

143157
m_titleLabel->setPosition(0.0f, 0.0f);
144158
root()->setSize(width, height);
159+
applyLabelColors();
145160
applyShadow();
146161
}
147162

163+
void DesktopLabelWidget::applyLabelColors() {
164+
ColorSpec effective = m_color;
165+
effective.alpha *= m_opacity;
166+
if (m_titleLabel != nullptr) {
167+
m_titleLabel->setColor(effective);
168+
}
169+
if (m_descriptionLabel != nullptr) {
170+
m_descriptionLabel->setColor(effective);
171+
}
172+
}
173+
148174
void DesktopLabelWidget::applyShadow() {
149175
const float offset = kShadowOffset * contentScale();
150-
const Color shadowColor(0.0f, 0.0f, 0.0f, kShadowAlpha);
176+
const Color shadowColor(0.0f, 0.0f, 0.0f, kShadowAlpha * m_opacity);
151177

152178
auto applyTo = [this, offset, shadowColor](Label* label) {
153179
if (label == nullptr) {

src/shell/desktop/widgets/desktop_label_widget.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Label;
99

1010
class DesktopLabelWidget : public DesktopWidget {
1111
public:
12-
DesktopLabelWidget(std::string title, std::string description, ColorSpec color, bool shadow);
12+
DesktopLabelWidget(std::string title, std::string description, ColorSpec color, float opacity, bool shadow);
1313

1414
void create() override;
1515
bool applySetting(
@@ -20,11 +20,13 @@ class DesktopLabelWidget : public DesktopWidget {
2020
private:
2121
void doLayout(Renderer& renderer) override;
2222
void onFontFamilyChanged(const std::string& family, Renderer& renderer) override;
23+
void applyLabelColors();
2324
void applyShadow();
2425

2526
std::string m_title;
2627
std::string m_description;
2728
ColorSpec m_color;
29+
float m_opacity = 1.0f;
2830
bool m_shadow;
2931
Label* m_titleLabel = nullptr;
3032
Label* m_descriptionLabel = nullptr;

0 commit comments

Comments
 (0)