feat(client): redesign native UI, themes, settings and avatars
Separate conversation drafts and voice view state, add native settings and restricted CSS themes, implement validated avatar cropping, and refresh dependencies with documented desktop compatibility backports.
This commit is contained in:
parent
44d284a568
commit
8e5ab56611
276 changed files with 53546 additions and 3399 deletions
BIN
patches/sctk-adwaita/src/title/Cantarell-Regular.ttf
vendored
Normal file
BIN
patches/sctk-adwaita/src/title/Cantarell-Regular.ttf
vendored
Normal file
Binary file not shown.
199
patches/sctk-adwaita/src/title/ab_glyph_renderer.rs
vendored
Normal file
199
patches/sctk-adwaita/src/title/ab_glyph_renderer.rs
vendored
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
//! Title renderer using ab_glyph.
|
||||
//!
|
||||
//! Requires no dynamically linked dependencies.
|
||||
//!
|
||||
//! Can fallback to a embedded Cantarell-Regular.ttf font (SIL Open Font Licence v1.1)
|
||||
//! if the system font doesn't work.
|
||||
use crate::title::{config, font_preference::FontPreference};
|
||||
use ab_glyph::{point, Font, FontRef, Glyph, PxScale, PxScaleFont, ScaleFont, VariableFont};
|
||||
use std::{fs::File, process::Command};
|
||||
use tiny_skia::{Color, Pixmap, PremultipliedColorU8};
|
||||
|
||||
const CANTARELL: &[u8] = include_bytes!("Cantarell-Regular.ttf");
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AbGlyphTitleText {
|
||||
title: String,
|
||||
font: Option<(memmap2::Mmap, FontPreference)>,
|
||||
original_px_size: f32,
|
||||
size: PxScale,
|
||||
color: Color,
|
||||
pixmap: Option<Pixmap>,
|
||||
}
|
||||
|
||||
impl AbGlyphTitleText {
|
||||
pub fn new(color: Color) -> Self {
|
||||
let font_pref = config::titlebar_font().unwrap_or_default();
|
||||
let font_pref_pt_size = font_pref.pt_size;
|
||||
let font = font_file_matching(&font_pref)
|
||||
.and_then(|f| mmap(&f))
|
||||
.map(|mmap| (mmap, font_pref));
|
||||
|
||||
let size = parse_font(&font)
|
||||
.pt_to_px_scale(font_pref_pt_size)
|
||||
.unwrap_or_else(|| {
|
||||
log::error!("invalid font units_per_em");
|
||||
PxScale { x: 17.6, y: 17.6 }
|
||||
});
|
||||
|
||||
Self {
|
||||
title: <_>::default(),
|
||||
font,
|
||||
original_px_size: size.x,
|
||||
size,
|
||||
color,
|
||||
pixmap: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_scale(&mut self, scale: u32) {
|
||||
let new_scale = PxScale::from(self.original_px_size * scale as f32);
|
||||
if (self.size.x - new_scale.x).abs() > f32::EPSILON {
|
||||
self.size = new_scale;
|
||||
self.pixmap = self.render();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_title(&mut self, title: impl Into<String>) {
|
||||
let new_title = title.into();
|
||||
if new_title != self.title {
|
||||
self.title = new_title;
|
||||
self.pixmap = self.render();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_color(&mut self, color: Color) {
|
||||
if color != self.color {
|
||||
self.color = color;
|
||||
self.pixmap = self.render();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pixmap(&self) -> Option<&Pixmap> {
|
||||
self.pixmap.as_ref()
|
||||
}
|
||||
|
||||
/// Render returning the new `Pixmap`.
|
||||
fn render(&self) -> Option<Pixmap> {
|
||||
let font = parse_font(&self.font);
|
||||
let font = font.as_scaled(self.size);
|
||||
|
||||
let glyphs = self.layout(&font);
|
||||
let last_glyph = glyphs.last()?;
|
||||
// + 2 because ab_glyph likes to draw outside of its area,
|
||||
// so we add 1px border around the pixmap
|
||||
let width = (last_glyph.position.x + font.h_advance(last_glyph.id)).ceil() as u32 + 2;
|
||||
let height = font.height().ceil() as u32 + 2;
|
||||
|
||||
let mut pixmap = Pixmap::new(width, height)?;
|
||||
|
||||
let pixels = pixmap.pixels_mut();
|
||||
|
||||
for glyph in glyphs {
|
||||
if let Some(outline) = font.outline_glyph(glyph) {
|
||||
let bounds = outline.px_bounds();
|
||||
let left = bounds.min.x as u32;
|
||||
let top = bounds.min.y as u32;
|
||||
outline.draw(|x, y, c| {
|
||||
// `ab_glyph` may return values greater than 1.0, but they are defined to be
|
||||
// same as 1.0. For our purposes, we need to constrain this value.
|
||||
let c = c.min(1.0);
|
||||
|
||||
// offset the index by 1, so it is in the center of the pixmap
|
||||
let p_idx = (top + y + 1) * width + (left + x + 1);
|
||||
let Some(pixel) = pixels.get_mut(p_idx as usize) else {
|
||||
log::warn!("Ignoring out of range pixel (pixel id: {p_idx}");
|
||||
return;
|
||||
};
|
||||
|
||||
let old_alpha_u8 = pixel.alpha();
|
||||
|
||||
let new_alpha = c + (old_alpha_u8 as f32 / 255.0);
|
||||
if let Some(px) = PremultipliedColorU8::from_rgba(
|
||||
(self.color.red() * new_alpha * 255.0) as _,
|
||||
(self.color.green() * new_alpha * 255.0) as _,
|
||||
(self.color.blue() * new_alpha * 255.0) as _,
|
||||
(new_alpha * 255.0) as _,
|
||||
) {
|
||||
*pixel = px;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Some(pixmap)
|
||||
}
|
||||
|
||||
/// Simple single-line glyph layout.
|
||||
fn layout(&self, font: &PxScaleFont<impl Font>) -> Vec<Glyph> {
|
||||
let mut caret = point(0.0, font.ascent());
|
||||
let mut last_glyph: Option<Glyph> = None;
|
||||
let mut target = Vec::new();
|
||||
for c in self.title.chars() {
|
||||
if c.is_control() {
|
||||
continue;
|
||||
}
|
||||
let mut glyph = font.scaled_glyph(c);
|
||||
if let Some(previous) = last_glyph.take() {
|
||||
caret.x += font.kern(previous.id, glyph.id);
|
||||
}
|
||||
glyph.position = caret;
|
||||
|
||||
last_glyph = Some(glyph.clone());
|
||||
caret.x += font.h_advance(glyph.id);
|
||||
|
||||
target.push(glyph);
|
||||
}
|
||||
target
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the memmapped system font or fallback to built-in cantarell.
|
||||
fn parse_font(sys_font: &Option<(memmap2::Mmap, FontPreference)>) -> FontRef<'_> {
|
||||
match sys_font {
|
||||
Some((mmap, font_pref)) => {
|
||||
FontRef::try_from_slice(mmap)
|
||||
.map(|mut f| {
|
||||
// basic "bold" handling for variable fonts
|
||||
if font_pref
|
||||
.style
|
||||
.as_deref()
|
||||
.map_or(false, |s| s.eq_ignore_ascii_case("bold"))
|
||||
{
|
||||
f.set_variation(b"wght", 700.0);
|
||||
}
|
||||
f
|
||||
})
|
||||
.unwrap_or_else(|_| {
|
||||
// We control the default font, so I guess it's fine to unwrap it
|
||||
#[allow(clippy::unwrap_used)]
|
||||
FontRef::try_from_slice(CANTARELL).unwrap()
|
||||
})
|
||||
}
|
||||
// We control the default font, so I guess it's fine to unwrap it
|
||||
#[allow(clippy::unwrap_used)]
|
||||
_ => FontRef::try_from_slice(CANTARELL).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Font-config without dynamically linked dependencies
|
||||
fn font_file_matching(pref: &FontPreference) -> Option<File> {
|
||||
let mut pattern = pref.name.clone();
|
||||
if let Some(style) = &pref.style {
|
||||
pattern.push(':');
|
||||
pattern.push_str(style);
|
||||
}
|
||||
Command::new("fc-match")
|
||||
.arg("-f")
|
||||
.arg("%{file}")
|
||||
.arg(&pattern)
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|out| String::from_utf8(out.stdout).ok())
|
||||
.and_then(|path| File::open(path.trim()).ok())
|
||||
}
|
||||
|
||||
fn mmap(file: &File) -> Option<memmap2::Mmap> {
|
||||
// Safety: System font files are not expected to be mutated during use
|
||||
unsafe { memmap2::Mmap::map(file).ok() }
|
||||
}
|
||||
20
patches/sctk-adwaita/src/title/config.rs
vendored
Normal file
20
patches/sctk-adwaita/src/title/config.rs
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
//! System font configuration.
|
||||
use crate::title::font_preference::FontPreference;
|
||||
use std::process::Command;
|
||||
|
||||
/// Query system for which font to use for window titles.
|
||||
pub(crate) fn titlebar_font() -> Option<FontPreference> {
|
||||
// outputs something like: `'Cantarell Bold 12'`
|
||||
let stdout = Command::new("gsettings")
|
||||
.args(["get", "org.gnome.desktop.wm.preferences", "titlebar-font"])
|
||||
.output()
|
||||
.ok()
|
||||
.and_then(|out| String::from_utf8(out.stdout).ok())?;
|
||||
|
||||
FontPreference::from_name_style_size(
|
||||
stdout
|
||||
.trim()
|
||||
.trim_end_matches('\'')
|
||||
.trim_start_matches('\''),
|
||||
)
|
||||
}
|
||||
227
patches/sctk-adwaita/src/title/crossfont_renderer.rs
vendored
Normal file
227
patches/sctk-adwaita/src/title/crossfont_renderer.rs
vendored
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
use std::mem;
|
||||
|
||||
use crossfont::{GlyphKey, Rasterize, RasterizedGlyph};
|
||||
use tiny_skia::{Color, Pixmap, PixmapPaint, PixmapRef, Transform};
|
||||
|
||||
use crate::title::config;
|
||||
|
||||
pub struct CrossfontTitleText {
|
||||
title: String,
|
||||
|
||||
font_desc: crossfont::FontDesc,
|
||||
font_key: crossfont::FontKey,
|
||||
size: crossfont::Size,
|
||||
scale: u32,
|
||||
metrics: crossfont::Metrics,
|
||||
rasterizer: crossfont::Rasterizer,
|
||||
color: Color,
|
||||
|
||||
pixmap: Option<Pixmap>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for CrossfontTitleText {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("TitleText")
|
||||
.field("title", &self.title)
|
||||
.field("font_desc", &self.font_desc)
|
||||
.field("font_key", &self.font_key)
|
||||
.field("size", &self.size)
|
||||
.field("scale", &self.scale)
|
||||
.field("pixmap", &self.pixmap)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl CrossfontTitleText {
|
||||
pub fn new(color: Color) -> Result<Self, crossfont::Error> {
|
||||
let title = "".into();
|
||||
|
||||
let font_pref = config::titlebar_font().unwrap_or_default();
|
||||
let font_style = font_pref
|
||||
.style
|
||||
.map(crossfont::Style::Specific)
|
||||
.unwrap_or_else(|| crossfont::Style::Description {
|
||||
slant: crossfont::Slant::Normal,
|
||||
weight: crossfont::Weight::Normal,
|
||||
});
|
||||
let font_desc = crossfont::FontDesc::new(&font_pref.name, font_style);
|
||||
|
||||
let mut rasterizer = crossfont::Rasterizer::new()?;
|
||||
let size = crossfont::Size::new(font_pref.pt_size);
|
||||
let font_key = rasterizer.load_font(&font_desc, size)?;
|
||||
|
||||
// Need to load at least one glyph for the face before calling metrics.
|
||||
// The glyph requested here ('m' at the time of writing) has no special
|
||||
// meaning.
|
||||
rasterizer.get_glyph(GlyphKey {
|
||||
font_key,
|
||||
character: 'm',
|
||||
size,
|
||||
})?;
|
||||
|
||||
let metrics = rasterizer.metrics(font_key, size)?;
|
||||
|
||||
let mut this = Self {
|
||||
pixmap: None,
|
||||
rasterizer,
|
||||
font_desc,
|
||||
font_key,
|
||||
scale: 1,
|
||||
metrics,
|
||||
title,
|
||||
color,
|
||||
size,
|
||||
};
|
||||
|
||||
this.rerender();
|
||||
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
fn update_metrics(&mut self) -> Result<(), crossfont::Error> {
|
||||
self.rasterizer.get_glyph(GlyphKey {
|
||||
font_key: self.font_key,
|
||||
character: 'm',
|
||||
size: self.size,
|
||||
})?;
|
||||
self.metrics = self.rasterizer.metrics(self.font_key, self.size)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_scale(&mut self, scale: u32) {
|
||||
let old_scale = mem::replace(&mut self.scale, scale);
|
||||
if old_scale != self.scale {
|
||||
self.size = self.size.scale(self.scale as f32 / old_scale as f32);
|
||||
self.update_metrics().ok();
|
||||
self.rerender();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_title<S: Into<String>>(&mut self, title: S) {
|
||||
let title = title.into();
|
||||
if self.title != title {
|
||||
self.title = title;
|
||||
self.rerender();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_color(&mut self, color: Color) {
|
||||
if self.color != color {
|
||||
self.color = color;
|
||||
self.rerender();
|
||||
}
|
||||
}
|
||||
|
||||
fn rerender(&mut self) {
|
||||
let glyphs: Vec<_> = self
|
||||
.title
|
||||
.chars()
|
||||
.filter_map(|character| {
|
||||
let key = GlyphKey {
|
||||
character,
|
||||
font_key: self.font_key,
|
||||
size: self.size,
|
||||
};
|
||||
|
||||
self.rasterizer
|
||||
.get_glyph(key)
|
||||
.map(|glyph| (key, glyph))
|
||||
.ok()
|
||||
})
|
||||
.collect();
|
||||
|
||||
if glyphs.is_empty() {
|
||||
self.pixmap = None;
|
||||
return;
|
||||
}
|
||||
|
||||
let width = self.calc_width(&glyphs);
|
||||
let height = self.metrics.line_height.round() as i32;
|
||||
|
||||
let mut pixmap = if let Some(p) = Pixmap::new(width as u32, height as u32) {
|
||||
p
|
||||
} else {
|
||||
self.pixmap = None;
|
||||
return;
|
||||
};
|
||||
// pixmap.fill(Color::from_rgba8(255, 0, 0, 55));
|
||||
|
||||
let mut caret = 0;
|
||||
let mut last_glyph = None;
|
||||
|
||||
for (key, glyph) in glyphs {
|
||||
let mut buffer = Vec::with_capacity(glyph.width as usize * 4);
|
||||
|
||||
let glyph_buffer = match &glyph.buffer {
|
||||
crossfont::BitmapBuffer::Rgb(v) => v.chunks(3),
|
||||
crossfont::BitmapBuffer::Rgba(v) => v.chunks(4),
|
||||
};
|
||||
|
||||
for px in glyph_buffer {
|
||||
let alpha = if let Some(alpha) = px.get(3) {
|
||||
*alpha as f32 / 255.0
|
||||
} else {
|
||||
let r = px[0] as f32 / 255.0;
|
||||
let g = px[1] as f32 / 255.0;
|
||||
let b = px[2] as f32 / 255.0;
|
||||
(r + g + b) / 3.0
|
||||
};
|
||||
|
||||
let mut color = self.color;
|
||||
color.set_alpha(alpha);
|
||||
let color = color.premultiply().to_color_u8();
|
||||
|
||||
buffer.push(color.red());
|
||||
buffer.push(color.green());
|
||||
buffer.push(color.blue());
|
||||
buffer.push(color.alpha());
|
||||
}
|
||||
|
||||
if let Some(last) = last_glyph {
|
||||
let (x, _) = self.rasterizer.kerning(last, key);
|
||||
caret += x as i32;
|
||||
}
|
||||
|
||||
if let Some(pixmap_glyph) =
|
||||
PixmapRef::from_bytes(&buffer, glyph.width as _, glyph.height as _)
|
||||
{
|
||||
pixmap.draw_pixmap(
|
||||
glyph.left + caret,
|
||||
height - glyph.top + self.metrics.descent.round() as i32,
|
||||
pixmap_glyph,
|
||||
&PixmapPaint::default(),
|
||||
Transform::identity(),
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
caret += glyph.advance.0;
|
||||
|
||||
last_glyph = Some(key);
|
||||
}
|
||||
|
||||
self.pixmap = Some(pixmap);
|
||||
}
|
||||
|
||||
pub fn pixmap(&self) -> Option<&Pixmap> {
|
||||
self.pixmap.as_ref()
|
||||
}
|
||||
|
||||
fn calc_width(&mut self, glyphs: &[(GlyphKey, RasterizedGlyph)]) -> i32 {
|
||||
let mut caret = 0;
|
||||
let mut last_glyph: Option<&GlyphKey> = None;
|
||||
|
||||
for (key, glyph) in glyphs.iter() {
|
||||
if let Some(last) = last_glyph {
|
||||
let (x, _) = self.rasterizer.kerning(*last, *key);
|
||||
caret += x as i32;
|
||||
}
|
||||
|
||||
caret += glyph.advance.0;
|
||||
|
||||
last_glyph = Some(key);
|
||||
}
|
||||
|
||||
caret
|
||||
}
|
||||
}
|
||||
16
patches/sctk-adwaita/src/title/dumb.rs
vendored
Normal file
16
patches/sctk-adwaita/src/title/dumb.rs
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use tiny_skia::{Color, Pixmap};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DumbTitleText {}
|
||||
|
||||
impl DumbTitleText {
|
||||
pub fn update_scale(&mut self, _scale: u32) {}
|
||||
|
||||
pub fn update_title<S: Into<String>>(&mut self, _title: S) {}
|
||||
|
||||
pub fn update_color(&mut self, _color: Color) {}
|
||||
|
||||
pub fn pixmap(&self) -> Option<&Pixmap> {
|
||||
None
|
||||
}
|
||||
}
|
||||
91
patches/sctk-adwaita/src/title/font_preference.rs
vendored
Normal file
91
patches/sctk-adwaita/src/title/font_preference.rs
vendored
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#[derive(Debug)]
|
||||
pub(crate) struct FontPreference {
|
||||
pub name: String,
|
||||
pub style: Option<String>,
|
||||
pub pt_size: f32,
|
||||
}
|
||||
|
||||
impl Default for FontPreference {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "sans-serif".into(),
|
||||
style: None,
|
||||
pt_size: 10.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FontPreference {
|
||||
/// Parse config string like `Cantarell 12`, `Cantarell Bold 11`, `Noto Serif CJK HK Bold 12`.
|
||||
pub fn from_name_style_size(conf: &str) -> Option<Self> {
|
||||
// assume last is size, 2nd last is style and the rest is name.
|
||||
match conf.rsplit_once(' ') {
|
||||
Some((head, tail)) if tail.chars().all(|c| c.is_numeric()) => {
|
||||
let pt_size: f32 = tail.parse().unwrap_or(10.0);
|
||||
match head.rsplit_once(' ') {
|
||||
Some((name, style)) if !name.is_empty() => Some(Self {
|
||||
name: name.into(),
|
||||
style: Some(style.into()),
|
||||
pt_size,
|
||||
}),
|
||||
None if !head.is_empty() => Some(Self {
|
||||
name: head.into(),
|
||||
style: None,
|
||||
pt_size,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
Some((head, tail)) if !head.is_empty() => Some(Self {
|
||||
name: head.into(),
|
||||
style: Some(tail.into()),
|
||||
pt_size: 10.0,
|
||||
}),
|
||||
None if !conf.is_empty() => Some(Self {
|
||||
name: conf.into(),
|
||||
style: None,
|
||||
pt_size: 10.0,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pref_from_multi_name_variant_size() {
|
||||
let pref = FontPreference::from_name_style_size("Noto Serif CJK HK Bold 12").unwrap();
|
||||
assert_eq!(pref.name, "Noto Serif CJK HK");
|
||||
assert_eq!(pref.style, Some("Bold".into()));
|
||||
assert!((pref.pt_size - 12.0).abs() < f32::EPSILON);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pref_from_name_variant_size() {
|
||||
let pref = FontPreference::from_name_style_size("Cantarell Bold 12").unwrap();
|
||||
assert_eq!(pref.name, "Cantarell");
|
||||
assert_eq!(pref.style, Some("Bold".into()));
|
||||
assert!((pref.pt_size - 12.0).abs() < f32::EPSILON);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pref_from_name_size() {
|
||||
let pref = FontPreference::from_name_style_size("Cantarell 12").unwrap();
|
||||
assert_eq!(pref.name, "Cantarell");
|
||||
assert_eq!(pref.style, None);
|
||||
assert!((pref.pt_size - 12.0).abs() < f32::EPSILON);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pref_from_name() {
|
||||
let pref = FontPreference::from_name_style_size("Cantarell").unwrap();
|
||||
assert_eq!(pref.name, "Cantarell");
|
||||
assert_eq!(pref.style, None);
|
||||
assert!((pref.pt_size - 10.0).abs() < f32::EPSILON);
|
||||
}
|
||||
#[test]
|
||||
fn pref_from_multi_name_style() {
|
||||
let pref = FontPreference::from_name_style_size("Foo Bar Baz Bold").unwrap();
|
||||
assert_eq!(pref.name, "Foo Bar Baz");
|
||||
assert_eq!(pref.style, Some("Bold".into()));
|
||||
assert!((pref.pt_size - 10.0).abs() < f32::EPSILON);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue