Skip to content

Commit f8aef03

Browse files
authored
Merge pull request #657 from clarkmoody/feature/pane-grid-title-contents
Generic Element Content in Pane Grid TitleBar
2 parents a366600 + e815c5b commit f8aef03

5 files changed

Lines changed: 81 additions & 114 deletions

File tree

examples/pane_grid/src/main.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use iced::{
22
button, executor, keyboard, pane_grid, scrollable, Align, Application,
3-
Button, Column, Command, Container, Element, HorizontalAlignment, Length,
4-
PaneGrid, Scrollable, Settings, Subscription, Text,
3+
Button, Color, Column, Command, Container, Element, HorizontalAlignment,
4+
Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text,
55
};
66
use iced_native::{event, subscription, Event};
77

@@ -141,10 +141,21 @@ impl Application for Example {
141141
let pane_grid = PaneGrid::new(&mut self.panes, |pane, content| {
142142
let is_focused = focus == Some(pane);
143143

144-
let title_bar =
145-
pane_grid::TitleBar::new(format!("Pane {}", content.id))
146-
.padding(10)
147-
.style(style::TitleBar { is_focused });
144+
let title = Row::with_children(vec![
145+
Text::new("Pane").into(),
146+
Text::new(content.id.to_string())
147+
.color(if is_focused {
148+
PANE_ID_COLOR_FOCUSED
149+
} else {
150+
PANE_ID_COLOR_UNFOCUSED
151+
})
152+
.into(),
153+
])
154+
.spacing(5);
155+
156+
let title_bar = pane_grid::TitleBar::new(title)
157+
.padding(10)
158+
.style(style::TitleBar { is_focused });
148159

149160
pane_grid::Content::new(content.view(pane, total_panes))
150161
.title_bar(title_bar)
@@ -165,6 +176,17 @@ impl Application for Example {
165176
}
166177
}
167178

179+
const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb(
180+
0xFF as f32 / 255.0,
181+
0xC7 as f32 / 255.0,
182+
0xC7 as f32 / 255.0,
183+
);
184+
const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb(
185+
0xFF as f32 / 255.0,
186+
0x47 as f32 / 255.0,
187+
0x47 as f32 / 255.0,
188+
);
189+
168190
fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
169191
use keyboard::KeyCode;
170192
use pane_grid::{Axis, Direction};

graphics/src/widget/pane_grid.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77
//! drag and drop, and hotkey support.
88
//!
99
//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid
10-
use crate::backend::{self, Backend};
1110
use crate::defaults;
12-
use crate::{Primitive, Renderer};
11+
use crate::{Backend, Primitive, Renderer};
1312
use iced_native::mouse;
1413
use iced_native::pane_grid;
15-
use iced_native::text;
16-
use iced_native::{
17-
Element, HorizontalAlignment, Layout, Point, Rectangle, Vector,
18-
VerticalAlignment,
19-
};
14+
use iced_native::{Element, Layout, Point, Rectangle, Vector};
2015

2116
pub use iced_native::pane_grid::{
2217
Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent,
@@ -34,7 +29,7 @@ pub type PaneGrid<'a, Message, Backend> =
3429

3530
impl<B> pane_grid::Renderer for Renderer<B>
3631
where
37-
B: Backend + backend::Text,
32+
B: Backend,
3833
{
3934
fn draw<Message>(
4035
&mut self,
@@ -188,14 +183,12 @@ where
188183
defaults: &Self::Defaults,
189184
bounds: Rectangle,
190185
style_sheet: &Self::Style,
191-
title: &str,
192-
title_size: u16,
193-
title_font: Self::Font,
194-
title_bounds: Rectangle,
186+
content: (&Element<'_, Message, Self>, Layout<'_>),
195187
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
196188
cursor_position: Point,
197189
) -> Self::Output {
198190
let style = style_sheet.style();
191+
let (title_content, title_layout) = content;
199192

200193
let defaults = Self::Defaults {
201194
text: defaults::Text {
@@ -205,16 +198,12 @@ where
205198

206199
let background = crate::widget::container::background(bounds, &style);
207200

208-
let (title_primitive, _) = text::Renderer::draw(
201+
let (title_primitive, title_interaction) = title_content.draw(
209202
self,
210203
&defaults,
211-
title_bounds,
212-
title,
213-
title_size,
214-
title_font,
215-
None,
216-
HorizontalAlignment::Left,
217-
VerticalAlignment::Top,
204+
title_layout,
205+
cursor_position,
206+
&bounds,
218207
);
219208

220209
if let Some((controls, controls_layout)) = controls {
@@ -234,7 +223,7 @@ where
234223
controls_primitive,
235224
],
236225
},
237-
controls_interaction,
226+
controls_interaction.max(title_interaction),
238227
)
239228
} else {
240229
(
@@ -245,7 +234,7 @@ where
245234
} else {
246235
title_primitive
247236
},
248-
mouse::Interaction::default(),
237+
title_interaction,
249238
)
250239
}
251240
}

native/src/renderer/null.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ impl pane_grid::Renderer for Null {
276276
_defaults: &Self::Defaults,
277277
_bounds: Rectangle,
278278
_style: &Self::Style,
279-
_title: &str,
280-
_title_size: u16,
281-
_title_font: Self::Font,
282-
_title_bounds: Rectangle,
279+
_content: (&Element<'_, Message, Self>, Layout<'_>),
283280
_controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
284281
_cursor_position: Point,
285282
) {

native/src/widget/pane_grid.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use crate::layout;
3333
use crate::mouse;
3434
use crate::overlay;
3535
use crate::row;
36-
use crate::text;
3736
use crate::{
3837
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector,
3938
Widget,
@@ -543,9 +542,7 @@ where
543542
/// able to use a [`PaneGrid`] in your user interface.
544543
///
545544
/// [renderer]: crate::renderer
546-
pub trait Renderer:
547-
crate::Renderer + container::Renderer + text::Renderer + Sized
548-
{
545+
pub trait Renderer: crate::Renderer + container::Renderer + Sized {
549546
/// Draws a [`PaneGrid`].
550547
///
551548
/// It receives:
@@ -586,18 +583,15 @@ pub trait Renderer:
586583
/// It receives:
587584
/// - the bounds, style of the [`TitleBar`]
588585
/// - the style of the [`TitleBar`]
589-
/// - the title of the [`TitleBar`] with its size, font, and bounds
590-
/// - the controls of the [`TitleBar`] with their [`Layout`+, if any
586+
/// - the content of the [`TitleBar`] with its layout
587+
/// - the controls of the [`TitleBar`] with their [`Layout`], if any
591588
/// - the cursor position
592589
fn draw_title_bar<Message>(
593590
&mut self,
594591
defaults: &Self::Defaults,
595592
bounds: Rectangle,
596593
style: &Self::Style,
597-
title: &str,
598-
title_size: u16,
599-
title_font: Self::Font,
600-
title_bounds: Rectangle,
594+
content: (&Element<'_, Message, Self>, Layout<'_>),
601595
controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>,
602596
cursor_position: Point,
603597
) -> Self::Output;

native/src/widget/pane_grid/title_bar.rs

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::event::{self, Event};
22
use crate::layout;
33
use crate::pane_grid;
4-
use crate::{Clipboard, Element, Hasher, Layout, Point, Rectangle, Size};
4+
use crate::{Clipboard, Element, Hasher, Layout, Point, Size};
55

66
/// The title bar of a [`Pane`].
77
///
88
/// [`Pane`]: crate::widget::pane_grid::Pane
99
#[allow(missing_debug_implementations)]
1010
pub struct TitleBar<'a, Message, Renderer: pane_grid::Renderer> {
11-
title: String,
12-
title_size: Option<u16>,
11+
content: Element<'a, Message, Renderer>,
1312
controls: Option<Element<'a, Message, Renderer>>,
1413
padding: u16,
1514
always_show_controls: bool,
@@ -20,24 +19,20 @@ impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
2019
where
2120
Renderer: pane_grid::Renderer,
2221
{
23-
/// Creates a new [`TitleBar`] with the given title.
24-
pub fn new(title: impl Into<String>) -> Self {
22+
/// Creates a new [`TitleBar`] with the given content.
23+
pub fn new<E>(content: E) -> Self
24+
where
25+
E: Into<Element<'a, Message, Renderer>>,
26+
{
2527
Self {
26-
title: title.into(),
27-
title_size: None,
28+
content: content.into(),
2829
controls: None,
2930
padding: 0,
3031
always_show_controls: false,
3132
style: Renderer::Style::default(),
3233
}
3334
}
3435

35-
/// Sets the size of the title of the [`TitleBar`].
36-
pub fn title_size(mut self, size: u16) -> Self {
37-
self.title_size = Some(size);
38-
self
39-
}
40-
4136
/// Sets the controls of the [`TitleBar`].
4237
pub fn controls(
4338
mut self,
@@ -91,48 +86,29 @@ where
9186
let mut children = layout.children();
9287
let padded = children.next().unwrap();
9388

94-
if let Some(controls) = &self.controls {
95-
let mut children = padded.children();
96-
let title_layout = children.next().unwrap();
89+
let mut children = padded.children();
90+
let title_layout = children.next().unwrap();
91+
92+
let controls = if let Some(controls) = &self.controls {
9793
let controls_layout = children.next().unwrap();
9894

99-
let (title_bounds, controls) =
100-
if show_controls || self.always_show_controls {
101-
(title_layout.bounds(), Some((controls, controls_layout)))
102-
} else {
103-
(
104-
Rectangle {
105-
width: padded.bounds().width,
106-
..title_layout.bounds()
107-
},
108-
None,
109-
)
110-
};
111-
112-
renderer.draw_title_bar(
113-
defaults,
114-
layout.bounds(),
115-
&self.style,
116-
&self.title,
117-
self.title_size.unwrap_or(renderer.default_size()),
118-
Renderer::Font::default(),
119-
title_bounds,
120-
controls,
121-
cursor_position,
122-
)
95+
if show_controls || self.always_show_controls {
96+
Some((controls, controls_layout))
97+
} else {
98+
None
99+
}
123100
} else {
124-
renderer.draw_title_bar::<()>(
125-
defaults,
126-
layout.bounds(),
127-
&self.style,
128-
&self.title,
129-
self.title_size.unwrap_or(renderer.default_size()),
130-
Renderer::Font::default(),
131-
padded.bounds(),
132-
None,
133-
cursor_position,
134-
)
135-
}
101+
None
102+
};
103+
104+
renderer.draw_title_bar(
105+
defaults,
106+
layout.bounds(),
107+
&self.style,
108+
(&self.content, title_layout),
109+
controls,
110+
cursor_position,
111+
)
136112
}
137113

138114
/// Returns whether the mouse cursor is over the pick area of the
@@ -165,8 +141,7 @@ where
165141
pub(crate) fn hash_layout(&self, hasher: &mut Hasher) {
166142
use std::hash::Hash;
167143

168-
self.title.hash(hasher);
169-
self.title_size.hash(hasher);
144+
self.content.hash_layout(hasher);
170145
self.padding.hash(hasher);
171146
}
172147

@@ -179,15 +154,10 @@ where
179154
let limits = limits.pad(padding);
180155
let max_size = limits.max();
181156

182-
let title_size = self.title_size.unwrap_or(renderer.default_size());
183-
let title_font = Renderer::Font::default();
184-
185-
let (title_width, title_height) = renderer.measure(
186-
&self.title,
187-
title_size,
188-
title_font,
189-
Size::new(f32::INFINITY, max_size.height),
190-
);
157+
let title_layout = self
158+
.content
159+
.layout(renderer, &layout::Limits::new(Size::ZERO, max_size));
160+
let title_size = title_layout.size();
191161

192162
let mut node = if let Some(controls) = &self.controls {
193163
let mut controls_layout = controls
@@ -196,24 +166,19 @@ where
196166
let controls_size = controls_layout.size();
197167
let space_before_controls = max_size.width - controls_size.width;
198168

199-
let mut title_layout = layout::Node::new(Size::new(
200-
title_width.min(space_before_controls),
201-
title_height,
202-
));
203-
204-
let title_size = title_layout.size();
205169
let height = title_size.height.max(controls_size.height);
206170

207-
title_layout
208-
.move_to(Point::new(0.0, (height - title_size.height) / 2.0));
209171
controls_layout.move_to(Point::new(space_before_controls, 0.0));
210172

211173
layout::Node::with_children(
212174
Size::new(max_size.width, height),
213175
vec![title_layout, controls_layout],
214176
)
215177
} else {
216-
layout::Node::new(Size::new(max_size.width, title_height))
178+
layout::Node::with_children(
179+
Size::new(max_size.width, title_size.height),
180+
vec![title_layout],
181+
)
217182
};
218183

219184
node.move_to(Point::new(padding, padding));

0 commit comments

Comments
 (0)