fix: notification title width does not include the width of the icon (#1000)

This commit is contained in:
三咲雅 · Misaki Masa 2024-05-04 00:35:04 +08:00 committed by GitHub
parent 0e26f5d3c7
commit aee65bc4d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 14 deletions

View file

@ -19,7 +19,9 @@ pub struct Message {
impl From<NotifyOpt> for Message {
fn from(opt: NotifyOpt) -> Self {
let title = opt.title.lines().next().unwrap_or_default();
let max_width = opt.content.lines().map(|s| s.width()).max().unwrap_or(0).max(title.width());
let title_width = title.width() + (opt.level.icon().width() + /* Space */ 1);
let max_width = opt.content.lines().map(|s| s.width()).max().unwrap_or(0).max(title_width);
Self {
title: title.to_owned(),

View file

@ -1,7 +1,5 @@
use ratatui::{buffer::Buffer, layout::{self, Constraint, Offset, Rect}, widgets::{Block, BorderType, Paragraph, Widget, Wrap}};
use yazi_config::THEME;
use yazi_core::notify::Message;
use yazi_proxy::options::NotifyLevel;
use crate::Ctx;
@ -49,12 +47,6 @@ impl<'a> Widget for Layout<'a> {
let tile = Self::tile(available, &notify.messages[..limit]);
for (i, m) in notify.messages.iter().enumerate().take(limit) {
let (icon, style) = match m.level {
NotifyLevel::Info => (&THEME.notify.icon_info, THEME.notify.title_info),
NotifyLevel::Warn => (&THEME.notify.icon_warn, THEME.notify.title_warn),
NotifyLevel::Error => (&THEME.notify.icon_error, THEME.notify.title_error),
};
let mut rect =
tile[i].offset(Offset { x: (100 - m.percent) as i32 * tile[i].width as i32 / 100, y: 0 });
rect.width -= rect.x - tile[i].x;
@ -65,9 +57,9 @@ impl<'a> Widget for Layout<'a> {
.block(
Block::bordered()
.border_type(BorderType::Rounded)
.title(format!("{icon} {}", m.title))
.title_style(style)
.border_style(style),
.title(format!("{} {}", m.level.icon(), m.title))
.title_style(*m.level.style())
.border_style(*m.level.style()),
)
.render(rect, buf);
}

View file

@ -2,7 +2,8 @@ use std::{str::FromStr, time::Duration};
use anyhow::bail;
use mlua::{ExternalError, ExternalResult};
use yazi_shared::event::Cmd;
use yazi_config::THEME;
use yazi_shared::{event::Cmd, theme::Style};
pub struct NotifyOpt {
pub title: String,
@ -41,7 +42,7 @@ impl<'a> TryFrom<mlua::Table<'a>> for NotifyOpt {
}
}
#[derive(Default)]
#[derive(Clone, Copy, Default)]
pub enum NotifyLevel {
#[default]
Info,
@ -49,6 +50,26 @@ pub enum NotifyLevel {
Error,
}
impl NotifyLevel {
#[inline]
pub fn icon(self) -> &'static str {
match self {
Self::Info => &THEME.notify.icon_info,
Self::Warn => &THEME.notify.icon_warn,
Self::Error => &THEME.notify.icon_error,
}
}
#[inline]
pub fn style(self) -> &'static Style {
match self {
Self::Info => &THEME.notify.title_info,
Self::Warn => &THEME.notify.title_warn,
Self::Error => &THEME.notify.title_error,
}
}
}
impl FromStr for NotifyLevel {
type Err = anyhow::Error;