use_bg_color for label

This commit is contained in:
galister 2026-07-13 03:21:20 +09:00
parent 6f5be3a157
commit 2ea3b222f3
15 changed files with 71 additions and 28 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, path::PathBuf, rc::Rc}; use std::{path::PathBuf, rc::Rc};
use chrono::Timelike; use chrono::Timelike;
use glam::Vec2; use glam::Vec2;

View File

@ -607,6 +607,7 @@ fn mount_requires_restart(layout: &mut Layout, parent: WidgetID) -> anyhow::Resu
}), }),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );

View File

@ -110,6 +110,7 @@ impl ToastManager {
wrap: true, wrap: true,
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );
let (label, _) = layout.add_child(rect.id, label, taffy::Style { ..Default::default() })?; let (label, _) = layout.add_child(rect.id, label, taffy::Style { ..Default::default() })?;

View File

@ -55,6 +55,7 @@ pub fn create_label(layout: &mut Layout, id_parent: WidgetID, content: Translati
wrap: true, wrap: true,
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );
@ -74,6 +75,7 @@ pub fn create_label_error(layout: &mut Layout, parent: WidgetID, content: String
weight: Some(FontWeight::Bold), weight: Some(FontWeight::Bold),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );

View File

@ -113,6 +113,7 @@ impl View {
shadow: Some(WguiTextShadow::default()), shadow: Some(WguiTextShadow::default()),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );

View File

@ -130,7 +130,7 @@
<Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _context_name="${name}" _press_left="::ContextMenuOpen menu_screen" _press_right="::OverlayToggle ${name}"> <Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _context_name="${name}" _press_left="::ContextMenuOpen menu_screen" _press_right="::OverlayToggle ${name}">
<sprite width="38" height="38" color="on_background_variant" src_builtin="edit/screen.svg" /> <sprite width="38" height="38" color="on_background_variant" src_builtin="edit/screen.svg" />
<div position="absolute" margin_top="-7" margin_left="-1"> <div position="absolute" margin_top="-7" margin_left="-1">
<label text="${display}" size="~text_size" color="on_background_variant(opacity-0.2)" weight="bold" /> <label text="${display}" size="~text_size" color="on_background_variant(opacity-0.2)" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>
@ -163,7 +163,7 @@
<Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _context_name="${name}" _press_left="::ContextMenuOpen menu_mirror" _press_right="::OverlayToggle ${name}"> <Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _context_name="${name}" _press_left="::ContextMenuOpen menu_mirror" _press_right="::OverlayToggle ${name}">
<sprite width="38" height="38" color="on_background_variant" src_builtin="edit/mirror.svg" /> <sprite width="38" height="38" color="on_background_variant" src_builtin="edit/mirror.svg" />
<div position="absolute" margin_top="5" margin_left="13"> <div position="absolute" margin_top="5" margin_left="13">
<label text="${display}" size="20" color="on_background(opacity-0.2)" weight="bold" /> <label text="${display}" size="20" color="on_background(opacity-0.2)" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>
@ -192,7 +192,7 @@
<Button macro="button_style" id="set_${idx}" _press="::SetSwitch ${idx}" tooltip="WATCH.SWITCH_TO_SET" tooltip_side="bottom"> <Button macro="button_style" id="set_${idx}" _press="::SetSwitch ${idx}" tooltip="WATCH.SWITCH_TO_SET" tooltip_side="bottom">
<sprite width="38" height="38" color="on_background_variant" src_builtin="watch/set2.svg" /> <sprite width="38" height="38" color="on_background_variant" src_builtin="watch/set2.svg" />
<div position="absolute" margin_top="10" margin_left="-7"> <div position="absolute" margin_top="10" margin_left="-7">
<label text="${display}" size="20" color="on_background(opacity-0.2)" weight="bold" /> <label text="${display}" size="20" color="on_background(opacity-0.2)" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>

View File

@ -38,7 +38,7 @@
<Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _press="::OverlayToggle ${name}"> <Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _press="::OverlayToggle ${name}">
<sprite width="38" height="38" color="on_background" src_builtin="edit/screen.svg" /> <sprite width="38" height="38" color="on_background" src_builtin="edit/screen.svg" />
<div position="absolute" margin_top="-7" margin_left="-1"> <div position="absolute" margin_top="-7" margin_left="-1">
<label text="${display}" size="18" color="on_background(opacity-0.2)" weight="bold" /> <label text="${display}" size="18" color="on_background(opacity-0.2)" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>
@ -51,7 +51,7 @@
<Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _press="::OverlayToggle ${name}"> <Button macro="button_style" id="overlay_${idx}" tooltip_str="${name}" _press="::OverlayToggle ${name}">
<sprite width="38" height="38" color="on_background" src_builtin="edit/mirror.svg" /> <sprite width="38" height="38" color="on_background" src_builtin="edit/mirror.svg" />
<div position="absolute" margin_top="5" margin_left="13"> <div position="absolute" margin_top="5" margin_left="13">
<label text="${display}" size="20" color="on_background" weight="bold" /> <label text="${display}" size="20" color="on_background" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>
@ -65,7 +65,7 @@
<Button macro="button_style" id="set_${idx}" _press="::SetToggle ${idx}" tooltip="WATCH.SWITCH_TO_SET" tooltip_side="top"> <Button macro="button_style" id="set_${idx}" _press="::SetToggle ${idx}" tooltip="WATCH.SWITCH_TO_SET" tooltip_side="top">
<sprite width="38" height="38" color="on_background" src_builtin="watch/set2.svg" /> <sprite width="38" height="38" color="on_background" src_builtin="watch/set2.svg" />
<div position="absolute" margin_top="9"> <div position="absolute" margin_top="9">
<label text="${display}" size="24" color="#00050F" weight="bold" /> <label text="${display}" size="24" color="#00050F" weight="bold" use_bg_color="1" />
</div> </div>
</Button> </Button>
</template> </template>

View File

@ -119,9 +119,19 @@ impl WguiColor {
} }
} }
pub fn on_color(&self) -> Option<Self> { /// Gets a matching foreground color from a background color
pub fn fg_color(&self) -> Option<Self> {
if let Self::Named(name) = self { if let Self::Named(name) = self {
name.name.on_color().map(|x| x.into()) name.name.fg_color().map(|x| x.into())
} else {
None
}
}
/// Gets a matching background color from a foreground color
pub fn bg_color(&self) -> Option<Self> {
if let Self::Named(name) = self {
name.name.bg_color().map(|x| x.into())
} else { } else {
None None
} }
@ -129,7 +139,7 @@ impl WguiColor {
} }
impl WguiColorName { impl WguiColorName {
pub fn on_color(&self) -> Option<Self> { pub fn fg_color(&self) -> Option<Self> {
match self { match self {
Self::Primary => Some(Self::OnPrimary), Self::Primary => Some(Self::OnPrimary),
Self::Secondary => Some(Self::OnSecondary), Self::Secondary => Some(Self::OnSecondary),
@ -142,6 +152,19 @@ impl WguiColorName {
} }
} }
pub fn bg_color(&self) -> Option<Self> {
match self {
Self::OnPrimary => Some(Self::Primary),
Self::OnSecondary => Some(Self::Secondary),
Self::OnTertiary => Some(Self::Tertiary),
Self::OnDanger => Some(Self::Danger),
Self::OnBackground => Some(Self::Background),
Self::OnBackgroundVariant => Some(Self::BackgroundVariant),
Self::OnBackgroundContrast => Some(Self::BackgroundContrast),
_ => None,
}
}
pub const fn to_wgui_color(&self) -> WguiColor { pub const fn to_wgui_color(&self) -> WguiColor {
WguiColor::Named(WguiNamedColor { WguiColor::Named(WguiNamedColor {
name: *self, name: *self,

View File

@ -149,24 +149,24 @@ impl ComponentTrait for ComponentButton {
log::error!("Button with more than one sprite!"); log::error!("Button with more than one sprite!");
} }
// apply initial color from button // apply initial color from button
if let Some(on_color) = state.colors.color.on_color() { if let Some(fg_color) = state.colors.color.fg_color() {
let common = &mut CallbackDataCommon { let common = &mut CallbackDataCommon {
state: &data.layout.state, state: &data.layout.state,
alterables: &mut data.layout.alterables, alterables: &mut data.layout.alterables,
}; };
widget.set_color(common, on_color); widget.set_color(common, fg_color);
} }
state.id_sprite = child; state.id_sprite = child;
} else if let Some(mut widget) = data.layout.state.widgets.get_as::<WidgetLabel>(child) { } else if let Some(mut widget) = data.layout.state.widgets.get_as::<WidgetLabel>(child) {
if !state.id_label.is_null() && state.id_label != child { if !state.id_label.is_null() && state.id_label != child {
log::error!("Button with more than one label!"); log::error!("Button with more than one label!");
} }
if let Some(on_color) = state.colors.color.on_color() { if let Some(fg_color) = state.colors.color.fg_color() {
let common = &mut CallbackDataCommon { let common = &mut CallbackDataCommon {
state: &data.layout.state, state: &data.layout.state,
alterables: &mut data.layout.alterables, alterables: &mut data.layout.alterables,
}; };
widget.set_color(common, on_color, true); widget.set_color(common, fg_color, true);
} }
state.id_label = child; state.id_label = child;
} }
@ -216,12 +216,12 @@ impl ComponentButton {
let mut state = self.state.borrow_mut(); let mut state = self.state.borrow_mut();
state.colors.color = color; state.colors.color = color;
if let Some(on_color) = color.on_color() { if let Some(fg_color) = color.fg_color() {
if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(state.id_label) { if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(state.id_label) {
label.set_color(common, on_color, true); label.set_color(common, fg_color, true);
} }
if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(state.id_sprite) { if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(state.id_sprite) {
sprite.set_color(common, on_color); sprite.set_color(common, fg_color);
} }
} }
} }
@ -287,15 +287,15 @@ impl ComponentButton {
.border_color .border_color
.lerp(&common.globals().palette, alt_border_color, mult); .lerp(&common.globals().palette, alt_border_color, mult);
if let Some(on_color0) = colors.color.on_color() if let Some(fg_color0) = colors.color.fg_color()
&& let Some(on_color1) = alt_color.on_color() && let Some(fg_color1) = alt_color.fg_color()
{ {
let on_color = on_color0.lerp(&common.globals().palette, &on_color1, mult); let fg_color = fg_color0.lerp(&common.globals().palette, &fg_color1, mult);
if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(state.id_label) { if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(state.id_label) {
label.set_color(common, on_color, true); label.set_color(common, fg_color, true);
} }
if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(state.id_sprite) { if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(state.id_sprite) {
sprite.set_color(common, on_color); sprite.set_color(common, fg_color);
} }
} }
} }
@ -328,15 +328,15 @@ fn anim_hover(
(colors.border_color, colors.color) (colors.border_color, colors.color)
}; };
if let Some(on_color0) = init_color.on_color() if let Some(fg_color0) = init_color.fg_color()
&& let Some(on_color1) = colors.hover_color.on_color() && let Some(fg_color1) = colors.hover_color.fg_color()
{ {
let on_color = on_color0.lerp(&common.globals().palette, &on_color1, mult); let fg_color = fg_color0.lerp(&common.globals().palette, &fg_color1, mult);
if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(label) { if let Some(mut label) = common.state.widgets.get_as::<WidgetLabel>(label) {
label.set_color(common, on_color, true); label.set_color(common, fg_color, true);
} }
if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(sprite) { if let Some(mut sprite) = common.state.widgets.get_as::<WidgetSprite>(sprite) {
sprite.set_color(common, on_color); sprite.set_color(common, fg_color);
} }
} }
@ -628,6 +628,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
color: Some(WguiColorName::OnBackground.into()), color: Some(WguiColorName::OnBackground.into()),
..params.text_style ..params.text_style
}, },
..Default::default()
}, },
); );

View File

@ -486,6 +486,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
color: Some(WguiColorName::OnBackground.into()), color: Some(WguiColorName::OnBackground.into()),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );
let (label, _node_label) = ess.layout.add_child(id_container, widget_label, Default::default())?; let (label, _node_label) = ess.layout.add_child(id_container, widget_label, Default::default())?;

View File

@ -348,6 +348,7 @@ pub fn construct(
}), }),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );
let (label, _node_label) = ess.layout.add_child( let (label, _node_label) = ess.layout.add_child(

View File

@ -686,6 +686,7 @@ fn mount_slider_handle(
align: Some(HorizontalAlign::Center), align: Some(HorizontalAlign::Center),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );
Some(ess.layout.add_child(slider_handle.id, label, Default::default())?) Some(ess.layout.add_child(slider_handle.id, label, Default::default())?)

View File

@ -260,6 +260,7 @@ pub fn construct(ess: &mut ConstructEssentials, params: Params) -> anyhow::Resul
weight: Some(FontWeight::Bold), weight: Some(FontWeight::Bold),
..Default::default() ..Default::default()
}, },
..Default::default()
}, },
); );

View File

@ -39,6 +39,9 @@ pub fn parse_widget_label<'a>(
"translation" if !value.is_empty() => { "translation" if !value.is_empty() => {
params.content = Translation::from_translation_key(value); params.content = Translation::from_translation_key(value);
} }
"use_bg_color" if !value.is_empty() => {
params.use_bg_color = true;
}
_ => {} _ => {}
} }
} }

View File

@ -22,6 +22,7 @@ use super::{WidgetObj, WidgetState};
pub struct WidgetLabelParams { pub struct WidgetLabelParams {
pub content: Translation, pub content: Translation,
pub style: TextStyle, pub style: TextStyle,
pub use_bg_color: bool,
} }
pub struct WidgetLabel { pub struct WidgetLabel {
@ -119,7 +120,13 @@ impl WidgetLabel {
} }
pub fn set_color(&mut self, common: &mut CallbackDataCommon, color: WguiColor, apply_to_existing_text: bool) { pub fn set_color(&mut self, common: &mut CallbackDataCommon, color: WguiColor, apply_to_existing_text: bool) {
self.params.style.color = Some(color); if self.params.use_bg_color {
if let Some(bg_color) = color.bg_color() {
self.params.style.color = Some(bg_color);
}
} else {
self.params.style.color = Some(color);
}
if apply_to_existing_text { if apply_to_existing_text {
self.update_attrs(&common.globals().palette); self.update_attrs(&common.globals().palette);
common.mark_widget_dirty(self.id); common.mark_widget_dirty(self.id);