libcosmic/cosmic-theme/src/output/qt_output.rs

463 lines
16 KiB
Rust
Raw Normal View History

2026-02-10 00:47:57 +00:00
use crate::Theme;
use palette::{Mix, Srgba, rgb::Rgba};
2026-02-09 02:43:37 +00:00
use std::{
fs::{self, File},
io::{self, Write},
path::Path,
};
use super::OutputError;
impl Theme {
#[must_use]
#[cold]
2026-02-10 00:47:57 +00:00
/// Produces a color scheme ini file for Qt.
///
2026-02-09 02:43:37 +00:00
/// Some high-level documentation for this file can be found at:
/// https://web.archive.org/web/20250402234329/https://docs.kde.org/stable5/en/plasma-workspace/kcontrol/colors/
2026-02-09 22:23:09 +00:00
pub fn as_qt(&self) -> String {
2026-02-09 02:43:37 +00:00
let mut output = "# GENERATED BY COSMIC\n".to_string();
// Usually, disabled elements will have strongly reduced contrast and are often notably darker or lighter
let disabled_color_effects = IniColorEffects {
color: self.button.disabled,
color_amount: 0.0,
color_effect: ColorEffect::Desaturate,
contrast_amount: 0.65,
contrast_effect: ColorEffect::Fade,
intensity_amount: 0.1,
2026-02-10 00:47:57 +00:00
intensity_effect: IntensityEffect::Lighten,
2026-02-09 02:43:37 +00:00
};
// Usually, inactive elements will have reduced contrast (text fades slightly into the background) and may have slightly reduced intensity
let inactive_color_effects = IniColorEffects {
color: self.palette.gray_1,
color_amount: 0.025,
color_effect: ColorEffect::Tint,
contrast_amount: 0.1,
contrast_effect: ColorEffect::Tint,
intensity_amount: 0.0,
intensity_effect: IntensityEffect::Shade,
};
2026-02-10 02:16:42 +00:00
// the background container
2026-02-09 02:43:37 +00:00
let view_colors = IniColors {
2026-02-10 02:16:42 +00:00
background_alternate: self.background.base.mix(self.accent.base, 0.05),
2026-02-09 02:43:37 +00:00
background_normal: self.background.base,
2026-02-10 02:16:42 +00:00
foreground_active: self.accent_text_color(),
foreground_inactive: self.background.on.mix(self.background.base, 0.1),
foreground_link: self.link_button.base,
foreground_negative: self.destructive_text_color(),
foreground_neutral: self.warning_text_color(),
foreground_normal: self.background.on,
foreground_positive: self.success_text_color(),
foreground_visited: self.accent_text_color(),
};
// components inside the background container
let window_colors = IniColors {
background_alternate: self.background.component.base.mix(self.accent.base, 0.05),
background_normal: self.background.component.base,
..view_colors
2026-02-09 02:43:37 +00:00
};
// selected text and items
let selection_colors = {
let selected = self.background.component.selected;
let selected_text = self.background.component.selected_text;
IniColors {
background_alternate: selected.mix(self.background.base, 0.5),
background_normal: selected,
// TODO: DecorationFocus and DecorationHover should = background_normal not foreground_active here
foreground_active: selected_text,
foreground_inactive: selected_text.mix(selected, 0.5),
foreground_link: self.link_button.on,
foreground_negative: self.destructive_color(),
foreground_neutral: self.warning_color(),
foreground_normal: selected_text,
foreground_positive: self.success_color(),
foreground_visited: self.accent_color(),
}
};
let button_colors = IniColors {
background_alternate: self.accent_button.base,
background_normal: self.button.base,
2026-02-10 02:16:42 +00:00
..view_colors
2026-02-09 02:43:37 +00:00
};
// Complementary: Areas of applications with an alternative color scheme; usually with a dark background for light color schemes.
let complementary_colors = {
let dark = if self.is_dark {
self.clone()
} else {
Self::get_active_with_brightness(false).unwrap_or_else(|_| self.clone())
};
IniColors {
background_alternate: dark.accent.base,
background_normal: dark.background.base,
foreground_active: dark.accent_text_color(),
foreground_inactive: dark.background.on.mix(dark.background.base, 0.1),
foreground_link: dark.link_button.base,
foreground_negative: dark.destructive_text_color(),
foreground_neutral: dark.warning_text_color(),
foreground_normal: dark.background.on,
foreground_positive: dark.success_text_color(),
foreground_visited: dark.accent_text_color(),
}
};
2026-02-10 02:16:42 +00:00
// headers in cosmic don't have a background
let header_colors = &view_colors;
let header_colors_inactive = &view_colors;
2026-02-09 02:43:37 +00:00
// tool tips, "What's This" tips, and similar elements
2026-02-10 02:16:42 +00:00
let tooltip_colors = &window_colors;
2026-02-09 02:43:37 +00:00
2026-02-10 00:58:24 +00:00
let light_or_dark = if self.is_dark { "Dark" } else { "Light" };
2026-02-09 02:43:37 +00:00
output.push_str(&format!(
r#"
[ColorEffects:Disabled]
{}
[ColorEffects:Inactive]
ChangeSelectionColor=false
Enable=false
{}
[Colors:Button]
{}
[Colors:Complementary]
{}
[Colors:Header]
{}
[Colors:Header][Inactive]
{}
[Colors:Selection]
{}
[Colors:Tooltip]
{}
[Colors:View]
{}
[Colors:Window]
{}
[General]
2026-02-10 00:58:24 +00:00
ColorScheme=Cosmic{light_or_dark}
Name=COSMIC {light_or_dark}
2026-02-09 02:43:37 +00:00
shadeSortColumn=true
[KDE]
contrast=4
[WM]
2026-02-10 00:58:24 +00:00
{}
2026-02-09 02:43:37 +00:00
"#,
format_ini_color_effects(&disabled_color_effects),
format_ini_color_effects(&inactive_color_effects),
format_ini_colors(&button_colors),
format_ini_colors(&complementary_colors),
format_ini_colors(&header_colors),
format_ini_colors(&header_colors_inactive),
format_ini_colors(&selection_colors),
format_ini_colors(&tooltip_colors),
format_ini_colors(&view_colors),
format_ini_colors(&window_colors),
2026-02-10 00:58:24 +00:00
format_ini_wm_colors(&view_colors, self.is_dark),
2026-02-09 02:43:37 +00:00
));
output
}
2026-02-09 22:23:09 +00:00
/// Write the color scheme to the appropriate directory.
/// Should be written in `~/.local/share/color-schemes/`.
///
/// See the docs: https://develop.kde.org/docs/plasma/#color-scheme
///
/// # Errors
///
/// Returns an `OutputError` if there is an error writing the colors file.
#[cold]
pub fn write_qt(&self) -> Result<(), OutputError> {
let colors = self.as_qt();
let Some(mut data_dir) = dirs::data_dir() else {
return Err(OutputError::MissingDataDir);
};
let file_name = if self.is_dark {
"CosmicDark.colors"
} else {
"CosmicLight.colors"
};
data_dir.push("color-schemes");
if !data_dir.exists() {
std::fs::create_dir_all(&data_dir).map_err(OutputError::Io)?;
}
let mut file = File::create(data_dir.join(file_name)).map_err(OutputError::Io)?;
file.write_all(colors.as_bytes()).map_err(OutputError::Io)?;
Ok(())
}
/// Apply the color scheme by copying it to `~/.config/kdeglobals`.
///
/// See the docs: https://develop.kde.org/docs/plasma/#color-scheme
///
/// # Errors
///
/// Returns an `OutputError` if there is an error applying the color scheme.
#[cold]
pub fn apply_qt(is_dark: bool) -> Result<(), OutputError> {
let Some(config_dir) = dirs::config_dir() else {
return Err(OutputError::MissingConfigDir);
};
let Some(mut data_dir) = dirs::data_dir() else {
return Err(OutputError::MissingDataDir);
};
data_dir.push("color-schemes");
let src_file = data_dir.join(if is_dark {
"CosmicDark.colors"
} else {
"CosmicLight.colors"
});
let dest_file = config_dir.join("kdeglobals");
#[cfg(target_family = "unix")]
{
use std::os::unix::fs::symlink;
Self::backup_non_cosmic_kdeglobals(&dest_file).map_err(OutputError::Io)?;
if dest_file.exists() {
// Currently we overwrite the file with our symlink.
// Maybe in the future we could merge kdeglobals with our color scheme ini?
fs::remove_file(&dest_file).map_err(OutputError::Io)?;
}
symlink(src_file, dest_file).map_err(OutputError::Io)?
}
Ok(())
}
/// Reset the applied qt colors by deleting the `~/.config/kdeglobals` file.
///
/// This does not restore the backed up kdeglobals file.
///
/// # Errors
///
/// Returns an `OutputError` if there is an error resetting the CSS file.
#[cold]
pub fn reset_qt() -> Result<(), OutputError> {
let Some(config_dir) = dirs::config_dir() else {
return Err(OutputError::MissingConfigDir);
};
let dest_file = config_dir.join("kdeglobals");
2026-02-10 00:58:24 +00:00
if Self::is_cosmic_kdeglobals(&dest_file)
.map_err(OutputError::Io)?
.unwrap_or_default()
{
2026-02-09 22:23:09 +00:00
fs::remove_file(dest_file).map_err(OutputError::Io)?
}
Ok(())
}
#[cold]
fn backup_non_cosmic_kdeglobals(path: &Path) -> io::Result<()> {
if !Self::is_cosmic_kdeglobals(path)?.unwrap_or(true) {
let backup_path = path.with_extension("bak");
fs::rename(path, &backup_path)?;
}
Ok(())
}
#[cold]
fn is_cosmic_kdeglobals(path: &Path) -> io::Result<Option<bool>> {
if !path.exists() {
return Ok(None);
}
if let Ok(metadata) = fs::symlink_metadata(path) {
if !metadata.file_type().is_symlink() {
return Ok(Some(false));
}
let Ok(target) = fs::read_link(path) else {
return Ok(Some(false));
};
let Some(target_filename) = target.file_name() else {
return Ok(Some(false));
};
return Ok(Some(
target_filename == "CosmicDark.colors" || target_filename == "CosmicLight.colors",
));
}
Ok(Some(false))
}
2026-02-09 02:43:37 +00:00
}
/// Formats a color in the form `r,g,b` e.g. `255,255,255`
2026-02-09 22:23:09 +00:00
fn to_rgb(c: Srgba) -> String {
2026-02-09 02:43:37 +00:00
let c_u8: Rgba<palette::encoding::Srgb, u8> = c.into_format();
format!("{},{},{}", c_u8.red, c_u8.green, c_u8.blue)
}
fn format_ini_color_effects(color_effects: &IniColorEffects) -> String {
format!(
r#"Color={}
ColorAmount={}
ColorEffect={}
ContrastAmount={}
ContrastEffect={}
IntensityAmount={}
IntensityEffect={}"#,
to_rgb(color_effects.color),
color_effects.color_amount,
color_effects.color_effect.as_u8(),
color_effects.contrast_amount,
color_effects.contrast_effect.as_u8(),
color_effects.intensity_amount,
color_effects.intensity_effect.as_u8(),
)
}
fn format_ini_colors(colors: &IniColors) -> String {
format!(
r#"BackgroundAlternate={}
BackgroundNormal={}
DecorationFocus={}
DecorationHover={}
ForegroundActive={}
ForegroundInactive={}
ForegroundLink={}
ForegroundNegative={}
ForegroundNeutral={}
ForegroundNormal={}
ForegroundPositive={}
ForegroundVisited={}"#,
to_rgb(colors.background_alternate),
to_rgb(colors.background_normal),
// Decoration colors are always the same as foreground_active
to_rgb(colors.foreground_active),
to_rgb(colors.foreground_active),
to_rgb(colors.foreground_active),
to_rgb(colors.foreground_inactive),
to_rgb(colors.foreground_link),
to_rgb(colors.foreground_negative),
to_rgb(colors.foreground_neutral),
to_rgb(colors.foreground_normal),
to_rgb(colors.foreground_positive),
to_rgb(colors.foreground_visited),
)
}
2026-02-10 00:58:24 +00:00
/// Sets the colors for the titlebars of active and inactive windows.
fn format_ini_wm_colors(view_colors: &IniColors, is_dark: bool) -> String {
let bg = view_colors.background_normal;
let fg = view_colors.foreground_active;
let blend = if is_dark { fg } else { bg };
format!(
r#"activeBackground={}
activeBlend={}
activeForeground={}
inactiveBackground={}
inactiveBlend={}
inactiveForeground={}"#,
to_rgb(bg),
to_rgb(blend),
to_rgb(fg),
to_rgb(bg),
to_rgb(blend),
to_rgb(fg),
)
}
2026-02-09 02:43:37 +00:00
struct IniColorEffects {
color: Srgba,
color_amount: f32,
color_effect: ColorEffect,
contrast_amount: f32,
/// Applied to the text, using the background as the reference color.
contrast_effect: ColorEffect,
intensity_amount: f32,
intensity_effect: IntensityEffect,
}
/// Each color set is made up of a number of roles which are available in all other sets.
/// In addition, except for Inactive Text, there is a corresponding background role for each of the text roles. Currently (except for Normal and Alternate Background), these colors are not chosen here but are automatically determined based on Normal Background and the corresponding Text color.
struct IniColors {
/// used when there is a need to subtly change the background to aid in item association. This might be used e.g. as the background of a heading, but is mostly used for alternating rows in lists, especially multi-column lists, to aid in visually tracking rows.
background_alternate: Srgba,
/// Normal background
background_normal: Srgba,
/// used to indicate an active element or attract attention, e.g. alerts, notifications; also for hovered hyperlinks
foreground_active: Srgba,
/// used for text which should be unobtrusive, e.g. comments, "subtitles", unimportant information, etc.
foreground_inactive: Srgba,
/// used for hyperlinks or to otherwise indicate "something which may be visited", or to show relationships
foreground_link: Srgba,
/// used for errors, failure notices, notifications that an action may be dangerous (e.g. unsafe web page or security context), etc.
foreground_negative: Srgba,
/// used to draw attention when another role is not appropriate; e.g. warnings, to indicate secure/encrypted content, etc.
foreground_neutral: Srgba,
/// Normal foreground
foreground_normal: Srgba,
/// used for success notices, to indicate trusted content, etc.
foreground_positive: Srgba,
/// used for "something (e.g. a hyperlink) that has been visited", or to indicate something that is "old".
foreground_visited: Srgba,
}
/// Intensity allows the overall color to be lightened or darkened.
enum IntensityEffect {
/// Makes everything lighter or darker in a controlled manner.
///
/// intensity_amount increases or decreases the overall intensity (i.e. perceived brightness) by an absolute amount.
Shade,
/// Changes the intensity to a percentage of the initial value.
Darken,
/// Conceptually the opposite of darken; lighten can be thought of as working with "distance from white", where darken works with "distance from black".
Lighten,
}
impl IntensityEffect {
pub fn as_u8(&self) -> u8 {
match self {
Self::Shade => 0,
Self::Darken => 1,
Self::Lighten => 2,
}
}
}
/// This also changes the overall color like [IntensityEffect],
/// but is not limited to intensity.
enum ColorEffect {
/// changes the relative chroma
///
/// This is available for "ColorEffect" but not "ContrastEffect".
Desaturate,
/// smoothly blends the original color into a reference color
Fade,
/// similar to Fade, except that the color (hue and chroma) changes more quickly while the intensity changes more slowly as the amount is increased
Tint,
}
impl ColorEffect {
pub fn as_u8(&self) -> u8 {
match self {
Self::Desaturate => 0,
Self::Fade => 1,
Self::Tint => 2,
}
}
}