Replace TitleBar string title with generic Content
This commit is contained in:
parent
d16b9cf7cd
commit
f54590d7ad
@ -12,11 +12,7 @@ use crate::defaults;
|
|||||||
use crate::{Primitive, Renderer};
|
use crate::{Primitive, Renderer};
|
||||||
use iced_native::mouse;
|
use iced_native::mouse;
|
||||||
use iced_native::pane_grid;
|
use iced_native::pane_grid;
|
||||||
use iced_native::text;
|
use iced_native::{Element, Layout, Point, Rectangle, Vector};
|
||||||
use iced_native::{
|
|
||||||
Element, HorizontalAlignment, Layout, Point, Rectangle, Vector,
|
|
||||||
VerticalAlignment,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub use iced_native::pane_grid::{
|
pub use iced_native::pane_grid::{
|
||||||
Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent,
|
Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent,
|
||||||
@ -188,14 +184,12 @@ where
|
|||||||
defaults: &Self::Defaults,
|
defaults: &Self::Defaults,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
style_sheet: &Self::Style,
|
style_sheet: &Self::Style,
|
||||||
title: &str,
|
content: (&Element<'_, Message, Self>, Layout<'_>),
|
||||||
title_size: u16,
|
|
||||||
title_font: Self::Font,
|
|
||||||
title_bounds: Rectangle,
|
|
||||||
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
) -> Self::Output {
|
) -> Self::Output {
|
||||||
let style = style_sheet.style();
|
let style = style_sheet.style();
|
||||||
|
let (title_content, title_layout) = content;
|
||||||
|
|
||||||
let defaults = Self::Defaults {
|
let defaults = Self::Defaults {
|
||||||
text: defaults::Text {
|
text: defaults::Text {
|
||||||
@ -205,16 +199,12 @@ where
|
|||||||
|
|
||||||
let background = crate::widget::container::background(bounds, &style);
|
let background = crate::widget::container::background(bounds, &style);
|
||||||
|
|
||||||
let (title_primitive, _) = text::Renderer::draw(
|
let (title_primitive, title_interaction) = title_content.draw(
|
||||||
self,
|
self,
|
||||||
&defaults,
|
&defaults,
|
||||||
title_bounds,
|
title_layout,
|
||||||
title,
|
cursor_position,
|
||||||
title_size,
|
&bounds,
|
||||||
title_font,
|
|
||||||
None,
|
|
||||||
HorizontalAlignment::Left,
|
|
||||||
VerticalAlignment::Top,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some((controls, controls_layout)) = controls {
|
if let Some((controls, controls_layout)) = controls {
|
||||||
@ -234,7 +224,7 @@ where
|
|||||||
controls_primitive,
|
controls_primitive,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
controls_interaction,
|
controls_interaction.max(title_interaction),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
@ -245,7 +235,7 @@ where
|
|||||||
} else {
|
} else {
|
||||||
title_primitive
|
title_primitive
|
||||||
},
|
},
|
||||||
mouse::Interaction::default(),
|
title_interaction,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -276,10 +276,7 @@ impl pane_grid::Renderer for Null {
|
|||||||
_defaults: &Self::Defaults,
|
_defaults: &Self::Defaults,
|
||||||
_bounds: Rectangle,
|
_bounds: Rectangle,
|
||||||
_style: &Self::Style,
|
_style: &Self::Style,
|
||||||
_title: &str,
|
_content: (&Element<'_, Message, Self>, Layout<'_>),
|
||||||
_title_size: u16,
|
|
||||||
_title_font: Self::Font,
|
|
||||||
_title_bounds: Rectangle,
|
|
||||||
_controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
_controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
||||||
_cursor_position: Point,
|
_cursor_position: Point,
|
||||||
) {
|
) {
|
||||||
|
@ -586,18 +586,15 @@ pub trait Renderer:
|
|||||||
/// It receives:
|
/// It receives:
|
||||||
/// - the bounds, style of the [`TitleBar`]
|
/// - the bounds, style of the [`TitleBar`]
|
||||||
/// - the style of the [`TitleBar`]
|
/// - the style of the [`TitleBar`]
|
||||||
/// - the title of the [`TitleBar`] with its size, font, and bounds
|
/// - the content of the [`TitleBar`] with its layout
|
||||||
/// - the controls of the [`TitleBar`] with their [`Layout`+, if any
|
/// - the controls of the [`TitleBar`] with their [`Layout`], if any
|
||||||
/// - the cursor position
|
/// - the cursor position
|
||||||
fn draw_title_bar<Message>(
|
fn draw_title_bar<Message>(
|
||||||
&mut self,
|
&mut self,
|
||||||
defaults: &Self::Defaults,
|
defaults: &Self::Defaults,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
style: &Self::Style,
|
style: &Self::Style,
|
||||||
title: &str,
|
content: (&Element<'_, Message, Self>, Layout<'_>),
|
||||||
title_size: u16,
|
|
||||||
title_font: Self::Font,
|
|
||||||
title_bounds: Rectangle,
|
|
||||||
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
|
||||||
cursor_position: Point,
|
cursor_position: Point,
|
||||||
) -> Self::Output;
|
) -> Self::Output;
|
||||||
|
@ -1,30 +1,31 @@
|
|||||||
use crate::event::{self, Event};
|
use crate::event::{self, Event};
|
||||||
use crate::layout;
|
use crate::layout;
|
||||||
use crate::pane_grid;
|
use crate::pane_grid;
|
||||||
use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
|
use crate::{Clipboard, Element, Hasher, Layout, Point, Size};
|
||||||
|
|
||||||
/// The title bar of a [`Pane`].
|
/// The title bar of a [`Pane`].
|
||||||
///
|
///
|
||||||
/// [`Pane`]: crate::widget::pane_grid::Pane
|
/// [`Pane`]: crate::widget::pane_grid::Pane
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> {
|
pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> {
|
||||||
title: String,
|
content: Element<'a, Message, Renderer>,
|
||||||
title_size: Option<u16>,
|
|
||||||
controls: Option<Element<'a, Message, Renderer>>,
|
controls: Option<Element<'a, Message, Renderer>>,
|
||||||
padding: u16,
|
padding: u16,
|
||||||
always_show_controls: bool,
|
always_show_controls: bool,
|
||||||
style: Renderer::Style,
|
style: Renderer::Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
|
impl<'a, Message, Renderer: 'a> TitleBar<'a, Message, Renderer>
|
||||||
where
|
where
|
||||||
Renderer: pane_grid::Renderer,
|
Renderer: pane_grid::Renderer,
|
||||||
{
|
{
|
||||||
/// Creates a new [`TitleBar`] with the given title.
|
/// Creates a new [`TitleBar`] with the given content.
|
||||||
pub fn new(title: impl Into<String>) -> Self {
|
pub fn new<E>(content: E) -> Self
|
||||||
|
where
|
||||||
|
E: Into<Element<'a, Message, Renderer>>,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
title: title.into(),
|
content: content.into(),
|
||||||
title_size: None,
|
|
||||||
controls: None,
|
controls: None,
|
||||||
padding: 0,
|
padding: 0,
|
||||||
always_show_controls: false,
|
always_show_controls: false,
|
||||||
@ -32,12 +33,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the size of the title of the [`TitleBar`].
|
|
||||||
pub fn title_size(mut self, size: u16) -> Self {
|
|
||||||
self.title_size = Some(size);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the controls of the [`TitleBar`].
|
/// Sets the controls of the [`TitleBar`].
|
||||||
pub fn controls(
|
pub fn controls(
|
||||||
mut self,
|
mut self,
|
||||||
@ -91,48 +86,29 @@ where
|
|||||||
let mut children = layout.children();
|
let mut children = layout.children();
|
||||||
let padded = children.next().unwrap();
|
let padded = children.next().unwrap();
|
||||||
|
|
||||||
if let Some(controls) = &self.controls {
|
let mut children = padded.children();
|
||||||
let mut children = padded.children();
|
let title_layout = children.next().unwrap();
|
||||||
let title_layout = children.next().unwrap();
|
|
||||||
|
let controls = if let Some(controls) = &self.controls {
|
||||||
let controls_layout = children.next().unwrap();
|
let controls_layout = children.next().unwrap();
|
||||||
|
|
||||||
let (title_bounds, controls) =
|
if show_controls || self.always_show_controls {
|
||||||
if show_controls || self.always_show_controls {
|
Some((controls, controls_layout))
|
||||||
(title_layout.bounds(), Some((controls, controls_layout)))
|
} else {
|
||||||
} else {
|
None
|
||||||
(
|
}
|
||||||
Rectangle {
|
|
||||||
width: padded.bounds().width,
|
|
||||||
..title_layout.bounds()
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
renderer.draw_title_bar(
|
|
||||||
defaults,
|
|
||||||
layout.bounds(),
|
|
||||||
&self.style,
|
|
||||||
&self.title,
|
|
||||||
self.title_size.unwrap_or(renderer.default_size()),
|
|
||||||
Renderer::Font::default(),
|
|
||||||
title_bounds,
|
|
||||||
controls,
|
|
||||||
cursor_position,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
renderer.draw_title_bar::<()>(
|
None
|
||||||
defaults,
|
};
|
||||||
layout.bounds(),
|
|
||||||
&self.style,
|
renderer.draw_title_bar(
|
||||||
&self.title,
|
defaults,
|
||||||
self.title_size.unwrap_or(renderer.default_size()),
|
layout.bounds(),
|
||||||
Renderer::Font::default(),
|
&self.style,
|
||||||
padded.bounds(),
|
(&self.content, title_layout),
|
||||||
None,
|
controls,
|
||||||
cursor_position,
|
cursor_position,
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the mouse cursor is over the pick area of the
|
/// Returns whether the mouse cursor is over the pick area of the
|
||||||
@ -165,8 +141,7 @@ where
|
|||||||
pub(crate) fn hash_layout(&self, hasher: &mut Hasher) {
|
pub(crate) fn hash_layout(&self, hasher: &mut Hasher) {
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
self.title.hash(hasher);
|
self.content.hash_layout(hasher);
|
||||||
self.title_size.hash(hasher);
|
|
||||||
self.padding.hash(hasher);
|
self.padding.hash(hasher);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,15 +154,10 @@ where
|
|||||||
let limits = limits.pad(padding);
|
let limits = limits.pad(padding);
|
||||||
let max_size = limits.max();
|
let max_size = limits.max();
|
||||||
|
|
||||||
let title_size = self.title_size.unwrap_or(renderer.default_size());
|
let title_layout = self
|
||||||
let title_font = Renderer::Font::default();
|
.content
|
||||||
|
.layout(renderer, &layout::Limits::new(Size::ZERO, max_size));
|
||||||
let (title_width, title_height) = renderer.measure(
|
let title_size = title_layout.size();
|
||||||
&self.title,
|
|
||||||
title_size,
|
|
||||||
title_font,
|
|
||||||
Size::new(f32::INFINITY, max_size.height),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut node = if let Some(controls) = &self.controls {
|
let mut node = if let Some(controls) = &self.controls {
|
||||||
let mut controls_layout = controls
|
let mut controls_layout = controls
|
||||||
@ -196,16 +166,8 @@ where
|
|||||||
let controls_size = controls_layout.size();
|
let controls_size = controls_layout.size();
|
||||||
let space_before_controls = max_size.width - controls_size.width;
|
let space_before_controls = max_size.width - controls_size.width;
|
||||||
|
|
||||||
let mut title_layout = layout::Node::new(Size::new(
|
|
||||||
title_width.min(space_before_controls),
|
|
||||||
title_height,
|
|
||||||
));
|
|
||||||
|
|
||||||
let title_size = title_layout.size();
|
|
||||||
let height = title_size.height.max(controls_size.height);
|
let height = title_size.height.max(controls_size.height);
|
||||||
|
|
||||||
title_layout
|
|
||||||
.move_to(Point::new(0.0, (height - title_size.height) / 2.0));
|
|
||||||
controls_layout.move_to(Point::new(space_before_controls, 0.0));
|
controls_layout.move_to(Point::new(space_before_controls, 0.0));
|
||||||
|
|
||||||
layout::Node::with_children(
|
layout::Node::with_children(
|
||||||
@ -213,7 +175,10 @@ where
|
|||||||
vec![title_layout, controls_layout],
|
vec![title_layout, controls_layout],
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
layout::Node::new(Size::new(max_size.width, title_height))
|
layout::Node::with_children(
|
||||||
|
Size::new(max_size.width, title_size.height),
|
||||||
|
vec![title_layout],
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
node.move_to(Point::new(padding, padding));
|
node.move_to(Point::new(padding, padding));
|
||||||
|
Loading…
Reference in New Issue
Block a user