Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass shared PietText by ref instead of cloning #1205

Merged
merged 1 commit into from
Sep 9, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ You can find its changes [documented below](#060---2020-06-01).
- Moved `Target` parameter from `submit_command` to `Command::new` and `Command::to`. ([#1185] by [@finnerale])
- `Movement::RightOfLine` to `Movement::NextLineBreak`, and `Movement::LeftOfLine` to `Movement::PrecedingLineBreak`. ([#1092] by [@sysint64])
- `AnimFrame` was moved from `lifecycle` to `event` ([#1155] by [@jneem])
- Contexts' `text()` methods return `&mut PietText` instead of cloning ([#1205] by [@cmyr])

### Deprecated

Expand Down Expand Up @@ -425,6 +426,7 @@ Last release without a changelog :(
[#1185]: https://github.com/linebender/druid/pull/1185
[#1092]: https://github.com/linebender/druid/pull/1092
[#1204]: https://github.com/linebender/druid/pull/1204
[#1205]: https://github.com/linebender/druid/pull/1205

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
2 changes: 1 addition & 1 deletion druid/examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Widget<String> for CustomWidget {
let mut layout = TextLayout::new(data.as_str());
layout.set_font(FontDescriptor::new(FontFamily::SERIF).with_size(24.0));
layout.set_text_color(fill_color);
layout.rebuild_if_needed(&mut ctx.text(), env);
layout.rebuild_if_needed(ctx.text(), env);

// Let's rotate our text slightly. First we save our current (default) context:
ctx.with_save(|ctx| {
Expand Down
6 changes: 4 additions & 2 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) struct ContextState<'a> {
pub(crate) ext_handle: &'a ExtEventSink,
pub(crate) window_id: WindowId,
pub(crate) window: &'a WindowHandle,
pub(crate) text: PietText,
/// The id of the widget that currently has focus.
pub(crate) focus_widget: Option<WidgetId>,
pub(crate) root_app_data_type: TypeId,
Expand Down Expand Up @@ -154,8 +155,8 @@ impl_context_method!(
}

/// Get an object which can create text layouts.
pub fn text(&self) -> PietText {
self.state.window.text()
pub fn text(&mut self) -> &mut PietText {
&mut self.state.text
}
}
);
Expand Down Expand Up @@ -675,6 +676,7 @@ impl<'a> ContextState<'a> {
window,
window_id,
focus_widget,
text: window.text(),
root_app_data_type: TypeId::of::<T>(),
}
}
Expand Down
19 changes: 9 additions & 10 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
self.debug_widget_text.set_text(id_string);
self.debug_widget_text.set_text_size(10.0);
self.debug_widget_text.set_text_color(text_color);
self.debug_widget_text
.rebuild_if_needed(&mut ctx.text(), env);
self.debug_widget_text.rebuild_if_needed(ctx.text(), env);
}
}

Expand Down Expand Up @@ -986,16 +985,16 @@ mod tests {

let mut command_queue: CommandQueue = VecDeque::new();
let mut widget_state = WidgetState::new(WidgetId::next(), None);
let window = WindowHandle::default();
let ext_host = ExtEventHost::default();
let ext_handle = ext_host.make_sink();
let mut state = ContextState {
command_queue: &mut command_queue,
ext_handle: &ext_handle,
window_id: WindowId::next(),
window: &WindowHandle::default(),
root_app_data_type: std::any::TypeId::of::<Option<u32>>(),
focus_widget: None,
};
let mut state = ContextState::new::<Option<u32>>(
&mut command_queue,
&ext_handle,
&window,
WindowId::next(),
None,
);

let mut ctx = LifeCycleCtx {
widget_state: &mut widget_state,
Expand Down
4 changes: 2 additions & 2 deletions druid/src/widget/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ impl Widget<bool> for Switch {

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, _data: &bool, env: &Env) {
if matches!(event, LifeCycle::WidgetAdded) {
self.on_text.rebuild_if_needed(&mut ctx.text(), env);
self.off_text.rebuild_if_needed(&mut ctx.text(), env);
self.on_text.rebuild_if_needed(ctx.text(), env);
self.off_text.rebuild_if_needed(ctx.text(), env);
}
}

Expand Down
6 changes: 3 additions & 3 deletions druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl Widget<String> for TextBox {
self.do_edit_action(edit_action, data);
self.reset_cursor_blink(ctx);
self.text.set_text(data.as_str());
self.text.rebuild_if_needed(&mut ctx.text(), env);
self.text.rebuild_if_needed(ctx.text(), env);

if !is_select_all {
self.update_hscroll();
Expand All @@ -374,7 +374,7 @@ impl Widget<String> for TextBox {
LifeCycle::WidgetAdded => {
ctx.register_for_focus();
self.text.set_text(data.clone());
self.text.rebuild_if_needed(&mut ctx.text(), env);
self.text.rebuild_if_needed(ctx.text(), env);
}
// an open question: should we be able to schedule timers here?
LifeCycle::FocusChanged(true) => ctx.submit_command(RESET_BLINK.to(ctx.widget_id())),
Expand All @@ -400,7 +400,7 @@ impl Widget<String> for TextBox {
}
}

self.text.rebuild_if_needed(&mut ctx.text(), env);
self.text.rebuild_if_needed(ctx.text(), env);
ctx.request_paint();
}

Expand Down