Skip to content

Commit f916f75

Browse files
committed
Move all text-related types to druid::text
Previously some of these were exposed at the root, and some of them existed only in druid_shell or in submodules; this reexports all text-related types from the module root, which should make finding text-related types much simpler.
1 parent 886f2b2 commit f916f75

File tree

12 files changed

+95
-73
lines changed

12 files changed

+95
-73
lines changed

druid/examples/flex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! knobs to change all the parameters. 99% of the time you will want to
1818
//! hard-code these parameters, which will simplify your code considerably.
1919
20-
use druid::text::format::ParseFormatter;
20+
use druid::text::ParseFormatter;
2121
use druid::widget::prelude::*;
2222
use druid::widget::{
2323
Button, Checkbox, CrossAxisAlignment, Flex, Label, MainAxisAlignment, ProgressBar, RadioGroup,

druid/examples/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! everything behaves.
1818
1919
use druid::piet::InterpolationMode;
20-
use druid::text::format::ParseFormatter;
20+
use druid::text::ParseFormatter;
2121
use druid::widget::{prelude::*, FillStrat, Image};
2222
use druid::widget::{
2323
Checkbox, CrossAxisAlignment, Flex, Label, RadioGroup, SizedBox, TextBox, WidgetExt,

druid/examples/value_formatting/src/formatters.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
//! Implementations of the [`druid::text::Formatter`] trait.
1616
17-
use druid::text::format::{Formatter, Validation, ValidationError};
18-
use druid::text::Selection;
17+
use druid::text::{Formatter, Selection, Validation, ValidationError};
1918
use druid::Data;
2019

2120
/// A formatter that can display currency values.

druid/examples/value_formatting/src/widgets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
//! Widgets, widget components, and functions for creating widgets
1616
17-
use druid::text::format::ValidationError;
17+
use druid::text::ValidationError;
1818
use druid::widget::{
1919
prelude::*, Controller, Either, Label, SizedBox, TextBoxEvent, ValidationDelegate,
2020
};

druid/src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ impl CursorChange {
12401240
mod tests {
12411241
use super::*;
12421242
use crate::ext_event::ExtEventHost;
1243-
use crate::text::format::ParseFormatter;
1243+
use crate::text::ParseFormatter;
12441244
use crate::widget::{Flex, Scroll, Split, TextBox};
12451245
use crate::{WidgetExt, WindowHandle, WindowId};
12461246
use test_env_log::test;

druid/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,8 @@ mod window;
176176

177177
// Types from kurbo & piet that are required by public API.
178178
pub use kurbo::{Affine, Insets, Point, Rect, Size, Vec2};
179-
pub use piet::{
180-
Color, FontFamily, FontStyle, FontWeight, ImageBuf, LinearGradient, RadialGradient,
181-
RenderContext, TextAlignment, UnitPoint,
182-
};
179+
pub use piet::{Color, ImageBuf, LinearGradient, RadialGradient, RenderContext, UnitPoint};
180+
183181
// these are the types from shell that we expose; others we only use internally.
184182
#[cfg(feature = "image")]
185183
pub use shell::image;
@@ -206,7 +204,6 @@ pub use lens::{Lens, LensExt};
206204
pub use localization::LocalizedString;
207205
pub use menu::{sys as platform_menus, Menu, MenuItem};
208206
pub use mouse::MouseEvent;
209-
pub use text::{ArcStr, FontDescriptor, TextLayout};
210207
pub use util::Handled;
211208
pub use widget::{Widget, WidgetExt, WidgetId};
212209
pub use win_handler::DruidHandler;
@@ -216,6 +213,11 @@ pub use window::{Window, WindowId};
216213
#[cfg(test)]
217214
pub(crate) use event::{StateCell, StateCheckFn};
218215

216+
#[deprecated(since = "0.8.0", note = "import from druid::text module instead")]
217+
pub use piet::{FontFamily, FontStyle, FontWeight, TextAlignment};
218+
#[deprecated(since = "0.8.0", note = "import from druid::text module instead")]
219+
pub use text::{ArcStr, FontDescriptor, TextLayout};
220+
219221
/// The meaning (mapped value) of a keypress.
220222
///
221223
/// Note that in previous versions, the `KeyCode` field referred to the

druid/src/text/backspace.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ fn backspace_offset(text: &impl EditableText, start: usize) -> usize {
193193
}
194194

195195
/// Calculate resulting offset for a backwards delete.
196+
///
197+
/// This involves complicated logic to handle various special cases that
198+
/// are unique to backspace.
196199
#[allow(clippy::trivially_copy_pass_by_ref)]
197200
pub fn offset_for_delete_backwards(region: &Selection, text: &impl EditableText) -> usize {
198201
if !region.is_caret() {

druid/src/text/input_component.rs

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ use std::sync::{Arc, Weak};
2020

2121
use tracing::instrument;
2222

23-
use super::{EditableText, ImeHandlerRef, Movement, Selection, TextLayout, TextStorage};
23+
use super::{
24+
EditableText, ImeHandlerRef, ImeInvalidation, InputHandler, Movement, Selection, TextAction,
25+
TextLayout, TextStorage,
26+
};
2427
use crate::kurbo::{Line, Point, Rect, Vec2};
2528
use crate::piet::TextLayout as _;
26-
use crate::shell::text::{Action as ImeAction, Event as ImeUpdate, InputHandler};
2729
use crate::widget::prelude::*;
2830
use crate::{text, theme, Cursor, Env, Modifiers, Selector, TextAlignment, UpdateCtx};
2931

@@ -83,9 +85,9 @@ pub struct EditSession<T> {
8385
external_text_change: Option<T>,
8486
external_selection_change: Option<Selection>,
8587
external_scroll_to: Option<bool>,
86-
external_action: Option<ImeAction>,
88+
external_action: Option<TextAction>,
8789
/// A flag set in `update` if the text has changed from a non-IME source.
88-
pending_ime_invalidation: Option<ImeUpdate>,
90+
pending_ime_invalidation: Option<ImeInvalidation>,
8991
/// If `true`, the component will send the [`TextComponent::RETURN`]
9092
/// notification when the user enters a newline.
9193
pub send_notification_on_return: bool,
@@ -283,12 +285,12 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
283285
self.borrow_mut().layout.set_text(data.clone());
284286
self.borrow_mut().layout.rebuild_if_needed(ctx.text(), env);
285287
self.borrow_mut()
286-
.update_pending_invalidation(ImeUpdate::Reset);
288+
.update_pending_invalidation(ImeInvalidation::Reset);
287289
}
288290
self.borrow_mut()
289291
.do_mouse_down(mouse.pos, mouse.mods, mouse.count);
290292
self.borrow_mut()
291-
.update_pending_invalidation(ImeUpdate::SelectionChanged);
293+
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
292294
ctx.request_update();
293295
ctx.request_paint();
294296
}
@@ -299,7 +301,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
299301
self.borrow_mut().do_drag(mouse.pos);
300302
if self.borrow().selection() != pre_sel {
301303
self.borrow_mut()
302-
.update_pending_invalidation(ImeUpdate::SelectionChanged);
304+
.update_pending_invalidation(ImeInvalidation::SelectionChanged);
303305
ctx.request_update();
304306
ctx.request_paint();
305307
}
@@ -324,12 +326,14 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
324326
}
325327
if let Some(action) = action {
326328
match action {
327-
ImeAction::Cancel => ctx.submit_notification(TextComponent::CANCEL),
328-
ImeAction::InsertNewLine { .. } => {
329+
TextAction::Cancel => ctx.submit_notification(TextComponent::CANCEL),
330+
TextAction::InsertNewLine { .. } => {
329331
ctx.submit_notification(TextComponent::RETURN)
330332
}
331-
ImeAction::InsertTab { .. } => ctx.submit_notification(TextComponent::TAB),
332-
ImeAction::InsertBacktab => ctx.submit_notification(TextComponent::BACKTAB),
333+
TextAction::InsertTab { .. } => ctx.submit_notification(TextComponent::TAB),
334+
TextAction::InsertBacktab => {
335+
ctx.submit_notification(TextComponent::BACKTAB)
336+
}
333337
_ => tracing::warn!("unexepcted external action '{:?}'", action),
334338
};
335339
}
@@ -372,7 +376,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
372376
let new_origin = ctx.window_origin();
373377
if prev_origin != new_origin {
374378
self.borrow_mut().origin = ctx.window_origin();
375-
ctx.invalidate_text_input(ImeUpdate::LayoutChanged);
379+
ctx.invalidate_text_input(ImeInvalidation::LayoutChanged);
376380
}
377381
}
378382
}
@@ -477,11 +481,11 @@ impl<T> EditSession<T> {
477481
/// invalidte the platform's IME state, by passing it to
478482
/// [`EventCtx::invalidate_text_input`].
479483
#[must_use]
480-
pub fn set_selection(&mut self, selection: Selection) -> Option<ImeUpdate> {
484+
pub fn set_selection(&mut self, selection: Selection) -> Option<ImeInvalidation> {
481485
if selection != self.selection {
482486
self.selection = selection;
483-
self.update_pending_invalidation(ImeUpdate::SelectionChanged);
484-
Some(ImeUpdate::SelectionChanged)
487+
self.update_pending_invalidation(ImeInvalidation::SelectionChanged);
488+
Some(ImeInvalidation::SelectionChanged)
485489
} else {
486490
None
487491
}
@@ -508,7 +512,7 @@ impl<T> EditSession<T> {
508512
/// Returns any invalidation action that should be passed to the platform.
509513
///
510514
/// The user of this component *must* check this after calling `update`.
511-
pub fn pending_ime_invalidation(&mut self) -> Option<ImeUpdate> {
515+
pub fn pending_ime_invalidation(&mut self) -> Option<ImeInvalidation> {
512516
self.pending_ime_invalidation.take()
513517
}
514518

@@ -524,20 +528,22 @@ impl<T> EditSession<T> {
524528
self.external_scroll_to.take()
525529
}
526530

527-
fn take_external_action(&mut self) -> Option<ImeAction> {
531+
fn take_external_action(&mut self) -> Option<TextAction> {
528532
self.external_action.take()
529533
}
530534

531535
// we don't want to replace a more aggressive invalidation with a less aggressive one.
532-
fn update_pending_invalidation(&mut self, new_invalidation: ImeUpdate) {
536+
fn update_pending_invalidation(&mut self, new_invalidation: ImeInvalidation) {
533537
self.pending_ime_invalidation = match self.pending_ime_invalidation.take() {
534538
None => Some(new_invalidation),
535539
Some(prev) => match (prev, new_invalidation) {
536-
(ImeUpdate::SelectionChanged, ImeUpdate::SelectionChanged) => {
537-
ImeUpdate::SelectionChanged
540+
(ImeInvalidation::SelectionChanged, ImeInvalidation::SelectionChanged) => {
541+
ImeInvalidation::SelectionChanged
542+
}
543+
(ImeInvalidation::LayoutChanged, ImeInvalidation::LayoutChanged) => {
544+
ImeInvalidation::LayoutChanged
538545
}
539-
(ImeUpdate::LayoutChanged, ImeUpdate::LayoutChanged) => ImeUpdate::LayoutChanged,
540-
_ => ImeUpdate::Reset,
546+
_ => ImeInvalidation::Reset,
541547
}
542548
.into(),
543549
}
@@ -558,12 +564,12 @@ impl<T: TextStorage + EditableText> EditSession<T> {
558564
/// The caller is responsible for notifying the platform of the change in
559565
/// text state, by calling [`EventCtx::invalidate_text_input`].
560566
#[must_use]
561-
pub fn insert_text(&mut self, data: &mut T, new_text: &str) -> ImeUpdate {
567+
pub fn insert_text(&mut self, data: &mut T, new_text: &str) -> ImeInvalidation {
562568
let new_cursor_pos = self.selection.min() + new_text.len();
563569
data.edit(self.selection.range(), new_text);
564570
self.selection = Selection::caret(new_cursor_pos);
565571
self.scroll_to_selection_end(true);
566-
ImeUpdate::Reset
572+
ImeInvalidation::Reset
567573
}
568574

569575
/// Sets the clipboard to the contents of the current selection.
@@ -588,23 +594,23 @@ impl<T: TextStorage + EditableText> EditSession<T> {
588594
self.external_scroll_to = Some(after_edit);
589595
}
590596

591-
fn do_action(&mut self, buffer: &mut T, action: ImeAction) {
597+
fn do_action(&mut self, buffer: &mut T, action: TextAction) {
592598
match action {
593-
ImeAction::Move(movement) => {
599+
TextAction::Move(movement) => {
594600
let sel = text::movement(movement, self.selection, &self.layout, false);
595601
self.external_selection_change = Some(sel);
596602
self.scroll_to_selection_end(false);
597603
}
598-
ImeAction::MoveSelecting(movement) => {
604+
TextAction::MoveSelecting(movement) => {
599605
let sel = text::movement(movement, self.selection, &self.layout, true);
600606
self.external_selection_change = Some(sel);
601607
self.scroll_to_selection_end(false);
602608
}
603-
ImeAction::SelectAll => {
609+
TextAction::SelectAll => {
604610
let len = buffer.len();
605611
self.external_selection_change = Some(Selection::new(0, len));
606612
}
607-
ImeAction::SelectWord => {
613+
TextAction::SelectWord => {
608614
if self.selection.is_caret() {
609615
let range =
610616
text::movement::word_range_for_pos(buffer.as_str(), self.selection.active);
@@ -615,17 +621,17 @@ impl<T: TextStorage + EditableText> EditSession<T> {
615621
// is not a caret (and may span multiple words)
616622
}
617623
// This requires us to have access to the layout, which might be stale?
618-
ImeAction::SelectLine => (),
624+
TextAction::SelectLine => (),
619625
// this assumes our internal selection is consistent with the buffer?
620-
ImeAction::SelectParagraph => {
626+
TextAction::SelectParagraph => {
621627
if !self.selection.is_caret() || buffer.len() < self.selection.active {
622628
return;
623629
}
624630
let prev = buffer.preceding_line_break(self.selection.active);
625631
let next = buffer.next_line_break(self.selection.active);
626632
self.external_selection_change = Some(Selection::new(prev, next));
627633
}
628-
ImeAction::Delete(movement) if self.selection.is_caret() => {
634+
TextAction::Delete(movement) if self.selection.is_caret() => {
629635
if movement == Movement::Grapheme(druid_shell::text::Direction::Upstream) {
630636
self.backspace(buffer);
631637
} else {
@@ -634,17 +640,17 @@ impl<T: TextStorage + EditableText> EditSession<T> {
634640
self.ime_insert_text(buffer, "")
635641
}
636642
}
637-
ImeAction::Delete(_) => self.ime_insert_text(buffer, ""),
638-
ImeAction::DecomposingBackspace => {
643+
TextAction::Delete(_) => self.ime_insert_text(buffer, ""),
644+
TextAction::DecomposingBackspace => {
639645
tracing::warn!("Decomposing Backspace is not implemented");
640646
self.backspace(buffer);
641647
}
642-
//ImeAction::UppercaseSelection
643-
//| ImeAction::LowercaseSelection
644-
//| ImeAction::TitlecaseSelection => {
648+
//TextAction::UppercaseSelection
649+
//| TextAction::LowercaseSelection
650+
//| TextAction::TitlecaseSelection => {
645651
//tracing::warn!("IME transformations are not implemented");
646652
//}
647-
ImeAction::InsertNewLine {
653+
TextAction::InsertNewLine {
648654
newline_type,
649655
ignore_hotkey,
650656
} => {
@@ -654,21 +660,21 @@ impl<T: TextStorage + EditableText> EditSession<T> {
654660
self.ime_insert_text(buffer, &newline_type.to_string());
655661
}
656662
}
657-
ImeAction::InsertTab { ignore_hotkey } => {
663+
TextAction::InsertTab { ignore_hotkey } => {
658664
if ignore_hotkey || self.accepts_tabs {
659665
self.ime_insert_text(buffer, "\t");
660666
} else if !ignore_hotkey {
661667
self.external_action = Some(action);
662668
}
663669
}
664-
ImeAction::InsertBacktab => {
670+
TextAction::InsertBacktab => {
665671
if !self.accepts_tabs {
666672
self.external_action = Some(action);
667673
}
668674
}
669-
ImeAction::InsertSingleQuoteIgnoringSmartQuotes => self.ime_insert_text(buffer, "'"),
670-
ImeAction::InsertDoubleQuoteIgnoringSmartQuotes => self.ime_insert_text(buffer, "\""),
671-
ImeAction::Cancel if self.send_notification_on_cancel => {
675+
TextAction::InsertSingleQuoteIgnoringSmartQuotes => self.ime_insert_text(buffer, "'"),
676+
TextAction::InsertDoubleQuoteIgnoringSmartQuotes => self.ime_insert_text(buffer, "\""),
677+
TextAction::Cancel if self.send_notification_on_cancel => {
672678
self.external_action = Some(action)
673679
}
674680
other => tracing::warn!("unhandled IME action {:?}", other),
@@ -793,7 +799,7 @@ impl<T: TextStorage + EditableText> EditSession<T> {
793799
.map(|t| !t.same(new_data))
794800
.unwrap_or(true)
795801
{
796-
self.update_pending_invalidation(ImeUpdate::Reset);
802+
self.update_pending_invalidation(ImeInvalidation::Reset);
797803
self.layout.set_text(new_data.clone());
798804
}
799805
if self.layout.needs_rebuild_after_update(ctx) {
@@ -802,7 +808,7 @@ impl<T: TextStorage + EditableText> EditSession<T> {
802808
let new_sel = self.selection.constrained(new_data.as_str());
803809
if new_sel != self.selection {
804810
self.selection = new_sel;
805-
self.update_pending_invalidation(ImeUpdate::SelectionChanged);
811+
self.update_pending_invalidation(ImeInvalidation::SelectionChanged);
806812
}
807813
self.layout.rebuild_if_needed(ctx.text(), env);
808814
}
@@ -888,7 +894,7 @@ impl<T: TextStorage + EditableText> InputHandler for EditSessionHandle<T> {
888894
.map(|rect| rect + origin.to_vec2())
889895
}
890896

891-
fn handle_action(&mut self, action: ImeAction) {
897+
fn handle_action(&mut self, action: TextAction) {
892898
self.inner.borrow_mut().do_action(&mut self.text, action);
893899
let text_changed = self
894900
.inner

0 commit comments

Comments
 (0)