Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- Replaced `Command::one_shot` and `::take_object` with a `SingleUse` payload wrapper type. ([#959] by [@finnerale])
- Renamed `WidgetPod` methods: `paint` to `paint_raw`, `paint_with_offset` to `paint`, `paint_with_offset_always` to `paint_always`. ([#980] by [@totsteps])
- `Command` and `Selector` have been reworked and are now statically typed, similarly to `Env` and `Key`. ([#993] by [@finnerale])
- Standardize the type returned by the contexts' `text()` methods. ([#996] by
[@cmyr])

### Deprecated

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

## [0.5.0] - 2020-04-01

Expand Down
14 changes: 9 additions & 5 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ pub struct UpdateCtx<'a, 'b> {
/// As of now, the main service provided is access to a factory for
/// creating text layout objects, which are likely to be useful
/// during widget layout.
pub struct LayoutCtx<'a, 'b, 'c: 'a> {
pub struct LayoutCtx<'a, 'b> {
pub(crate) state: &'a mut ContextState<'b>,
pub(crate) widget_state: &'a mut WidgetState,
pub(crate) text_factory: &'a mut Text<'c>,
pub(crate) mouse_pos: Option<Point>,
}

Expand Down Expand Up @@ -685,10 +684,10 @@ impl<'a, 'b> UpdateCtx<'a, 'b> {
}
}

impl<'c> LayoutCtx<'_, '_, 'c> {
impl LayoutCtx<'_, '_> {
/// Get an object which can create text layouts.
pub fn text(&mut self) -> &mut Text<'c> {
&mut self.text_factory
pub fn text(&mut self) -> Text {
self.state.window.text()
}

/// Get the window id.
Expand Down Expand Up @@ -718,6 +717,11 @@ impl PaintCtx<'_, '_, '_> {
self.widget_state.id
}

/// Get an object which can create text layouts.
pub fn text(&mut self) -> Text {
self.state.window.text()
}

/// Query the "hot" state of the widget.
///
/// See [`EventCtx::is_hot`](struct.EventCtx.html#method.is_hot) for
Expand Down
1 change: 0 additions & 1 deletion druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
let mut child_ctx = LayoutCtx {
widget_state: &mut self.state,
state: ctx.state,
text_factory: ctx.text_factory,
mouse_pos: child_mouse_pos,
};
let size = self.inner.layout(&mut child_ctx, bc, data, env);
Expand Down
6 changes: 3 additions & 3 deletions druid/src/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<T: Data> Harness<'_, T> {

/// Only do a layout pass, without painting
pub fn just_layout(&mut self) {
self.inner.layout(&mut self.piet)
self.inner.layout()
}

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

fn layout(&mut self, piet: &mut Piet) {
fn layout(&mut self) {
self.window
.just_layout(piet, &mut self.cmds, &self.data, &self.env);
.just_layout(&mut self.cmds, &self.data, &self.env);
}

#[allow(dead_code)]
Expand Down
12 changes: 3 additions & 9 deletions druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,11 @@ impl<T: Data> Widget<T> for Label<T> {
}
}

fn layout(
&mut self,
layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &T,
env: &Env,
) -> Size {
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, _data: &T, env: &Env) -> Size {
bc.debug_check("Label");

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

fn paint(&mut self, ctx: &mut PaintCtx, _data: &T, env: &Env) {
let font_size = self.size.resolve(env);
let text_layout = self.get_layout(ctx.text(), env);
let text_layout = self.get_layout(&mut ctx.text(), env);
let line_height = font_size * LINE_HEIGHT_FACTOR;

// Find the origin for the text
Expand Down
2 changes: 1 addition & 1 deletion druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl Widget<String> for TextBox {
rc.clip(clip_rect);

// Calculate layout
let text_layout = self.get_layout(rc.text(), &content, env);
let text_layout = self.get_layout(&mut rc.text(), &content, env);

// Shift everything inside the clip by the hscroll_offset
rc.transform(Affine::translate((-self.hscroll_offset, 0.)));
Expand Down
15 changes: 4 additions & 11 deletions druid/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<T: Data> Window<T> {
self.lifecycle(queue, &LifeCycle::AnimFrame(0), data, env, true);

if self.root.state().needs_layout {
self.layout(piet, queue, data, env);
self.layout(queue, data, env);
}

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

fn layout(&mut self, piet: &mut Piet, queue: &mut CommandQueue, data: &T, env: &Env) {
fn layout(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
let mut widget_state = WidgetState::new(self.root.id());
let mut state = ContextState::new::<T>(queue, &self.handle, self.id, self.focus);
let mut layout_ctx = LayoutCtx {
state: &mut state,
widget_state: &mut widget_state,
text_factory: piet.text(),
mouse_pos: self.last_mouse_pos,
};
let bc = BoxConstraints::tight(self.size);
Expand All @@ -346,14 +345,8 @@ impl<T: Data> Window<T> {
/// only expose `layout` for testing; normally it is called as part of `do_paint`
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
pub(crate) fn just_layout(
&mut self,
piet: &mut Piet,
queue: &mut CommandQueue,
data: &T,
env: &Env,
) {
self.layout(piet, queue, data, env)
pub(crate) fn just_layout(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
self.layout(queue, data, env)
}

fn paint(
Expand Down