Skip to content

Commit 47eaf1f

Browse files
committed
Standardize Text type in Contexts
A long standing pet-peeve of mine: Previously there were a number of different lifetypes associated with the text factory depending on where it came from. This standardizes that, so that all contexts' text() method returns an owned PietText handle. This has the added advantage of getting rid of the weird lifetime in LayoutContext, which will also unblock my macro work.
1 parent f012f2c commit 47eaf1f

File tree

7 files changed

+23
-30
lines changed

7 files changed

+23
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
7373
- Replaced `Command::one_shot` and `::take_object` with a `SingleUse` payload wrapper type. ([#959] by [@finnerale])
7474
- Renamed `WidgetPod` methods: `paint` to `paint_raw`, `paint_with_offset` to `paint`, `paint_with_offset_always` to `paint_always`. ([#980] by [@totsteps])
7575
- `Command` and `Selector` have been reworked and are now statically typed, similarly to `Env` and `Key`. ([#993] by [@finnerale])
76+
- Standardize the type returned by the contexts' `text()` methods. ([#996] by
77+
[@cmyr])
7678

7779
### Deprecated
7880

@@ -244,6 +246,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
244246
[#990]: https://github.com/xi-editor/druid/pull/990
245247
[#991]: https://github.com/xi-editor/druid/pull/991
246248
[#993]: https://github.com/xi-editor/druid/pull/993
249+
[#996]: https://github.com/xi-editor/druid/pull/996
247250

248251
## [0.5.0] - 2020-04-01
249252

druid/src/contexts.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ pub struct UpdateCtx<'a, 'b> {
8585
/// As of now, the main service provided is access to a factory for
8686
/// creating text layout objects, which are likely to be useful
8787
/// during widget layout.
88-
pub struct LayoutCtx<'a, 'b, 'c: 'a> {
88+
pub struct LayoutCtx<'a, 'b> {
8989
pub(crate) state: &'a mut ContextState<'b>,
9090
pub(crate) widget_state: &'a mut WidgetState,
91-
pub(crate) text_factory: &'a mut Text<'c>,
9291
pub(crate) mouse_pos: Option<Point>,
9392
}
9493

@@ -685,10 +684,10 @@ impl<'a, 'b> UpdateCtx<'a, 'b> {
685684
}
686685
}
687686

688-
impl<'c> LayoutCtx<'_, '_, 'c> {
687+
impl LayoutCtx<'_, '_> {
689688
/// Get an object which can create text layouts.
690-
pub fn text(&mut self) -> &mut Text<'c> {
691-
&mut self.text_factory
689+
pub fn text(&mut self) -> Text {
690+
self.state.window.text()
692691
}
693692

694693
/// Get the window id.
@@ -718,6 +717,11 @@ impl PaintCtx<'_, '_, '_> {
718717
self.widget_state.id
719718
}
720719

720+
/// Get an object which can create text layouts.
721+
pub fn text(&mut self) -> Text {
722+
self.state.window.text()
723+
}
724+
721725
/// Query the "hot" state of the widget.
722726
///
723727
/// See [`EventCtx::is_hot`](struct.EventCtx.html#method.is_hot) for

druid/src/core.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
497497
let mut child_ctx = LayoutCtx {
498498
widget_state: &mut self.state,
499499
state: ctx.state,
500-
text_factory: ctx.text_factory,
501500
mouse_pos: child_mouse_pos,
502501
};
503502
let size = self.inner.layout(&mut child_ctx, bc, data, env);

druid/src/tests/harness.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<T: Data> Harness<'_, T> {
259259

260260
/// Only do a layout pass, without painting
261261
pub fn just_layout(&mut self) {
262-
self.inner.layout(&mut self.piet)
262+
self.inner.layout()
263263
}
264264

265265
pub fn paint_rect(&mut self, invalid_rect: Rect) {
@@ -287,9 +287,9 @@ impl<T: Data> Inner<T> {
287287
self.window.update(&mut self.cmds, &self.data, &self.env);
288288
}
289289

290-
fn layout(&mut self, piet: &mut Piet) {
290+
fn layout(&mut self) {
291291
self.window
292-
.just_layout(piet, &mut self.cmds, &self.data, &self.env);
292+
.just_layout(&mut self.cmds, &self.data, &self.env);
293293
}
294294

295295
#[allow(dead_code)]

druid/src/widget/label.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,11 @@ impl<T: Data> Widget<T> for Label<T> {
265265
}
266266
}
267267

268-
fn layout(
269-
&mut self,
270-
layout_ctx: &mut LayoutCtx,
271-
bc: &BoxConstraints,
272-
_data: &T,
273-
env: &Env,
274-
) -> Size {
268+
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, _data: &T, env: &Env) -> Size {
275269
bc.debug_check("Label");
276270

277271
let font_size = self.size.resolve(env);
278-
let text_layout = self.get_layout(layout_ctx.text(), env);
272+
let text_layout = self.get_layout(&mut ctx.text(), env);
279273
bc.constrain(Size::new(
280274
text_layout.width() + 2. * LABEL_X_PADDING,
281275
font_size * LINE_HEIGHT_FACTOR,
@@ -284,7 +278,7 @@ impl<T: Data> Widget<T> for Label<T> {
284278

285279
fn paint(&mut self, ctx: &mut PaintCtx, _data: &T, env: &Env) {
286280
let font_size = self.size.resolve(env);
287-
let text_layout = self.get_layout(ctx.text(), env);
281+
let text_layout = self.get_layout(&mut ctx.text(), env);
288282
let line_height = font_size * LINE_HEIGHT_FACTOR;
289283

290284
// Find the origin for the text

druid/src/widget/textbox.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl Widget<String> for TextBox {
417417
rc.clip(clip_rect);
418418

419419
// Calculate layout
420-
let text_layout = self.get_layout(rc.text(), &content, env);
420+
let text_layout = self.get_layout(&mut rc.text(), &content, env);
421421

422422
// Shift everything inside the clip by the hscroll_offset
423423
rc.transform(Affine::translate((-self.hscroll_offset, 0.)));

druid/src/window.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<T: Data> Window<T> {
313313
self.lifecycle(queue, &LifeCycle::AnimFrame(0), data, env, true);
314314

315315
if self.root.state().needs_layout {
316-
self.layout(piet, queue, data, env);
316+
self.layout(queue, data, env);
317317
}
318318

319319
piet.fill(
@@ -323,13 +323,12 @@ impl<T: Data> Window<T> {
323323
self.paint(piet, invalid_rect, queue, data, env);
324324
}
325325

326-
fn layout(&mut self, piet: &mut Piet, queue: &mut CommandQueue, data: &T, env: &Env) {
326+
fn layout(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
327327
let mut widget_state = WidgetState::new(self.root.id());
328328
let mut state = ContextState::new::<T>(queue, &self.handle, self.id, self.focus);
329329
let mut layout_ctx = LayoutCtx {
330330
state: &mut state,
331331
widget_state: &mut widget_state,
332-
text_factory: piet.text(),
333332
mouse_pos: self.last_mouse_pos,
334333
};
335334
let bc = BoxConstraints::tight(self.size);
@@ -346,14 +345,8 @@ impl<T: Data> Window<T> {
346345
/// only expose `layout` for testing; normally it is called as part of `do_paint`
347346
#[cfg(not(target_arch = "wasm32"))]
348347
#[cfg(test)]
349-
pub(crate) fn just_layout(
350-
&mut self,
351-
piet: &mut Piet,
352-
queue: &mut CommandQueue,
353-
data: &T,
354-
env: &Env,
355-
) {
356-
self.layout(piet, queue, data, env)
348+
pub(crate) fn just_layout(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
349+
self.layout(queue, data, env)
357350
}
358351

359352
fn paint(

0 commit comments

Comments
 (0)