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

Basic rich text #1255

Merged
merged 2 commits into from
Sep 29, 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 @@ -43,6 +43,7 @@ You can find its changes [documented below](#060---2020-06-01).
- CONFIGURE_WINDOW command to allow reconfiguration of an existing window. ([#1235] by [@rjwittams])
- `RawLabel` widget displays text `Data`. ([#1252] by [@cmyr])
- 'Tabs' widget allowing static and dynamic tabbed layouts. ([#1160] by [@rjwittams])
- `RichText` and `Attribute` types for creating rich text ([#1255] by [@cmyr])

### Changed

Expand Down Expand Up @@ -476,6 +477,7 @@ Last release without a changelog :(
[#1245]: https://github.com/linebender/druid/pull/1245
[#1251]: https://github.com/linebender/druid/pull/1251
[#1252]: https://github.com/linebender/druid/pull/1252
[#1255]: https://github.com/linebender/druid/pull/1255

[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
4 changes: 2 additions & 2 deletions druid/examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use druid::kurbo::BezPath;
use druid::piet::{FontFamily, ImageFormat, InterpolationMode};
use druid::widget::prelude::*;
use druid::{
Affine, AppLauncher, Color, FontDescriptor, LocalizedString, Point, Rect, TextLayout,
Affine, AppLauncher, ArcStr, Color, FontDescriptor, LocalizedString, Point, Rect, TextLayout,
WindowDesc,
};

Expand Down Expand Up @@ -86,7 +86,7 @@ impl Widget<String> for CustomWidget {

// Text is easy; in real use TextLayout should be stored in the widget
// and reused.
let mut layout = TextLayout::new(data.as_str());
let mut layout = TextLayout::<ArcStr>::from_text(data.to_owned());
layout.set_font(FontDescriptor::new(FontFamily::SERIF).with_size(24.0));
layout.set_text_color(fill_color);
layout.rebuild_if_needed(ctx.text(), env);
Expand Down
43 changes: 36 additions & 7 deletions druid/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

//! An example of various text layout features.

use druid::widget::{Controller, Flex, Label, LineBreaking, RadioGroup, Scroll};
use druid::piet::{PietTextLayoutBuilder, TextStorage as PietTextStorage};
use druid::text::{Attribute, RichText, TextStorage};
use druid::widget::prelude::*;
use druid::widget::{Controller, Flex, Label, LineBreaking, RadioGroup, RawLabel, Scroll};
use druid::{
AppLauncher, Color, Data, Env, Lens, LocalizedString, TextAlignment, UpdateCtx, Widget,
WidgetExt, WindowDesc,
AppLauncher, Color, Data, FontFamily, FontStyle, FontWeight, Lens, LocalizedString,
TextAlignment, Widget, WidgetExt, WindowDesc,
};

const WINDOW_TITLE: LocalizedString<AppState> = LocalizedString::new("Text Options");
Expand All @@ -29,18 +32,34 @@ const SPACER_SIZE: f64 = 8.0;

#[derive(Clone, Data, Lens)]
struct AppState {
text: RichText,
line_break_mode: LineBreaking,
alignment: TextAlignment,
}

/// A controller that sets properties on a label.
//NOTE: we implement these traits for our base data (instead of just lensing
//into the RichText object, for the label) so that our label controller can
//have access to the other fields.
impl PietTextStorage for AppState {
fn as_str(&self) -> &str {
self.text.as_str()
}
}

impl TextStorage for AppState {
fn add_attributes(&self, builder: PietTextLayoutBuilder, env: &Env) -> PietTextLayoutBuilder {
self.text.add_attributes(builder, env)
}
}

/// A controller that updates label properties as required.
struct LabelController;

impl Controller<AppState, Label<AppState>> for LabelController {
impl Controller<AppState, RawLabel<AppState>> for LabelController {
#[allow(clippy::float_cmp)]
fn update(
&mut self,
child: &mut Label<AppState>,
child: &mut RawLabel<AppState>,
ctx: &mut UpdateCtx,
old_data: &AppState,
data: &AppState,
Expand All @@ -52,6 +71,7 @@ impl Controller<AppState, Label<AppState>> for LabelController {
}
if old_data.alignment != data.alignment {
child.set_text_alignment(data.alignment);
ctx.request_layout();
}
child.update(ctx, old_data, data, env);
}
Expand All @@ -63,10 +83,19 @@ pub fn main() {
.title(WINDOW_TITLE)
.window_size((400.0, 600.0));

let text = RichText::new(TEXT.into())
.with_attribute(0..9, Attribute::text_color(Color::rgb(1.0, 0.2, 0.1)))
.with_attribute(0..9, Attribute::size(24.0))
.with_attribute(0..9, Attribute::font_family(FontFamily::SERIF))
.with_attribute(194..239, Attribute::weight(FontWeight::BOLD))
.with_attribute(764.., Attribute::size(12.0))
.with_attribute(764.., Attribute::style(FontStyle::Italic));

// create the initial app state
let initial_state = AppState {
line_break_mode: LineBreaking::Clip,
alignment: Default::default(),
text,
};

// start the application
Expand All @@ -78,7 +107,7 @@ pub fn main() {

fn build_root_widget() -> impl Widget<AppState> {
let label = Scroll::new(
Label::new(TEXT)
RawLabel::new()
.with_text_color(Color::BLACK)
.controller(LabelController)
.background(Color::WHITE)
Expand Down
12 changes: 6 additions & 6 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::contexts::ContextState;
use crate::kurbo::{Affine, Insets, Point, Rect, Shape, Size, Vec2};
use crate::util::ExtendDrain;
use crate::{
BoxConstraints, Color, Command, Data, Env, Event, EventCtx, InternalEvent, InternalLifeCycle,
LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Region, RenderContext, Target, TextLayout,
TimerToken, UpdateCtx, Widget, WidgetId,
ArcStr, BoxConstraints, Color, Command, Data, Env, Event, EventCtx, InternalEvent,
InternalLifeCycle, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Region, RenderContext, Target,
TextLayout, TimerToken, UpdateCtx, Widget, WidgetId,
};

/// Our queue type
Expand All @@ -50,7 +50,7 @@ pub struct WidgetPod<T, W> {
env: Option<Env>,
inner: W,
// stashed layout so we don't recompute this when debugging
debug_widget_text: TextLayout,
debug_widget_text: TextLayout<ArcStr>,
}

/// Generic state for all widgets in the hierarchy.
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
old_data: None,
env: None,
inner,
debug_widget_text: TextLayout::new(""),
debug_widget_text: TextLayout::new(),
}
}

Expand Down Expand Up @@ -433,7 +433,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
Color::BLACK
};
let id_string = id.to_raw().to_string();
self.debug_widget_text.set_text(id_string);
self.debug_widget_text.set_text(id_string.into());
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(ctx.text(), env);
Expand Down
6 changes: 0 additions & 6 deletions druid/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ pub trait Data: Clone + 'static {
//// ANCHOR_END: same_fn
}

/// A reference counted string slice.
///
/// This is a data-friendly way to represent strings in druid. Unlike `String`
/// it cannot be mutated, but unlike `String` it can be cheaply cloned.
pub type ArcStr = Arc<str>;

/// An impl of `Data` suitable for simple types.
///
/// The `same` method is implemented with equality, so the type should
Expand Down
4 changes: 2 additions & 2 deletions druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ pub use app_delegate::{AppDelegate, DelegateCtx};
pub use box_constraints::BoxConstraints;
pub use command::{sys as commands, Command, Selector, SingleUse, Target};
pub use contexts::{EventCtx, LayoutCtx, LifeCycleCtx, PaintCtx, UpdateCtx};
pub use data::{ArcStr, Data};
pub use data::Data;
pub use env::{Env, Key, KeyOrValue, Value, ValueType};
pub use event::{Event, InternalEvent, InternalLifeCycle, LifeCycle};
pub use ext_event::{ExtEventError, ExtEventSink};
pub use lens::{Lens, LensExt};
pub use localization::LocalizedString;
pub use menu::{sys as platform_menus, ContextMenu, MenuDesc, MenuItem};
pub use mouse::MouseEvent;
pub use text::{FontDescriptor, TextLayout};
pub use text::{ArcStr, FontDescriptor, TextLayout};
pub use widget::{Widget, WidgetExt, WidgetId};
pub use win_handler::DruidHandler;
pub use window::{Window, WindowId};
Expand Down
Loading