mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-03-28 12:11:59 +01:00
This commit adds new settings to some widgets that allows to auto select/hide them based on their current values. The cpu/memory/network/storage widgets get a setting that auto selects the widget if the current value/percentage is over a value. The battery widget gets a setting that auto selects the widget if the current percentage is under a value. The storage widget gets a setting that auto hides the disk widget if the percentage is under a value. Also added 2 new settings (auto_select_fill and auto_select_text) to the theme, in order to select the fill and text colors of an auto selected widget. (Easter egg: the network icons change if the value is over the limit) PR: #1353
90 lines
3.3 KiB
Rust
90 lines
3.3 KiB
Rust
use eframe::egui::Color32;
|
|
use eframe::egui::CursorIcon;
|
|
use eframe::egui::Frame;
|
|
use eframe::egui::Margin;
|
|
use eframe::egui::Response;
|
|
use eframe::egui::Sense;
|
|
use eframe::egui::Stroke;
|
|
use eframe::egui::Ui;
|
|
|
|
/// Same as SelectableLabel, but supports all content
|
|
pub struct SelectableFrame {
|
|
selected: bool,
|
|
selected_fill: Option<Color32>,
|
|
}
|
|
|
|
impl SelectableFrame {
|
|
pub fn new(selected: bool) -> Self {
|
|
Self {
|
|
selected,
|
|
selected_fill: None,
|
|
}
|
|
}
|
|
|
|
pub fn new_auto(selected: bool, selected_fill: Option<Color32>) -> Self {
|
|
Self {
|
|
selected,
|
|
selected_fill,
|
|
}
|
|
}
|
|
|
|
pub fn show<R>(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> Response {
|
|
let Self {
|
|
selected,
|
|
selected_fill,
|
|
} = self;
|
|
|
|
Frame::NONE
|
|
.show(ui, |ui| {
|
|
let response = ui.interact(ui.max_rect(), ui.unique_id(), Sense::click());
|
|
|
|
if ui.is_rect_visible(response.rect) {
|
|
// take into account the stroke width
|
|
let inner_margin = Margin::symmetric(
|
|
ui.style().spacing.button_padding.x as i8 - 1,
|
|
ui.style().spacing.button_padding.y as i8 - 1,
|
|
);
|
|
|
|
// since the stroke is drawn inside the frame, we always reserve space for it
|
|
if selected && response.hovered() {
|
|
let visuals = ui.style().interact_selectable(&response, selected);
|
|
|
|
Frame::NONE
|
|
.stroke(Stroke::new(1.0, visuals.bg_stroke.color))
|
|
.corner_radius(visuals.corner_radius)
|
|
.fill(selected_fill.unwrap_or(visuals.bg_fill))
|
|
.inner_margin(inner_margin)
|
|
.show(ui, add_contents);
|
|
} else if response.hovered() || response.highlighted() || response.has_focus() {
|
|
let visuals = ui.style().interact_selectable(&response, selected);
|
|
|
|
Frame::NONE
|
|
.stroke(Stroke::new(1.0, visuals.bg_stroke.color))
|
|
.corner_radius(visuals.corner_radius)
|
|
.fill(visuals.bg_fill)
|
|
.inner_margin(inner_margin)
|
|
.show(ui, add_contents);
|
|
} else if selected {
|
|
let visuals = ui.style().interact_selectable(&response, selected);
|
|
|
|
Frame::NONE
|
|
.stroke(Stroke::new(1.0, visuals.bg_fill))
|
|
.corner_radius(visuals.corner_radius)
|
|
.fill(selected_fill.unwrap_or(visuals.bg_fill))
|
|
.inner_margin(inner_margin)
|
|
.show(ui, add_contents);
|
|
} else {
|
|
Frame::NONE
|
|
.stroke(Stroke::new(1.0, Color32::TRANSPARENT))
|
|
.inner_margin(inner_margin)
|
|
.show(ui, add_contents);
|
|
}
|
|
}
|
|
|
|
response
|
|
})
|
|
.inner
|
|
.on_hover_cursor(CursorIcon::PointingHand)
|
|
}
|
|
}
|