feat: custom icon fallbacks
This commit is contained in:
parent
a731ebc1b2
commit
983d4d3378
1 changed files with 30 additions and 9 deletions
|
|
@ -2,7 +2,17 @@
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
use super::{Handle, Icon};
|
use super::{Handle, Icon};
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{borrow::Cow, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default, Hash)]
|
||||||
|
/// Fallback icon to use if the icon was not found.
|
||||||
|
pub enum IconFallback {
|
||||||
|
#[default]
|
||||||
|
/// Default fallback using the icon name.
|
||||||
|
Default,
|
||||||
|
/// Fallback to specific icon names.
|
||||||
|
Names(Vec<Cow<'static, str>>),
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[derive(derive_setters::Setters, Clone, Debug, Hash)]
|
#[derive(derive_setters::Setters, Clone, Debug, Hash)]
|
||||||
|
|
@ -11,7 +21,7 @@ pub struct Named {
|
||||||
pub(super) name: Arc<str>,
|
pub(super) name: Arc<str>,
|
||||||
|
|
||||||
/// Checks for a fallback if the icon was not found.
|
/// Checks for a fallback if the icon was not found.
|
||||||
pub fallback: bool,
|
pub fallback: Option<IconFallback>,
|
||||||
|
|
||||||
/// Restrict the lookup to a given scale.
|
/// Restrict the lookup to a given scale.
|
||||||
#[setters(strip_option)]
|
#[setters(strip_option)]
|
||||||
|
|
@ -34,7 +44,7 @@ impl Named {
|
||||||
Self {
|
Self {
|
||||||
symbolic: name.ends_with("-symbolic"),
|
symbolic: name.ends_with("-symbolic"),
|
||||||
name,
|
name,
|
||||||
fallback: true,
|
fallback: Some(IconFallback::Default),
|
||||||
size: None,
|
size: None,
|
||||||
scale: None,
|
scale: None,
|
||||||
prefer_svg: false,
|
prefer_svg: false,
|
||||||
|
|
@ -45,6 +55,7 @@ impl Named {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn path(self) -> Option<PathBuf> {
|
pub fn path(self) -> Option<PathBuf> {
|
||||||
let mut name = &*self.name;
|
let mut name = &*self.name;
|
||||||
|
let fallback = &self.fallback;
|
||||||
crate::icon_theme::DEFAULT.with(|theme| {
|
crate::icon_theme::DEFAULT.with(|theme| {
|
||||||
let theme = theme.borrow();
|
let theme = theme.borrow();
|
||||||
|
|
||||||
|
|
@ -71,12 +82,22 @@ impl Named {
|
||||||
let mut result = locate();
|
let mut result = locate();
|
||||||
|
|
||||||
// On failure, attempt to locate fallback icon.
|
// On failure, attempt to locate fallback icon.
|
||||||
if result.is_none() && self.fallback {
|
if result.is_none() {
|
||||||
for new_name in name.rmatch_indices('-').map(|(pos, _)| &name[..pos]) {
|
if matches!(fallback, Some(IconFallback::Default)) {
|
||||||
name = new_name;
|
for new_name in name.rmatch_indices('-').map(|(pos, _)| &name[..pos]) {
|
||||||
result = locate();
|
name = new_name;
|
||||||
if result.is_some() {
|
result = locate();
|
||||||
break;
|
if result.is_some() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Some(IconFallback::Names(fallbacks)) = fallback {
|
||||||
|
for fallback in fallbacks {
|
||||||
|
name = fallback;
|
||||||
|
result = locate();
|
||||||
|
if result.is_some() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue