2024-02-27 16:53:55 +01:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
|
2023-06-13 18:32:38 +02:00
|
|
|
use cosmic::{
|
2024-02-27 16:53:55 +01:00
|
|
|
iced::{alignment, Point},
|
2023-06-13 18:32:38 +02:00
|
|
|
iced_core::{
|
2023-06-22 21:01:20 +02:00
|
|
|
gradient,
|
2023-06-13 18:32:38 +02:00
|
|
|
layout::{Layout, Limits, Node},
|
2023-06-22 21:01:05 +02:00
|
|
|
mouse::Cursor,
|
2023-10-02 19:37:23 +02:00
|
|
|
renderer::{self, Renderer as IcedRenderer},
|
2024-02-27 16:53:55 +01:00
|
|
|
text::{LineHeight, Paragraph, Renderer as TextRenderer, Shaping},
|
|
|
|
|
widget::{tree, Tree, Widget},
|
|
|
|
|
Background, Color, Degrees, Gradient, Length, Rectangle, Size, Text,
|
2023-06-13 18:32:38 +02:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
/// Text in a stack tab with an overflow gradient.
|
|
|
|
|
pub fn tab_text(text: String) -> TabText {
|
2024-02-27 19:10:14 +01:00
|
|
|
TabText::new(text)
|
2024-02-27 16:53:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct LocalState {
|
|
|
|
|
text_hash: u64,
|
|
|
|
|
paragraph: <cosmic::Renderer as TextRenderer>::Paragraph,
|
|
|
|
|
overflowed: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Text in a stack tab with an overflow gradient.
|
|
|
|
|
pub struct TabText {
|
|
|
|
|
text: String,
|
|
|
|
|
font: cosmic::font::Font,
|
|
|
|
|
font_size: f32,
|
2023-06-13 18:32:38 +02:00
|
|
|
height: Length,
|
|
|
|
|
width: Length,
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
impl TabText {
|
2024-02-27 19:10:14 +01:00
|
|
|
pub fn new(text: String) -> Self {
|
2023-06-13 18:32:38 +02:00
|
|
|
TabText {
|
|
|
|
|
width: Length::Shrink,
|
|
|
|
|
height: Length::Shrink,
|
2024-02-27 16:53:55 +01:00
|
|
|
font: cosmic::font::DEFAULT,
|
|
|
|
|
font_size: 14.0,
|
|
|
|
|
text,
|
2023-06-13 18:32:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
pub fn font(mut self, font: cosmic::font::Font) -> Self {
|
|
|
|
|
self.font = font;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn font_size(mut self, font_size: f32) -> Self {
|
|
|
|
|
self.font_size = font_size;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 18:32:38 +02:00
|
|
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
|
|
|
|
let width = width.into();
|
|
|
|
|
self.width = width;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn height(mut self, height: impl Into<Length>) -> Self {
|
|
|
|
|
let height = height.into();
|
|
|
|
|
self.height = height;
|
|
|
|
|
self
|
|
|
|
|
}
|
2024-02-27 16:53:55 +01:00
|
|
|
|
|
|
|
|
fn create_hash(&self) -> u64 {
|
|
|
|
|
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
|
|
|
|
self.text.hash(&mut hasher);
|
|
|
|
|
hasher.finish()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_paragraph(&self) -> <cosmic::Renderer as TextRenderer>::Paragraph {
|
|
|
|
|
<cosmic::Renderer as TextRenderer>::Paragraph::with_text(Text {
|
|
|
|
|
content: &self.text,
|
|
|
|
|
size: cosmic::iced_core::Pixels(self.font_size),
|
|
|
|
|
bounds: Size::INFINITY,
|
|
|
|
|
font: self.font,
|
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
|
|
|
|
shaping: Shaping::Advanced,
|
|
|
|
|
line_height: LineHeight::default(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-06-13 18:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
impl<Message> Widget<Message, cosmic::Renderer> for TabText {
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::of::<LocalState>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
tree::State::new(LocalState {
|
|
|
|
|
text_hash: self.create_hash(),
|
|
|
|
|
paragraph: self.create_paragraph(),
|
|
|
|
|
overflowed: false,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 18:32:38 +02:00
|
|
|
fn width(&self) -> Length {
|
|
|
|
|
self.width
|
|
|
|
|
}
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
|
self.height
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
fn layout(&self, tree: &mut Tree, _renderer: &cosmic::Renderer, limits: &Limits) -> Node {
|
|
|
|
|
let limits = limits.width(self.width).height(self.height);
|
2023-06-13 18:32:38 +02:00
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
let state = tree.state.downcast_mut::<LocalState>();
|
|
|
|
|
let text_bounds = state.paragraph.min_bounds();
|
|
|
|
|
state.overflowed = limits.max().width < text_bounds.width;
|
|
|
|
|
let actual_size = limits.resolve(text_bounds);
|
2023-06-13 18:32:38 +02:00
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
Node::new(actual_size)
|
|
|
|
|
}
|
2023-06-13 18:32:38 +02:00
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
fn diff(&mut self, tree: &mut Tree) {
|
|
|
|
|
// If the text changes, update the paragraph.
|
|
|
|
|
let state = tree.state.downcast_mut::<LocalState>();
|
|
|
|
|
let text_hash = self.create_hash();
|
|
|
|
|
if state.text_hash != text_hash {
|
|
|
|
|
state.text_hash = text_hash;
|
|
|
|
|
state.paragraph = self.create_paragraph();
|
|
|
|
|
}
|
2023-06-13 18:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &Tree,
|
2024-02-27 16:53:55 +01:00
|
|
|
renderer: &mut cosmic::Renderer,
|
2024-02-27 19:10:14 +01:00
|
|
|
theme: &cosmic::Theme,
|
2023-06-13 18:32:38 +02:00
|
|
|
style: &renderer::Style,
|
|
|
|
|
layout: Layout<'_>,
|
2024-02-27 16:53:55 +01:00
|
|
|
_cursor: Cursor,
|
2023-06-13 18:32:38 +02:00
|
|
|
_viewport: &Rectangle,
|
|
|
|
|
) {
|
|
|
|
|
let bounds = layout.bounds();
|
2024-02-27 16:53:55 +01:00
|
|
|
let state = tree.state.downcast_ref::<LocalState>();
|
2023-06-13 18:32:38 +02:00
|
|
|
|
|
|
|
|
renderer.with_layer(bounds, |renderer| {
|
2024-02-27 16:53:55 +01:00
|
|
|
renderer.fill_paragraph(
|
|
|
|
|
&state.paragraph,
|
|
|
|
|
Point::new(bounds.x, bounds.y + bounds.height / 2.0),
|
|
|
|
|
style.text_color,
|
|
|
|
|
bounds,
|
2023-06-13 18:32:38 +02:00
|
|
|
);
|
|
|
|
|
});
|
2023-06-22 21:01:20 +02:00
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
if state.overflowed {
|
2024-02-27 19:10:14 +01:00
|
|
|
let background = super::tab::primary_container_color(theme.cosmic());
|
|
|
|
|
let transparent = Color {
|
|
|
|
|
a: 0.0,
|
|
|
|
|
..background
|
2024-02-27 16:53:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
2024-02-27 19:10:14 +01:00
|
|
|
bounds: Rectangle {
|
|
|
|
|
x: (bounds.x + bounds.width - 24.).max(bounds.x),
|
|
|
|
|
width: 24.0_f32.min(bounds.width),
|
|
|
|
|
..bounds
|
|
|
|
|
},
|
2024-02-27 16:53:55 +01:00
|
|
|
border_radius: 0.0.into(),
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
Background::Gradient(Gradient::Linear(
|
|
|
|
|
gradient::Linear::new(Degrees(90.))
|
2024-02-27 19:10:14 +01:00
|
|
|
.add_stop(0.0, transparent)
|
|
|
|
|
.add_stop(1.0, background),
|
2024-02-27 16:53:55 +01:00
|
|
|
)),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-06-13 18:32:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 16:53:55 +01:00
|
|
|
impl<Message: 'static> From<TabText> for cosmic::Element<'_, Message> {
|
|
|
|
|
fn from(value: TabText) -> Self {
|
|
|
|
|
Self::new(value)
|
2023-06-13 18:32:38 +02:00
|
|
|
}
|
|
|
|
|
}
|